Skip to content

Commit

Permalink
Merge pull request #343 from balancer-labs/develop
Browse files Browse the repository at this point in the history
Release 4.0.1-beta.18
  • Loading branch information
John Grant authored Jan 10, 2023
2 parents fb6b8c0 + 7f17002 commit 5a79b0e
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 56 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@balancer-labs/sor",
"version": "4.0.1-beta.17",
"version": "4.0.1-beta.18",
"license": "GPL-3.0-only",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "rollup -c",
"prepack": "yarn build",
"test": "TS_NODE_PROJECT='tsconfig.testing.json' nyc mocha -r ts-node/register test/*.spec.ts",
"test": "TS_NODE_PROJECT='tsconfig.testing.json' nyc mocha -r ts-node/register test/*.spec.ts --timeout 20000",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint ./src ./test --ext .ts --max-warnings 0",
"node": "npx hardhat node --tsconfig tsconfig.testing.json --fork $(grep ALCHEMY_URL .env | cut -d '=' -f2) --fork-block-number 14828550"
Expand Down
11 changes: 9 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ import { BigNumber as OldBigNumber } from './utils/bignumber';

// priceErrorTolerance is how close we expect prices after swap to be in SOR
// suggested paths
const priceErrorTolerance: string =
process.env.PRICE_ERROR_TOLERANCE || '0.00001';
let priceErrorTolerance = '0.00001';
try {
priceErrorTolerance = process.env.PRICE_ERROR_TOLERANCE || '0.00001';
} catch (err) {
if (console) {
console.log(err);
}
}

export const PRICE_ERROR_TOLERANCE: OldBigNumber = new OldBigNumber(
priceErrorTolerance
);
Expand Down
8 changes: 6 additions & 2 deletions src/pools/elementPool/elementPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ export class ElementPool implements PoolBase<ElementPoolPairData> {
// Updates the balance of a given token for the pool
updateTokenBalanceForPool(token: string, newBalance: BigNumber): void {
// token is BPT
if (this.address == token) {
this.totalShares = newBalance;
if (isSameAddress(this.address, token)) {
this.updateTotalShares(newBalance);
} else {
// token is underlying in the pool
const T = this.tokens.find((t) => isSameAddress(t.address, token));
Expand All @@ -200,6 +200,10 @@ export class ElementPool implements PoolBase<ElementPoolPairData> {
}
}

updateTotalShares(newTotalShares: BigNumber): void {
this.totalShares = newTotalShares;
}

_exactTokenInForTokenOut(
poolPairData: ElementPoolPairData,
amount: OldBigNumber
Expand Down
8 changes: 6 additions & 2 deletions src/pools/gyro2Pool/gyro2Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ export class Gyro2Pool implements PoolBase<Gyro2PoolPairData> {
// Updates the balance of a given token for the pool
updateTokenBalanceForPool(token: string, newBalance: BigNumber): void {
// token is BPT
if (this.address == token) {
this.totalShares = newBalance;
if (isSameAddress(this.address, token)) {
this.updateTotalShares(newBalance);
} else {
// token is underlying in the pool
const T = this.tokens.find((t) => isSameAddress(t.address, token));
Expand All @@ -196,6 +196,10 @@ export class Gyro2Pool implements PoolBase<Gyro2PoolPairData> {
}
}

updateTotalShares(newTotalShares: BigNumber): void {
this.totalShares = newTotalShares;
}

_exactTokenInForTokenOut(
poolPairData: Gyro2PoolPairData,
amount: OldBigNumber
Expand Down
8 changes: 6 additions & 2 deletions src/pools/gyro3Pool/gyro3Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ export class Gyro3Pool implements PoolBase<Gyro3PoolPairData> {
// Updates the balance of a given token for the pool
updateTokenBalanceForPool(token: string, newBalance: BigNumber): void {
// token is BPT
if (this.address == token) {
this.totalShares = newBalance;
if (isSameAddress(this.address, token)) {
this.updateTotalShares(newBalance);
} else {
// token is underlying in the pool
const T = this.tokens.find((t) => isSameAddress(t.address, token));
Expand All @@ -219,6 +219,10 @@ export class Gyro3Pool implements PoolBase<Gyro3PoolPairData> {
}
}

updateTotalShares(newTotalShares: BigNumber): void {
this.totalShares = newTotalShares;
}

_exactTokenInForTokenOut(
poolPairData: Gyro3PoolPairData,
amount: OldBigNumber
Expand Down
8 changes: 6 additions & 2 deletions src/pools/gyroEPool/gyroEPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ export class GyroEPool implements PoolBase<GyroEPoolPairData> {
// Updates the balance of a given token for the pool
updateTokenBalanceForPool(token: string, newBalance: BigNumber): void {
// token is BPT
if (this.address == token) {
this.totalShares = newBalance;
if (isSameAddress(this.address, token)) {
this.updateTotalShares(newBalance);
} else {
// token is underlying in the pool
const T = this.tokens.find((t) => isSameAddress(t.address, token));
Expand All @@ -294,6 +294,10 @@ export class GyroEPool implements PoolBase<GyroEPoolPairData> {
}
}

updateTotalShares(newTotalShares: BigNumber): void {
this.totalShares = newTotalShares;
}

_exactTokenInForTokenOut(
poolPairData: GyroEPoolPairData,
amount: OldBigNumber
Expand Down
17 changes: 13 additions & 4 deletions src/pools/linearPool/linearPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,19 @@ export class LinearPool implements PoolBase<LinearPoolPairData> {

// Updates the balance of a given token for the pool
updateTokenBalanceForPool(token: string, newBalance: BigNumber): void {
const T = this.tokens.find((t) => isSameAddress(t.address, token));
if (!T) throw Error('Pool does not contain this token');
// Converts to human scaled number and saves.
T.balance = formatFixed(newBalance, T.decimals);
// token is BPT
if (isSameAddress(this.address, token)) {
this.updateTotalShares(newBalance);
} else {
const T = this.tokens.find((t) => isSameAddress(t.address, token));
if (!T) throw Error('Pool does not contain this token');
// Converts to human scaled number and saves.
T.balance = formatFixed(newBalance, T.decimals);
}
}

updateTotalShares(newTotalShares: BigNumber): void {
this.totalShares = newTotalShares;
}

_exactTokenInForTokenOut(
Expand Down
8 changes: 6 additions & 2 deletions src/pools/metaStablePool/metaStablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ export class MetaStablePool implements PoolBase<MetaStablePoolPairData> {
// Updates the balance of a given token for the pool
updateTokenBalanceForPool(token: string, newBalance: BigNumber): void {
// token is BPT
if (this.address == token) {
this.totalShares = newBalance;
if (isSameAddress(this.address, token)) {
this.updateTotalShares(newBalance);
} else {
// token is underlying in the pool
const T = this.tokens.find((t) => isSameAddress(t.address, token));
Expand All @@ -192,6 +192,10 @@ export class MetaStablePool implements PoolBase<MetaStablePoolPairData> {
}
}

updateTotalShares(newTotalShares: BigNumber): void {
this.totalShares = newTotalShares;
}

_exactTokenInForTokenOut(
poolPairData: MetaStablePoolPairData,
amount: OldBigNumber
Expand Down
13 changes: 13 additions & 0 deletions src/pools/phantomStablePool/phantomStablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,22 @@ export class PhantomStablePool implements PoolBase<PhantomStablePoolPairData> {
// token is underlying in the pool
const T = this.tokens.find((t) => isSameAddress(t.address, token));
if (!T) throw Error('Pool does not contain this token');

// update total shares with BPT balance diff
if (isSameAddress(this.address, token)) {
const parsedTokenBalance = parseFixed(T.balance, T.decimals);
const diff = parsedTokenBalance.sub(newBalance);
const newTotalShares = this.totalShares.add(diff);
this.updateTotalShares(newTotalShares);
}
// update token balance with new balance
T.balance = formatFixed(newBalance, T.decimals);
}

updateTotalShares(newTotalShares: BigNumber): void {
this.totalShares = newTotalShares;
}

_exactTokenInForTokenOut(
poolPairData: PhantomStablePoolPairData,
amount: OldBigNumber
Expand Down
8 changes: 6 additions & 2 deletions src/pools/stablePool/stablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ export class StablePool implements PoolBase<StablePoolPairData> {
// Updates the balance of a given token for the pool
updateTokenBalanceForPool(token: string, newBalance: BigNumber): void {
// token is BPT
if (this.address == token) {
this.totalShares = newBalance;
if (isSameAddress(this.address, token)) {
this.updateTotalShares(newBalance);
} else {
// token is underlying in the pool
const T = this.tokens.find((t) => isSameAddress(t.address, token));
Expand All @@ -175,6 +175,10 @@ export class StablePool implements PoolBase<StablePoolPairData> {
}
}

updateTotalShares(newTotalShares: BigNumber): void {
this.totalShares = newTotalShares;
}

_exactTokenInForTokenOut(
poolPairData: StablePoolPairData,
amount: OldBigNumber
Expand Down
6 changes: 5 additions & 1 deletion src/pools/weightedPool/weightedPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,18 @@ export class WeightedPool implements PoolBase<WeightedPoolPairData> {
updateTokenBalanceForPool(token: string, newBalance: BigNumber): void {
// token is BPT
if (isSameAddress(this.address, token)) {
this.totalShares = newBalance;
this.updateTotalShares(newBalance);
}
// token is underlying in the pool
const T = this.tokens.find((t) => isSameAddress(t.address, token));
if (!T) throw Error('Pool does not contain this token');
T.balance = formatFixed(newBalance, T.decimals);
}

updateTotalShares(newTotalShares: BigNumber): void {
this.totalShares = newTotalShares;
}

// 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)
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export interface PoolBase<D extends PoolPairBase = PoolPairBase> {
* @param {BigNumber} newBalance - New balance of token. EVM scaled.
*/
updateTokenBalanceForPool: (token: string, newBalance: BigNumber) => void;
updateTotalShares: (newTotalShares: BigNumber) => void;
_exactTokenInForTokenOut: (
poolPairData: D,
amount: OldBigNumber
Expand Down
2 changes: 1 addition & 1 deletion test/elementTrades.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe(`Tests against Element generated test trade file.`, () => {
const poolsFromFile: SubgraphPoolBase[] = [
{
id: 'n/a',
address: 'n/a',
address: '0x000000000000000000000000000000000000000a',
poolType: 'Element',
swapFee: testTrades.init.percent_fee.toString(),
swapEnabled: true,
Expand Down
1 change: 1 addition & 0 deletions test/testData/elementPools/elementFinanceTest1.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"pools": [
{
"id": "0xelementPool1",
"address": "0x000000000000000000000000000000000000000a",
"poolType": "Element",
"swapFee": "0.05",
"swapEnabled": true,
Expand Down
Loading

0 comments on commit 5a79b0e

Please sign in to comment.