From 6e5ae7b2ce9d0de782a13b5d1c86427d4c34614f Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Mon, 19 Oct 2020 02:47:12 -0700 Subject: [PATCH] move to usePricing --- example/src/App.tsx | 4 +- example/src/Pricing.tsx | 57 +++++++ example/src/Publish.tsx | 40 +---- example/src/Trade.tsx | 55 ------- src/hooks/index.ts | 3 +- src/hooks/useCreatePricing/index.ts | 2 - .../useCreatePricing/useCreatePricing.ts | 114 ------------- .../PriceOptions.ts | 0 .../README.md | 16 +- src/hooks/usePricing/index.ts | 2 + .../useTrade.ts => usePricing/usePricing.ts} | 150 ++++++++++++++---- src/hooks/usePublish/DataTokenOptions.ts | 1 - src/hooks/usePublish/README.md | 4 +- src/hooks/usePublish/usePublish.ts | 13 -- src/hooks/useTrade/README.md | 35 ---- src/hooks/useTrade/index.ts | 1 - 16 files changed, 202 insertions(+), 295 deletions(-) create mode 100644 example/src/Pricing.tsx delete mode 100644 example/src/Trade.tsx delete mode 100644 src/hooks/useCreatePricing/index.ts delete mode 100644 src/hooks/useCreatePricing/useCreatePricing.ts rename src/hooks/{useCreatePricing => usePricing}/PriceOptions.ts (100%) rename src/hooks/{useCreatePricing => usePricing}/README.md (60%) create mode 100644 src/hooks/usePricing/index.ts rename src/hooks/{useTrade/useTrade.ts => usePricing/usePricing.ts} (54%) delete mode 100644 src/hooks/useTrade/README.md delete mode 100644 src/hooks/useTrade/index.ts diff --git a/example/src/App.tsx b/example/src/App.tsx index 80dd955..ace802b 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -8,7 +8,7 @@ import { AllDdos } from './AllDdos' import { ConsumeDdo } from './ConsumeDdo' import { NetworkMonitor } from './NetworkMonitor' import { LogLevel } from '@oceanprotocol/lib/dist/node/utils' -import { Trade } from './Trade' +import { Pricing } from './Pricing' const configRinkeby = new ConfigHelper().getConfig('rinkeby') const providerOptions = {} @@ -41,7 +41,7 @@ function App() {
- +
diff --git a/example/src/Pricing.tsx b/example/src/Pricing.tsx new file mode 100644 index 0000000..c4d63f8 --- /dev/null +++ b/example/src/Pricing.tsx @@ -0,0 +1,57 @@ +import React from 'react' +import { useOcean, usePricing } from '@oceanprotocol/react' +// import { useOcean, usePublish } from '@oceanprotocol/react' +import { DDO } from '@oceanprotocol/lib' +import { useState } from 'react' +import { Metadata } from '@oceanprotocol/lib/dist/node/ddo/interfaces/Metadata' + +export function Trade() { + const { ocean, accountId } = useOcean() + const { createPricing,buyDT,sellDT,mint,pricingStep,pricingStepText,pricingIsLoading, pricingError} = usePricing() + const [datatoken, setDatatoken] = useState() + const handleBuy = async () => { + const tx = await buyDT(datatoken,'1') + console.log(tx) + } + const handleSell = async () => { + const tx = await buyDT(datatoken,'1') + console.log(tx) + } + const handleChange = (e: any) => { + setDatatoken(e.target.value) + } + const handlePostForSale = async () => { + if(datatoken){ + const priceOptions = { + price: 7, + dtAmount: 10, + type: 'fixed', + weightOnDataToken: '', + swapFee: '' + } + const tx = await createPricing(datatoken,priceOptions) + console.log(tx) + } + } + return ( + <> +
Trade Datatoken
+
+ Datatoken +
+
+ +
+
+ +
+
+ +
+
+ IsLoading: {pricingIsLoading.toString()} || Status: {pricingStepText} +
+ + + ) +} diff --git a/example/src/Publish.tsx b/example/src/Publish.tsx index 57066d1..6e7e46c 100644 --- a/example/src/Publish.tsx +++ b/example/src/Publish.tsx @@ -1,13 +1,12 @@ import React from 'react' -import { usePublish, useCreatePricing } from '@oceanprotocol/react' -// import { useOcean, usePublish } from '@oceanprotocol/react' +import { usePublish } from '@oceanprotocol/react' import { DDO } from '@oceanprotocol/lib' import { useState } from 'react' import { Metadata } from '@oceanprotocol/lib/dist/node/ddo/interfaces/Metadata' export function Publish() { const { publish, publishStepText, isLoading } = usePublish() - const { createPricing, pricingStep, pricingStepText, pricingIsLoading, pricingError} = useCreatePricing() + const { createPricing, pricingStep, pricingStepText, pricingIsLoading, pricingError} = usePricing() const [ddo, setDdo] = useState() const asset = { @@ -32,37 +31,16 @@ export function Publish() { } const publishAsset = async () => { - const priceOptions = { - price: 7, - dtAmount: 10, - type: 'fixed', - weightOnDataToken: '', - swapFee: '' - } + const datatokenOptions = { - tokensToMint:10 + } const ddo = await publish(asset as Metadata, 'access', datatokenOptions) console.log(ddo) setDdo(ddo) } - const handlePostForSale = async () => { - if(ddo){ - const priceOptions = { - price: 7, - dtAmount: 10, - type: 'fixed', - weightOnDataToken: '', - swapFee: '' - } - const tx = await createPricing(ddo.dataToken,priceOptions) - console.log(tx) - } - else{ - console.error("Publish the asset first") - } - } + return ( <>
Publish
@@ -73,13 +51,7 @@ export function Publish() { IsLoading: {isLoading.toString()} || Status: {publishStepText}
DID: {ddo && ddo.id}
-
- -
-
- IsLoading: {pricingIsLoading.toString()} || pricingStatus: {pricingStepText} -
- + ) } diff --git a/example/src/Trade.tsx b/example/src/Trade.tsx deleted file mode 100644 index f880c01..0000000 --- a/example/src/Trade.tsx +++ /dev/null @@ -1,55 +0,0 @@ -import React from 'react' -import { useOcean, useTrade } from '@oceanprotocol/react' -// import { useOcean, usePublish } from '@oceanprotocol/react' -import { DDO } from '@oceanprotocol/lib' -import { useState } from 'react' -import { Metadata } from '@oceanprotocol/lib/dist/node/ddo/interfaces/Metadata' - -export function Trade() { - const { ocean, accountId } = useOcean() - const { buyDT, sellDT, tradeStep, tradeStepText, tradeIsLoading, tradeError} = useTrade() - const [did, setDid] = useState() - const handleBuy = async () => { - if (!did) { console.error("No DID"); return} - const ddo = await ocean.assets.resolve(did) - if(ddo){ - const tx = await buyDT(ddo.dataToken,'1') - console.log(tx) - } - else{ - console.error("Publish the asset first and create a pricing") - } - } - const handleSell = async () => { - if (!did) { console.error("No DID"); return} - const ddo = await ocean.assets.resolve(did) - if(ddo){ - const tx = await buyDT(ddo.dataToken,'1') - console.log(tx) - } - else{ - console.error("Publish the asset first and create a pricing") - } - } - const handleChange = (e: any) => { - setDid(e.target.value) - } - return ( - <> -
Trade Datatoken
-
- DID -
-
- -
-
- -
-
- IsLoading: {tradeIsLoading.toString()} || Status: {tradeStepText} -
- - - ) -} diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 519a8c1..d4c70a6 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -2,5 +2,4 @@ export * from './useConsume' export * from './useMetadata' export * from './usePublish' export * from './useCompute' -export * from './useCreatePricing' -export * from './useTrade' +export * from './usePricing' diff --git a/src/hooks/useCreatePricing/index.ts b/src/hooks/useCreatePricing/index.ts deleted file mode 100644 index 0982522..0000000 --- a/src/hooks/useCreatePricing/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './useCreatePricing' -export * from './PriceOptions' diff --git a/src/hooks/useCreatePricing/useCreatePricing.ts b/src/hooks/useCreatePricing/useCreatePricing.ts deleted file mode 100644 index 9169470..0000000 --- a/src/hooks/useCreatePricing/useCreatePricing.ts +++ /dev/null @@ -1,114 +0,0 @@ -import { Logger } from '@oceanprotocol/lib' -import { useState } from 'react' -import { useOcean } from 'providers' -import { PriceOptions } from './PriceOptions' -import { TransactionReceipt } from 'web3-core' -import { getBestDataTokenPrice, getFirstPool } from 'utils/dtUtils' -import { Decimal } from 'decimal.js' - -interface UseCreatePricing { - createPricing: ( - dataTokenAddress: string, - priceOptions: PriceOptions - ) => Promise - pricingStep?: number - pricingStepText?: string - pricingError?: string - pricingIsLoading: boolean -} - -export const createPricingFeedback: { [key in number]: string } = { - 0: '1/4 Approving DT ...', - 1: '2/4 Approving Ocean ...', - 2: '3/4 Creating ....', - 3: '4/4 Pricing created' -} - -function useCreatePricing(): UseCreatePricing { - const { ocean, status, account, accountId, config } = useOcean() - const [pricingIsLoading, setPricingIsLoading] = useState(false) - const [pricingStep, setPricingStep] = useState() - const [pricingStepText, setPricingStepText] = useState() - const [pricingError, setPricingError] = useState() - const [dtSymbol, setDtSymbol] = useState() - - function setStepCreatePricing(index?: number) { - setPricingStep(index) - let message - if (index) { - if (dtSymbol) - message = createPricingFeedback[index].replace(/DT/g, dtSymbol) - else message = createPricingFeedback[index] - setPricingStepText(message) - } - } - - async function createPricing( - dataTokenAddress: string, - priceOptions: PriceOptions - ): Promise { - if (!ocean || !account || !accountId) return null - - let response = null - try { - setPricingIsLoading(true) - setPricingError(undefined) - setDtSymbol(await ocean.datatokens.getSymbol(dataTokenAddress)) - setStepCreatePricing(0) - switch (priceOptions.type) { - case 'dynamic': { - setStepCreatePricing(2) - response = await ocean.pool.createDTPool( - accountId, - dataTokenAddress, - priceOptions.dtAmount.toString(), - priceOptions.weightOnDataToken, - priceOptions.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( - dataTokenAddress, - priceOptions.price.toString(), - accountId - ) - setStepCreatePricing(1) - await ocean.datatokens.approve( - dataTokenAddress, - config.fixedRateExchangeAddress, - String(priceOptions.dtAmount), - accountId - ) - setStepCreatePricing(3) - return response - } - } - } catch (error) { - setPricingError(error.message) - Logger.error(error) - } finally { - setPricingStep(undefined) - setPricingStepText(undefined) - setPricingIsLoading(false) - } - return null - } - - return { - createPricing, - pricingStep, - pricingStepText, - pricingIsLoading, - pricingError - } -} - -export { useCreatePricing, UseCreatePricing } -export default useCreatePricing diff --git a/src/hooks/useCreatePricing/PriceOptions.ts b/src/hooks/usePricing/PriceOptions.ts similarity index 100% rename from src/hooks/useCreatePricing/PriceOptions.ts rename to src/hooks/usePricing/PriceOptions.ts diff --git a/src/hooks/useCreatePricing/README.md b/src/hooks/usePricing/README.md similarity index 60% rename from src/hooks/useCreatePricing/README.md rename to src/hooks/usePricing/README.md index 563d572..5ca406f 100644 --- a/src/hooks/useCreatePricing/README.md +++ b/src/hooks/usePricing/README.md @@ -1,6 +1,6 @@ # `usePricing` -Hook with helper utilities to create fixed price exchanges or liquidity pools for your data set +Hook with helper utilities to create fixed price exchanges or liquidity pools for your data set, mint datatokens , buy and sell datatokens ## Usage @@ -13,7 +13,7 @@ export default function MyComponent() { const { accountId } = useOcean() const dataTokenAddress = '0x00000' // Publish helpers - const { createPricing } = useCreatePricing() + const { createPricing } = usePricing() const priceOptions = { price: 10, @@ -27,13 +27,25 @@ export default function MyComponent() { await createPricing(dataTokenAddress, priceOptions) } + async function handleMint() { + await mint(dataTokenAddress, '1') + } + async function handleBuyDT() { + await buyDT(dataTokenAddress, '1') + } + async function handleSellDT() { + await sellDT(dataTokenAddress, '1') + } return (

Post for sale

Your account: {accountId}

+ + +
) } diff --git a/src/hooks/usePricing/index.ts b/src/hooks/usePricing/index.ts new file mode 100644 index 0000000..3b43af5 --- /dev/null +++ b/src/hooks/usePricing/index.ts @@ -0,0 +1,2 @@ +export * from './usePricing' +export * from './PriceOptions' diff --git a/src/hooks/useTrade/useTrade.ts b/src/hooks/usePricing/usePricing.ts similarity index 54% rename from src/hooks/useTrade/useTrade.ts rename to src/hooks/usePricing/usePricing.ts index 8d1380c..a5b1874 100644 --- a/src/hooks/useTrade/useTrade.ts +++ b/src/hooks/usePricing/usePricing.ts @@ -1,11 +1,16 @@ import { Logger } from '@oceanprotocol/lib' import { useState } from 'react' import { useOcean } from 'providers' +import { PriceOptions } from './PriceOptions' import { TransactionReceipt } from 'web3-core' import { getBestDataTokenPrice, getFirstPool } from 'utils/dtUtils' import { Decimal } from 'decimal.js' -interface UseTrade { +interface UsePricing { + createPricing: ( + dataTokenAddress: string, + priceOptions: PriceOptions + ) => Promise buyDT: ( dataTokenAddress: string, dtAmount: number | string @@ -14,12 +19,19 @@ interface UseTrade { dataTokenAddress: string, dtAmount: number | string ) => Promise - tradeStep?: number - tradeStepText?: string - tradeError?: string - tradeIsLoading: boolean + mint: (tokenAddress: string, tokensToMint: string) => void + pricingStep?: number + pricingStepText?: string + pricingError?: string + pricingIsLoading: boolean } +export const createPricingFeedback: { [key in number]: string } = { + 0: '1/4 Approving DT ...', + 1: '2/4 Approving Ocean ...', + 2: '3/4 Creating ....', + 3: '4/4 Pricing created' +} export const buyDTFeedback: { [key in number]: string } = { 0: '1/3 Approving OCEAN ...', 1: '2/3 Buying DT ...', @@ -30,34 +42,48 @@ export const sellDTFeedback: { [key in number]: string } = { 1: '2/3 Selling DT ...', 2: '3/3 DT sold' } - -function useTrade(): UseTrade { +function usePricing(): UsePricing { const { ocean, status, account, accountId, config } = useOcean() - const [tradeIsLoading, setTradeIsLoading] = useState(false) - const [tradeStep, setTradeStep] = useState() - const [tradeStepText, setTradeStepText] = useState() - const [tradeError, setTradeError] = useState() + const [pricingIsLoading, setPricingIsLoading] = useState(false) + const [pricingStep, setPricingStep] = useState() + const [pricingStepText, setPricingStepText] = useState() + const [pricingError, setPricingError] = useState() const [dtSymbol, setDtSymbol] = useState() + function setStepCreatePricing(index?: number) { + setPricingStep(index) + let message + if (index) { + if (dtSymbol) + message = createPricingFeedback[index].replace(/DT/g, dtSymbol) + else message = createPricingFeedback[index] + setPricingStepText(message) + } + } + function setStepBuyDT(index?: number) { - setTradeStep(index) + setPricingStep(index) let message if (index) { if (dtSymbol) message = buyDTFeedback[index].replace(/DT/g, dtSymbol) else message = buyDTFeedback[index] - setTradeStepText(message) + setPricingStepText(message) } } function setStepSellDT(index?: number) { - setTradeStep(index) + setPricingStep(index) let message if (index) { if (dtSymbol) message = sellDTFeedback[index].replace(/DT/g, dtSymbol) else message = sellDTFeedback[index] - setTradeStepText(message) + setPricingStepText(message) } } + async function mint(tokenAddress: string, tokensToMint: string) { + Logger.log('mint function', tokenAddress, accountId) + await ocean.datatokens.mint(tokenAddress, accountId, tokensToMint) + } async function buyDT( dataTokenAddress: string, dtAmount: number | string @@ -66,8 +92,8 @@ function useTrade(): UseTrade { try { setDtSymbol(await ocean.datatokens.getSymbol(dataTokenAddress)) - setTradeIsLoading(true) - setTradeError(undefined) + setPricingIsLoading(true) + setPricingError(undefined) setStepBuyDT(0) const bestPrice = await getBestDataTokenPrice(ocean, dataTokenAddress) switch (bestPrice?.type) { @@ -120,12 +146,12 @@ function useTrade(): UseTrade { } } } catch (error) { - setTradeError(error.message) + setPricingError(error.message) Logger.error(error) } finally { - setTradeStep(undefined) - setTradeStepText(undefined) - setTradeIsLoading(false) + setStepBuyDT(undefined) + setPricingStepText(undefined) + setPricingIsLoading(false) } return null } @@ -141,8 +167,8 @@ function useTrade(): UseTrade { } try { setDtSymbol(await ocean.datatokens.getSymbol(dataTokenAddress)) - setTradeIsLoading(true) - setTradeError(undefined) + setPricingIsLoading(true) + setPricingError(undefined) setStepSellDT(0) const pool = await getFirstPool(ocean, dataTokenAddress) if (!pool || pool.price === 0) return null @@ -159,25 +185,85 @@ function useTrade(): UseTrade { Logger.log('DT sell response', sellResponse) return sellResponse } catch (error) { - setTradeError(error.message) + setPricingError(error.message) Logger.error(error) } finally { setStepSellDT(undefined) - setTradeStepText(undefined) - setTradeIsLoading(false) + setPricingStepText(undefined) + setPricingIsLoading(false) + } + return null + } + + async function createPricing( + dataTokenAddress: string, + priceOptions: PriceOptions + ): Promise { + if (!ocean || !account || !accountId) return null + + let response = null + try { + setPricingIsLoading(true) + setPricingError(undefined) + setDtSymbol(await ocean.datatokens.getSymbol(dataTokenAddress)) + setStepCreatePricing(0) + switch (priceOptions.type) { + case 'dynamic': { + setStepCreatePricing(2) + response = await ocean.pool.createDTPool( + accountId, + dataTokenAddress, + priceOptions.dtAmount.toString(), + priceOptions.weightOnDataToken, + priceOptions.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( + dataTokenAddress, + priceOptions.price.toString(), + accountId + ) + setStepCreatePricing(1) + await ocean.datatokens.approve( + dataTokenAddress, + config.fixedRateExchangeAddress, + String(priceOptions.dtAmount), + accountId + ) + setStepCreatePricing(3) + return response + } + } + } catch (error) { + setPricingError(error.message) + Logger.error(error) + } finally { + setPricingStep(undefined) + setPricingStepText(undefined) + setPricingIsLoading(false) } return null } return { + createPricing, buyDT, sellDT, - tradeStep, - tradeStepText, - tradeIsLoading, - tradeError + mint, + pricingStep, + pricingStepText, + pricingIsLoading, + pricingError } } -export { useTrade, UseTrade } -export default useTrade +export { usePricing, UsePricing } +export default usePricing diff --git a/src/hooks/usePublish/DataTokenOptions.ts b/src/hooks/usePublish/DataTokenOptions.ts index 2579a9c..9bb28fb 100644 --- a/src/hooks/usePublish/DataTokenOptions.ts +++ b/src/hooks/usePublish/DataTokenOptions.ts @@ -1,5 +1,4 @@ export interface DataTokenOptions { - tokensToMint: number cap?: string name?: string symbol?: string diff --git a/src/hooks/usePublish/README.md b/src/hooks/usePublish/README.md index c1e789f..1acab0b 100644 --- a/src/hooks/usePublish/README.md +++ b/src/hooks/usePublish/README.md @@ -1,6 +1,6 @@ # `usePublish` -Create data tokens, Mint and Publish data sets +Create datatoken and publish data sets ## Usage @@ -25,7 +25,7 @@ export default function MyComponent() { } const dataTokenOptions = { - tokensToMint: 10 + } async function handlePublish() { diff --git a/src/hooks/usePublish/usePublish.ts b/src/hooks/usePublish/usePublish.ts index 844ef36..2f37b7f 100644 --- a/src/hooks/usePublish/usePublish.ts +++ b/src/hooks/usePublish/usePublish.ts @@ -18,7 +18,6 @@ interface UsePublish { timeout?: number, providerUri?: string ) => Promise - mint: (tokenAddress: string, tokensToMint: string) => void publishStep?: number publishStepText?: string publishError?: string @@ -36,12 +35,6 @@ function usePublish(): UsePublish { setPublishStep(index) index && setPublishStepText(publishFeedback[index]) } - - async function mint(tokenAddress: string, tokensToMint: string) { - Logger.log('mint function', tokenAddress, accountId) - await ocean.datatokens.mint(tokenAddress, accountId, tokensToMint) - } - /** * Publish an asset.It also creates the datatoken, mints tokens and gives the market allowance * @param {Metadata} asset The metadata of the asset. @@ -62,8 +55,6 @@ function usePublish(): UsePublish { setPublishError(undefined) try { - const tokensToMint = dataTokenOptions.tokensToMint.toString() - const publishedDate = new Date(Date.now()).toISOString().split('.')[0] + 'Z' let timeout = 0 @@ -151,9 +142,6 @@ function usePublish(): UsePublish { .next(setStep) Logger.log('ddo created', ddo) setStep(7) - await mint(ddo.dataToken, tokensToMint) - Logger.log(`minted ${tokensToMint} tokens`) - // await createPricing(priceOptions, ddo.dataToken, tokensToMint) // setStep(8) return ddo @@ -168,7 +156,6 @@ function usePublish(): UsePublish { return { publish, - mint, publishStep, publishStepText, isLoading, diff --git a/src/hooks/useTrade/README.md b/src/hooks/useTrade/README.md deleted file mode 100644 index e6f0e85..0000000 --- a/src/hooks/useTrade/README.md +++ /dev/null @@ -1,35 +0,0 @@ -# `usePricing` - -Hook to buy and sell datatokens - -## Usage - -```tsx -import React from 'react' -import { useOcean, useTrade } from '@oceanprotocol/react' -import { Metadata } from '@oceanprotocol/lib' - -export default function MyComponent() { - const { accountId } = useOcean() - const dataTokenAddress = '0x00000' - // Publish helpers - const { buyDT, sellDT } = useTrade() - - - async function handleBuyDT() { - await buyDT(dataTokenAddress, '1') - } - async function handleSellDT() { - await sellDT(dataTokenAddress, '1') - } - - return ( -
-

Trade

- - - -
- ) -} -``` diff --git a/src/hooks/useTrade/index.ts b/src/hooks/useTrade/index.ts deleted file mode 100644 index ba87662..0000000 --- a/src/hooks/useTrade/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './useTrade'