Skip to content

Commit

Permalink
Wrapped<>BPT use rate correctly. Limit - use correct scaling for BPT …
Browse files Browse the repository at this point in the history
…in for exact Wrapped Out.
  • Loading branch information
johngrantuk committed Mar 3, 2022
1 parent 7603520 commit df1c095
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
48 changes: 23 additions & 25 deletions src/pools/linearPool/linearPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,26 +263,14 @@ export class LinearPool implements PoolBase {
linearPoolPairData.pairType === PairTypes.BptToWrappedToken
) {
// Limit is amount of BPT in for pool balance of tokenOut
const limit = _calcBptInPerWrappedOut(
bnum(poolPairData.balanceOut.toString()),
bnum(linearPoolPairData.mainBalanceScaled.toString()),
bnum(linearPoolPairData.wrappedBalanceScaled.toString()),
bnum(linearPoolPairData.bptBalanceScaled.toString()),
{
fee: bnum(poolPairData.swapFee.toString()),
lowerTarget: bnum(
linearPoolPairData.lowerTarget.toString()
),
upperTarget: bnum(
linearPoolPairData.upperTarget.toString()
),
}
)
.times(bnum(this.ALMOST_ONE.toString()))
.div(bnum(ONE.toString()))
.div(bnum(ONE.toString()));

// Returning Human scale
const limit = this._BPTInForExactWrappedTokenOut(
linearPoolPairData,
balanceOutHuman
.times(this.ALMOST_ONE.toString())
.div(ONE.toString()),
true
);
return limit;
} else if (
linearPoolPairData.pairType ===
Expand Down Expand Up @@ -523,11 +511,14 @@ export class LinearPool implements PoolBase {
upperTarget: bnum(poolPairData.upperTarget.toString()),
}
);
const amtWithRate = amt
.times(poolPairData.rate)
.div(bnum(ONE.toString()));
// return human readable number
// Using BigNumber.js decimalPlaces (dp), allows us to consider token decimal accuracy correctly,
// i.e. when using token with 2decimals 0.002 should be returned as 0
// Uses ROUND_DOWN mode (1)
return scale(amt, -18).dp(poolPairData.decimalsOut, 1);
return scale(amtWithRate, -18).dp(poolPairData.decimalsOut, 1);
} catch (err) {
return ZERO;
}
Expand All @@ -554,11 +545,12 @@ export class LinearPool implements PoolBase {
upperTarget: bnum(poolPairData.upperTarget.toString()),
}
);
const amtWithoutRate = amt.div(poolPairData.rate);
// return human readable number
// Using BigNumber.js decimalPlaces (dp), allows us to consider token decimal accuracy correctly,
// i.e. when using token with 2decimals 0.002 should be returned as 0
// Uses ROUND_DOWN mode (1)
return scale(amt, -18).dp(poolPairData.decimalsOut, 1);
return amtWithoutRate.dp(poolPairData.decimalsOut, 1);
} catch (err) {
return ZERO;
}
Expand Down Expand Up @@ -752,11 +744,13 @@ export class LinearPool implements PoolBase {
upperTarget: bnum(poolPairData.upperTarget.toString()),
}
);

const amtWithRate = amt.div(poolPairData.rate);
// return human readable number
// Using BigNumber.js decimalPlaces (dp), allows us to consider token decimal accuracy correctly,
// i.e. when using token with 2decimals 0.002 should be returned as 0
// Uses ROUND_DOWN mode (1)
return scale(amt, -18).dp(poolPairData.decimalsOut, 1);
// Uses ROUND_UP mode (0)
return amtWithRate.dp(poolPairData.decimalsIn, 0);
} catch (err) {
return ZERO;
}
Expand All @@ -783,11 +777,15 @@ export class LinearPool implements PoolBase {
upperTarget: bnum(poolPairData.upperTarget.toString()),
}
);
const amtWithRate = amt
.times(poolPairData.rate)
.div(bnum(ONE.toString()));

// return human readable number
// Using BigNumber.js decimalPlaces (dp), allows us to consider token decimal accuracy correctly,
// i.e. when using token with 2decimals 0.002 should be returned as 0
// Uses ROUND_DOWN mode (1)
return scale(amt, -18).dp(poolPairData.decimalsOut, 1);
// Uses ROUND_UP mode (0)
return scale(amtWithRate, -18).dp(poolPairData.decimalsIn, 0);
} catch (err) {
return ZERO;
}
Expand Down
10 changes: 5 additions & 5 deletions test/linear.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ describe('linear pool tests', () => {
parseFixed('25.001542', aUSDT.decimals),
kovanPools.pools
);
expect(returnAmount).to.eq('25002051893909811321');
expect(returnAmount).to.eq('25019071730792915901');
});

it('WrappedToken>BPT, SwapExactOut', async () => {
Expand All @@ -623,7 +623,7 @@ describe('linear pool tests', () => {
parseFixed('0.981028', LINEAR_AUSDT.decimals),
kovanPools.pools
);
expect(returnAmount).to.eq('981007');
expect(returnAmount).to.eq('980341');
});

it('BPT>WrappedToken, SwapExactIn', async () => {
Expand All @@ -634,18 +634,18 @@ describe('linear pool tests', () => {
parseFixed('26.0872140', LINEAR_AUSDT.decimals),
kovanPools.pools
);
expect(returnAmount).to.eq('26086681');
expect(returnAmount).to.eq('26068935');
});

it('BPT>MainToken, SwapExactOut', async () => {
it('BPT>WrappedToken, SwapExactOut', async () => {
const returnAmount = await testFullSwap(
LINEAR_AUSDT.address,
aUSDT.address,
SwapTypes.SwapExactOut,
parseFixed('71.204293', aUSDT.decimals),
kovanPools.pools
);
expect(returnAmount).to.eq('71205745000000000000');
expect(returnAmount).to.eq('71254217604154012028');
});
});
});
Expand Down
30 changes: 25 additions & 5 deletions test/testScripts/swapExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ export const ADDRESSES = {
decimals: 18,
symbol: 'wSTETH',
},
bbausd: {
address: '0x7b50775383d3d6f0215a8f290f2c9e2eebbeceb2',
decimals: 18,
symbol: 'bbausd',
},
bbausdc: {
address: '0x9210F1204b5a24742Eba12f710636D76240dF3d0',
decimals: 18,
symbol: 'bbausdc',
},
waDAI: {
address: '0x02d60b84491589974263d922d9cc7a3152618ef6',
decimals: 18,
symbol: 'waDAI',
},
waUSDC: {
address: '0xd093fa4fb80d09bb30817fdcd442d4d02ed3e5de',
decimals: 6,
symbol: 'waUSDC',
},
},
[Network.KOVAN]: {
// Visit https://balancer-faucet.on.fleek.co/#/faucet for test tokens
Expand Down Expand Up @@ -654,13 +674,13 @@ async function makeRelayerTrade(
}

export async function simpleSwap() {
const networkId = Network.KOVAN;
const networkId = Network.MAINNET;
// Pools source can be Subgraph URL or pools data set passed directly
// Update pools list with most recent onchain balances
const tokenIn = ADDRESSES[networkId].DAI_from_AAVE;
const tokenOut = ADDRESSES[networkId].USDC_from_AAVE;
const tokenIn = ADDRESSES[networkId].bbausd;
const tokenOut = ADDRESSES[networkId].waDAI;
const swapType = SwapTypes.SwapExactIn;
const swapAmount = parseFixed('100', 18);
const swapAmount = parseFixed('1000000', 18);
const executeTrade = true;

const provider = new JsonRpcProvider(PROVIDER_URLS[networkId]);
Expand Down Expand Up @@ -694,7 +714,7 @@ export async function simpleSwap() {

const swapInfo = await getSwap(
provider,
SOR_CONFIG[Network.KOVAN],
SOR_CONFIG[networkId],
subgraphPoolDataService,
// mockPoolDataService,
coingeckoTokenPriceService,
Expand Down

0 comments on commit df1c095

Please sign in to comment.