2020-11-16 16:21:15 +01:00
|
|
|
import { FormikContextType, useFormikContext } from 'formik'
|
|
|
|
import React, { ReactElement, useEffect, useState } from 'react'
|
2021-10-18 20:44:33 +02:00
|
|
|
import { useAsset } from '@context/Asset'
|
2020-11-16 16:21:15 +01:00
|
|
|
import Token from '../Pool/Token'
|
|
|
|
import styles from './Output.module.css'
|
|
|
|
|
2021-10-18 20:44:33 +02:00
|
|
|
import { isValidNumber } from '@utils/numbers'
|
2021-08-24 10:11:01 +02:00
|
|
|
import Decimal from 'decimal.js'
|
2021-10-13 18:48:59 +02:00
|
|
|
import { FormTradeData } from './_types'
|
2021-08-24 10:11:01 +02:00
|
|
|
|
|
|
|
Decimal.set({ toExpNeg: -18, precision: 18, rounding: 1 })
|
|
|
|
|
2020-11-16 16:21:15 +01:00
|
|
|
export default function Output({
|
|
|
|
dtSymbol,
|
2021-09-01 16:05:59 +02:00
|
|
|
oceanSymbol,
|
2020-11-16 16:21:15 +01:00
|
|
|
poolAddress
|
|
|
|
}: {
|
|
|
|
dtSymbol: string
|
2021-09-01 16:05:59 +02:00
|
|
|
oceanSymbol: string
|
2020-11-16 16:21:15 +01:00
|
|
|
poolAddress: string
|
|
|
|
}): ReactElement {
|
2021-09-30 12:54:44 +02:00
|
|
|
const { isAssetNetwork } = useAsset()
|
2020-11-16 16:21:15 +01:00
|
|
|
const [maxOutput, setMaxOutput] = useState<string>()
|
|
|
|
const [swapFee, setSwapFee] = useState<string>()
|
|
|
|
const [swapFeeValue, setSwapFeeValue] = useState<string>()
|
|
|
|
// Connect with form
|
|
|
|
const { values }: FormikContextType<FormTradeData> = useFormikContext()
|
|
|
|
|
|
|
|
// Get swap fee
|
|
|
|
useEffect(() => {
|
2022-01-11 20:05:07 +01:00
|
|
|
if (!poolAddress || !isAssetNetwork) return
|
2020-11-16 16:21:15 +01:00
|
|
|
|
|
|
|
async function getSwapFee() {
|
2022-01-11 20:05:07 +01:00
|
|
|
// const swapFee = await ocean.pool.getSwapFee(poolAddress)
|
2021-08-24 10:11:01 +02:00
|
|
|
|
2022-01-11 20:05:07 +01:00
|
|
|
// // swapFee is tricky: to get 0.1% you need to convert from 0.001
|
|
|
|
// setSwapFee(
|
|
|
|
// isValidNumber(swapFee) ? new Decimal(swapFee).mul(100).toString() : '0'
|
|
|
|
// )
|
2021-08-24 10:11:01 +02:00
|
|
|
|
2020-11-16 16:21:15 +01:00
|
|
|
const value =
|
|
|
|
values.type === 'buy'
|
2022-02-02 12:49:59 +01:00
|
|
|
? isValidNumber(swapFee) && isValidNumber(values.baseToken)
|
|
|
|
? new Decimal(swapFee).mul(new Decimal(values.baseToken))
|
2021-08-24 10:11:01 +02:00
|
|
|
: 0
|
|
|
|
: isValidNumber(swapFee) && isValidNumber(values.datatoken)
|
|
|
|
? new Decimal(swapFee).mul(new Decimal(values.datatoken))
|
|
|
|
: 0
|
2020-11-16 16:21:15 +01:00
|
|
|
setSwapFeeValue(value.toString())
|
|
|
|
}
|
|
|
|
getSwapFee()
|
2022-02-02 12:49:59 +01:00
|
|
|
}, [poolAddress, values, isAssetNetwork, swapFee])
|
2020-11-16 16:21:15 +01:00
|
|
|
|
|
|
|
// Get output values
|
|
|
|
useEffect(() => {
|
2022-01-11 20:05:07 +01:00
|
|
|
if (!poolAddress || !isAssetNetwork) return
|
2020-11-16 16:21:15 +01:00
|
|
|
|
|
|
|
async function getOutput() {
|
|
|
|
// Minimum received
|
|
|
|
// TODO: check if this here is redundant cause we call some of that already in Swap.tsx
|
|
|
|
const maxImpact = 1 - Number(values.slippage) / 100
|
|
|
|
const maxPrice =
|
|
|
|
values.type === 'buy'
|
2021-08-24 10:11:01 +02:00
|
|
|
? isValidNumber(values.datatoken) && isValidNumber(maxImpact)
|
|
|
|
? new Decimal(values.datatoken)
|
|
|
|
.mul(new Decimal(maxImpact))
|
|
|
|
.toString()
|
|
|
|
: '0'
|
2022-02-02 12:49:59 +01:00
|
|
|
: isValidNumber(values.baseToken) && isValidNumber(maxImpact)
|
|
|
|
? new Decimal(values.baseToken).mul(new Decimal(maxImpact)).toString()
|
2021-08-24 10:11:01 +02:00
|
|
|
: '0'
|
2020-11-16 16:21:15 +01:00
|
|
|
|
|
|
|
setMaxOutput(maxPrice)
|
|
|
|
}
|
|
|
|
getOutput()
|
2022-01-11 20:05:07 +01:00
|
|
|
}, [poolAddress, values, isAssetNetwork])
|
2020-11-16 16:21:15 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.output}>
|
|
|
|
<div>
|
|
|
|
<p>Minimum Received</p>
|
|
|
|
<Token
|
2021-09-01 16:05:59 +02:00
|
|
|
symbol={values.type === 'buy' ? dtSymbol : oceanSymbol}
|
2020-11-16 16:21:15 +01:00
|
|
|
balance={maxOutput}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<p>Swap fee</p>
|
|
|
|
<Token
|
2021-09-01 16:05:59 +02:00
|
|
|
symbol={`${values.type === 'buy' ? oceanSymbol : dtSymbol} ${
|
2020-11-16 16:21:15 +01:00
|
|
|
swapFee ? `(${swapFee}%)` : ''
|
|
|
|
}`}
|
|
|
|
balance={swapFeeValue}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|