1
0
mirror of https://github.com/oceanprotocol/react.git synced 2025-02-14 21:10:38 +01:00

refactor messages

This commit is contained in:
Matthias Kretschmann 2020-10-21 12:32:43 +02:00
parent 30fd0b2258
commit 1caa9e1b69
Signed by: m
GPG Key ID: 606EEEF3C479A91F

View File

@ -21,24 +21,30 @@ interface UsePricing {
pricingIsLoading: boolean pricingIsLoading: boolean
} }
export const createPricingFeedback: { [key in number]: string } = { function getCreatePricingFeedback(dtSymbol: string): { [key: number]: string } {
0: 'Minting DT ...', return {
1: 'Approving DT ...', 1: `Minting ${dtSymbol} ...`,
2: 'Approving Ocean ...', 2: `Approving ${dtSymbol} ...`,
3: 'Creating ...', 3: 'Approving OCEAN ...',
4: 'Pricing created.' 4: 'Creating ...',
5: 'Pricing created.'
}
} }
export const buyDTFeedback: { [key in number]: string } = { function getBuyDTFeedback(dtSymbol: string): { [key: number]: string } {
0: '1/3 Approving OCEAN ...', return {
1: '2/3 Buying DT ...', 1: '1/3 Approving OCEAN ...',
2: '3/3 DT Bought' 2: `2/3 Buying ${dtSymbol} ...`,
3: `3/3 ${dtSymbol} bought.`
}
} }
export const sellDTFeedback: { [key in number]: string } = { function getSellDTFeedback(dtSymbol: string): { [key: number]: string } {
0: '1/3 Approving DT ...', return {
1: '2/3 Selling DT ...', 1: '1/3 Approving OCEAN ...',
2: '3/3 DT sold' 2: `2/3 Selling ${dtSymbol} ...`,
3: `3/3 ${dtSymbol} sold.`
}
} }
function usePricing(ddo: DDO): UsePricing { function usePricing(ddo: DDO): UsePricing {
@ -70,33 +76,25 @@ function usePricing(ddo: DDO): UsePricing {
init() init()
}, [ocean, dataToken, dataTokenInfo]) }, [ocean, dataToken, dataTokenInfo])
function setStepCreatePricing(index?: number) { function setStepCreatePricing(index: number) {
setPricingStep(index) setPricingStep(index)
if (!index) return if (!dtSymbol) return
const messages = getCreatePricingFeedback(dtSymbol)
const message = dtSymbol setPricingStepText(messages[index])
? createPricingFeedback[index].replace(/DT/g, dtSymbol)
: createPricingFeedback[index]
setPricingStepText(message)
} }
function setStepBuyDT(index?: number) { function setStepBuyDT(index: number) {
setPricingStep(index) setPricingStep(index)
if (!index) return if (!dtSymbol) return
const messages = getBuyDTFeedback(dtSymbol)
const message = dtSymbol setPricingStepText(messages[index])
? buyDTFeedback[index].replace(/DT/g, dtSymbol)
: buyDTFeedback[index]
setPricingStepText(message)
} }
function setStepSellDT(index?: number) {
setPricingStep(index)
if (!index) return
const message = dtSymbol function setStepSellDT(index: number) {
? sellDTFeedback[index].replace(/DT/g, dtSymbol) setPricingStep(index)
: sellDTFeedback[index] if (!dtSymbol) return
setPricingStepText(message) const messages = getSellDTFeedback(dtSymbol)
setPricingStepText(messages[index])
} }
async function mint(tokensToMint: string): Promise<TransactionReceipt> { async function mint(tokensToMint: string): Promise<TransactionReceipt> {
@ -113,14 +111,14 @@ function usePricing(ddo: DDO): UsePricing {
try { try {
setPricingIsLoading(true) setPricingIsLoading(true)
setPricingError(undefined) setPricingError(undefined)
setStepBuyDT(0) setStepBuyDT(1)
const bestPrice = await getBestDataTokenPrice(ocean, dataToken) const bestPrice = await getBestDataTokenPrice(ocean, dataToken)
switch (bestPrice?.type) { switch (bestPrice?.type) {
case 'pool': { case 'pool': {
const price = new Decimal(bestPrice.value).times(1.05).toString() const price = new Decimal(bestPrice.value).times(1.05).toString()
const maxPrice = new Decimal(bestPrice.value).times(2).toString() const maxPrice = new Decimal(bestPrice.value).times(2).toString()
setStepBuyDT(1) setStepBuyDT(2)
Logger.log( Logger.log(
'Buying token from pool', 'Buying token from pool',
bestPrice, bestPrice,
@ -134,7 +132,7 @@ function usePricing(ddo: DDO): UsePricing {
price, price,
maxPrice maxPrice
) )
setStepBuyDT(2) setStepBuyDT(3)
Logger.log('DT buy response', buyResponse) Logger.log('DT buy response', buyResponse)
return buyResponse return buyResponse
} }
@ -154,13 +152,13 @@ function usePricing(ddo: DDO): UsePricing {
bestPrice.value.toString(), bestPrice.value.toString(),
account.getId() account.getId()
) )
setStepBuyDT(1) setStepBuyDT(2)
const exchange = await ocean.fixedRateExchange.buyDT( const exchange = await ocean.fixedRateExchange.buyDT(
bestPrice.address, bestPrice.address,
String(dtAmount), String(dtAmount),
account.getId() account.getId()
) )
setStepBuyDT(2) setStepBuyDT(3)
Logger.log('DT exchange buy response', exchange) Logger.log('DT exchange buy response', exchange)
return exchange return exchange
} }
@ -169,7 +167,7 @@ function usePricing(ddo: DDO): UsePricing {
setPricingError(error.message) setPricingError(error.message)
Logger.error(error) Logger.error(error)
} finally { } finally {
setStepBuyDT(undefined) setStepBuyDT(0)
setPricingStepText(undefined) setPricingStepText(undefined)
setPricingIsLoading(false) setPricingIsLoading(false)
} }
@ -188,11 +186,11 @@ function usePricing(ddo: DDO): UsePricing {
try { try {
setPricingIsLoading(true) setPricingIsLoading(true)
setPricingError(undefined) setPricingError(undefined)
setStepSellDT(0) setStepSellDT(1)
const pool = await getFirstPool(ocean, dataToken) const pool = await getFirstPool(ocean, dataToken)
if (!pool || pool.price === 0) return if (!pool || pool.price === 0) return
const price = new Decimal(pool.price).times(0.95).toString() const price = new Decimal(pool.price).times(0.95).toString()
setStepSellDT(1) setStepSellDT(2)
Logger.log('Selling token to pool', pool, account.getId(), price) Logger.log('Selling token to pool', pool, account.getId(), price)
const sellResponse = await ocean.pool.sellDT( const sellResponse = await ocean.pool.sellDT(
account.getId(), account.getId(),
@ -200,14 +198,14 @@ function usePricing(ddo: DDO): UsePricing {
String(dtAmount), String(dtAmount),
price price
) )
setStepSellDT(2) setStepSellDT(3)
Logger.log('DT sell response', sellResponse) Logger.log('DT sell response', sellResponse)
return sellResponse return sellResponse
} catch (error) { } catch (error) {
setPricingError(error.message) setPricingError(error.message)
Logger.error(error) Logger.error(error)
} finally { } finally {
setStepSellDT(undefined) setStepSellDT(0)
setPricingStepText(undefined) setPricingStepText(undefined)
setPricingIsLoading(false) setPricingIsLoading(false)
} }
@ -216,7 +214,7 @@ function usePricing(ddo: DDO): UsePricing {
async function createPricing( async function createPricing(
priceOptions: PriceOptions priceOptions: PriceOptions
): Promise<TransactionReceipt | string | void> { ): Promise<TransactionReceipt | string | void> {
if (!ocean || !account || !accountId) return if (!ocean || !account || !accountId || !dtSymbol) return
const { type, dtAmount, price, weightOnDataToken, swapFee } = priceOptions const { type, dtAmount, price, weightOnDataToken, swapFee } = priceOptions
const isPool = type === 'dynamic' const isPool = type === 'dynamic'
@ -226,11 +224,11 @@ function usePricing(ddo: DDO): UsePricing {
return return
} }
try { setPricingIsLoading(true)
setPricingIsLoading(true) setPricingError(undefined)
setPricingError(undefined) setStepCreatePricing(1)
setStepCreatePricing(0) try {
await mint(`${dtAmount}`) await mint(`${dtAmount}`)
setStepCreatePricing(3) setStepCreatePricing(3)
@ -264,7 +262,7 @@ function usePricing(ddo: DDO): UsePricing {
setPricingError(error.message) setPricingError(error.message)
Logger.error(error) Logger.error(error)
} finally { } finally {
setPricingStep(undefined) setPricingStep(0)
setPricingStepText(undefined) setPricingStepText(undefined)
setPricingIsLoading(false) setPricingIsLoading(false)
} }