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

View File

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