Skip to content

Commit

Permalink
Merge pull request #585 from balancer/develop
Browse files Browse the repository at this point in the history
Release 1.1.6
  • Loading branch information
johngrantuk authored Aug 19, 2024
2 parents b0fe922 + 85ca8e6 commit 8f44a0e
Show file tree
Hide file tree
Showing 46 changed files with 3,866 additions and 1,533 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/beta-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
echo -n "$SIGNING_KEY" | base64 --decode | gpg --import
git config --global user.name "johngrantuk"
git config --global user.email "[email protected]"
git config user.signingkey 0B86E3F46D321811DC4330D1376AF1CD2A15D127
git config user.signingkey 5D1644BB7D087635E36B06C1ABCBFFAEE5EAEE77
git config gpg.program /usr/bin/gpg
yarn version --prerelease --preid beta --no-git-tag-version
export NEW_VERSION=$(jq -r '.version' package.json)
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Balancer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions balancer-js/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# **Deprecation Notice** #

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).

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:

* [Documentation Pages](https://docs-v3.balancer.fi/developer-reference/sdk/)
* [Examples](https://github.com/balancer/b-sdk/tree/main/examples)

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/).

Thank you for your understanding, and we hope to see you in our new repository!

# Balancer Javascript SDK

A JavaScript SDK which provides commonly used utilties for interacting with Balancer Protocol V2.
Expand Down
25 changes: 25 additions & 0 deletions balancer-js/examples/data/api-token-price-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Display APRs for pool ids hardcoded under `const ids`
* Run command: yarn example ./examples/data/token-prices.ts
*/
import { ApiTokenPriceService } from '@/modules/sor/token-price/apiTokenPriceService';

const dai = '0x6b175474e89094c44da98b954eedeac495271d0f';
const weth = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
const ohm = '0X64AA3364F17A4D01C6F1751FD97C2BD3D7E7F1D5';

(async () => {
const apiTokenPriceService = new ApiTokenPriceService(1);
const daiPriceInEth = await apiTokenPriceService.getNativeAssetPriceInToken(
dai
);
console.log('Dai Price In ETH: ' + daiPriceInEth);
const wethPriceInEth = await apiTokenPriceService.getNativeAssetPriceInToken(
weth
);
console.log('WETH Price In ETH: ' + wethPriceInEth);
const ohmPriceInEth = await apiTokenPriceService.getNativeAssetPriceInToken(
ohm
);
console.log('OHM Price In ETH: ' + ohmPriceInEth);
})();
44 changes: 44 additions & 0 deletions balancer-js/examples/pools/aprs/aprs.base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Display APRs for pool ids hardcoded under `const ids`
*
* Run command
* yarn example ./examples/pools/aprs/aprs.arbitrum.ts
*/
import { BalancerSDK, Pool } from '@balancer-labs/sdk';

const sdk = new BalancerSDK({
network: 8453,
rpcUrl: 'https://rpc.ankr.com/base',
});

const { pools } = sdk;

const main = async () => {
const id =
'0xfb4c2e6e6e27b5b4a07a36360c89ede29bb3c9b6000000000000000000000026';
const pool = (await pools.find(id)) as Pool;
const apr = await pools.apr(pool);
console.log(pool.id, apr);

// const list = (
// await pools.where(
// (pool) =>
// pool.poolType != 'Element' &&
// pool.poolType != 'AaveLinear' &&
// pool.poolType != 'LiquidityBootstrapping'
// )
// )
// .sort((a, b) => parseFloat(b.totalLiquidity) - parseFloat(a.totalLiquidity))
// .slice(0, 30)

// list.forEach(async (pool) => {
// try {
// const apr = await pools.apr(pool)
// console.log(pool.id, apr)
// } catch (e) {
// console.log(e)
// }
// });
};

main();
23 changes: 23 additions & 0 deletions balancer-js/examples/pools/bpt-price.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { BalancerSDK } from '@balancer-labs/sdk';

const sdk = new BalancerSDK({
network: 1,
rpcUrl: 'https://rpc.ankr.com/eth',
coingecko: {
coingeckoApiKey: 'CG-ViHyrfvtLz2WSCJzm59TfGow',
isDemoApiKey: true,
},
});

const bptPriceExample = async () => {
const poolId =
'0x26cc136e9b8fd65466f193a8e5710661ed9a98270002000000000000000005ad';
const pool = await sdk.pools.find(poolId);
if (!pool) {
throw new Error('Pool not found');
}
const bptPrice = await sdk.pools.bptPrice(pool);
console.log('bpt price: ', bptPrice);
};

bptPriceExample().catch((error) => console.error(error));
13 changes: 7 additions & 6 deletions balancer-js/examples/swaps/advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ async function getAndProcessSwaps(
console.log('No Swap');
return;
}
// console.log(swapInfo.swaps);
// console.log(swapInfo.tokenAddresses);
console.log(swapInfo.swaps);
console.log(swapInfo.tokenAddresses);
console.log(`Return amount: `, swapInfo.returnAmount.toString());

const pools = balancer.swaps.sor.getPools();
Expand Down Expand Up @@ -120,15 +120,16 @@ async function getAndProcessSwaps(
}

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

console.log(rpcUrl);
const balancer = new BalancerSDK({
network,
rpcUrl,
Expand Down
104 changes: 104 additions & 0 deletions balancer-js/examples/swaps/swapDebug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Helper example to facilitate swap debugging within the SDK
*
* How to run:
* yarn example examples/swaps/swapDebug.ts
*/
import { ADDRESSES } from '@/test/lib/constants';
import { FORK_NODES, RPC_URLS, forkSetup } from '@/test/lib/utils';
import { BalancerSDK, Network } from '@balancer-labs/sdk';
import { formatFixed } from '@ethersproject/bignumber';

const network = Network.MAINNET;
const rpcUrl = RPC_URLS[network];
const sdk = new BalancerSDK({
network,
rpcUrl,
});

const tokenIn = ADDRESSES[network].BAL8020BPT;
const tokenOut = ADDRESSES[network].auraBal;
const amount = String(BigInt(1000e18)); // 1000 eth

const { swaps } = sdk;
const erc20Out = sdk.contracts.ERC20(tokenOut.address, sdk.provider);

async function swap() {
const signer = sdk.provider.getSigner();
const account = await signer.getAddress();

await forkSetup(
signer,
[tokenIn.address],
[tokenIn.slot],
[amount],
FORK_NODES[network]
);

// Finding a trading route rely on on-chain data.
// fetchPools will fetch the current data from the subgraph.
// Let's fetch just 5 pools with highest liquidity of tokenOut.
await swaps.fetchPools({
first: 5,
where: {
swapEnabled: {
eq: true,
},
tokensList: {
contains: [tokenOut.address],
},
},
orderBy: 'totalLiquidity',
orderDirection: 'desc',
});

// Set exectution deadline to 60 seconds from now
const deadline = String(Math.ceil(Date.now() / 1000) + 60);

// Avoid getting rekt by setting low slippage from expected amounts out, 10 bsp = 0.1%
const maxSlippage = 10;

// Building the route payload
const payload = await swaps.buildRouteExactIn(
account,
account,
tokenIn.address,
tokenOut.address,
amount,
{
maxSlippage,
deadline,
}
);

// Extract parameters required for sendTransaction
const { to, data, value } = payload;

// Execution with ethers.js
try {
const balanceBefore = await erc20Out.balanceOf(account);

await (
await signer.sendTransaction({
to,
data,
value,
gasLimit: 8e6,
})
).wait();

// check delta
const balanceAfter = await erc20Out.balanceOf(account);

console.log(
`Amount of tokenOut received: ${formatFixed(
balanceAfter.sub(balanceBefore),
tokenOut.decimals
)}`
);
} catch (err) {
console.log(err);
}
}

swap();
7 changes: 3 additions & 4 deletions balancer-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@balancer-labs/sdk",
"version": "1.1.5",
"version": "1.1.6",
"description": "JavaScript SDK for interacting with the Balancer Protocol V2",
"license": "GPL-3.0-only",
"homepage": "https://github.com/balancer-labs/balancer-sdk#readme",
Expand All @@ -20,7 +20,6 @@
"dist/"
],
"scripts": {
"address-book:generate": "npx ts-node -P tsconfig.testing.json -r tsconfig-paths/register ./src/lib/utils/generate-address-books.ts",
"build": "rimraf dist && rollup -c",
"dev": "rollup -c -w",
"test": "ts-mocha --paths --recursive -p tsconfig.testing.json 'src/**/*.spec.ts' --timeout 20000",
Expand Down Expand Up @@ -70,7 +69,6 @@
"eslint": "^7.9.0",
"eslint-plugin-mocha-no-only": "^1.1.1",
"eslint-plugin-prettier": "^3.1.4",
"ethers": "^5.0.0",
"fishery": "^2.2.2",
"hardhat": "^2.9.3",
"mocha": "^8.2.1",
Expand All @@ -88,7 +86,7 @@
"typescript": "^4.0.2"
},
"dependencies": {
"@balancer-labs/sor": "^4.1.1-beta.16",
"@balancer-labs/sor": "^4.1.1-beta.17",
"@ethersproject/abi": "^5.4.0",
"@ethersproject/abstract-signer": "^5.4.0",
"@ethersproject/address": "^5.4.0",
Expand All @@ -99,6 +97,7 @@
"@ethersproject/contracts": "^5.4.0",
"@ethersproject/providers": "^5.4.5",
"axios": "^0.24.0",
"ethers": "^5",
"graphql": "^15.6.1",
"graphql-request": "^3.5.0",
"json-to-graphql-query": "^2.2.4",
Expand Down
Loading

0 comments on commit 8f44a0e

Please sign in to comment.