1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-20 11:23:24 +02:00

pricing defaults changes, fixes

This commit is contained in:
Matthias Kretschmann 2021-10-29 11:24:20 +01:00
parent 228b09098a
commit 235d8333eb
Signed by: m
GPG Key ID: 606EEEF3C479A91F
9 changed files with 62 additions and 58 deletions

2
.gitignore vendored
View File

@ -13,4 +13,4 @@ networks-metadata.json
src/@types/apollo
graphql.schema.json
src/@types/graph.types.ts
public
tsconfig.tsbuildinfo

View File

@ -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

View File

@ -22,7 +22,7 @@ interface UsePricing {
) => Promise<TransactionReceipt | string | void>
mint: (tokensToMint: string, ddo: DDO) => Promise<TransactionReceipt | void>
buyDT: (
dtAmount: number | string,
amountDataToken: number | string,
price: BestPrice,
ddo: DDO
) => Promise<TransactionReceipt | void>
@ -112,7 +112,7 @@ function usePricing(): UsePricing {
}
async function buyDT(
dtAmount: number | string,
amountDataToken: number | string,
price: BestPrice,
ddo: DDO
): Promise<TransactionReceipt | void> {
@ -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)

View File

@ -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

View File

@ -22,7 +22,6 @@ export default function URLInput({
<input
className={styles.input}
{...props}
{...field}
type="url"
onBlur={(e: React.SyntheticEvent) => handleButtonClick(e, field.value)}
/>

View File

@ -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()
}))
}

View File

@ -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<string>()
// 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 {
<div className={styles.tokens}>
<Coin
name="oceanAmount"
name="amountOcean"
datatokenOptions={{ symbol: 'OCEAN', name: 'Ocean Token' }}
weight={`${Number(weightOnOcean) * 10}%`}
/>
<Coin
name="dtAmount"
name="amountDataToken"
datatokenOptions={{
symbol: dataTokenOptions.symbol,
name: dataTokenOptions.name

View File

@ -18,7 +18,7 @@ export default function PricingFields(): ReactElement {
// Connect with main publish form
const { values, setFieldValue } = useFormikContext<FormPublishData>()
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'

View File

@ -9,13 +9,13 @@ export const initialValues: Partial<FormPublishData> = {
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<FormPublishData> = {
: 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()