Skip to content

Commit

Permalink
modify fee handling for phantomStable bpt swaps, adapt test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioyuhjtman committed Dec 21, 2022
1 parent 82543c6 commit dd90e2f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 44 deletions.
32 changes: 12 additions & 20 deletions src/pools/phantomStablePool/phantomStablePool.ts
Expand Up @@ -228,11 +228,8 @@ export class PhantomStablePool implements PoolBase {
// All values should use 1e18 fixed point
// i.e. 1USDC => 1e18 not 1e6
// In Phantom Pools every time there is a swap (token per token, bpt per token or token per bpt), we substract the fee from the amount in
const amtWithFeeEvm = this.subtractSwapFeeAmount(
parseFixed(amount.dp(18).toString(), 18),
poolPairData.swapFee
);
const amountConvertedEvm = amtWithFeeEvm

const amountConverted = parseFixed(amount.dp(18).toString(), 18)
.mul(tokenInPriceRate)
.div(ONE);

Expand All @@ -243,32 +240,32 @@ export class PhantomStablePool implements PoolBase {
poolPairData.allBalancesScaled.length
).fill(BigInt(0));
amountsInBigInt[poolPairData.tokenIndexIn] =
amountConvertedEvm.toBigInt();
amountConverted.toBigInt();

returnEvm = _calcBptOutGivenExactTokensIn(
this.amp.toBigInt(),
poolPairData.allBalancesScaled.map((b) => b.toBigInt()),
amountsInBigInt,
this.totalShares.toBigInt(),
BigInt(0)
poolPairData.swapFee.toBigInt()
);
} else if (poolPairData.pairType === PairTypes.BptToToken) {
returnEvm = _calcTokenOutGivenExactBptIn(
this.amp.toBigInt(),
poolPairData.allBalancesScaled.map((b) => b.toBigInt()),
poolPairData.tokenIndexOut,
amountConvertedEvm.toBigInt(),
amountConverted.toBigInt(),
this.totalShares.toBigInt(),
BigInt(0)
poolPairData.swapFee.toBigInt()
);
} else {
returnEvm = _calcOutGivenIn(
this.amp.toBigInt(),
poolPairData.allBalancesScaled.map((b) => b.toBigInt()),
poolPairData.tokenIndexIn,
poolPairData.tokenIndexOut,
amountConvertedEvm.toBigInt(),
BigInt(0)
amountConverted.toBigInt(),
poolPairData.swapFee.toBigInt()
);
}

Expand Down Expand Up @@ -309,7 +306,7 @@ export class PhantomStablePool implements PoolBase {
poolPairData.tokenIndexIn,
amountConvertedEvm.toBigInt(),
this.totalShares.toBigInt(),
BigInt(0)
poolPairData.swapFee.toBigInt()
);
} else if (poolPairData.pairType === PairTypes.BptToToken) {
const amountsOutBigInt = Array(
Expand All @@ -323,7 +320,7 @@ export class PhantomStablePool implements PoolBase {
poolPairData.allBalancesScaled.map((b) => b.toBigInt()),
amountsOutBigInt,
this.totalShares.toBigInt(),
BigInt(0) // Fee is handled below
poolPairData.swapFee.toBigInt()
);
} else {
returnEvm = _calcInGivenOut(
Expand All @@ -332,21 +329,16 @@ export class PhantomStablePool implements PoolBase {
poolPairData.tokenIndexIn,
poolPairData.tokenIndexOut,
amountConvertedEvm.toBigInt(),
BigInt(0) // Fee is handled below
poolPairData.swapFee.toBigInt()
);
}
// In Phantom Pools every time there is a swap (token per token, bpt per token or token per bpt), we substract the fee from the amount in
const returnEvmWithRate = BigNumber.from(returnEvm)
.mul(ONE)
.div(tokenInPriceRate);

const returnEvmWithFee = this.addSwapFeeAmount(
returnEvmWithRate,
poolPairData.swapFee
);

// return human number
return bnum(formatFixed(returnEvmWithFee, 18));
return bnum(formatFixed(returnEvmWithRate, 18));
} catch (err) {
console.error(`PhantomStable _evminGivenOut: ${err.message}`);
return ZERO;
Expand Down
4 changes: 2 additions & 2 deletions test/composableStablePool.spec.ts
Expand Up @@ -19,8 +19,8 @@ describe('composable stable pool', () => {
// parsePoolPairData contains pool's allBalances and allBalancesScaled
// both already multiplied by the price rates.
const poolPairData = composableStablePool.parsePoolPairData(
ADDRESSES[Network.MAINNET].bbausdt2.address,
ADDRESSES[Network.MAINNET].bbausd2.address
ADDRESSES[Network.MAINNET].bbausdt.address,
ADDRESSES[Network.MAINNET].bbausd.address
);
it('token -> BPT spot price with no rate', () => {
// spot prices
Expand Down
14 changes: 7 additions & 7 deletions test/linear.spec.ts
Expand Up @@ -718,7 +718,7 @@ describe('linear pool tests', () => {
fullKovanPools.pools,
sorConfigFullKovan
);
expect(returnAmount).to.eq('6606146264948964392');
expect(returnAmount).to.eq('6638439512219283357');
});

it('BAL>USDT, SwapExactIn', async () => {
Expand Down Expand Up @@ -756,7 +756,7 @@ describe('linear pool tests', () => {
fullKovanPools.pools,
sorConfigFullKovan
);
expect(returnAmount).to.eq('702055');
expect(returnAmount).to.eq('698604');
});

it('BAL>USDT, SwapExactOut', async () => {
Expand All @@ -768,7 +768,7 @@ describe('linear pool tests', () => {
fullKovanPools.pools,
sorConfigFullKovan
);
expect(returnAmount).to.eq('81899098582251741376');
expect(returnAmount).to.eq('81419601531090714841');
});
});

Expand All @@ -784,7 +784,7 @@ describe('linear pool tests', () => {
pools,
sorConfigKovan
);
expect(returnAmount).to.eq('989985749906811070');
expect(returnAmount).to.eq('993338125331362114');
});

it('USDT>staBAL3, SwapExactOut', async () => {
Expand All @@ -796,7 +796,7 @@ describe('linear pool tests', () => {
kovanPools.pools,
sorConfigKovan
);
expect(returnAmount).to.eq('1009969');
expect(returnAmount).to.eq('1006617');
});

it('staBAL3>USDT, SwapExactIn', async () => {
Expand All @@ -808,7 +808,7 @@ describe('linear pool tests', () => {
kovanPools.pools,
sorConfigKovan
);
expect(returnAmount).to.eq('989869');
expect(returnAmount).to.eq('993188');
});

it('staBAL3>USDT, SwapExactOut', async () => {
Expand All @@ -820,7 +820,7 @@ describe('linear pool tests', () => {
kovanPools.pools,
sorConfigKovan
);
expect(returnAmount).to.eq('1010233805404347502');
expect(returnAmount).to.eq('1006881038845023935');
});

// it('aUSDT>staBAL3, SwapExactIn', async () => {
Expand Down
8 changes: 4 additions & 4 deletions test/phantomStablePools.spec.ts
Expand Up @@ -174,7 +174,7 @@ describe(`Tests for PhantomStable Pools.`, () => {
parseFixed('0.010001000098489046', 18),
[pool]
);
expect(returnAmount).to.eq('9901097957797894');
expect(returnAmount).to.eq('9934289675943491');
});

it('BPT>Token, SwapExactIn', async () => {
Expand All @@ -185,7 +185,7 @@ describe(`Tests for PhantomStable Pools.`, () => {
parseFixed('401.873', 18),
[pool]
);
expect(returnAmount).to.eq('397821023707679256401');
expect(returnAmount).to.eq('399154341172470359058');
});
});

Expand All @@ -209,7 +209,7 @@ describe(`Tests for PhantomStable Pools.`, () => {
parseFixed('654.98', 18),
[pool]
);
expect(returnAmount).to.eq('661659057984436270211');
expect(returnAmount).to.eq('659463141422133620208');
});

it('BPT>Token, SwapExactIn', async () => {
Expand All @@ -220,7 +220,7 @@ describe(`Tests for PhantomStable Pools.`, () => {
parseFixed('0.007321', 18),
[pool]
);
expect(returnAmount).to.eq('7395030076860800');
expect(returnAmount).to.eq('7370487423382863');
});
});
});
Expand Down
19 changes: 12 additions & 7 deletions test/testScripts/constants.ts
Expand Up @@ -124,16 +124,21 @@ export const ADDRESSES = {
decimals: 18,
symbol: 'wSTETH',
},
bbausd: {
bbausdOld: {
address: '0x7b50775383d3d6f0215a8f290f2c9e2eebbeceb2',
decimals: 18,
symbol: 'bbausd',
},
bbausdc: {
bbausdcOld: {
address: '0x9210F1204b5a24742Eba12f710636D76240dF3d0',
decimals: 18,
symbol: 'bbausdc',
},
bbausdc: {
address: '0x82698aecc9e28e9bb27608bd52cf57f704bd1b83',
decimals: 18,
symbol: 'bbausdc',
},
bbadai: {
address: '0x804cdb9116a10bb78768d3252355a1b18067bf8f',
decimals: 18,
Expand All @@ -149,20 +154,20 @@ export const ADDRESSES = {
decimals: 6,
symbol: 'waUSDC',
},
bbausd2: {
bbausd: {
address: '0xA13a9247ea42D743238089903570127DdA72fE44',
decimals: 18,
symbol: 'bbausd2',
symbol: 'bbausd',
},
bbadai2: {
address: '0xae37d54ae477268b9997d4161b96b8200755935c',
decimals: 18,
symbol: 'bb-a-dai2',
},
bbausdt2: {
bbausdt: {
address: '0x2F4eb100552ef93840d5aDC30560E5513DFfFACb',
decimals: 18,
symbol: 'bb-a-usdt2',
symbol: 'bb-a-usdt',
},
RPL: {
address: '0xD33526068D116cE69F19A9ee46F0bd304F21A51f',
Expand All @@ -189,7 +194,7 @@ export const ADDRESSES = {
decimals: 6,
symbol: 'USDT',
},
bbausdt: {
bbausdtOld: {
address: '0x2f4eb100552ef93840d5adc30560e5513dfffacb',
decimals: 18,
symbol: 'bbaUSDT',
Expand Down
8 changes: 4 additions & 4 deletions test/testScripts/swapExample.ts
Expand Up @@ -65,10 +65,10 @@ export async function swap(): Promise<void> {
const gasPrice = BigNumber.from('14000000000');
// This determines the max no of pools the SOR will use to swap.
const maxPools = 4;
const tokenIn = ADDRESSES[networkId].USDT;
const tokenOut = ADDRESSES[networkId].bbausdt;
const swapType: SwapTypes = SwapTypes.SwapExactIn;
const swapAmount = parseFixed('10', 6);
const tokenIn = ADDRESSES[networkId].bbausdt;
const tokenOut = ADDRESSES[networkId].bbausd;
const swapType: SwapTypes = SwapTypes.SwapExactOut;
const swapAmount = parseFixed('10', 18);

const sor = setUp(networkId, provider);

Expand Down

0 comments on commit dd90e2f

Please sign in to comment.