This commit is contained in:
alexcos20 2020-10-14 05:05:45 -07:00
parent ad270f8a39
commit 8896e776b3
3 changed files with 29 additions and 25 deletions

View File

@ -16,15 +16,15 @@ import { Decimal } from 'decimal.js'
interface UsePricing { interface UsePricing {
createPricing: ( createPricing: (
dataTokenAddress: string, dataTokenAddress: string,
priceOptions: PriceOptions, priceOptions: PriceOptions
) => Promise<TransactionReceipt | null> ) => Promise<TransactionReceipt | null>
buyDT: ( buyDT: (
dataTokenAddress: string, dataTokenAddress: string,
dtAmount: number | string, dtAmount: number | string
) => Promise<TransactionReceipt | null> ) => Promise<TransactionReceipt | null>
sellDT: ( sellDT: (
dataTokenAddress: string, dataTokenAddress: string,
dtAmount: number | string, dtAmount: number | string
) => Promise<TransactionReceipt | null> ) => Promise<TransactionReceipt | null>
pricingStep?: number pricingStep?: number
pricingStepText?: string pricingStepText?: string
@ -35,19 +35,19 @@ interface UsePricing {
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'
} }
export const createPricingFeedback:{ [key in number]: string } = { export const createPricingFeedback: { [key in number]: string } = {
0: '1/4 Approving DT ...', 0: '1/4 Approving DT ...',
1: '2/4 Approving Ocean ...', 1: '2/4 Approving Ocean ...',
2: '3/4 Creating ....', 2: '3/4 Creating ....',
3: '4/4 Pricing created', 3: '4/4 Pricing created'
} }
function usePricing(): UsePricing { function usePricing(): UsePricing {
@ -76,11 +76,11 @@ function usePricing(): UsePricing {
): Promise<TransactionReceipt | null> { ): Promise<TransactionReceipt | null> {
setStepCreatePricing(0) setStepCreatePricing(0)
let response = null let response = null
try{ try {
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, dataTokenAddress,
priceOptions.dtAmount.toString(), priceOptions.dtAmount.toString(),
@ -96,7 +96,7 @@ function usePricing(): UsePricing {
return null return null
} }
setStepCreatePricing(2) setStepCreatePricing(2)
response=await ocean.fixedRateExchange.create( response = await ocean.fixedRateExchange.create(
dataTokenAddress, dataTokenAddress,
priceOptions.price.toString(), priceOptions.price.toString(),
accountId accountId
@ -112,20 +112,19 @@ function usePricing(): UsePricing {
return response return response
} }
} }
} } catch (error) {
catch (error) {
setPricingError(error.message) setPricingError(error.message)
Logger.error(error) Logger.error(error)
} finally { } finally {
setPricingStep(undefined) setPricingStep(undefined)
setPricingStepText(undefined) setPricingStepText(undefined)
setIsLoading(false) setIsLoading(false)
}
} }
}
async function buyDT( async function buyDT(
dataTokenAddress: string, dataTokenAddress: string,
dtAmount: number | string, dtAmount: number | string
): Promise<TransactionReceipt | null> { ): Promise<TransactionReceipt | null> {
if (!ocean || !account || !accountId) return if (!ocean || !account || !accountId) return
@ -140,7 +139,12 @@ function usePricing(): UsePricing {
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)
Logger.log('Buying token from pool', bestPrice, account.getId(), price) Logger.log(
'Buying token from pool',
bestPrice,
account.getId(),
price
)
const buyResponse = await ocean.pool.buyDT( const buyResponse = await ocean.pool.buyDT(
account.getId(), account.getId(),
bestPrice.address, bestPrice.address,
@ -191,7 +195,7 @@ function usePricing(): UsePricing {
async function sellDT( async function sellDT(
dataTokenAddress: string, dataTokenAddress: string,
dtAmount: number | string, dtAmount: number | string
): Promise<TransactionReceipt | null> { ): Promise<TransactionReceipt | null> {
if (!ocean || !account || !accountId) return if (!ocean || !account || !accountId) return
@ -200,16 +204,16 @@ function usePricing(): UsePricing {
setStepSellDT(0) setStepSellDT(0)
try { try {
const pool=await getFirstPool(ocean, dataTokenAddress) const pool = await getFirstPool(ocean, dataTokenAddress)
const price = new Decimal(pool.value).times(0.95).toString() const price = new Decimal(pool.value).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)
const sellResponse = await ocean.pool.sellDT( const sellResponse = await ocean.pool.sellDT(
account.getId(), account.getId(),
pool.address, pool.address,
String(dtAmount), String(dtAmount),
price price
) )
setStepSellDT(2) setStepSellDT(2)
Logger.log('DT sell response', sellResponse) Logger.log('DT sell response', sellResponse)
return sellResponse return sellResponse
@ -222,7 +226,7 @@ function usePricing(): UsePricing {
setIsLoading(false) setIsLoading(false)
} }
} }
return { return {
createPricing, createPricing,
buyDT, buyDT,

View File

@ -137,7 +137,7 @@ function OceanProvider({
} }
async function logout() { async function logout() {
// TODO: #67 check how is the proper way to logout // TODO: #67 check how is the proper way to logout
web3Modal?.clearCachedProvider() if (web3Modal) web3Modal.clearCachedProvider()
} }
// TODO: #68 Refetch balance periodically, or figure out some event to subscribe to // TODO: #68 Refetch balance periodically, or figure out some event to subscribe to

View File

@ -14,5 +14,5 @@
"importHelpers": true, "importHelpers": true,
"strict": true "strict": true
}, },
"include": ["./src/@types", "./src/index.ts"] "include": ["./src/@types", "./src/hooks", "./src/index.ts"]
} }