Skip to content

Commit

Permalink
Merge pull request #244 from balancer-labs/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
John Grant authored Mar 29, 2022
2 parents 8126850 + ebf85a3 commit 0ba19a7
Show file tree
Hide file tree
Showing 22 changed files with 558 additions and 1,118 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@balancer-labs/sor",
"version": "4.0.0-beta.0",
"version": "4.0.0-beta.1",
"license": "GPL-3.0-only",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
13 changes: 12 additions & 1 deletion src/formatSwaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ const getTokenAddresses = (swaps: Swap[][]): string[] => {
return [...tokenAddressesSet];
};

/**
* @returns an array of deduplicated token addresses used in the provided swaps
*/
export const getTokenAddressesForSwap = (swaps: Swap[]): string[] => {
const tokenAddressesSet: Set<string> = new Set(
swaps.flatMap((swap): [string, string] => [swap.tokenIn, swap.tokenOut])
);

return [...tokenAddressesSet];
};

/**
* @dev Assumes that intermediate swaps have been properly formatted using the zero sentinel value
* @returns the total amount of tokens used in the described batchSwap
Expand All @@ -38,7 +49,7 @@ const getTotalSwapAmount = (swaps: SwapV2[]) => {
* @param tokenAddresses - an array of all the token address which are involved in the batchSwap
* @returns
*/
const formatSequence = (
export const formatSequence = (
swapKind: SwapTypes,
sequence: Swap[],
tokenAddresses: string[]
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ export {
queryBatchSwapTokensOut,
} from './frontendHelpers/queryBatchSwapHelpers';
export * from './types';
export { formatSequence, getTokenAddressesForSwap } from './formatSwaps';
export { RouteProposer } from './routeProposal';
export { parseToPoolsDict } from './routeProposal/filtering';
6 changes: 0 additions & 6 deletions src/pools/elementPool/elementPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { BigNumber as OldBigNumber, bnum } from '../../utils/bignumber';
import {
PoolBase,
PoolTypes,
SwapPairType,
PoolPairBase,
SwapTypes,
SubgraphPoolBase,
Expand Down Expand Up @@ -35,7 +34,6 @@ export type ElementPoolPairData = PoolPairBase & {

export class ElementPool implements PoolBase {
poolType: PoolTypes = PoolTypes.Element;
swapPairType: SwapPairType;
id: string;
address: string;
swapFee: BigNumber;
Expand Down Expand Up @@ -101,10 +99,6 @@ export class ElementPool implements PoolBase {
this.currentBlockTimestamp = timestamp;
}

setTypeForSwap(type: SwapPairType): void {
this.swapPairType = type;
}

parsePoolPairData(tokenIn: string, tokenOut: string): ElementPoolPairData {
const tokenIndexIn = this.tokens.findIndex(
(t) => getAddress(t.address) === getAddress(tokenIn)
Expand Down
6 changes: 0 additions & 6 deletions src/pools/gyro2Pool/gyro2Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
PoolBase,
PoolPairBase,
PoolTypes,
SwapPairType,
SubgraphToken,
SwapTypes,
SubgraphPoolBase,
Expand Down Expand Up @@ -41,7 +40,6 @@ export type Gyro2PoolToken = Pick<

export class Gyro2Pool implements PoolBase {
poolType: PoolTypes = PoolTypes.Gyro2;
swapPairType: SwapPairType;
id: string;
address: string;
tokensList: string[];
Expand Down Expand Up @@ -107,10 +105,6 @@ export class Gyro2Pool implements PoolBase {
this.priceBounds = priceBounds;
}

setTypeForSwap(type: SwapPairType): void {
this.swapPairType = type;
}

parsePoolPairData(tokenIn: string, tokenOut: string): Gyro2PoolPairData {
const tokenInIndex = this.tokens.findIndex(
(t) => getAddress(t.address) === getAddress(tokenIn)
Expand Down
6 changes: 0 additions & 6 deletions src/pools/gyro3Pool/gyro3Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
PoolBase,
PoolPairBase,
PoolTypes,
SwapPairType,
SubgraphToken,
SwapTypes,
SubgraphPoolBase,
Expand Down Expand Up @@ -39,7 +38,6 @@ export type Gyro3PoolToken = Pick<

export class Gyro3Pool implements PoolBase {
poolType: PoolTypes = PoolTypes.Gyro3;
swapPairType: SwapPairType;
id: string;
address: string;
tokensList: string[];
Expand Down Expand Up @@ -106,10 +104,6 @@ export class Gyro3Pool implements PoolBase {
this.root3Alpha = root3Alpha;
}

setTypeForSwap(type: SwapPairType): void {
this.swapPairType = type;
}

parsePoolPairData(tokenIn: string, tokenOut: string): Gyro3PoolPairData {
const tI = Gyro3Pool.findToken(
this.tokens,
Expand Down
40 changes: 18 additions & 22 deletions src/pools/linearPool/linearMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ export function _calcBptOutPerMainIn(
wrappedBalance,
params
);
return MathSol.divDownFixed(
MathSol.mulDownFixed(bptSupply, deltaNominalMain),
invariant
);
return MathSol.divDown(MathSol.mul(bptSupply, deltaNominalMain), invariant);
}

export function _calcBptInPerMainOut(
Expand All @@ -55,10 +52,7 @@ export function _calcBptInPerMainOut(
wrappedBalance,
params
);
return MathSol.divUpFixed(
MathSol.mulUpFixed(bptSupply, deltaNominalMain),
invariant
);
return MathSol.divUp(MathSol.mul(bptSupply, deltaNominalMain), invariant);
}

export function _calcBptInPerWrappedOut(
Expand All @@ -81,8 +75,8 @@ export function _calcBptInPerWrappedOut(
newWrappedBalance,
params
);
const newBptBalance = MathSol.divDownFixed(
MathSol.mulDownFixed(bptSupply, newInvariant),
const newBptBalance = MathSol.divDown(
MathSol.mul(bptSupply, newInvariant),
previousInvariant
);
return bptSupply - newBptBalance;
Expand Down Expand Up @@ -129,8 +123,8 @@ export function _calcMainInPerBptOut(
wrappedBalance,
params
);
const deltaNominalMain = MathSol.divUpFixed(
MathSol.mulUpFixed(invariant, bptOut),
const deltaNominalMain = MathSol.divUp(
MathSol.mul(invariant, bptOut),
bptSupply
);
const afterNominalMain = previousNominalMain + deltaNominalMain;
Expand All @@ -152,8 +146,8 @@ export function _calcMainOutPerBptIn(
wrappedBalance,
params
);
const deltaNominalMain = MathSol.divDownFixed(
MathSol.mulDownFixed(invariant, bptIn),
const deltaNominalMain = MathSol.divDown(
MathSol.mul(invariant, bptIn),
bptSupply
);
const afterNominalMain = previousNominalMain - deltaNominalMain;
Expand Down Expand Up @@ -212,8 +206,8 @@ export function _calcBptOutPerWrappedIn(
newWrappedBalance,
params
);
const newBptBalance = MathSol.divDownFixed(
MathSol.mulDownFixed(bptSupply, newInvariant),
const newBptBalance = MathSol.divDown(
MathSol.mul(bptSupply, newInvariant),
previousInvariant
);
return newBptBalance - bptSupply;
Expand All @@ -240,12 +234,13 @@ export function _calcWrappedInPerBptOut(
);
const newBptBalance = bptSupply + bptOut;
const newWrappedBalance = MathSol.divUpFixed(
MathSol.mulUpFixed(
MathSol.divUpFixed(newBptBalance, bptSupply),
previousInvariant
MathSol.divUp(
MathSol.mul(newBptBalance, previousInvariant),
bptSupply
) - nominalMain,
params.rate
);

return newWrappedBalance - wrappedBalance;
}

Expand All @@ -265,12 +260,13 @@ export function _calcWrappedOutPerBptIn(
);
const newBptBalance = bptSupply - bptIn;
const newWrappedBalance = MathSol.divUpFixed(
MathSol.mulUpFixed(
MathSol.divUpFixed(newBptBalance, bptSupply),
previousInvariant
MathSol.divUp(
MathSol.mul(newBptBalance, previousInvariant),
bptSupply
) - nominalMain,
params.rate
);

return wrappedBalance - newWrappedBalance;
}

Expand Down
12 changes: 3 additions & 9 deletions src/pools/linearPool/linearPool.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { BigNumber, parseFixed } from '@ethersproject/bignumber';
import { BigNumber, parseFixed, formatFixed } from '@ethersproject/bignumber';
import { bnum, scale, ZERO } from '../../utils/bignumber';
import { BigNumber as OldBigNumber } from '../../utils/bignumber';
import { WeiPerEther as ONE } from '@ethersproject/constants';
import { isSameAddress } from '../../utils';
import {
PoolBase,
PoolTypes,
SwapPairType,
PoolPairBase,
SwapTypes,
SubgraphPoolBase,
Expand Down Expand Up @@ -68,7 +67,6 @@ export type LinearPoolPairData = PoolPairBase & {

export class LinearPool implements PoolBase {
poolType: PoolTypes = PoolTypes.Linear;
swapPairType: SwapPairType;
id: string;
address: string;
swapFee: BigNumber;
Expand Down Expand Up @@ -136,10 +134,6 @@ export class LinearPool implements PoolBase {
this.upperTarget = parseFixed(upperTarget, 18);
}

setTypeForSwap(type: SwapPairType): void {
this.swapPairType = type;
}

parsePoolPairData(tokenIn: string, tokenOut: string): LinearPoolPairData {
let pairType: PairTypes;

Expand Down Expand Up @@ -306,7 +300,8 @@ export class LinearPool implements PoolBase {
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');
T.balance = newBalance.toString();
// Converts to human scaled number and saves.
T.balance = formatFixed(newBalance, T.decimals);
}

_exactTokenInForTokenOut(
Expand Down Expand Up @@ -681,7 +676,6 @@ export class LinearPool implements PoolBase {
0
);
} catch (err) {
console.log('OUCH?', err);
return ZERO;
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/pools/metaStablePool/metaStablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { BigNumber as OldBigNumber, bnum, ZERO } from '../../utils/bignumber';
import {
PoolBase,
PoolTypes,
SwapPairType,
SwapTypes,
SubgraphPoolBase,
SubgraphToken,
Expand Down Expand Up @@ -35,7 +34,6 @@ export type MetaStablePoolPairData = StablePoolPairData & {

export class MetaStablePool implements PoolBase {
poolType: PoolTypes = PoolTypes.MetaStable;
swapPairType: SwapPairType;
id: string;
address: string;
amp: BigNumber;
Expand Down Expand Up @@ -79,10 +77,6 @@ export class MetaStablePool implements PoolBase {
this.tokensList = tokensList;
}

setTypeForSwap(type: SwapPairType): void {
this.swapPairType = type;
}

parsePoolPairData(
tokenIn: string,
tokenOut: string
Expand Down
6 changes: 0 additions & 6 deletions src/pools/phantomStablePool/phantomStablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { BigNumber as OldBigNumber, bnum, ZERO } from '../../utils/bignumber';
import {
PoolBase,
PoolTypes,
SwapPairType,
SwapTypes,
SubgraphPoolBase,
SubgraphToken,
Expand Down Expand Up @@ -42,7 +41,6 @@ export type PhantomStablePoolPairData = MetaStablePoolPairData & {

export class PhantomStablePool implements PoolBase {
poolType: PoolTypes = PoolTypes.MetaStable;
swapPairType: SwapPairType;
id: string;
address: string;
amp: BigNumber;
Expand Down Expand Up @@ -104,10 +102,6 @@ export class PhantomStablePool implements PoolBase {
this.tokensList = tokensList;
}

setTypeForSwap(type: SwapPairType): void {
this.swapPairType = type;
}

parsePoolPairData(
tokenIn: string,
tokenOut: string
Expand Down
6 changes: 0 additions & 6 deletions src/pools/stablePool/stablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { isSameAddress } from '../../utils';
import {
PoolBase,
PoolTypes,
SwapPairType,
PoolPairBase,
SwapTypes,
SubgraphPoolBase,
Expand All @@ -37,7 +36,6 @@ export type StablePoolPairData = PoolPairBase & {

export class StablePool implements PoolBase {
poolType: PoolTypes = PoolTypes.Stable;
swapPairType: SwapPairType;
id: string;
address: string;
amp: BigNumber;
Expand Down Expand Up @@ -81,10 +79,6 @@ export class StablePool implements PoolBase {
this.tokensList = tokensList;
}

setTypeForSwap(type: SwapPairType): void {
this.swapPairType = type;
}

parsePoolPairData(tokenIn: string, tokenOut: string): StablePoolPairData {
const tokenIndexIn = this.tokens.findIndex(
(t) => getAddress(t.address) === getAddress(tokenIn)
Expand Down
6 changes: 0 additions & 6 deletions src/pools/weightedPool/weightedPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { isSameAddress } from '../../utils';
import {
PoolBase,
PoolTypes,
SwapPairType,
PoolPairBase,
SwapTypes,
SubgraphPoolBase,
Expand Down Expand Up @@ -39,7 +38,6 @@ export type WeightedPoolPairData = PoolPairBase & {

export class WeightedPool implements PoolBase {
poolType: PoolTypes = PoolTypes.Weighted;
swapPairType: SwapPairType;
id: string;
address: string;
swapFee: BigNumber;
Expand Down Expand Up @@ -85,10 +83,6 @@ export class WeightedPool implements PoolBase {
this.totalWeight = parseFixed(totalWeight, 18);
}

setTypeForSwap(type: SwapPairType): void {
this.swapPairType = type;
}

parsePoolPairData(tokenIn: string, tokenOut: string): WeightedPoolPairData {
const tokenIndexIn = this.tokens.findIndex(
(t) => getAddress(t.address) === getAddress(tokenIn)
Expand Down
Loading

0 comments on commit 0ba19a7

Please sign in to comment.