Skip to content

Commit

Permalink
Merge pull request paraswap#291 from paraswap/fix/balancer-v2-pricing
Browse files Browse the repository at this point in the history
Fix BalancerV2 linear pool issue
  • Loading branch information
Verisana authored Jan 31, 2023
2 parents 04c9a98 + d2ccadc commit eb30ca7
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paraswap/dex-lib",
"version": "2.9.51",
"version": "2.9.53",
"main": "build/index.js",
"types": "build/index.d.ts",
"repository": "https://github.com/paraswap/paraswap-dex-lib",
Expand Down
5 changes: 1 addition & 4 deletions src/dex/balancer-v2/LinearPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,7 @@ export class LinearPool extends BasePool {
const tokens = poolTokens.tokens.map((address: string, idx: number) => ({
address: address.toLowerCase(),
balance: BigInt(poolTokens.balances[idx].toString()),
scalingFactor:
idx === bptIndex
? BigInt(rate.toString())
: BigInt(scalingFactors[idx].toString()),
scalingFactor: BigInt(scalingFactors[idx].toString()),
}));

const poolState: PoolState = {
Expand Down
26 changes: 26 additions & 0 deletions src/dex/balancer-v2/balancer-v2-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,32 @@ describe('BalancerV2 E2E', () => {
);

describe('Simpleswap', () => {
it('FTM -> BOO', async () => {
await testE2E(
tokens['FTM'],
tokens['BOO'],
holders['FTM'],
'1000000000000000000',
SwapSide.SELL,
dexKey,
ContractMethod.simpleSwap,
network,
provider,
);
});
it('WETH -> FTM', async () => {
await testE2E(
tokens['WETH'],
tokens['FTM'],
holders['WETH'],
'1000000000000000000',
SwapSide.SELL,
dexKey,
ContractMethod.simpleSwap,
network,
provider,
);
});
it('FTM -> TOKEN', async () => {
await testE2E(
tokens['FTM'],
Expand Down
81 changes: 81 additions & 0 deletions src/dex/balancer-v2/balancer-v2-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,87 @@ const amounts = [0n, BI_POWS[18], 2000000000000000000n];
const dexKey = 'BalancerV2';

describe('BalancerV2', function () {
describe('BeetsFi', () => {
it('FTM -> BOO: getPoolIdentifiers and getPricesVolume', async () => {
const dexKey = 'BeetsFi';
const network = Network.FANTOM;
const srcToken = Tokens[network]['FTM'];
const destToken = Tokens[network]['BOO'];
const amounts = [0n, BI_POWS[18]];

const dexHelper = new DummyDexHelper(network);
// const blocknumber = await dexHelper.web3Provider.eth.getBlockNumber();
const blockNumber = 54734626;
const beetsFi = new BalancerV2(network, dexKey, dexHelper);

await beetsFi.initializePricing(blockNumber);

const pools = await beetsFi.getPoolIdentifiers(
srcToken,
destToken,
SwapSide.SELL,
blockNumber,
);
console.log('Pool Identifiers: ', pools);

expect(pools.length).toBeGreaterThan(0);

const poolPrices = await beetsFi.getPricesVolume(
srcToken,
destToken,
amounts,
SwapSide.SELL,
blockNumber,
pools,
);
console.log('Pool Prices: ', poolPrices);

expect(poolPrices).not.toBeNull();
checkPoolPrices(poolPrices!, amounts, SwapSide.SELL, dexKey);

await beetsFi.releaseResources();
});
it('WETH -> FTM: getPoolIdentifiers and getPricesVolume', async () => {
const dexKey = 'BeetsFi';
const network = Network.FANTOM;
const srcToken = Tokens[network]['WETH'];
const destToken = Tokens[network]['FTM'];
const amounts = [0n, BI_POWS[18]];

const dexHelper = new DummyDexHelper(network);
// const blocknumber = await dexHelper.web3Provider.eth.getBlockNumber();
const blockNumber = 54782005;
const beetsFi = new BalancerV2(network, dexKey, dexHelper);

await beetsFi.initializePricing(blockNumber);

const pools = await beetsFi.getPoolIdentifiers(
srcToken,
destToken,
SwapSide.SELL,
blockNumber,
);
console.log('Pool Identifiers: ', pools);

expect(pools.length).toBeGreaterThan(0);

const poolPrices = await beetsFi.getPricesVolume(
srcToken,
destToken,
amounts,
SwapSide.SELL,
blockNumber,
pools,
);
console.log('Pool Prices: ', poolPrices);

expect(poolPrices).not.toBeNull();
checkPoolPrices(poolPrices!, amounts, SwapSide.SELL, dexKey);

await beetsFi.releaseResources();
});
});

describe('ComposableStable', () => {
it('getPoolIdentifiers and getPricesVolume', async () => {
const dexHelper = new DummyDexHelper(Network.POLYGON);
Expand Down
10 changes: 5 additions & 5 deletions src/dex/balancer-v2/balancer-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,9 @@ export class BalancerV2EventPool extends StatefulEventSubscriber<PoolStateMap> {
BalancerPoolTypes.LiquidityBootstrapping,
BalancerPoolTypes.Investment,

// Need to check if we can support these pools with event base
// BalancerPoolTypes.ComposableStable,

// Added all these pools to event base since all math is already implemented
// BalancerPoolTypes.Linear,
// I turned off this pools as I don't understand if they have bad impact or not. Need to investigate one by one if events are
// working on them

// BalancerPoolTypes.MetaStable,
// BalancerPoolTypes.AaveLinear,
// BalancerPoolTypes.ERC4626Linear,
Expand Down Expand Up @@ -650,6 +646,10 @@ export class BalancerV2
const _from = this.dexHelper.config.wrapETH(from);
const _to = this.dexHelper.config.wrapETH(to);

if (_from.address === _to.address) {
return null;
}

const allPools = this.getPoolsWithTokenPair(_from, _to);
const allowedPools = limitPools
? allPools.filter(({ address }) =>
Expand Down
7 changes: 6 additions & 1 deletion tests/constants-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ export const Tokens: {
address: '0x74e23df9110aa9ea0b6ff2faee01e740ca1c642e',
decimals: 18,
},
BOO: {
address: '0x841fad6eae12c286d1fd18d1d525dffa75c7effe',
decimals: 18,
},
},
[Network.BSC]: {
DAI: {
Expand Down Expand Up @@ -821,10 +825,11 @@ export const Holders: {
aFanWFTM: '0x639ade8805c0081ea5da9495bb50751003e827cc',
FRAX: '0x4423ac71f53ca92e2f2be5917a9c2468e7412f4a',
nETH: '0x16b658270ac50c0063940ed287c401b3df7ccf70',
WETH: '0x2400bb4d7221ba530daee061d5afe219e9223eae',
WETH: '0x4ad64fd7ca6d6150614179b9bce4094bc18f29cb',
SPIRIT: '0x0d0707963952f2fba59dd06f2b425ace40b492fe',
wBOMB: '0x28aa4f9ffe21365473b64c161b566c3cdead0108',
TOR: '0x70de4b5ed310fd93da3c0bae824fb99cb4d44dd8',
BOO: '0xf778f4d7a14a8cb73d5261f9c61970ef4e7d7842',
},
[Network.BSC]: {
DAI: '0xf68a4b64162906eff0ff6ae34e2bb1cd42fef62d',
Expand Down
9 changes: 6 additions & 3 deletions tests/utils-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ export async function testE2E(
poolIdentifiers?: string[],
limitOrderProvider?: DummyLimitOrderProvider,
transferFees?: TransferFeeParams,
// Specified in BPS: part of 10000
slippage?: number,
) {
const amount = BigInt(_amount);
const ts = new TenderlySimulation(network);
Expand Down Expand Up @@ -244,11 +246,12 @@ export async function testE2E(
);
expect(parseFloat(priceRoute.destAmount)).toBeGreaterThan(0);

// Slippage to be 7%
// Calculate slippage. Default is 7%
const _slippage = slippage || 700;
const minMaxAmount =
(swapSide === SwapSide.SELL
? BigInt(priceRoute.destAmount) * 93n
: BigInt(priceRoute.srcAmount) * 107n) / 100n;
? BigInt(priceRoute.destAmount) * (10000n - BigInt(_slippage))
: BigInt(priceRoute.srcAmount) * (10000n + BigInt(_slippage))) / 10000n;
const swapParams = await paraswap.buildTransaction(
priceRoute,
minMaxAmount,
Expand Down

0 comments on commit eb30ca7

Please sign in to comment.