Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

factor in slippage for poolswap calculation #433

Merged
merged 3 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions app/screens/AppNavigator/screens/Dex/PoolSwap/PoolSwapScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function PoolSwapScreen ({ route }: Props): JSX.Element {
// props derived state
const [tokenA, setTokenA] = useState<DerivedTokenState>()
const [tokenB, setTokenB] = useState<DerivedTokenState>()
const [isComputing, setIsComputing] = useState<boolean>(false)

// component UI state
const { control, setValue, formState: { isValid }, getValues, trigger } = useForm({ mode: 'onChange' })
Expand Down Expand Up @@ -112,11 +113,14 @@ export function PoolSwapScreen ({ route }: Props): JSX.Element {
token={tokenA} control={control} controlName={tokenAForm}
title={`${translate('screens/PoolSwapScreen', 'SWAP')} ${tokenA.symbol}`}
onChangeFromAmount={async (amount) => {
setIsComputing(true)
amount = isNaN(+amount) ? '0' : amount
setValue(tokenAForm, amount)
await trigger(tokenAForm)
setValue(tokenBForm, aToBPrice.times(amount !== undefined && amount !== '' ? amount : 0).toFixed(8))
const reserveA = getReserveAmount(tokenA.id, poolpair)
setValue(tokenBForm, calculateEstimatedAmount(amount, reserveA, aToBPrice.toFixed(8)).toFixed(8))
await trigger(tokenBForm)
setIsComputing(false)
}}
maxAmount={tokenA.amount}
/>
Expand All @@ -129,7 +133,7 @@ export function PoolSwapScreen ({ route }: Props): JSX.Element {
maxAmount={aToBPrice.times(getValues()[tokenAForm]).toFixed(8)}
/>
{
(new BigNumber(getValues()[tokenAForm]).isGreaterThan(0) && new BigNumber(getValues()[tokenBForm]).isGreaterThan(0)) &&
!isComputing && (new BigNumber(getValues()[tokenAForm]).isGreaterThan(0) && new BigNumber(getValues()[tokenBForm]).isGreaterThan(0)) &&
<SwapSummary
poolpair={poolpair} tokenA={tokenA} tokenB={tokenB} tokenAAmount={getValues()[tokenAForm]}
tokenBAmount={getValues()[tokenBForm]}
Expand Down Expand Up @@ -268,9 +272,9 @@ interface SwapSummaryItems {
tokenBAmount: string
}

function SwapSummary ({ poolpair, tokenA, tokenB, tokenAAmount, tokenBAmount }: SwapSummaryItems): JSX.Element {
const reserveA = tokenA.id === poolpair.tokenA.id ? poolpair.tokenA.reserve : poolpair.tokenB.reserve
const reserveB = tokenB.id === poolpair.tokenB.id ? poolpair.tokenB.reserve : poolpair.tokenA.reserve
function SwapSummary ({ poolpair, tokenA, tokenB, tokenAAmount }: SwapSummaryItems): JSX.Element {
const reserveA = getReserveAmount(tokenA.id, poolpair)
const reserveB = getReserveAmount(tokenB.id, poolpair)
const price = [{
amount: new BigNumber(reserveA).div(reserveB).toFixed(8),
symbol: `${tokenA.symbol} per ${tokenB.symbol}`
Expand All @@ -286,12 +290,10 @@ function SwapSummary ({ poolpair, tokenA, tokenB, tokenAAmount, tokenBAmount }:
<PriceRow
testID='estimated'
title={translate('screens/PoolSwapScreen', 'Estimated to receive')}
values={[{ amount: new BigNumber(tokenAAmount).times(price[1].amount).toFixed(8), symbol: tokenB.symbol }]}
/>
<PriceRow
testID='minimum'
title={translate('screens/PoolSwapScreen', 'Minimum to receive')}
values={[{ amount: new BigNumber(tokenBAmount).toFixed(8), symbol: tokenB.symbol }]}
values={[{
amount: calculateEstimatedAmount(tokenAAmount, reserveA, price[1].amount).toFixed(8),
symbol: tokenB.symbol
}]}
/>
<PriceRow
testID='fee'
Expand Down Expand Up @@ -336,3 +338,13 @@ async function constructSignedSwapAndSend (
title: `${translate('screens/PoolSwapScreen', 'Swapping Token')}`
}))
}

function calculateEstimatedAmount (tokenAAmount: string, reserveA: string, price: string): BigNumber {
tokenAAmount = tokenAAmount !== undefined && tokenAAmount !== '' ? tokenAAmount : '0'
const slippage = (new BigNumber(1).minus(new BigNumber(tokenAAmount).div(reserveA)))
return new BigNumber(tokenAAmount).times(price).times(slippage)
}

function getReserveAmount (id: string, poolpair: PoolPairData): string {
return id === poolpair.tokenA.id ? poolpair.tokenA.reserve : poolpair.tokenB.reserve
}
21 changes: 10 additions & 11 deletions cypress/integration/functional/dex/poolswap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ context('poolswap with values', () => {
cy.getByTestID('swap_button').click().wait(4000)
// Valid form
cy.getByTestID('text_input_tokenA').type('1')
cy.getByTestID('text_price_row_price_1').then(($txt: any) => {
const tokenValue = $txt[0].textContent.replace(' LTC per DFI', '').replace(',', '')
cy.getByTestID('text_price_row_estimated_0').then(($txt: any) => {
const tokenValue = $txt[0].textContent.replace(' LTC', '').replace(',', '')
cy.getByTestID('text_input_tokenB').should('have.value', new BigNumber(tokenValue).toFixed(8))
cy.getByTestID('button_submit').should('not.have.attr', 'disabled')

Expand All @@ -56,35 +56,34 @@ context('poolswap with values', () => {
it('should be able to click max', function () {
cy.getByTestID('MAX_amount_button').click().wait(3000)
cy.getByTestID('text_input_tokenA').should('have.value', '10.00000000')
cy.getByTestID('text_price_row_minimum_0').then(($txt: any) => {
cy.getByTestID('text_price_row_estimated_0').then(($txt: any) => {
const tokenValue = $txt[0].textContent.replace(' LTC', '').replace(',', '')
cy.getByTestID('text_input_tokenB').should('have.value', new BigNumber(tokenValue).toFixed(8))
})
})

it('should be able to click half', function () {
cy.getByTestID('50%_amount_button').click().wait(3000)
cy.getByTestID('text_input_tokenA').should('have.value', '5.00000000')
cy.getByTestID('text_price_row_minimum_0').then(($txt: any) => {
cy.getByTestID('text_input_tokenA').should('have.value', '5.00000000').wait(3000)
cy.getByTestID('text_price_row_estimated_0').then(($txt: any) => {
const tokenValue = $txt[0].textContent.replace(' LTC', '').replace(',', '')
cy.getByTestID('text_input_tokenB').should('have.value', new BigNumber(tokenValue).div(2).toFixed(8))
cy.getByTestID('text_input_tokenB').should('have.value', new BigNumber(tokenValue).toFixed(8))
})
})

it('should be able to swap', function () {
cy.getByTestID('text_price_row_minimum_0').then(() => {
// const tokenValue = $txt[0].textContent.replace(' LTC', '').replace(',', '')
cy.getByTestID('text_price_row_estimated_0').then(($txt: any) => {
const tokenValue = $txt[0].textContent.replace(' LTC', '').replace(',', '')
cy.getByTestID('button_submit').click()
cy.closeOceanInterface()
cy.getByTestID('playground_wallet_fetch_balances').click()
cy.getByTestID('bottom_tab_balances').click()
cy.getByTestID('balances_row_4').should('exist')

// Need to update once poolswap calculations are correct
/* cy.getByTestID('balances_row_4_amount').then(($txt: any) => {
cy.getByTestID('balances_row_4_amount').then(($txt: any) => {
const balanceAmount = $txt[0].textContent.replace(' LTC', '').replace(',', '')
expect(new BigNumber(balanceAmount).toNumber()).be.gte(new BigNumber(tokenValue).toNumber())
}) */
})

cy.getByTestID('bottom_tab_dex').click()
cy.getByTestID('swap_button').click().wait(4000)
Expand Down