Skip to content

Commit fad3b49

Browse files
authored
Merge pull request #529 from balancer/develop
Release 1.1.4
2 parents 2c9b350 + ac1121e commit fad3b49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2672
-237
lines changed

balancer-js/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ cache/
1010
balancer-js.iml
1111
temp/
1212
src/contracts/
13+
14+
.vscode/*

balancer-js/examples/helpers/erc20.ts

+9
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,12 @@ export const getTokenBalance = async (
7575
const erc20 = new Contract(token, iERC20, provider);
7676
return erc20.balanceOf(account);
7777
};
78+
79+
export const getNativeAssetBalance = async (
80+
account: string,
81+
provider: JsonRpcProvider
82+
): Promise<string> => {
83+
return BigNumber.from(
84+
await provider.send('eth_getBalance', [account, 'latest'])
85+
).toString();
86+
};

balancer-js/examples/pools/aprs/aprs.polygon.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const { pools } = sdk;
1515

1616
const main = async () => {
1717
const pool = await pools.find(
18-
'0x216690738aac4aa0c4770253ca26a28f0115c595000000000000000000000b2c'
18+
'0xf0ad209e2e969eaaa8c882aac71f02d8a047d5c2000200000000000000000b49'
1919
);
2020

2121
if (pool) {

balancer-js/examples/pools/aprs/aprs.zkevm.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import { BalancerSDK } from '@balancer-labs/sdk';
88

99
const sdk = new BalancerSDK({
1010
network: 1101,
11-
rpcUrl: 'https://rpc.ankr.com/polygon_zkevm',
11+
rpcUrl: 'https://zkevm-rpc.com',
1212
});
1313

1414
const { pools } = sdk;
1515

1616
const main = async () => {
1717
const pool = await pools.find(
18-
'0xe1f2c039a68a216de6dd427be6c60decf405762a00000000000000000000000e'
18+
'0x1d0a8a31cdb04efac3153237526fb15cc65a252000000000000000000000000f'
1919
);
2020

2121
if (pool) {

balancer-js/examples/pools/exit/recovery-exit.ts

+84-24
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,122 @@
44
* Run command:
55
* yarn example ./examples/pools/exit/recovery-exit.ts
66
*/
7+
import { FORK_NODES } from '@/test/lib/utils';
78
import {
89
BalancerSDK,
9-
insert,
10+
removeItem,
1011
Network,
1112
truncateAddresses,
1213
} from '@balancer-labs/sdk';
1314
import { parseEther } from '@ethersproject/units';
1415
import { getTokenBalance, reset, setTokenBalance } from 'examples/helpers';
1516

16-
async function recoveryExit() {
17+
async function recoveryExitLive() {
18+
const network = Network.MAINNET;
19+
const rpcUrl = FORK_NODES[network];
20+
const poolId =
21+
'0x20b156776114e8a801e9767d90c6ccccc8adf398000000000000000000000499';
22+
const userAddress = '0x0000000000000000000000000000000000000000';
23+
const bptAmount = String(parseEther('1'));
24+
const slippage = '1'; // 1 bps = 0.1%
25+
26+
const balancer = new BalancerSDK({
27+
network,
28+
rpcUrl,
29+
});
30+
const { poolsOnChain, pools } = balancer.data;
31+
32+
// Use SDK to find pool info
33+
let pool = await pools.find(poolId);
34+
if (!pool) throw 'POOL_DOESNT_EXIST';
35+
36+
// Refresh pool data from chain before building and sending tx
37+
pool = await poolsOnChain.refresh(pool);
38+
39+
// Build transaction
40+
const { expectedAmountsOut, minAmountsOut } =
41+
balancer.pools.buildRecoveryExit({
42+
pool,
43+
bptAmount,
44+
userAddress,
45+
slippage,
46+
});
47+
48+
console.log(expectedAmountsOut.toString());
49+
console.log(minAmountsOut.toString());
50+
}
51+
52+
async function recoveryExitFork() {
53+
const poolId =
54+
'0x20b156776114e8a801e9767d90c6ccccc8adf398000000000000000000000499';
55+
const blockNo = 17700000;
56+
1757
const balancer = new BalancerSDK({
1858
network: Network.MAINNET,
1959
rpcUrl: 'http://127.0.0.1:8545', // Using local fork for simulation
2060
});
61+
const { poolsOnChain, pools } = balancer.data;
2162

2263
// Setup exit parameters
2364
const signer = balancer.provider.getSigner();
24-
const address = await signer.getAddress();
25-
26-
const poolId =
27-
// '0x50cf90b954958480b8df7958a9e965752f62712400000000000000000000046f'; // bb-e-usd
28-
// '0xd4e7c1f3da1144c9e2cfd1b015eda7652b4a439900000000000000000000046a'; // bb-e-usdc
29-
// '0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d'; // bb-a-usd
30-
'0xa718042e5622099e5f0ace4e7122058ab39e1bbe000200000000000000000475'; // 50temple_50bb-e-usd
65+
const userAddress = await signer.getAddress();
3166

32-
const bptIn = String(parseEther('1'));
67+
const bptAmount = String(parseEther('1'));
3368
const slippage = '200'; // 200 bps = 2%
3469

3570
// Use SDK to find pool info
36-
const pool = await balancer.pools.find(poolId);
71+
let pool = await pools.find(poolId);
3772
if (!pool) throw 'POOL_DOESNT_EXIST';
3873

3974
// Prepare local fork for simulation
40-
await reset(balancer.provider, 17700000);
41-
await setTokenBalance(balancer.provider, address, pool.address, bptIn, 0);
75+
await reset(balancer.provider, blockNo);
76+
await setTokenBalance(
77+
balancer.provider,
78+
userAddress,
79+
pool.address,
80+
bptAmount,
81+
0
82+
);
83+
84+
// Refresh pool data from chain before building and sending tx
85+
pool = await poolsOnChain.refresh(pool);
4286

4387
// Build transaction
4488
const { to, data, expectedAmountsOut, minAmountsOut } =
45-
pool.buildRecoveryExit(address, bptIn, slippage);
89+
balancer.pools.buildRecoveryExit({
90+
pool,
91+
bptAmount,
92+
userAddress,
93+
slippage,
94+
});
4695

4796
// Send transaction
4897
await signer.sendTransaction({ to, data });
4998

99+
// Refresh pool data from chain before building and sending tx
100+
pool = await poolsOnChain.refresh(pool);
101+
102+
const bptIndex = pool.tokensList.indexOf(pool.address);
103+
const tokensWithoutBpt =
104+
bptIndex === -1 ? pool.tokensList : removeItem(pool.tokensList, bptIndex);
50105
// Check balances after transaction to confirm success
51-
const balances = await Promise.all(
52-
pool.tokensList.map((token) =>
53-
getTokenBalance(token, address, balancer.provider)
54-
)
55-
);
106+
const balances = await Promise.all([
107+
...tokensWithoutBpt.map((token) =>
108+
getTokenBalance(token, userAddress, balancer.provider)
109+
),
110+
getTokenBalance(pool.address, userAddress, balancer.provider),
111+
]);
56112

57113
console.table({
58-
tokensOut: truncateAddresses(pool.tokensList),
59-
minAmountsOut: insert(minAmountsOut, pool.bptIndex, bptIn),
60-
expectedAmountsOut: insert(expectedAmountsOut, pool.bptIndex, bptIn),
61-
amountsOut: balances.map((b) => b.toString()),
114+
tokensOut: truncateAddresses(tokensWithoutBpt),
115+
minAmountsOut: minAmountsOut,
116+
expectedAmountsOut: expectedAmountsOut,
117+
amountsOut: removeItem(balances, balances.length - 1).map((b) =>
118+
b.toString()
119+
),
62120
});
121+
console.log(`BPT Balance: `, balances[balances.length - 1].toString());
63122
}
64123

65-
recoveryExit();
124+
// recoveryExitFork();
125+
recoveryExitLive();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Display APRs for pool ids hardcoded under `const ids`
3+
*
4+
* Run command:
5+
* yarn example ./examples/pools/volume/volume.avalanche.ts
6+
*/
7+
import { BalancerSDK } from '@balancer-labs/sdk';
8+
9+
const sdk = new BalancerSDK({
10+
network: 43114,
11+
rpcUrl: 'https://avalanche.public-rpc.com',
12+
});
13+
14+
const { pools } = sdk;
15+
16+
const main = async () => {
17+
const pool = await pools.find(
18+
'0xa1d14d922a575232066520eda11e27760946c991000000000000000000000012'
19+
);
20+
21+
if (pool) {
22+
const volume = await pools.volume(pool);
23+
console.table([
24+
{
25+
id: pool.id,
26+
type: pool.poolType,
27+
totalVolume: pool.totalSwapVolume,
28+
volume,
29+
},
30+
]);
31+
}
32+
};
33+
34+
main();
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import '@nomiclabs/hardhat-ethers';
2+
3+
/**
4+
* @type import('hardhat/config').HardhatUserConfig
5+
*/
6+
export default {
7+
networks: {
8+
hardhat: {
9+
chainId: 43114,
10+
},
11+
},
12+
};

balancer-js/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@balancer-labs/sdk",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "JavaScript SDK for interacting with the Balancer Protocol V2",
55
"license": "GPL-3.0-only",
66
"homepage": "https://github.com/balancer-labs/balancer-sdk#readme",
@@ -35,6 +35,7 @@
3535
"node:arbitrum": "npx hardhat --tsconfig tsconfig.testing.json --config hardhat.config.arbitrum.ts node --fork $(. ./.env && echo $ALCHEMY_URL_ARBITRUM) --port 8161",
3636
"node:gnosis": "npx hardhat --tsconfig tsconfig.testing.json --config hardhat.config.gnosis.ts node --fork $(. ./.env && echo $RPC_URL_GNOSIS) --port 8100",
3737
"node:zkevm": "npx hardhat --tsconfig tsconfig.testing.json --config hardhat.config.zkevm.ts node --fork $(. ./.env && echo $ALCHEMY_URL_ZKEVM) --port 8101",
38+
"node:avalanche": "npx hardhat --tsconfig tsconfig.testing.json --config hardhat.config.avalanche.ts node --fork $(. ./.env && echo $RPC_URL_AVALANCHE) --port 8114",
3839
"typechain:generate": "npx typechain --target ethers-v5 --out-dir src/contracts './src/lib/abi/*.json'"
3940
},
4041
"devDependencies": {
@@ -87,7 +88,7 @@
8788
"typescript": "^4.0.2"
8889
},
8990
"dependencies": {
90-
"@balancer-labs/sor": "4.1.1-beta.13",
91+
"@balancer-labs/sor": "^4.1.1-beta.16",
9192
"@ethersproject/abi": "^5.4.0",
9293
"@ethersproject/abstract-signer": "^5.4.0",
9394
"@ethersproject/address": "^5.4.0",

0 commit comments

Comments
 (0)