mirror of
https://github.com/oceanprotocol/react.git
synced 2025-02-14 21:10:38 +01:00
refactor createPricing
This commit is contained in:
parent
1cd3c6055d
commit
7c5eba07c3
@ -11,10 +11,10 @@ interface UsePricing {
|
|||||||
dtName?: string
|
dtName?: string
|
||||||
createPricing: (
|
createPricing: (
|
||||||
priceOptions: PriceOptions
|
priceOptions: PriceOptions
|
||||||
) => Promise<TransactionReceipt | string | null>
|
) => Promise<TransactionReceipt | string | void>
|
||||||
buyDT: (dtAmount: number | string) => Promise<TransactionReceipt | null>
|
buyDT: (dtAmount: number | string) => Promise<TransactionReceipt | void>
|
||||||
sellDT: (dtAmount: number | string) => Promise<TransactionReceipt | null>
|
sellDT: (dtAmount: number | string) => Promise<TransactionReceipt | void>
|
||||||
mint: (tokensToMint: string) => void
|
mint: (tokensToMint: string) => Promise<TransactionReceipt>
|
||||||
pricingStep?: number
|
pricingStep?: number
|
||||||
pricingStepText?: string
|
pricingStepText?: string
|
||||||
pricingError?: string
|
pricingError?: string
|
||||||
@ -22,10 +22,11 @@ interface UsePricing {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const createPricingFeedback: { [key in number]: string } = {
|
export const createPricingFeedback: { [key in number]: string } = {
|
||||||
0: '1/4 Approving DT ...',
|
0: 'Minting DT ...',
|
||||||
1: '2/4 Approving Ocean ...',
|
1: 'Approving DT ...',
|
||||||
2: '3/4 Creating ....',
|
2: 'Approving Ocean ...',
|
||||||
3: '4/4 Pricing created'
|
3: 'Creating ...',
|
||||||
|
4: 'Pricing created.'
|
||||||
}
|
}
|
||||||
|
|
||||||
export const buyDTFeedback: { [key in number]: string } = {
|
export const buyDTFeedback: { [key in number]: string } = {
|
||||||
@ -98,15 +99,16 @@ function usePricing(ddo: DDO): UsePricing {
|
|||||||
setPricingStepText(message)
|
setPricingStepText(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function mint(tokensToMint: string) {
|
async function mint(tokensToMint: string): Promise<TransactionReceipt> {
|
||||||
Logger.log('mint function', dataToken, accountId)
|
Logger.log('mint function', dataToken, accountId)
|
||||||
await ocean.datatokens.mint(dataToken, accountId, tokensToMint)
|
const tx = await ocean.datatokens.mint(dataToken, accountId, tokensToMint)
|
||||||
|
return tx
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buyDT(
|
async function buyDT(
|
||||||
dtAmount: number | string
|
dtAmount: number | string
|
||||||
): Promise<TransactionReceipt | null> {
|
): Promise<TransactionReceipt | void> {
|
||||||
if (!ocean || !account || !accountId) return null
|
if (!ocean || !account || !accountId) return
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setPricingIsLoading(true)
|
setPricingIsLoading(true)
|
||||||
@ -139,11 +141,11 @@ function usePricing(ddo: DDO): UsePricing {
|
|||||||
case 'exchange': {
|
case 'exchange': {
|
||||||
if (!config.oceanTokenAddress) {
|
if (!config.oceanTokenAddress) {
|
||||||
Logger.error(`'oceanTokenAddress' not set in config`)
|
Logger.error(`'oceanTokenAddress' not set in config`)
|
||||||
return null
|
return
|
||||||
}
|
}
|
||||||
if (!config.fixedRateExchangeAddress) {
|
if (!config.fixedRateExchangeAddress) {
|
||||||
Logger.error(`'fixedRateExchangeAddress' not set in config`)
|
Logger.error(`'fixedRateExchangeAddress' not set in config`)
|
||||||
return null
|
return
|
||||||
}
|
}
|
||||||
Logger.log('Buying token from exchange', bestPrice, account.getId())
|
Logger.log('Buying token from exchange', bestPrice, account.getId())
|
||||||
await ocean.datatokens.approve(
|
await ocean.datatokens.approve(
|
||||||
@ -171,17 +173,16 @@ function usePricing(ddo: DDO): UsePricing {
|
|||||||
setPricingStepText(undefined)
|
setPricingStepText(undefined)
|
||||||
setPricingIsLoading(false)
|
setPricingIsLoading(false)
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sellDT(
|
async function sellDT(
|
||||||
dtAmount: number | string
|
dtAmount: number | string
|
||||||
): Promise<TransactionReceipt | null> {
|
): Promise<TransactionReceipt | void> {
|
||||||
if (!ocean || !account || !accountId) return null
|
if (!ocean || !account || !accountId) return
|
||||||
|
|
||||||
if (!config.oceanTokenAddress) {
|
if (!config.oceanTokenAddress) {
|
||||||
Logger.error(`'oceanTokenAddress' not set in config`)
|
Logger.error(`'oceanTokenAddress' not set in config`)
|
||||||
return null
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -189,7 +190,7 @@ function usePricing(ddo: DDO): UsePricing {
|
|||||||
setPricingError(undefined)
|
setPricingError(undefined)
|
||||||
setStepSellDT(0)
|
setStepSellDT(0)
|
||||||
const pool = await getFirstPool(ocean, dataToken)
|
const pool = await getFirstPool(ocean, dataToken)
|
||||||
if (!pool || pool.price === 0) return null
|
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(1)
|
||||||
Logger.log('Selling token to pool', pool, account.getId(), price)
|
Logger.log('Selling token to pool', pool, account.getId(), price)
|
||||||
@ -210,55 +211,55 @@ function usePricing(ddo: DDO): UsePricing {
|
|||||||
setPricingStepText(undefined)
|
setPricingStepText(undefined)
|
||||||
setPricingIsLoading(false)
|
setPricingIsLoading(false)
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createPricing(
|
async function createPricing(
|
||||||
priceOptions: PriceOptions
|
priceOptions: PriceOptions
|
||||||
): Promise<TransactionReceipt | string | null> {
|
): Promise<TransactionReceipt | string | void> {
|
||||||
if (!ocean || !account || !accountId) return null
|
if (!ocean || !account || !accountId) return
|
||||||
|
|
||||||
|
const { type, dtAmount, price, weightOnDataToken, swapFee } = priceOptions
|
||||||
|
const isPool = type === 'dynamic'
|
||||||
|
|
||||||
|
if (!isPool && !config.fixedRateExchangeAddress) {
|
||||||
|
Logger.error(`'fixedRateExchangeAddress' not set in ccnfig.`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let response = null
|
|
||||||
try {
|
try {
|
||||||
setPricingIsLoading(true)
|
setPricingIsLoading(true)
|
||||||
setPricingError(undefined)
|
setPricingError(undefined)
|
||||||
setStepCreatePricing(0)
|
|
||||||
|
|
||||||
switch (priceOptions.type) {
|
setStepCreatePricing(0)
|
||||||
case 'dynamic': {
|
await mint(`${dtAmount}`)
|
||||||
setStepCreatePricing(2)
|
|
||||||
response = await ocean.pool.createDTPool(
|
setStepCreatePricing(3)
|
||||||
|
const response = isPool
|
||||||
|
? // TODO: in ocean.js: ocean.pool.createDTPool should be ocean.pool.create
|
||||||
|
// And if it involves mutliple wallet interacts the method itself should emit step events.
|
||||||
|
await ocean.pool.createDTPool(
|
||||||
accountId,
|
accountId,
|
||||||
dataToken,
|
dataToken,
|
||||||
priceOptions.dtAmount.toString(),
|
`${dtAmount}`,
|
||||||
priceOptions.weightOnDataToken,
|
weightOnDataToken,
|
||||||
priceOptions.swapFee
|
swapFee
|
||||||
)
|
|
||||||
setStepCreatePricing(3)
|
|
||||||
return response
|
|
||||||
}
|
|
||||||
case 'fixed': {
|
|
||||||
if (!config.fixedRateExchangeAddress) {
|
|
||||||
Logger.error(`'fixedRateExchangeAddress' not set in ccnfig.`)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
setStepCreatePricing(2)
|
|
||||||
response = await ocean.fixedRateExchange.create(
|
|
||||||
dataToken,
|
|
||||||
priceOptions.price.toString(),
|
|
||||||
accountId
|
|
||||||
)
|
)
|
||||||
|
: // TODO: in ocean.js: ocean.fixedRateExchange.create should return tx receipt
|
||||||
|
await ocean.fixedRateExchange.create(dataToken, `${price}`, accountId)
|
||||||
|
|
||||||
|
// TODO: why is approve after the creation?
|
||||||
|
if (!isPool && config.fixedRateExchangeAddress) {
|
||||||
setStepCreatePricing(1)
|
setStepCreatePricing(1)
|
||||||
await ocean.datatokens.approve(
|
await ocean.datatokens.approve(
|
||||||
dataToken,
|
dataToken,
|
||||||
config.fixedRateExchangeAddress,
|
config.fixedRateExchangeAddress,
|
||||||
String(priceOptions.dtAmount),
|
`${dtAmount}`,
|
||||||
accountId
|
accountId
|
||||||
)
|
)
|
||||||
setStepCreatePricing(3)
|
}
|
||||||
|
|
||||||
|
setStepCreatePricing(4)
|
||||||
return response
|
return response
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setPricingError(error.message)
|
setPricingError(error.message)
|
||||||
Logger.error(error)
|
Logger.error(error)
|
||||||
@ -267,7 +268,6 @@ function usePricing(ddo: DDO): UsePricing {
|
|||||||
setPricingStepText(undefined)
|
setPricingStepText(undefined)
|
||||||
setPricingIsLoading(false)
|
setPricingIsLoading(false)
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user