From 235d8333eb5d7e2c5eba5df3e4f74cce57336628 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Fri, 29 Oct 2021 11:24:20 +0100 Subject: [PATCH] pricing defaults changes, fixes --- .gitignore | 2 +- src/@hooks/useGraphSyncStatus.ts | 2 +- src/@hooks/usePricing.ts | 24 ++++++++-------- src/@types/Price.d.ts | 4 +-- .../Form/FormFields/URLInput/Input.tsx | 1 - .../Asset/AssetActions/Trade/Swap.tsx | 28 +++++++++---------- .../Publish/FormPublish/Pricing/Dynamic.tsx | 18 ++++++------ .../Publish/FormPublish/Pricing/index.tsx | 14 +++++----- src/components/Publish/_constants.ts | 27 ++++++++++-------- 9 files changed, 62 insertions(+), 58 deletions(-) diff --git a/.gitignore b/.gitignore index 4a22a48ec..455176b6c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ networks-metadata.json src/@types/apollo graphql.schema.json src/@types/graph.types.ts -public +tsconfig.tsbuildinfo diff --git a/src/@hooks/useGraphSyncStatus.ts b/src/@hooks/useGraphSyncStatus.ts index d6216cfbe..0c9db884c 100644 --- a/src/@hooks/useGraphSyncStatus.ts +++ b/src/@hooks/useGraphSyncStatus.ts @@ -35,7 +35,7 @@ async function getBlockHead(config: ConfigHelperConfig) { // for ETH main, get block from graph fetch if (config.network === 'mainnet') { const response: any = await fetchGraph(ethGraphUrl, ethGraphQuery) - return Number(response?.data?.blocks[0].number) + return Number(response?.data?.blocks[0]?.number) } // for everything else, create new web3 instance with infura diff --git a/src/@hooks/usePricing.ts b/src/@hooks/usePricing.ts index 0d9e44088..06a0a6ab6 100644 --- a/src/@hooks/usePricing.ts +++ b/src/@hooks/usePricing.ts @@ -22,7 +22,7 @@ interface UsePricing { ) => Promise mint: (tokensToMint: string, ddo: DDO) => Promise buyDT: ( - dtAmount: number | string, + amountDataToken: number | string, price: BestPrice, ddo: DDO ) => Promise @@ -112,7 +112,7 @@ function usePricing(): UsePricing { } async function buyDT( - dtAmount: number | string, + amountDataToken: number | string, price: BestPrice, ddo: DDO ): Promise { @@ -144,7 +144,7 @@ function usePricing(): UsePricing { tx = await ocean.pool.buyDT( accountId, price.address, - String(dtAmount), + String(amountDataToken), oceanAmmount, maxPrice ) @@ -171,7 +171,7 @@ function usePricing(): UsePricing { setStep(2, 'buy', ddo) tx = await ocean.fixedRateExchange.buyDT( price.address, - `${dtAmount}`, + `${amountDataToken}`, accountId ) setStep(3, 'buy', ddo) @@ -222,10 +222,10 @@ function usePricing(): UsePricing { if (!ocean || !accountId || !dtSymbol) return - const { type, oceanAmount, price, weightOnDataToken, swapFee } = + const { type, amountOcean, price, weightOnDataToken, swapFee } = priceOptions - let { dtAmount } = priceOptions + let { amountDataToken } = priceOptions const isPool = type === 'dynamic' if (!isPool && !config.fixedRateExchangeAddress) { @@ -244,25 +244,25 @@ function usePricing(): UsePricing { await ocean.OceanDispenser.activate(dataToken, '1', '1', accountId) } else { // if fixedPrice set dt to max amount - if (!isPool) dtAmount = 1000 - await mint(`${dtAmount}`, ddo) + if (!isPool) amountDataToken = 1000 + await mint(`${amountDataToken}`, ddo) } - // dtAmount for fixed price is set to max + // amountDataToken for fixed price is set to max const tx = isPool ? await ocean.pool .create( accountId, dataToken, - `${dtAmount}`, + `${amountDataToken}`, weightOnDataToken, - `${oceanAmount}`, + `${amountOcean}`, `${swapFee}` ) .next((step: number) => setStep(step, 'pool', ddo)) : type === 'fixed' ? await ocean.fixedRateExchange - .create(dataToken, `${price}`, accountId, `${dtAmount}`) + .create(dataToken, `${price}`, accountId, `${amountDataToken}`) .next((step: number) => setStep(step, 'exchange', ddo)) : await ocean.OceanDispenser.makeMinter(dataToken, accountId).next( (step: number) => setStep(step, 'free', ddo) diff --git a/src/@types/Price.d.ts b/src/@types/Price.d.ts index b4aa3d1d0..37d927c96 100644 --- a/src/@types/Price.d.ts +++ b/src/@types/Price.d.ts @@ -13,8 +13,8 @@ interface BestPrice { interface PriceOptions { price: number - dtAmount: number - oceanAmount: number + amountDataToken: number + amountOcean: number type: 'fixed' | 'dynamic' | 'free' | string weightOnDataToken: string weightOnOcean: string diff --git a/src/components/@shared/Form/FormFields/URLInput/Input.tsx b/src/components/@shared/Form/FormFields/URLInput/Input.tsx index 80c474133..d71fa5fc3 100644 --- a/src/components/@shared/Form/FormFields/URLInput/Input.tsx +++ b/src/components/@shared/Form/FormFields/URLInput/Input.tsx @@ -22,7 +22,6 @@ export default function URLInput({ handleButtonClick(e, field.value)} /> diff --git a/src/components/Asset/AssetActions/Trade/Swap.tsx b/src/components/Asset/AssetActions/Trade/Swap.tsx index 1fc119d0c..2ae41791c 100644 --- a/src/components/Asset/AssetActions/Trade/Swap.tsx +++ b/src/components/Asset/AssetActions/Trade/Swap.tsx @@ -64,54 +64,54 @@ export default function Swap({ if (!ddo || !balance || !values?.type || !price) return async function calculateMaximum() { - const dtAmount = + const amountDataToken = values.type === 'buy' ? new Decimal(maxDt) : new Decimal(balance.datatoken) - const oceanAmount = + const amountOcean = values.type === 'buy' ? new Decimal(balance.ocean) : new Decimal(maxOcean) const maxBuyOcean = await ocean.pool.getOceanReceived( price.address, - `${dtAmount.toString()}` + `${amountDataToken.toString()}` ) const maxBuyDt = await ocean.pool.getDTReceived( price.address, - `${oceanAmount.toString()}` + `${amountOcean.toString()}` ) const maximumDt = values.type === 'buy' - ? dtAmount.greaterThan(new Decimal(maxBuyDt)) + ? amountDataToken.greaterThan(new Decimal(maxBuyDt)) ? maxBuyDt - : dtAmount - : dtAmount.greaterThan(new Decimal(balance.datatoken)) + : amountDataToken + : amountDataToken.greaterThan(new Decimal(balance.datatoken)) ? balance.datatoken - : dtAmount + : amountDataToken const maximumOcean = values.type === 'sell' - ? oceanAmount.greaterThan(new Decimal(maxBuyOcean)) + ? amountOcean.greaterThan(new Decimal(maxBuyOcean)) ? maxBuyOcean - : oceanAmount - : oceanAmount.greaterThan(new Decimal(balance.ocean)) + : amountOcean + : amountOcean.greaterThan(new Decimal(balance.ocean)) ? balance.ocean - : oceanAmount + : amountOcean setMaximumDt(maximumDt.toString()) setMaximumOcean(maximumOcean.toString()) setOceanItem((prevState) => ({ ...prevState, - amount: oceanAmount.toString(), + amount: amountOcean.toString(), maxAmount: maximumOcean.toString() })) setDtItem((prevState) => ({ ...prevState, - amount: dtAmount.toString(), + amount: amountDataToken.toString(), maxAmount: maximumDt.toString() })) } diff --git a/src/components/Publish/FormPublish/Pricing/Dynamic.tsx b/src/components/Publish/FormPublish/Pricing/Dynamic.tsx index f2fd3a650..5ea198836 100644 --- a/src/components/Publish/FormPublish/Pricing/Dynamic.tsx +++ b/src/components/Publish/FormPublish/Pricing/Dynamic.tsx @@ -28,28 +28,28 @@ export default function Dynamic({ content }: { content: any }): ReactElement { weightOnDataToken, weightOnOcean, swapFee, - dtAmount, - oceanAmount + amountDataToken, + amountOcean } = values.pricing const [error, setError] = useState() // Calculate firstPrice whenever user values change useEffect(() => { - if (`${oceanAmount}` === '') return + if (`${amountOcean}` === '') return const tokenAmountOut = 1 const weightRatio = new Decimal(weightOnDataToken).div( new Decimal(weightOnOcean) ) - const diff = new Decimal(dtAmount).minus(tokenAmountOut) - const y = new Decimal(dtAmount).div(diff) + const diff = new Decimal(amountDataToken).minus(tokenAmountOut) + const y = new Decimal(amountDataToken).div(diff) const foo = y.pow(weightRatio).minus(new Decimal(1)) - const tokenAmountIn = new Decimal(oceanAmount) + const tokenAmountIn = new Decimal(amountOcean) .times(foo) .div(new Decimal(1).minus(new Decimal(swapFee / 100))) setFirstPrice(`${tokenAmountIn}`) - }, [swapFee, weightOnOcean, weightOnDataToken, dtAmount, oceanAmount]) + }, [swapFee, weightOnOcean, weightOnDataToken, amountDataToken, amountOcean]) // Check: account, network & insufficient balance useEffect(() => { @@ -90,12 +90,12 @@ export default function Dynamic({ content }: { content: any }): ReactElement {
() const { pricing } = values - const { price, oceanAmount, weightOnOcean, weightOnDataToken, type } = pricing + const { price, amountOcean, weightOnOcean, weightOnDataToken, type } = pricing console.log(pricing) @@ -26,7 +26,7 @@ export default function PricingFields(): ReactElement { function handleTabChange(tabName: string) { const type = tabName.toLowerCase() setFieldValue('pricing.type', type) - type === 'fixed' && setFieldValue('pricing.dtAmount', 1000) + type === 'fixed' && setFieldValue('pricing.amountDataToken', 1000) type === 'free' && price < 1 && setFieldValue('pricing.price', 1) } @@ -34,19 +34,19 @@ export default function PricingFields(): ReactElement { useEffect(() => { if (type === 'fixed' || type === 'free') return - const dtAmount = - isValidNumber(oceanAmount) && + const amountDataToken = + isValidNumber(amountOcean) && isValidNumber(weightOnOcean) && isValidNumber(price) && isValidNumber(weightOnDataToken) - ? new Decimal(oceanAmount) + ? new Decimal(amountOcean) .dividedBy(new Decimal(weightOnOcean)) .dividedBy(new Decimal(price)) .mul(new Decimal(weightOnDataToken)) : 0 - setFieldValue('pricing.dtAmount', dtAmount) - }, [price, oceanAmount, weightOnOcean, weightOnDataToken, type]) + setFieldValue('pricing.amountDataToken', amountDataToken) + }, [price, amountOcean, weightOnOcean, weightOnDataToken, type]) const tabs = [ appConfig.allowFixedPricing === 'true' diff --git a/src/components/Publish/_constants.ts b/src/components/Publish/_constants.ts index 1be75eddd..047378bd7 100644 --- a/src/components/Publish/_constants.ts +++ b/src/components/Publish/_constants.ts @@ -9,13 +9,13 @@ export const initialValues: Partial = { name: '', author: '', description: '', - termsAndConditions: false, - tags: '' + tags: '', + termsAndConditions: false }, services: [ { - files: '', - links: '', + files: [], + links: [], dataTokenOptions: { name: '', symbol: '' }, timeout: 'Forever', access: '', @@ -30,10 +30,10 @@ export const initialValues: Partial = { : allowFixedPricing === 'true' ? 'fixed' : 'free', - dtAmount: allowDynamicPricing === 'true' ? 9 : 1000, - oceanAmount: 21, - weightOnOcean: '7', // 70% on OCEAN - weightOnDataToken: '3', // 30% on datatoken + amountDataToken: allowDynamicPricing === 'true' ? 50 : 1000, + amountOcean: 50, + weightOnOcean: '5', // 50% on OCEAN + weightOnDataToken: '5', // 50% on datatoken swapFee: 0.1 // in % } } @@ -42,7 +42,9 @@ const validationMetadata = { name: Yup.string() .min(4, (param) => `Title must be at least ${param.min} characters`) .required('Required'), - description: Yup.string().min(10).required('Required'), + description: Yup.string() + .min(10, (param) => `Description must be at least ${param.min} characters`) + .required('Required'), author: Yup.string().required('Required'), tags: Yup.string().nullable(), termsAndConditions: Yup.boolean().required('Required') @@ -60,6 +62,9 @@ const validationService = { }) .required('Required'), timeout: Yup.string().required('Required'), + type: Yup.string() + .matches(/Dataset|Algorithm/g, { excludeEmptyString: true }) + .required('Required'), access: Yup.string() .matches(/Compute|Download/g, { excludeEmptyString: true }) .required('Required'), @@ -70,10 +75,10 @@ const validationPricing = { price: Yup.number() .min(1, (param) => `Must be more or equal to ${param.min}`) .required('Required'), - dtAmount: Yup.number() + amountDataToken: Yup.number() .min(9, (param) => `Must be more or equal to ${param.min}`) .required('Required'), - oceanAmount: Yup.number() + amountOcean: Yup.number() .min(21, (param) => `Must be more or equal to ${param.min}`) .required('Required'), type: Yup.string()