mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
small refactor, pass DDO, set datatoken symbol
This commit is contained in:
parent
a26454ff99
commit
95b7849ca6
@ -1,5 +1,5 @@
|
|||||||
import { Logger } from '@oceanprotocol/lib'
|
import { DDO, Logger } from '@oceanprotocol/lib'
|
||||||
import { useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { useOcean } from 'providers'
|
import { useOcean } from 'providers'
|
||||||
import { PriceOptions } from './PriceOptions'
|
import { PriceOptions } from './PriceOptions'
|
||||||
import { TransactionReceipt } from 'web3-core'
|
import { TransactionReceipt } from 'web3-core'
|
||||||
@ -8,18 +8,11 @@ import { Decimal } from 'decimal.js'
|
|||||||
|
|
||||||
interface UsePricing {
|
interface UsePricing {
|
||||||
createPricing: (
|
createPricing: (
|
||||||
dataTokenAddress: string,
|
|
||||||
priceOptions: PriceOptions
|
priceOptions: PriceOptions
|
||||||
) => Promise<TransactionReceipt | string | null>
|
) => Promise<TransactionReceipt | string | null>
|
||||||
buyDT: (
|
buyDT: (dtAmount: number | string) => Promise<TransactionReceipt | null>
|
||||||
dataTokenAddress: string,
|
sellDT: (dtAmount: number | string) => Promise<TransactionReceipt | null>
|
||||||
dtAmount: number | string
|
mint: (tokensToMint: string) => void
|
||||||
) => Promise<TransactionReceipt | null>
|
|
||||||
sellDT: (
|
|
||||||
dataTokenAddress: string,
|
|
||||||
dtAmount: number | string
|
|
||||||
) => Promise<TransactionReceipt | null>
|
|
||||||
mint: (tokenAddress: string, tokensToMint: string) => void
|
|
||||||
pricingStep?: number
|
pricingStep?: number
|
||||||
pricingStepText?: string
|
pricingStepText?: string
|
||||||
pricingError?: string
|
pricingError?: string
|
||||||
@ -32,70 +25,87 @@ export const createPricingFeedback: { [key in number]: string } = {
|
|||||||
2: '3/4 Creating ....',
|
2: '3/4 Creating ....',
|
||||||
3: '4/4 Pricing created'
|
3: '4/4 Pricing created'
|
||||||
}
|
}
|
||||||
|
|
||||||
export const buyDTFeedback: { [key in number]: string } = {
|
export const buyDTFeedback: { [key in number]: string } = {
|
||||||
0: '1/3 Approving OCEAN ...',
|
0: '1/3 Approving OCEAN ...',
|
||||||
1: '2/3 Buying DT ...',
|
1: '2/3 Buying DT ...',
|
||||||
2: '3/3 DT Bought'
|
2: '3/3 DT Bought'
|
||||||
}
|
}
|
||||||
|
|
||||||
export const sellDTFeedback: { [key in number]: string } = {
|
export const sellDTFeedback: { [key in number]: string } = {
|
||||||
0: '1/3 Approving DT ...',
|
0: '1/3 Approving DT ...',
|
||||||
1: '2/3 Selling DT ...',
|
1: '2/3 Selling DT ...',
|
||||||
2: '3/3 DT sold'
|
2: '3/3 DT sold'
|
||||||
}
|
}
|
||||||
function usePricing(): UsePricing {
|
|
||||||
const { ocean, status, account, accountId, config } = useOcean()
|
function usePricing(ddo: DDO): UsePricing {
|
||||||
|
const { ocean, account, accountId, config } = useOcean()
|
||||||
const [pricingIsLoading, setPricingIsLoading] = useState(false)
|
const [pricingIsLoading, setPricingIsLoading] = useState(false)
|
||||||
const [pricingStep, setPricingStep] = useState<number | undefined>()
|
const [pricingStep, setPricingStep] = useState<number>()
|
||||||
const [pricingStepText, setPricingStepText] = useState<string | undefined>()
|
const [pricingStepText, setPricingStepText] = useState<string>()
|
||||||
const [pricingError, setPricingError] = useState<string | undefined>()
|
const [pricingError, setPricingError] = useState<string>()
|
||||||
const [dtSymbol, setDtSymbol] = useState<string>()
|
const [dtSymbol, setDtSymbol] = useState<string>()
|
||||||
|
|
||||||
|
const { dataToken, dataTokenInfo } = ddo
|
||||||
|
|
||||||
|
// Get Datatoken symbol, from DDO first, then from chain
|
||||||
|
useEffect(() => {
|
||||||
|
if (!dataToken) return
|
||||||
|
|
||||||
|
async function init() {
|
||||||
|
const dtSymbol =
|
||||||
|
dataTokenInfo?.symbol || (await ocean?.datatokens.getSymbol(dataToken))
|
||||||
|
setDtSymbol(dtSymbol)
|
||||||
|
}
|
||||||
|
init()
|
||||||
|
}, [ocean, dataToken, dataTokenInfo?.symbol])
|
||||||
|
|
||||||
function setStepCreatePricing(index?: number) {
|
function setStepCreatePricing(index?: number) {
|
||||||
setPricingStep(index)
|
setPricingStep(index)
|
||||||
let message
|
if (!index) return
|
||||||
if (index) {
|
|
||||||
if (dtSymbol)
|
const message = dtSymbol
|
||||||
message = createPricingFeedback[index].replace(/DT/g, dtSymbol)
|
? createPricingFeedback[index].replace(/DT/g, dtSymbol)
|
||||||
else message = createPricingFeedback[index]
|
: createPricingFeedback[index]
|
||||||
setPricingStepText(message)
|
setPricingStepText(message)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setStepBuyDT(index?: number) {
|
function setStepBuyDT(index?: number) {
|
||||||
setPricingStep(index)
|
setPricingStep(index)
|
||||||
let message
|
if (!index) return
|
||||||
if (index) {
|
|
||||||
if (dtSymbol) message = buyDTFeedback[index].replace(/DT/g, dtSymbol)
|
const message = dtSymbol
|
||||||
else message = buyDTFeedback[index]
|
? buyDTFeedback[index].replace(/DT/g, dtSymbol)
|
||||||
setPricingStepText(message)
|
: buyDTFeedback[index]
|
||||||
}
|
setPricingStepText(message)
|
||||||
}
|
}
|
||||||
function setStepSellDT(index?: number) {
|
function setStepSellDT(index?: number) {
|
||||||
setPricingStep(index)
|
setPricingStep(index)
|
||||||
let message
|
if (!index) return
|
||||||
if (index) {
|
|
||||||
if (dtSymbol) message = sellDTFeedback[index].replace(/DT/g, dtSymbol)
|
const message = dtSymbol
|
||||||
else message = sellDTFeedback[index]
|
? sellDTFeedback[index].replace(/DT/g, dtSymbol)
|
||||||
setPricingStepText(message)
|
: sellDTFeedback[index]
|
||||||
}
|
setPricingStepText(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function mint(tokenAddress: string, tokensToMint: string) {
|
async function mint(tokensToMint: string) {
|
||||||
Logger.log('mint function', tokenAddress, accountId)
|
Logger.log('mint function', dataToken, accountId)
|
||||||
await ocean.datatokens.mint(tokenAddress, accountId, tokensToMint)
|
await ocean.datatokens.mint(dataToken, accountId, tokensToMint)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buyDT(
|
async function buyDT(
|
||||||
dataTokenAddress: string,
|
|
||||||
dtAmount: number | string
|
dtAmount: number | string
|
||||||
): Promise<TransactionReceipt | null> {
|
): Promise<TransactionReceipt | null> {
|
||||||
if (!ocean || !account || !accountId) return null
|
if (!ocean || !account || !accountId) return null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setDtSymbol(await ocean.datatokens.getSymbol(dataTokenAddress))
|
setDtSymbol(await ocean.datatokens.getSymbol(dataToken))
|
||||||
setPricingIsLoading(true)
|
setPricingIsLoading(true)
|
||||||
setPricingError(undefined)
|
setPricingError(undefined)
|
||||||
setStepBuyDT(0)
|
setStepBuyDT(0)
|
||||||
const bestPrice = await getBestDataTokenPrice(ocean, dataTokenAddress)
|
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()
|
||||||
@ -157,20 +167,21 @@ function usePricing(): UsePricing {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function sellDT(
|
async function sellDT(
|
||||||
dataTokenAddress: string,
|
|
||||||
dtAmount: number | string
|
dtAmount: number | string
|
||||||
): Promise<TransactionReceipt | null> {
|
): Promise<TransactionReceipt | null> {
|
||||||
if (!ocean || !account || !accountId) return null
|
if (!ocean || !account || !accountId) return null
|
||||||
|
|
||||||
if (!config.oceanTokenAddress) {
|
if (!config.oceanTokenAddress) {
|
||||||
Logger.error(`'oceanTokenAddress' not set in config`)
|
Logger.error(`'oceanTokenAddress' not set in config`)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setDtSymbol(await ocean.datatokens.getSymbol(dataTokenAddress))
|
setDtSymbol(await ocean.datatokens.getSymbol(dataToken))
|
||||||
setPricingIsLoading(true)
|
setPricingIsLoading(true)
|
||||||
setPricingError(undefined)
|
setPricingError(undefined)
|
||||||
setStepSellDT(0)
|
setStepSellDT(0)
|
||||||
const pool = await getFirstPool(ocean, dataTokenAddress)
|
const pool = await getFirstPool(ocean, dataToken)
|
||||||
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)
|
||||||
@ -196,7 +207,6 @@ function usePricing(): UsePricing {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createPricing(
|
async function createPricing(
|
||||||
dataTokenAddress: string,
|
|
||||||
priceOptions: PriceOptions
|
priceOptions: PriceOptions
|
||||||
): Promise<TransactionReceipt | string | null> {
|
): Promise<TransactionReceipt | string | null> {
|
||||||
if (!ocean || !account || !accountId) return null
|
if (!ocean || !account || !accountId) return null
|
||||||
@ -205,14 +215,15 @@ function usePricing(): UsePricing {
|
|||||||
try {
|
try {
|
||||||
setPricingIsLoading(true)
|
setPricingIsLoading(true)
|
||||||
setPricingError(undefined)
|
setPricingError(undefined)
|
||||||
setDtSymbol(await ocean.datatokens.getSymbol(dataTokenAddress))
|
setDtSymbol(await ocean.datatokens.getSymbol(dataToken))
|
||||||
setStepCreatePricing(0)
|
setStepCreatePricing(0)
|
||||||
|
|
||||||
switch (priceOptions.type) {
|
switch (priceOptions.type) {
|
||||||
case 'dynamic': {
|
case 'dynamic': {
|
||||||
setStepCreatePricing(2)
|
setStepCreatePricing(2)
|
||||||
response = await ocean.pool.createDTPool(
|
response = await ocean.pool.createDTPool(
|
||||||
accountId,
|
accountId,
|
||||||
dataTokenAddress,
|
dataToken,
|
||||||
priceOptions.dtAmount.toString(),
|
priceOptions.dtAmount.toString(),
|
||||||
priceOptions.weightOnDataToken,
|
priceOptions.weightOnDataToken,
|
||||||
priceOptions.swapFee
|
priceOptions.swapFee
|
||||||
@ -227,13 +238,13 @@ function usePricing(): UsePricing {
|
|||||||
}
|
}
|
||||||
setStepCreatePricing(2)
|
setStepCreatePricing(2)
|
||||||
response = await ocean.fixedRateExchange.create(
|
response = await ocean.fixedRateExchange.create(
|
||||||
dataTokenAddress,
|
dataToken,
|
||||||
priceOptions.price.toString(),
|
priceOptions.price.toString(),
|
||||||
accountId
|
accountId
|
||||||
)
|
)
|
||||||
setStepCreatePricing(1)
|
setStepCreatePricing(1)
|
||||||
await ocean.datatokens.approve(
|
await ocean.datatokens.approve(
|
||||||
dataTokenAddress,
|
dataToken,
|
||||||
config.fixedRateExchangeAddress,
|
config.fixedRateExchangeAddress,
|
||||||
String(priceOptions.dtAmount),
|
String(priceOptions.dtAmount),
|
||||||
accountId
|
accountId
|
||||||
|
Loading…
x
Reference in New Issue
Block a user