Skip to content

Commit

Permalink
Refactor WeightedPool test helpers (#2099)
Browse files Browse the repository at this point in the history
* refactor: remove the deployer that is one big special case

* refactor: generalize BasePool and introduce BaseWeighted for shared weighted functions

* refactor: introduce classes for individual weighted pool types

* test: update pool-utils tests using weighted pools

* test: adjust weighted pool tests to use individual models

* test: adjust dependencies in standalone utils

* remove unused functions; move statics together

* factor out pause parameter defaults

* factor: adjust factory parameters

* refactor: use string enum ManagedPoolType instead of string

* lint: single vs double quotes

* refactor: consistent capitalization

* fix: consistent capitalization

* checkpoint

* lint

* fix: overload TokenList.indicesOf

* lint

* docs: cleanup/clarify comments

* allow WeightedPool to be created with all flavors of Account

* create WeightedPool with signer owner
  • Loading branch information
EndymionJkb authored Apr 27, 2023
1 parent 72f869c commit 8a6dbbd
Show file tree
Hide file tree
Showing 17 changed files with 1,256 additions and 1,157 deletions.
31 changes: 21 additions & 10 deletions pkg/pool-weighted/test/BaseWeightedPool.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { ZERO_ADDRESS } from '@balancer-labs/v2-helpers/src/constants';
import { sharedBeforeEach } from '@balancer-labs/v2-common/sharedBeforeEach';
import { expectBalanceChange } from '@balancer-labs/v2-helpers/src/test/tokenBalance';
import Vault from '@balancer-labs/v2-helpers/src/models/vault/Vault';
import LiquidityBootstrappingPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/LiquidityBootstrappingPool';
import BaseWeightedPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/BaseWeightedPool';

export function itBehavesAsWeightedPool(numberOfTokens: number, poolType: WeightedPoolType): void {
const POOL_SWAP_FEE_PERCENTAGE = fp(0.01);
Expand All @@ -22,22 +24,31 @@ export function itBehavesAsWeightedPool(numberOfTokens: number, poolType: Weight

let recipient: SignerWithAddress, other: SignerWithAddress, lp: SignerWithAddress;
let vault: Vault;
let pool: WeightedPool, allTokens: TokenList, tokens: TokenList;
let pool: BaseWeightedPool, allTokens: TokenList, tokens: TokenList;

const ZEROS = Array(numberOfTokens).fill(bn(0));
const weights: BigNumberish[] = WEIGHTS.slice(0, numberOfTokens);
const initialBalances = INITIAL_BALANCES.slice(0, numberOfTokens);

async function deployPool(params: RawWeightedPoolDeployment = {}): Promise<void> {
pool = await WeightedPool.create({
vault,
tokens,
weights,
swapFeePercentage: POOL_SWAP_FEE_PERCENTAGE,
poolType,
owner: lp, // needed for LBP tests
...params,
});
if (poolType == WeightedPoolType.WEIGHTED_POOL) {
pool = await WeightedPool.create({
vault,
tokens,
weights,
swapFeePercentage: POOL_SWAP_FEE_PERCENTAGE,
...params,
});
} else if (poolType == WeightedPoolType.LIQUIDITY_BOOTSTRAPPING_POOL) {
pool = await LiquidityBootstrappingPool.create({
vault,
tokens,
weights,
swapFeePercentage: POOL_SWAP_FEE_PERCENTAGE,
owner: lp.address, // needed for LBP tests (only owner can join)
...params,
});
}
}

before('setup signers', async () => {
Expand Down
29 changes: 11 additions & 18 deletions pkg/pool-weighted/test/LiquidityBootstrappingPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { MINUTE, currentTimestamp } from '@balancer-labs/v2-helpers/src/time';
import * as expectEvent from '@balancer-labs/v2-helpers/src/test/expectEvent';

import TokenList from '@balancer-labs/v2-helpers/src/models/tokens/TokenList';
import WeightedPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/WeightedPool';
import LiquidityBootstrappingPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/LiquidityBootstrappingPool';
import { range } from 'lodash';
import { WeightedPoolType } from '../../../pvt/helpers/src/models/pools/weighted/types';
import { itBehavesAsWeightedPool } from './BaseWeightedPool.behavior';
import { WeightedPoolType } from '@balancer-labs/v2-helpers/src/models/pools/weighted/types';
import { sharedBeforeEach } from '@balancer-labs/v2-common/sharedBeforeEach';

describe('LiquidityBootstrappingPool', function () {
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('LiquidityBootstrappingPool', function () {
});

let sender: SignerWithAddress;
let pool: WeightedPool;
let pool: LiquidityBootstrappingPool;
const weights = [fp(0.3), fp(0.55), fp(0.1), fp(0.05)];
const initialBalances = [fp(0.9), fp(1.8), fp(2.7), fp(3.6)];

Expand All @@ -56,41 +56,37 @@ describe('LiquidityBootstrappingPool', function () {
const params = {
tokens: allTokens.subset(1),
weights: [fp(0.3)],
poolType: WeightedPoolType.LIQUIDITY_BOOTSTRAPPING_POOL,
};
await expect(WeightedPool.create(params)).to.be.revertedWith('MIN_TOKENS');
await expect(LiquidityBootstrappingPool.create(params)).to.be.revertedWith('MIN_TOKENS');
});

it('fails with > 4 tokens', async () => {
const params = {
tokens: allTokens,
weights: tooManyWeights,
poolType: WeightedPoolType.LIQUIDITY_BOOTSTRAPPING_POOL,
};
await expect(WeightedPool.create(params)).to.be.revertedWith('MAX_TOKENS');
await expect(LiquidityBootstrappingPool.create(params)).to.be.revertedWith('MAX_TOKENS');
});

it('fails with mismatched tokens/weights', async () => {
const params = {
tokens,
weights: tooManyWeights,
poolType: WeightedPoolType.LIQUIDITY_BOOTSTRAPPING_POOL,
};
await expect(WeightedPool.create(params)).to.be.revertedWith('INPUT_LENGTH_MISMATCH');
await expect(LiquidityBootstrappingPool.create(params)).to.be.revertedWith('INPUT_LENGTH_MISMATCH');
});
});

describe('weights and scaling factors', () => {
for (const numTokens of range(2, MAX_TOKENS + 1)) {
context(`with ${numTokens} tokens`, () => {
let pool: WeightedPool;
let pool: LiquidityBootstrappingPool;
let tokens: TokenList;

sharedBeforeEach('deploy pool', async () => {
tokens = allTokens.subset(numTokens);

pool = await WeightedPool.create({
poolType: WeightedPoolType.LIQUIDITY_BOOTSTRAPPING_POOL,
pool = await LiquidityBootstrappingPool.create({
tokens,
weights: weights.slice(0, numTokens),
});
Expand Down Expand Up @@ -119,10 +115,9 @@ describe('LiquidityBootstrappingPool', function () {
const params = {
tokens,
weights,
poolType: WeightedPoolType.LIQUIDITY_BOOTSTRAPPING_POOL,
fromFactory: true,
};
pool = await WeightedPool.create(params);
pool = await LiquidityBootstrappingPool.create(params);
});

it('has no asset managers', async () => {
Expand All @@ -139,10 +134,9 @@ describe('LiquidityBootstrappingPool', function () {
const params = {
tokens,
weights,
poolType: WeightedPoolType.LIQUIDITY_BOOTSTRAPPING_POOL,
swapEnabledOnStart: false,
};
pool = await WeightedPool.create(params);
pool = await LiquidityBootstrappingPool.create(params);
});

it('swaps show disabled on start', async () => {
Expand All @@ -160,10 +154,9 @@ describe('LiquidityBootstrappingPool', function () {
tokens,
weights,
owner: owner.address,
poolType: WeightedPoolType.LIQUIDITY_BOOTSTRAPPING_POOL,
swapEnabledOnStart: true,
};
pool = await WeightedPool.create(params);
pool = await LiquidityBootstrappingPool.create(params);
});

it('swaps show enabled on start', async () => {
Expand Down
14 changes: 7 additions & 7 deletions pkg/pool-weighted/test/ManagedPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import * as expectEvent from '@balancer-labs/v2-helpers/src/test/expectEvent';
import TokenList from '@balancer-labs/v2-helpers/src/models/tokens/TokenList';

import Vault from '@balancer-labs/v2-helpers/src/models/vault/Vault';
import WeightedPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/WeightedPool';
import ManagedPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/ManagedPool';
import {
ExitResult,
JoinQueryResult,
JoinResult,
RawWeightedPoolDeployment,
RawManagedPoolDeployment,
SwapResult,
WeightedPoolType,
ManagedPoolType,
} from '@balancer-labs/v2-helpers/src/models/pools/weighted/types';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/dist/src/signer-with-address';
import { PoolSpecialization, SwapKind } from '@balancer-labs/balancer-js';
Expand All @@ -35,7 +35,7 @@ describe('ManagedPool', function () {
let allTokens: TokenList;
let poolTokens: TokenList;
let admin: SignerWithAddress, owner: SignerWithAddress, other: SignerWithAddress;
let pool: WeightedPool;
let pool: ManagedPool;
let vault: Vault;

const poolVersion = JSON.stringify({
Expand Down Expand Up @@ -71,18 +71,18 @@ describe('ManagedPool', function () {
await allTokens.approve({ from: owner, to: vault });
});

async function deployPool(overrides: RawWeightedPoolDeployment = {}): Promise<WeightedPool> {
async function deployPool(overrides: RawManagedPoolDeployment = {}): Promise<ManagedPool> {
const params = {
vault,
tokens: poolTokens,
weights: poolWeights,
owner: owner.address,
aumFeeId: ProtocolFee.AUM,
poolType: WeightedPoolType.MOCK_MANAGED_POOL,
poolType: ManagedPoolType.MOCK_MANAGED_POOL,
poolVersion,
...overrides,
};
return WeightedPool.create(params);
return ManagedPool.create(params);
}

async function getUnscaledBptPrice(tokenIndex: number): Promise<BigNumber> {
Expand Down
16 changes: 8 additions & 8 deletions pkg/pool-weighted/test/ManagedPoolSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import * as expectEvent from '@balancer-labs/v2-helpers/src/test/expectEvent';
import { deploy } from '@balancer-labs/v2-helpers/src/contract';
import TokenList from '@balancer-labs/v2-helpers/src/models/tokens/TokenList';
import Vault from '@balancer-labs/v2-helpers/src/models/vault/Vault';
import WeightedPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/WeightedPool';
import { CircuitBreakerState, WeightedPoolType } from '@balancer-labs/v2-helpers/src/models/pools/weighted/types';
import ManagedPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/ManagedPool';
import { CircuitBreakerState, ManagedPoolType } from '@balancer-labs/v2-helpers/src/models/pools/weighted/types';
import { expectEqualWithError } from '@balancer-labs/v2-helpers/src/test/relativeError';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/dist/src/signer-with-address';
import { toNormalizedWeights } from '@balancer-labs/balancer-js';
Expand All @@ -43,7 +43,7 @@ describe('ManagedPoolSettings', function () {
let poolTokens: TokenList;
let tooManyWeights: BigNumber[];
let admin: SignerWithAddress, owner: SignerWithAddress, other: SignerWithAddress;
let pool: WeightedPool;
let pool: ManagedPool;
let vault: Vault;

before('setup signers', async () => {
Expand Down Expand Up @@ -79,13 +79,13 @@ describe('ManagedPoolSettings', function () {
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function createMockPool(params: any): Promise<WeightedPool> {
async function createMockPool(params: any): Promise<ManagedPool> {
const fullParams = {
...params,
swapFeePercentage: INITIAL_SWAP_FEE,
poolType: WeightedPoolType.MOCK_MANAGED_POOL_SETTINGS,
poolType: ManagedPoolType.MOCK_MANAGED_POOL_SETTINGS,
};
return WeightedPool.create(fullParams);
return ManagedPool.create(fullParams);
}

describe('constructor', () => {
Expand Down Expand Up @@ -173,11 +173,11 @@ describe('ManagedPoolSettings', function () {
function itStoresProviderFeeIds(aumFeeId: number) {
context(`when aum fee ID is ${ProtocolFee[aumFeeId]}`, () => {
sharedBeforeEach('deploy pool', async () => {
pool = await WeightedPool.create({
poolType: WeightedPoolType.MANAGED_POOL,
pool = await ManagedPool.create({
tokens: allTokens.subset(2),
vault,
aumFeeId,
poolType: ManagedPoolType.MOCK_MANAGED_POOL_SETTINGS,
});
});

Expand Down
4 changes: 0 additions & 4 deletions pkg/pool-weighted/test/WeightedPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import TokenList from '@balancer-labs/v2-helpers/src/models/tokens/TokenList';
import WeightedPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/WeightedPool';
import Vault from '@balancer-labs/v2-helpers/src/models/vault/Vault';
import { FundManagement, SwapKind } from '@balancer-labs/balancer-js';
import { WeightedPoolType } from '@balancer-labs/v2-helpers/src/models/pools/weighted/types';
import { fp, fpDiv, fpMul, FP_100_PCT } from '@balancer-labs/v2-helpers/src/numbers';
import { range } from 'lodash';
import { itPaysProtocolFeesFromInvariantGrowth } from './WeightedPoolProtocolFees.behavior';
Expand Down Expand Up @@ -47,7 +46,6 @@ describe('WeightedPool', function () {
tokens = allTokens.subset(2);

pool = await WeightedPool.create({
poolType: WeightedPoolType.WEIGHTED_POOL,
tokens,
weights: WEIGHTS.slice(0, 2),
swapFeePercentage: POOL_SWAP_FEE_PERCENTAGE,
Expand Down Expand Up @@ -88,7 +86,6 @@ describe('WeightedPool', function () {
tokens = allTokens.subset(numTokens);

pool = await WeightedPool.create({
poolType: WeightedPoolType.WEIGHTED_POOL,
tokens,
weights: WEIGHTS.slice(0, numTokens),
swapFeePercentage: POOL_SWAP_FEE_PERCENTAGE,
Expand Down Expand Up @@ -193,7 +190,6 @@ describe('WeightedPool', function () {
await vault.setSwapFeePercentage(protocolFeePercentage);

pool = await WeightedPool.create({
poolType: WeightedPoolType.WEIGHTED_POOL,
tokens,
weights: WEIGHTS.slice(0, numTokens),
swapFeePercentage: swapFeePercentage,
Expand Down
3 changes: 0 additions & 3 deletions pkg/pool-weighted/test/WeightedPoolProtocolFees.behavior.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { WeightedPoolEncoder } from '@balancer-labs/balancer-js';
import { deploy } from '@balancer-labs/v2-helpers/src/contract';
import { calculateInvariant } from '@balancer-labs/v2-helpers/src/models/pools/weighted/math';
import { WeightedPoolType } from '@balancer-labs/v2-helpers/src/models/pools/weighted/types';
import WeightedPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/WeightedPool';
import TokenList from '@balancer-labs/v2-helpers/src/models/tokens/TokenList';
import Vault from '@balancer-labs/v2-helpers/src/models/vault/Vault';
Expand Down Expand Up @@ -47,7 +46,6 @@ export function itPaysProtocolFeesFromInvariantGrowth(): void {

pool = await WeightedPool.create({
vault,
poolType: WeightedPoolType.WEIGHTED_POOL,
tokens,
weights: WEIGHTS.slice(0, numTokens),
rateProviders,
Expand Down Expand Up @@ -109,7 +107,6 @@ export function itPaysProtocolFeesFromInvariantGrowth(): void {

sharedBeforeEach(async () => {
yieldFeeExemptPool = await WeightedPool.create({
poolType: WeightedPoolType.WEIGHTED_POOL,
tokens,
weights: WEIGHTS.slice(0, numTokens),
swapFeePercentage: POOL_SWAP_FEE_PERCENTAGE,
Expand Down
14 changes: 7 additions & 7 deletions pkg/pool-weighted/test/managed/AddRemove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { toNormalizedWeights } from '@balancer-labs/balancer-js';
import { sharedBeforeEach } from '@balancer-labs/v2-common/sharedBeforeEach';
import { MAX_UINT256, ZERO_ADDRESS } from '@balancer-labs/v2-helpers/src/constants';
import { deploy } from '@balancer-labs/v2-helpers/src/contract';
import { WeightedPoolType } from '@balancer-labs/v2-helpers/src/models/pools/weighted/types';
import WeightedPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/WeightedPool';
import ManagedPool from '@balancer-labs/v2-helpers/src/models/pools/weighted/ManagedPool';
import Token from '@balancer-labs/v2-helpers/src/models/tokens/Token';
import TokenList from '@balancer-labs/v2-helpers/src/models/tokens/TokenList';
import Vault from '@balancer-labs/v2-helpers/src/models/vault/Vault';
Expand All @@ -17,6 +16,7 @@ import { random, range } from 'lodash';
import * as expectEvent from '@balancer-labs/v2-helpers/src/test/expectEvent';
import { ProtocolFee } from '@balancer-labs/v2-helpers/src/models/vault/types';
import { expectTransferEvent } from '@balancer-labs/v2-helpers/src/test/expectTransfer';
import { ManagedPoolType } from '@balancer-labs/v2-helpers/src/models/pools/weighted/types';

describe('ManagedPoolSettings - add/remove token', () => {
let vault: Vault;
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('ManagedPoolSettings - add/remove token', () => {
async function createPool(
numberOfTokens: number,
weights?: Array<BigNumber>
): Promise<{ pool: WeightedPool; poolTokens: TokenList }> {
): Promise<{ pool: ManagedPool; poolTokens: TokenList }> {
const poolTokens = allTokens.subset(numberOfTokens);
if (weights == undefined) {
// We pick random weights, but ones that are not so far apart as to cause issues due to minimum weights. The
Expand All @@ -63,15 +63,15 @@ describe('ManagedPoolSettings - add/remove token', () => {
weights = range(numberOfTokens).map(() => fp(100 + random(50)));
}

const pool = await WeightedPool.create({
const pool = await ManagedPool.create({
tokens: poolTokens,
weights,
owner: owner.address,
poolType: WeightedPoolType.MANAGED_POOL,
assetManagers: Array(numberOfTokens).fill(assetManager.address),
swapEnabledOnStart: true,
vault,
managementAumFeePercentage: fp(0.1), // Non-zero so that some protocol AUM fees are charged
poolType: ManagedPoolType.MOCK_MANAGED_POOL,
});

return { pool, poolTokens };
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('ManagedPoolSettings - add/remove token', () => {
itAddsATokenAtTokenCount(MAX_TOKENS - 1);

function itAddsATokenAtTokenCount(poolTokenCount: number) {
let pool: WeightedPool;
let pool: ManagedPool;
let poolTokens: TokenList;

context(`when the pool has ${poolTokenCount} tokens`, () => {
Expand Down Expand Up @@ -456,7 +456,7 @@ describe('ManagedPoolSettings - add/remove token', () => {
itRemovesATokenAtTokenCount(MAX_TOKENS);

function itRemovesATokenAtTokenCount(poolTokenCount: number) {
let pool: WeightedPool;
let pool: ManagedPool;
let poolTokens: TokenList;

context(`when the pool has ${poolTokenCount} tokens`, () => {
Expand Down
Loading

0 comments on commit 8a6dbbd

Please sign in to comment.