Skip to content

Commit e6f3296

Browse files
authored
Merge pull request #477 from balancer/develop
Release 1.1.2
2 parents c02f1f5 + 76e8a31 commit e6f3296

Some content is hidden

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

50 files changed

+1401
-735
lines changed

balancer-js/examples/data/pools.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Network } from '../../src/index';
2-
import { BalancerSDK } from '../../src/modules/sdk.module';
1+
import { BalancerSDK, Network } from '@balancer-labs/sdk';
32

43
const sdk = new BalancerSDK({
5-
network: Network.MAINNET,
6-
rpcUrl: ''
7-
});
8-
const { pools } = sdk.data;
4+
network: Network.MAINNET,
5+
rpcUrl: 'https://rpc.ankr.com/eth'
6+
});
7+
8+
const { pools, poolsOnChain } = sdk.data;
99

1010
async function main() {
1111

@@ -23,6 +23,14 @@ async function main() {
2323

2424
result = await pools.where(pool => POOL_IDs.includes(pool.id));
2525
console.log('Filter pools by attributes', result);
26+
27+
// Fefetch on-chain balances for a given pool
28+
const pool = await pools.find(POOL_ID1);
29+
for (const idx in pool!.tokens) {
30+
pool!.tokens[idx].balance = '0';
31+
}
32+
const onchain = await poolsOnChain.refresh(pool!);
33+
console.log('onchain pool', onchain);
2634
}
2735

2836
main();

balancer-js/examples/helpers/print-logs.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,23 @@ export const printLogs = async (logs: any[]) => {
7070
};
7171

7272
const printInternalBalanceChanged = (log: any) => {
73-
const { user, token, delta } = log.args
74-
console.log('\x1b[32m%s\x1b[0m', 'User: ', user)
75-
console.log('\x1b[32m%s\x1b[0m', 'Token:', token)
76-
console.log('\x1b[32m%s\x1b[0m', 'Delta:', formatEther(delta))
77-
}
73+
const { user, token, delta } = log.args;
74+
console.log('\x1b[32m%s\x1b[0m', 'User: ', user);
75+
console.log('\x1b[32m%s\x1b[0m', 'Token:', token);
76+
console.log('\x1b[32m%s\x1b[0m', 'Delta:', formatEther(delta));
77+
};
7878

7979
const printTransfer = (log: any) => {
8080
console.log(log.address);
81-
const { from, to, value, src, dst, wad, _to, _from, _value } = log.args
82-
console.log('\x1b[32m%s\x1b[0m', 'From: ', from || _from || src)
83-
console.log('\x1b[32m%s\x1b[0m', 'To: ', to || _to || dst)
84-
console.log('\x1b[32m%s\x1b[0m', 'Value:', formatEther(value || _value || wad))
85-
}
81+
const { from, to, value, src, dst, wad, _to, _from, _value } = log.args;
82+
console.log('\x1b[32m%s\x1b[0m', 'From: ', from || _from || src);
83+
console.log('\x1b[32m%s\x1b[0m', 'To: ', to || _to || dst);
84+
console.log(
85+
'\x1b[32m%s\x1b[0m',
86+
'Value:',
87+
formatEther(value || _value || wad)
88+
);
89+
};
8690

8791
decodedLogs.map((log: any) => {
8892
console.log('-'.repeat(80));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Display APRs
3+
*
4+
* Run command:
5+
* yarn example ./examples/pools/aprs/aprs.zkevm.ts
6+
*/
7+
import { BalancerSDK } from '@balancer-labs/sdk';
8+
9+
const sdk = new BalancerSDK({
10+
network: 1101,
11+
rpcUrl: 'https://rpc.ankr.com/polygon_zkevm',
12+
});
13+
14+
const { pools } = sdk;
15+
16+
const main = async () => {
17+
const pool = await pools.find(
18+
'0xe1f2c039a68a216de6dd427be6c60decf405762a00000000000000000000000e'
19+
);
20+
21+
if (pool) {
22+
const apr = await pools.apr(pool);
23+
console.log(pool.id, apr);
24+
}
25+
};
26+
27+
main();

balancer-js/examples/pools/create/create-linear-pool.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Linear - Create. (Linear Pools are initialized upon creation and can be joined immediately after creation using swaps)
3-
*
3+
*
44
* Run command:
55
* yarn example ./examples/pools/create/create-linear-pool.ts
66
*/
@@ -10,21 +10,21 @@ import {
1010
Network,
1111
PoolType,
1212
ProtocolId,
13-
} from '@balancer-labs/sdk'
14-
import { parseEther } from '@ethersproject/units'
13+
} from '@balancer-labs/sdk';
14+
import { parseEther } from '@ethersproject/units';
1515

1616
async function createLinearPool() {
1717
const balancer = new BalancerSDK({
1818
network: Network.MAINNET,
1919
rpcUrl: 'http://127.0.0.1:8545', // Using local fork for simulation
20-
})
20+
});
2121

2222
// Setup join parameters
23-
const signer = balancer.provider.getSigner()
24-
const ownerAddress = await signer.getAddress()
25-
const dai = '0x6b175474e89094c44da98b954eedeac495271d0f'
26-
const sape = '0x7966c5bae631294d7cffcea5430b78c2f76db6fa'
27-
const poolTokens = [dai, sape]
23+
const signer = balancer.provider.getSigner();
24+
const ownerAddress = await signer.getAddress();
25+
const dai = '0x6b175474e89094c44da98b954eedeac495271d0f';
26+
const sape = '0x7966c5bae631294d7cffcea5430b78c2f76db6fa';
27+
const poolTokens = [dai, sape];
2828

2929
const poolParameters: LinearCreatePoolParameters = {
3030
name: 'My-Test-Pool-Name',
@@ -49,9 +49,8 @@ async function createLinearPool() {
4949
await signer.sendTransaction({
5050
to,
5151
data,
52-
gasLimit: 30000000,
5352
})
54-
).wait()
53+
).wait();
5554

5655
// Check logs of creation receipt to get new pool ID and address
5756
const { poolAddress, poolId } =

balancer-js/examples/pools/create/create-weighted-pool.ts

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* Weighted - Create and do an initial join.
3-
*
3+
*
44
* Run command:
55
* yarn example ./examples/pools/create/create-weighted-pool.ts
66
*/
7-
import { BalancerSDK, Network, PoolType } from '@balancer-labs/sdk'
8-
import { reset, setTokenBalance, approveToken } from 'examples/helpers'
9-
import { AddressZero } from '@ethersproject/constants'
10-
import { parseFixed } from '@ethersproject/bignumber'
7+
import { BalancerSDK, Network, PoolType } from '@balancer-labs/sdk';
8+
import { reset, setTokenBalance, approveToken } from 'examples/helpers';
9+
import { AddressZero } from '@ethersproject/constants';
10+
import { parseFixed } from '@ethersproject/bignumber';
1111

1212
async function createAndInitJoinWeightedPool() {
1313
const balancer = new BalancerSDK({
@@ -16,26 +16,46 @@ async function createAndInitJoinWeightedPool() {
1616
});
1717

1818
// Setup join parameters
19-
const signer = balancer.provider.getSigner()
20-
const ownerAddress = await signer.getAddress()
21-
const usdc = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
22-
const usdt = '0xdac17f958d2ee523a2206206994597c13d831ec7'
23-
const poolTokens = [usdc, usdt]
19+
const signer = balancer.provider.getSigner();
20+
const ownerAddress = await signer.getAddress();
21+
const usdc = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
22+
const usdt = '0xdac17f958d2ee523a2206206994597c13d831ec7';
23+
const poolTokens = [usdc, usdt];
2424
const amountsIn = [
2525
parseFixed('1000000000', 6).toString(),
2626
parseFixed('1000000000', 6).toString(),
2727
];
2828

2929
// Prepare local fork for simulation
30-
await reset(balancer.provider, 17347414)
31-
await setTokenBalance(balancer.provider, ownerAddress, poolTokens[0], amountsIn[0], 9)
32-
await setTokenBalance(balancer.provider, ownerAddress, poolTokens[1], amountsIn[1], 2)
33-
await approveToken(poolTokens[0], balancer.contracts.vault.address, amountsIn[0], signer)
34-
await approveToken(poolTokens[1], balancer.contracts.vault.address, amountsIn[1], signer)
30+
await reset(balancer.provider, 17347414);
31+
await setTokenBalance(
32+
balancer.provider,
33+
ownerAddress,
34+
poolTokens[0],
35+
amountsIn[0],
36+
9
37+
);
38+
await setTokenBalance(
39+
balancer.provider,
40+
ownerAddress,
41+
poolTokens[1],
42+
amountsIn[1],
43+
2
44+
);
45+
await approveToken(
46+
poolTokens[0],
47+
balancer.contracts.vault.address,
48+
amountsIn[0],
49+
signer
50+
);
51+
await approveToken(
52+
poolTokens[1],
53+
balancer.contracts.vault.address,
54+
amountsIn[1],
55+
signer
56+
);
3557

36-
const weightedPoolFactory = balancer.pools.poolFactory.of(
37-
PoolType.Weighted
38-
)
58+
const weightedPoolFactory = balancer.pools.poolFactory.of(PoolType.Weighted);
3959

4060
const poolParameters = {
4161
name: 'My-Test-Pool-Name',
@@ -59,9 +79,8 @@ async function createAndInitJoinWeightedPool() {
5979
from: ownerAddress,
6080
to,
6181
data,
62-
gasLimit: 30000000,
6382
})
64-
).wait()
83+
).wait();
6584

6685
// Check logs of creation receipt to get new pool ID and address
6786
const { poolAddress, poolId } =
@@ -86,7 +105,6 @@ async function createAndInitJoinWeightedPool() {
86105
await signer.sendTransaction({
87106
to: initJoinParams.to,
88107
data: initJoinParams.data,
89-
gasLimit: 30000000,
90108
});
91109

92110
// Check that pool balances are as expected after join
Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* This example shows how to use the SDK generalisedJoin method.
3-
*
3+
*
44
* It depends on a forked mainnet node running on localhost:8545
5-
*
5+
*
66
* Use the following command to start a forked mainnet node:
77
* anvil --fork-url https://rpc.ankr.com/eth --fork-block-number 16411000 --fork-chain-id 1
88
* or
@@ -18,7 +18,7 @@
1818
*
1919
* The example joins the USD stable pool with DAI for decimals convinience.
2020
* However the pool can be joined with any other token or composition of tokens.
21-
*
21+
*
2222
* Expected frontend (FE) flow:
2323
* 1. User selects tokens and amounts to join a pool
2424
* 2. FE calls joinGeneralised with simulation type Tenderly or VaultModel
@@ -28,27 +28,28 @@
2828
* 6. SDK calculates expectedAmountOut that is 100% accurate
2929
* 7. SDK returns joinGeneralised transaction data with proper minAmountsOut limits in place
3030
* 8. User is now able to submit a safe transaction to the blockchain
31-
*
31+
*
3232
* Run with:
3333
* yarn example ./examples/pools/join/join-composable-stable-with-underlying.ts
3434
*/
35+
import { BalancerSDK, Relayer, SimulationType } from '@balancer-labs/sdk';
36+
import { parseEther } from '@ethersproject/units';
3537
import {
36-
BalancerSDK,
37-
Relayer,
38-
SimulationType
39-
} from '@balancer-labs/sdk'
40-
import { parseEther } from '@ethersproject/units'
41-
import { approveToken, printLogs, reset, setTokenBalance } from 'examples/helpers'
38+
approveToken,
39+
printLogs,
40+
reset,
41+
setTokenBalance,
42+
} from 'examples/helpers';
4243

4344
// Joining bbaUSD2 pool with DAI
4445
const poolId =
45-
'0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d'
46-
const dai = '0x6B175474E89094C44Da98b954EedeAC495271d0F'
47-
const amount = parseEther('1000').toString()
46+
'0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d';
47+
const dai = '0x6B175474E89094C44Da98b954EedeAC495271d0F';
48+
const amount = parseEther('1000').toString();
4849

4950
const balancer = new BalancerSDK({
5051
network: 1,
51-
rpcUrl: 'http://localhost:8545',
52+
rpcUrl: 'http://127.0.0.1:8545',
5253
subgraphQuery: {
5354
args: {
5455
where: {
@@ -62,31 +63,31 @@ const balancer = new BalancerSDK({
6263
},
6364
attrs: {},
6465
},
65-
})
66+
});
6667

67-
const { provider, contracts } = balancer
68-
const signer = provider.getSigner()
68+
const { provider, contracts } = balancer;
69+
const signer = provider.getSigner();
6970

7071
/**
7172
* Get some DAI to the signer and approve the vault to move it on our behalf.
7273
* This is only needed for the example to work, in a real world scenario the signer
7374
* would already have DAI and the vault would already be approved.
7475
*/
7576
async function setup(address: string) {
76-
await reset(provider, 17000000)
77-
await setTokenBalance(provider, address, dai, amount, 2)
78-
await approveToken(dai, contracts.vault.address, amount, signer)
77+
await reset(provider, 17000000);
78+
await setTokenBalance(provider, address, dai, amount, 2);
79+
await approveToken(dai, contracts.vault.address, amount, signer);
7980
}
8081

8182
async function join() {
82-
const address = await signer.getAddress()
83+
const address = await signer.getAddress();
8384

84-
setup(address)
85+
setup(address);
8586

8687
// Here we join with DAI, but we could join with any other token or combination of tokens
87-
const tokensIn = [dai]
88-
const amountsIn = [amount]
89-
const slippage = '100' // 100 bps = 1%
88+
const tokensIn = [dai];
89+
const amountsIn = [amount];
90+
const slippage = '100'; // 100 bps = 1%
9091

9192
// Use SDK to create join using either Tenderly or VaultModel simulation
9293
// Note that this does not require authorisation to be defined
@@ -98,19 +99,18 @@ async function join() {
9899
slippage,
99100
signer,
100101
SimulationType.VaultModel
101-
)
102+
);
102103

103104
// User reviews expectedAmountOut
104105
console.log('Expected BPT out - VaultModel: ', expectedOut);
105106

106-
107107
// Need to sign the approval only once per relayer
108108
const relayerAuth = await Relayer.signRelayerApproval(
109109
contracts.relayer.address,
110110
address,
111111
signer,
112112
contracts.vault
113-
)
113+
);
114114

115115
// Use SDK to create join callData
116116
const query = await balancer.pools.generalisedJoin(
@@ -122,7 +122,7 @@ async function join() {
122122
signer,
123123
SimulationType.VaultModel,
124124
relayerAuth
125-
)
125+
);
126126

127127
// Join
128128
const joinReciept = await (
@@ -131,9 +131,9 @@ async function join() {
131131
data: query.encodedCall,
132132
gasLimit: 8e6,
133133
})
134-
).wait()
134+
).wait();
135135

136-
await printLogs(joinReciept.logs)
136+
await printLogs(joinReciept.logs);
137137
}
138138

139-
join()
139+
join();

0 commit comments

Comments
 (0)