1
0
mirror of https://github.com/oceanprotocol/react.git synced 2025-01-03 18:35:18 +01:00

use token name instead of DT

This commit is contained in:
alexcos20 2020-10-16 03:59:10 -07:00
parent 06ef2746f4
commit 40929faedf
2 changed files with 47 additions and 25 deletions

View File

@ -7,7 +7,7 @@ import { Metadata } from '@oceanprotocol/lib/dist/node/ddo/interfaces/Metadata'
export function Publish() { export function Publish() {
const { publish, publishStepText, isLoading } = usePublish() const { publish, publishStepText, isLoading } = usePublish()
const { createPricing, buyDT, sellDT, pricingStep, pricingStepText, isLoading: pricingIsLoading, pricingError} = usePricing() const { createPricing, buyDT, sellDT, pricingStep, pricingStepText, pricingIsLoading, pricingError} = usePricing()
const [ddo, setDdo] = useState<DDO | undefined | null>() const [ddo, setDdo] = useState<DDO | undefined | null>()
const asset = { const asset = {

View File

@ -22,7 +22,7 @@ interface UsePricing {
pricingStep?: number pricingStep?: number
pricingStepText?: string pricingStepText?: string
pricingError?: string pricingError?: string
isLoading: boolean pricingIsLoading: boolean
} }
export const buyDTFeedback: { [key in number]: string } = { export const buyDTFeedback: { [key in number]: string } = {
@ -45,22 +45,40 @@ export const createPricingFeedback: { [key in number]: string } = {
function usePricing(): UsePricing { function usePricing(): UsePricing {
const { ocean, status, account, accountId, config } = useOcean() const { ocean, status, account, accountId, config } = useOcean()
const [isLoading, setIsLoading] = useState(false) const [pricingIsLoading, setPricingIsLoading] = useState(false)
const [pricingStep, setPricingStep] = useState<number | undefined>() const [pricingStep, setPricingStep] = useState<number | undefined>()
const [pricingStepText, setPricingStepText] = useState<string | undefined>() const [pricingStepText, setPricingStepText] = useState<string | undefined>()
const [pricingError, setPricingError] = useState<string | undefined>() const [pricingError, setPricingError] = useState<string | undefined>()
function setStepBuyDT(index?: number) { function setStepBuyDT(index?: number, datatokenName?: string) {
setPricingStep(index) setPricingStep(index)
index && setPricingStepText(buyDTFeedback[index]) let message
if (index) {
if (datatokenName)
message = buyDTFeedback[index].replace(/DT/g, datatokenName)
else message = buyDTFeedback[index]
setPricingStepText(message)
}
} }
function setStepSellDT(index?: number) { function setStepSellDT(index?: number, datatokenName?: string) {
setPricingStep(index) setPricingStep(index)
index && setPricingStepText(sellDTFeedback[index]) let message
if (index) {
if (datatokenName)
message = sellDTFeedback[index].replace(/DT/g, datatokenName)
else message = sellDTFeedback[index]
setPricingStepText(message)
}
} }
function setStepCreatePricing(index?: number) { function setStepCreatePricing(index?: number, datatokenName?: string) {
setPricingStep(index) setPricingStep(index)
index && setPricingStepText(createPricingFeedback[index]) let message
if (index) {
if (datatokenName)
message = createPricingFeedback[index].replace(/DT/g, datatokenName)
else message = createPricingFeedback[index]
setPricingStepText(message)
}
} }
async function createPricing( async function createPricing(
@ -68,9 +86,13 @@ function usePricing(): UsePricing {
priceOptions: PriceOptions priceOptions: PriceOptions
): Promise<TransactionReceipt | string | null> { ): Promise<TransactionReceipt | string | null> {
if (!ocean || !account || !accountId) return null if (!ocean || !account || !accountId) return null
setStepCreatePricing(0)
let response = null let response = null
try { try {
setPricingIsLoading(true)
setPricingError(undefined)
const datatokenName = await ocean.datatokens.getName(dataTokenAddress)
setStepCreatePricing(0, datatokenName)
switch (priceOptions.type) { switch (priceOptions.type) {
case 'dynamic': { case 'dynamic': {
setStepCreatePricing(2) setStepCreatePricing(2)
@ -112,7 +134,7 @@ function usePricing(): UsePricing {
} finally { } finally {
setPricingStep(undefined) setPricingStep(undefined)
setPricingStepText(undefined) setPricingStepText(undefined)
setIsLoading(false) setPricingIsLoading(false)
} }
return null return null
} }
@ -123,17 +145,17 @@ function usePricing(): UsePricing {
): Promise<TransactionReceipt | null> { ): Promise<TransactionReceipt | null> {
if (!ocean || !account || !accountId) return null if (!ocean || !account || !accountId) return null
setIsLoading(true)
setPricingError(undefined)
setStepBuyDT(0)
try { try {
const datatokenName = await ocean.datatokens.getName(dataTokenAddress)
setPricingIsLoading(true)
setPricingError(undefined)
setStepBuyDT(0)
const bestPrice = await getBestDataTokenPrice(ocean, dataTokenAddress) const bestPrice = await getBestDataTokenPrice(ocean, dataTokenAddress)
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(1, datatokenName)
Logger.log( Logger.log(
'Buying token from pool', 'Buying token from pool',
bestPrice, bestPrice,
@ -167,7 +189,7 @@ function usePricing(): UsePricing {
bestPrice.value.toString(), bestPrice.value.toString(),
account.getId() account.getId()
) )
setStepBuyDT(1) setStepBuyDT(1, datatokenName)
const exchange = await ocean.fixedRateExchange.buyDT( const exchange = await ocean.fixedRateExchange.buyDT(
bestPrice.address, bestPrice.address,
String(dtAmount), String(dtAmount),
@ -184,7 +206,7 @@ function usePricing(): UsePricing {
} finally { } finally {
setPricingStep(undefined) setPricingStep(undefined)
setPricingStepText(undefined) setPricingStepText(undefined)
setIsLoading(false) setPricingIsLoading(false)
} }
return null return null
} }
@ -198,15 +220,15 @@ function usePricing(): UsePricing {
Logger.error(`'oceanTokenAddress' not set in config`) Logger.error(`'oceanTokenAddress' not set in config`)
return null return null
} }
setIsLoading(true)
setPricingError(undefined)
setStepSellDT(0)
try { try {
const datatokenName = await ocean.datatokens.getName(dataTokenAddress)
setPricingIsLoading(true)
setPricingError(undefined)
setStepSellDT(0, datatokenName)
const pool = await getFirstPool(ocean, dataTokenAddress) const pool = await getFirstPool(ocean, dataTokenAddress)
if (!pool || pool.price === 0) return null if (!pool || pool.price === 0) return null
const price = new Decimal(pool.price).times(0.95).toString() const price = new Decimal(pool.price).times(0.95).toString()
setStepSellDT(1) setStepSellDT(1, datatokenName)
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(),
@ -223,7 +245,7 @@ function usePricing(): UsePricing {
} finally { } finally {
setStepSellDT(undefined) setStepSellDT(undefined)
setPricingStepText(undefined) setPricingStepText(undefined)
setIsLoading(false) setPricingIsLoading(false)
} }
return null return null
} }
@ -234,7 +256,7 @@ function usePricing(): UsePricing {
sellDT, sellDT,
pricingStep, pricingStep,
pricingStepText, pricingStepText,
isLoading, pricingIsLoading,
pricingError pricingError
} }
} }