1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

restore live calculations

This commit is contained in:
Matthias Kretschmann 2022-01-24 10:39:07 +00:00
parent 487bcad8b4
commit 0c5c8effde
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 77 additions and 50 deletions

View File

@ -16,8 +16,10 @@ import { useWeb3 } from '@context/Web3'
import { isValidNumber } from '@utils/numbers' import { isValidNumber } from '@utils/numbers'
import Decimal from 'decimal.js' import Decimal from 'decimal.js'
import { useAsset } from '@context/Asset' import { useAsset } from '@context/Asset'
import { LoggerInstance, Pool } from '@oceanprotocol/lib'
export default function FormAdd({ export default function FormAdd({
tokenInAddress,
coin, coin,
dtBalance, dtBalance,
dtSymbol, dtSymbol,
@ -30,6 +32,7 @@ export default function FormAdd({
setNewPoolTokens, setNewPoolTokens,
setNewPoolShare setNewPoolShare
}: { }: {
tokenInAddress: string
coin: string coin: string
dtBalance: string dtBalance: string
dtSymbol: string dtSymbol: string
@ -67,39 +70,42 @@ export default function FormAdd({
useEffect(() => { useEffect(() => {
async function calculatePoolShares() { async function calculatePoolShares() {
// if (!web3) return if (!web3) return
// const tokenInAddress =
// coin === 'OCEAN' ? ocean.pool.oceanAddress : ocean.pool.dtAddress if (!values.amount || !tokenInAddress) {
// if (!values.amount || !tokenInAddress) { setNewPoolTokens('0')
// setNewPoolTokens('0') setNewPoolShare('0')
// setNewPoolShare('0') return
// return }
// } if (Number(values.amount) > Number(amountMax)) return
// if (Number(values.amount) > Number(amountMax)) return
// const poolTokens = await ocean.pool.calcPoolOutGivenSingleIn( const poolInstance = new Pool(web3, LoggerInstance)
// poolAddress,
// tokenInAddress, const poolTokens = await poolInstance.calcPoolOutGivenSingleIn(
// `${values.amount}` poolAddress,
// ) tokenInAddress,
// setNewPoolTokens(poolTokens) `${values.amount}`
// const newPoolShareDecimal = )
// isValidNumber(poolTokens) && isValidNumber(totalPoolTokens) setNewPoolTokens(poolTokens)
// ? new Decimal(poolTokens) const newPoolShareDecimal =
// .dividedBy( isValidNumber(poolTokens) && isValidNumber(totalPoolTokens)
// new Decimal(totalPoolTokens).plus(new Decimal(poolTokens)) ? new Decimal(poolTokens)
// ) .dividedBy(
// .mul(100) new Decimal(totalPoolTokens).plus(new Decimal(poolTokens))
// .toString() )
// : '0' .mul(100)
// totalBalance && setNewPoolShare(newPoolShareDecimal) .toString()
: '0'
totalBalance && setNewPoolShare(newPoolShareDecimal)
} }
calculatePoolShares() calculatePoolShares()
}, [ }, [
tokenInAddress,
web3,
values.amount, values.amount,
totalBalance, totalBalance,
totalPoolTokens, totalPoolTokens,
amountMax, amountMax,
coin,
poolAddress, poolAddress,
setNewPoolTokens, setNewPoolTokens,
setNewPoolShare setNewPoolShare

View File

@ -13,7 +13,7 @@ import DebugOutput from '@shared/DebugOutput'
import { useWeb3 } from '@context/Web3' import { useWeb3 } from '@context/Web3'
import { useAsset } from '@context/Asset' import { useAsset } from '@context/Asset'
import content from '../../../../../../content/price.json' import content from '../../../../../../content/price.json'
import { Datatoken } from '@oceanprotocol/lib' import { Datatoken, LoggerInstance, Pool } from '@oceanprotocol/lib'
export interface FormAddLiquidity { export interface FormAddLiquidity {
amount: number amount: number
@ -53,6 +53,7 @@ export default function Add({
const [newPoolTokens, setNewPoolTokens] = useState('0') const [newPoolTokens, setNewPoolTokens] = useState('0')
const [newPoolShare, setNewPoolShare] = useState('0') const [newPoolShare, setNewPoolShare] = useState('0')
const [isWarningAccepted, setIsWarningAccepted] = useState(false) const [isWarningAccepted, setIsWarningAccepted] = useState(false)
const [tokenInAddress, setTokenInAddress] = useState<string>()
// Live validation rules // Live validation rules
// https://github.com/jquense/yup#number // https://github.com/jquense/yup#number
@ -80,37 +81,56 @@ export default function Add({
// Get maximum amount for either OCEAN or datatoken // Get maximum amount for either OCEAN or datatoken
useEffect(() => { useEffect(() => {
if (!accountId || !isAssetNetwork || !poolAddress) return if (!web3 || !accountId || !isAssetNetwork || !poolAddress) return
async function getMaximum() { async function getMaximum() {
// const amountMaxPool = const poolInstance = new Pool(web3, LoggerInstance)
// coin === 'OCEAN' const baseTokenAddress = await poolInstance.getBasetoken(poolAddress)
// ? await ocean.pool.getOceanMaxAddLiquidity(poolAddress)
// : await ocean.pool.getDTMaxAddLiquidity(poolAddress) const tokenInAddress = coin === 'OCEAN' ? baseTokenAddress : dtAddress
// const amountMax = setTokenInAddress(tokenInAddress)
// coin === 'OCEAN'
// ? Number(balance.ocean) > Number(amountMaxPool) const amountMaxPool = await poolInstance.getReserve(
// ? amountMaxPool poolAddress,
// : balance.ocean tokenInAddress
// : Number(dtBalance) > Number(amountMaxPool) )
// ? amountMaxPool
// : dtBalance // coin === 'OCEAN'
// setAmountMax(Number(amountMax).toFixed(3)) // ? await poolInstance.getOceanMaxAddLiquidity(poolAddress)
// : await poolInstance.getDTMaxAddLiquidity(poolAddress)
const amountMax =
coin === 'OCEAN'
? Number(balance.ocean) > Number(amountMaxPool)
? amountMaxPool
: balance.ocean
: Number(dtBalance) > Number(amountMaxPool)
? amountMaxPool
: dtBalance
setAmountMax(Number(amountMax).toFixed(3))
} }
getMaximum() getMaximum()
}, [accountId, isAssetNetwork, poolAddress, coin, dtBalance, balance.ocean]) }, [
web3,
accountId,
isAssetNetwork,
poolAddress,
dtAddress,
coin,
dtBalance,
balance.ocean
])
// Submit // Submit
async function handleAddLiquidity(amount: number, resetForm: () => void) { async function handleAddLiquidity(amount: number, resetForm: () => void) {
const poolInstance = new Pool(web3, LoggerInstance)
try { try {
// const result = // const result = await poolInstance.joinPool(
// coin === 'OCEAN' // accountId,
// ? await ocean.pool.addOceanLiquidity( // poolAddress,
// accountId, // poolAmountOut,
// poolAddress, // amountMax
// `${amount}` // )
// )
// : await ocean.pool.addDTLiquidity(accountId, poolAddress, `${amount}`)
// setTxId(result?.transactionHash) // setTxId(result?.transactionHash)
// resetForm() // resetForm()
fetchAllData() fetchAllData()
@ -139,6 +159,7 @@ export default function Add({
<div className={styles.addInput}> <div className={styles.addInput}>
{isWarningAccepted ? ( {isWarningAccepted ? (
<FormAdd <FormAdd
tokenInAddress={tokenInAddress}
coin={coin} coin={coin}
dtBalance={dtBalance} dtBalance={dtBalance}
dtSymbol={dtSymbol} dtSymbol={dtSymbol}