Skip to content

Commit 8f44a0e

Browse files
authored
Merge pull request #585 from balancer/develop
Release 1.1.6
2 parents b0fe922 + 85ca8e6 commit 8f44a0e

Some content is hidden

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

46 files changed

+3866
-1533
lines changed

.github/workflows/beta-release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
echo -n "$SIGNING_KEY" | base64 --decode | gpg --import
4141
git config --global user.name "johngrantuk"
4242
git config --global user.email "4797222+johngrantuk@users.noreply.github.com"
43-
git config user.signingkey 0B86E3F46D321811DC4330D1376AF1CD2A15D127
43+
git config user.signingkey 5D1644BB7D087635E36B06C1ABCBFFAEE5EAEE77
4444
git config gpg.program /usr/bin/gpg
4545
yarn version --prerelease --preid beta --no-git-tag-version
4646
export NEW_VERSION=$(jq -r '.version' package.json)

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Balancer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

balancer-js/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# **Deprecation Notice** #
2+
3+
This package/library is being deprecated in favor of our new and improved version located at [repo](https://github.com/balancer/b-sdk)/[package](https://www.npmjs.com/package/@balancer/sdk).
4+
5+
We recommend migrating to the new repository to access the latest features (including V3 support), bug fixes, and security patches. For more information about this change, please check out the examples and documentation pages:
6+
7+
* [Documentation Pages](https://docs-v3.balancer.fi/developer-reference/sdk/)
8+
* [Examples](https://github.com/balancer/b-sdk/tree/main/examples)
9+
10+
If you have any questions or need assistance with migrating to the new repository, feel free to reach out via the Dev channel on [Discord](https://discord.balancer.fi/).
11+
12+
Thank you for your understanding, and we hope to see you in our new repository!
13+
114
# Balancer Javascript SDK
215

316
A JavaScript SDK which provides commonly used utilties for interacting with Balancer Protocol V2.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Display APRs for pool ids hardcoded under `const ids`
3+
* Run command: yarn example ./examples/data/token-prices.ts
4+
*/
5+
import { ApiTokenPriceService } from '@/modules/sor/token-price/apiTokenPriceService';
6+
7+
const dai = '0x6b175474e89094c44da98b954eedeac495271d0f';
8+
const weth = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
9+
const ohm = '0X64AA3364F17A4D01C6F1751FD97C2BD3D7E7F1D5';
10+
11+
(async () => {
12+
const apiTokenPriceService = new ApiTokenPriceService(1);
13+
const daiPriceInEth = await apiTokenPriceService.getNativeAssetPriceInToken(
14+
dai
15+
);
16+
console.log('Dai Price In ETH: ' + daiPriceInEth);
17+
const wethPriceInEth = await apiTokenPriceService.getNativeAssetPriceInToken(
18+
weth
19+
);
20+
console.log('WETH Price In ETH: ' + wethPriceInEth);
21+
const ohmPriceInEth = await apiTokenPriceService.getNativeAssetPriceInToken(
22+
ohm
23+
);
24+
console.log('OHM Price In ETH: ' + ohmPriceInEth);
25+
})();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Display APRs for pool ids hardcoded under `const ids`
3+
*
4+
* Run command
5+
* yarn example ./examples/pools/aprs/aprs.arbitrum.ts
6+
*/
7+
import { BalancerSDK, Pool } from '@balancer-labs/sdk';
8+
9+
const sdk = new BalancerSDK({
10+
network: 8453,
11+
rpcUrl: 'https://rpc.ankr.com/base',
12+
});
13+
14+
const { pools } = sdk;
15+
16+
const main = async () => {
17+
const id =
18+
'0xfb4c2e6e6e27b5b4a07a36360c89ede29bb3c9b6000000000000000000000026';
19+
const pool = (await pools.find(id)) as Pool;
20+
const apr = await pools.apr(pool);
21+
console.log(pool.id, apr);
22+
23+
// const list = (
24+
// await pools.where(
25+
// (pool) =>
26+
// pool.poolType != 'Element' &&
27+
// pool.poolType != 'AaveLinear' &&
28+
// pool.poolType != 'LiquidityBootstrapping'
29+
// )
30+
// )
31+
// .sort((a, b) => parseFloat(b.totalLiquidity) - parseFloat(a.totalLiquidity))
32+
// .slice(0, 30)
33+
34+
// list.forEach(async (pool) => {
35+
// try {
36+
// const apr = await pools.apr(pool)
37+
// console.log(pool.id, apr)
38+
// } catch (e) {
39+
// console.log(e)
40+
// }
41+
// });
42+
};
43+
44+
main();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { BalancerSDK } from '@balancer-labs/sdk';
2+
3+
const sdk = new BalancerSDK({
4+
network: 1,
5+
rpcUrl: 'https://rpc.ankr.com/eth',
6+
coingecko: {
7+
coingeckoApiKey: 'CG-ViHyrfvtLz2WSCJzm59TfGow',
8+
isDemoApiKey: true,
9+
},
10+
});
11+
12+
const bptPriceExample = async () => {
13+
const poolId =
14+
'0x26cc136e9b8fd65466f193a8e5710661ed9a98270002000000000000000005ad';
15+
const pool = await sdk.pools.find(poolId);
16+
if (!pool) {
17+
throw new Error('Pool not found');
18+
}
19+
const bptPrice = await sdk.pools.bptPrice(pool);
20+
console.log('bpt price: ', bptPrice);
21+
};
22+
23+
bptPriceExample().catch((error) => console.error(error));

balancer-js/examples/swaps/advanced.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ async function getAndProcessSwaps(
4242
console.log('No Swap');
4343
return;
4444
}
45-
// console.log(swapInfo.swaps);
46-
// console.log(swapInfo.tokenAddresses);
45+
console.log(swapInfo.swaps);
46+
console.log(swapInfo.tokenAddresses);
4747
console.log(`Return amount: `, swapInfo.returnAmount.toString());
4848

4949
const pools = balancer.swaps.sor.getPools();
@@ -120,15 +120,16 @@ async function getAndProcessSwaps(
120120
}
121121

122122
async function swapExample() {
123-
const network = Network.GNOSIS;
123+
const network = Network.ARBITRUM;
124124
const rpcUrl = FORK_NODES[network];
125-
const tokenIn = '0xe91d153e0b41518a2ce8dd3d7944fa863463a97d';
126-
const tokenOut = '0x6A023CCd1ff6F2045C3309768eAd9E68F978f6e1';
125+
const tokenIn = '0xaf88d065e77c8cC2239327C5EDb3A432268e5831';
126+
const tokenOut = '0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9';
127127
const swapType = SwapTypes.SwapExactIn;
128-
const amount = parseFixed('20000', 18);
128+
const amount = parseFixed('200000', 6);
129129
// Currently Relayer only suitable for ExactIn and non-eth swaps
130130
const canUseJoinExitPaths = canUseJoinExit(swapType, tokenIn, tokenOut);
131131

132+
console.log(rpcUrl);
132133
const balancer = new BalancerSDK({
133134
network,
134135
rpcUrl,
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Helper example to facilitate swap debugging within the SDK
3+
*
4+
* How to run:
5+
* yarn example examples/swaps/swapDebug.ts
6+
*/
7+
import { ADDRESSES } from '@/test/lib/constants';
8+
import { FORK_NODES, RPC_URLS, forkSetup } from '@/test/lib/utils';
9+
import { BalancerSDK, Network } from '@balancer-labs/sdk';
10+
import { formatFixed } from '@ethersproject/bignumber';
11+
12+
const network = Network.MAINNET;
13+
const rpcUrl = RPC_URLS[network];
14+
const sdk = new BalancerSDK({
15+
network,
16+
rpcUrl,
17+
});
18+
19+
const tokenIn = ADDRESSES[network].BAL8020BPT;
20+
const tokenOut = ADDRESSES[network].auraBal;
21+
const amount = String(BigInt(1000e18)); // 1000 eth
22+
23+
const { swaps } = sdk;
24+
const erc20Out = sdk.contracts.ERC20(tokenOut.address, sdk.provider);
25+
26+
async function swap() {
27+
const signer = sdk.provider.getSigner();
28+
const account = await signer.getAddress();
29+
30+
await forkSetup(
31+
signer,
32+
[tokenIn.address],
33+
[tokenIn.slot],
34+
[amount],
35+
FORK_NODES[network]
36+
);
37+
38+
// Finding a trading route rely on on-chain data.
39+
// fetchPools will fetch the current data from the subgraph.
40+
// Let's fetch just 5 pools with highest liquidity of tokenOut.
41+
await swaps.fetchPools({
42+
first: 5,
43+
where: {
44+
swapEnabled: {
45+
eq: true,
46+
},
47+
tokensList: {
48+
contains: [tokenOut.address],
49+
},
50+
},
51+
orderBy: 'totalLiquidity',
52+
orderDirection: 'desc',
53+
});
54+
55+
// Set exectution deadline to 60 seconds from now
56+
const deadline = String(Math.ceil(Date.now() / 1000) + 60);
57+
58+
// Avoid getting rekt by setting low slippage from expected amounts out, 10 bsp = 0.1%
59+
const maxSlippage = 10;
60+
61+
// Building the route payload
62+
const payload = await swaps.buildRouteExactIn(
63+
account,
64+
account,
65+
tokenIn.address,
66+
tokenOut.address,
67+
amount,
68+
{
69+
maxSlippage,
70+
deadline,
71+
}
72+
);
73+
74+
// Extract parameters required for sendTransaction
75+
const { to, data, value } = payload;
76+
77+
// Execution with ethers.js
78+
try {
79+
const balanceBefore = await erc20Out.balanceOf(account);
80+
81+
await (
82+
await signer.sendTransaction({
83+
to,
84+
data,
85+
value,
86+
gasLimit: 8e6,
87+
})
88+
).wait();
89+
90+
// check delta
91+
const balanceAfter = await erc20Out.balanceOf(account);
92+
93+
console.log(
94+
`Amount of tokenOut received: ${formatFixed(
95+
balanceAfter.sub(balanceBefore),
96+
tokenOut.decimals
97+
)}`
98+
);
99+
} catch (err) {
100+
console.log(err);
101+
}
102+
}
103+
104+
swap();

balancer-js/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@balancer-labs/sdk",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
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",
@@ -20,7 +20,6 @@
2020
"dist/"
2121
],
2222
"scripts": {
23-
"address-book:generate": "npx ts-node -P tsconfig.testing.json -r tsconfig-paths/register ./src/lib/utils/generate-address-books.ts",
2423
"build": "rimraf dist && rollup -c",
2524
"dev": "rollup -c -w",
2625
"test": "ts-mocha --paths --recursive -p tsconfig.testing.json 'src/**/*.spec.ts' --timeout 20000",
@@ -70,7 +69,6 @@
7069
"eslint": "^7.9.0",
7170
"eslint-plugin-mocha-no-only": "^1.1.1",
7271
"eslint-plugin-prettier": "^3.1.4",
73-
"ethers": "^5.0.0",
7472
"fishery": "^2.2.2",
7573
"hardhat": "^2.9.3",
7674
"mocha": "^8.2.1",
@@ -88,7 +86,7 @@
8886
"typescript": "^4.0.2"
8987
},
9088
"dependencies": {
91-
"@balancer-labs/sor": "^4.1.1-beta.16",
89+
"@balancer-labs/sor": "^4.1.1-beta.17",
9290
"@ethersproject/abi": "^5.4.0",
9391
"@ethersproject/abstract-signer": "^5.4.0",
9492
"@ethersproject/address": "^5.4.0",
@@ -99,6 +97,7 @@
9997
"@ethersproject/contracts": "^5.4.0",
10098
"@ethersproject/providers": "^5.4.5",
10199
"axios": "^0.24.0",
100+
"ethers": "^5",
102101
"graphql": "^15.6.1",
103102
"graphql-request": "^3.5.0",
104103
"json-to-graphql-query": "^2.2.4",

0 commit comments

Comments
 (0)