Skip to content

Commit

Permalink
Merge pull request #529 from balancer/develop
Browse files Browse the repository at this point in the history
Release 1.1.4
  • Loading branch information
johngrantuk authored Aug 25, 2023
2 parents 2c9b350 + ac1121e commit fad3b49
Show file tree
Hide file tree
Showing 52 changed files with 2,672 additions and 237 deletions.
2 changes: 2 additions & 0 deletions balancer-js/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ cache/
balancer-js.iml
temp/
src/contracts/

.vscode/*
9 changes: 9 additions & 0 deletions balancer-js/examples/helpers/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,12 @@ export const getTokenBalance = async (
const erc20 = new Contract(token, iERC20, provider);
return erc20.balanceOf(account);
};

export const getNativeAssetBalance = async (
account: string,
provider: JsonRpcProvider
): Promise<string> => {
return BigNumber.from(
await provider.send('eth_getBalance', [account, 'latest'])
).toString();
};
2 changes: 1 addition & 1 deletion balancer-js/examples/pools/aprs/aprs.polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { pools } = sdk;

const main = async () => {
const pool = await pools.find(
'0x216690738aac4aa0c4770253ca26a28f0115c595000000000000000000000b2c'
'0xf0ad209e2e969eaaa8c882aac71f02d8a047d5c2000200000000000000000b49'
);

if (pool) {
Expand Down
4 changes: 2 additions & 2 deletions balancer-js/examples/pools/aprs/aprs.zkevm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { BalancerSDK } from '@balancer-labs/sdk';

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

const { pools } = sdk;

const main = async () => {
const pool = await pools.find(
'0xe1f2c039a68a216de6dd427be6c60decf405762a00000000000000000000000e'
'0x1d0a8a31cdb04efac3153237526fb15cc65a252000000000000000000000000f'
);

if (pool) {
Expand Down
108 changes: 84 additions & 24 deletions balancer-js/examples/pools/exit/recovery-exit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,122 @@
* Run command:
* yarn example ./examples/pools/exit/recovery-exit.ts
*/
import { FORK_NODES } from '@/test/lib/utils';
import {
BalancerSDK,
insert,
removeItem,
Network,
truncateAddresses,
} from '@balancer-labs/sdk';
import { parseEther } from '@ethersproject/units';
import { getTokenBalance, reset, setTokenBalance } from 'examples/helpers';

async function recoveryExit() {
async function recoveryExitLive() {
const network = Network.MAINNET;
const rpcUrl = FORK_NODES[network];
const poolId =
'0x20b156776114e8a801e9767d90c6ccccc8adf398000000000000000000000499';
const userAddress = '0x0000000000000000000000000000000000000000';
const bptAmount = String(parseEther('1'));
const slippage = '1'; // 1 bps = 0.1%

const balancer = new BalancerSDK({
network,
rpcUrl,
});
const { poolsOnChain, pools } = balancer.data;

// Use SDK to find pool info
let pool = await pools.find(poolId);
if (!pool) throw 'POOL_DOESNT_EXIST';

// Refresh pool data from chain before building and sending tx
pool = await poolsOnChain.refresh(pool);

// Build transaction
const { expectedAmountsOut, minAmountsOut } =
balancer.pools.buildRecoveryExit({
pool,
bptAmount,
userAddress,
slippage,
});

console.log(expectedAmountsOut.toString());
console.log(minAmountsOut.toString());
}

async function recoveryExitFork() {
const poolId =
'0x20b156776114e8a801e9767d90c6ccccc8adf398000000000000000000000499';
const blockNo = 17700000;

const balancer = new BalancerSDK({
network: Network.MAINNET,
rpcUrl: 'http://127.0.0.1:8545', // Using local fork for simulation
});
const { poolsOnChain, pools } = balancer.data;

// Setup exit parameters
const signer = balancer.provider.getSigner();
const address = await signer.getAddress();

const poolId =
// '0x50cf90b954958480b8df7958a9e965752f62712400000000000000000000046f'; // bb-e-usd
// '0xd4e7c1f3da1144c9e2cfd1b015eda7652b4a439900000000000000000000046a'; // bb-e-usdc
// '0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d'; // bb-a-usd
'0xa718042e5622099e5f0ace4e7122058ab39e1bbe000200000000000000000475'; // 50temple_50bb-e-usd
const userAddress = await signer.getAddress();

const bptIn = String(parseEther('1'));
const bptAmount = String(parseEther('1'));
const slippage = '200'; // 200 bps = 2%

// Use SDK to find pool info
const pool = await balancer.pools.find(poolId);
let pool = await pools.find(poolId);
if (!pool) throw 'POOL_DOESNT_EXIST';

// Prepare local fork for simulation
await reset(balancer.provider, 17700000);
await setTokenBalance(balancer.provider, address, pool.address, bptIn, 0);
await reset(balancer.provider, blockNo);
await setTokenBalance(
balancer.provider,
userAddress,
pool.address,
bptAmount,
0
);

// Refresh pool data from chain before building and sending tx
pool = await poolsOnChain.refresh(pool);

// Build transaction
const { to, data, expectedAmountsOut, minAmountsOut } =
pool.buildRecoveryExit(address, bptIn, slippage);
balancer.pools.buildRecoveryExit({
pool,
bptAmount,
userAddress,
slippage,
});

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

// Refresh pool data from chain before building and sending tx
pool = await poolsOnChain.refresh(pool);

const bptIndex = pool.tokensList.indexOf(pool.address);
const tokensWithoutBpt =
bptIndex === -1 ? pool.tokensList : removeItem(pool.tokensList, bptIndex);
// Check balances after transaction to confirm success
const balances = await Promise.all(
pool.tokensList.map((token) =>
getTokenBalance(token, address, balancer.provider)
)
);
const balances = await Promise.all([
...tokensWithoutBpt.map((token) =>
getTokenBalance(token, userAddress, balancer.provider)
),
getTokenBalance(pool.address, userAddress, balancer.provider),
]);

console.table({
tokensOut: truncateAddresses(pool.tokensList),
minAmountsOut: insert(minAmountsOut, pool.bptIndex, bptIn),
expectedAmountsOut: insert(expectedAmountsOut, pool.bptIndex, bptIn),
amountsOut: balances.map((b) => b.toString()),
tokensOut: truncateAddresses(tokensWithoutBpt),
minAmountsOut: minAmountsOut,
expectedAmountsOut: expectedAmountsOut,
amountsOut: removeItem(balances, balances.length - 1).map((b) =>
b.toString()
),
});
console.log(`BPT Balance: `, balances[balances.length - 1].toString());
}

recoveryExit();
// recoveryExitFork();
recoveryExitLive();
34 changes: 34 additions & 0 deletions balancer-js/examples/pools/volume/volume.avalanche.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Display APRs for pool ids hardcoded under `const ids`
*
* Run command:
* yarn example ./examples/pools/volume/volume.avalanche.ts
*/
import { BalancerSDK } from '@balancer-labs/sdk';

const sdk = new BalancerSDK({
network: 43114,
rpcUrl: 'https://avalanche.public-rpc.com',
});

const { pools } = sdk;

const main = async () => {
const pool = await pools.find(
'0xa1d14d922a575232066520eda11e27760946c991000000000000000000000012'
);

if (pool) {
const volume = await pools.volume(pool);
console.table([
{
id: pool.id,
type: pool.poolType,
totalVolume: pool.totalSwapVolume,
volume,
},
]);
}
};

main();
12 changes: 12 additions & 0 deletions balancer-js/hardhat.config.avalanche.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import '@nomiclabs/hardhat-ethers';

/**
* @type import('hardhat/config').HardhatUserConfig
*/
export default {
networks: {
hardhat: {
chainId: 43114,
},
},
};
5 changes: 3 additions & 2 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.3",
"version": "1.1.4",
"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 Down Expand Up @@ -35,6 +35,7 @@
"node:arbitrum": "npx hardhat --tsconfig tsconfig.testing.json --config hardhat.config.arbitrum.ts node --fork $(. ./.env && echo $ALCHEMY_URL_ARBITRUM) --port 8161",
"node:gnosis": "npx hardhat --tsconfig tsconfig.testing.json --config hardhat.config.gnosis.ts node --fork $(. ./.env && echo $RPC_URL_GNOSIS) --port 8100",
"node:zkevm": "npx hardhat --tsconfig tsconfig.testing.json --config hardhat.config.zkevm.ts node --fork $(. ./.env && echo $ALCHEMY_URL_ZKEVM) --port 8101",
"node:avalanche": "npx hardhat --tsconfig tsconfig.testing.json --config hardhat.config.avalanche.ts node --fork $(. ./.env && echo $RPC_URL_AVALANCHE) --port 8114",
"typechain:generate": "npx typechain --target ethers-v5 --out-dir src/contracts './src/lib/abi/*.json'"
},
"devDependencies": {
Expand Down Expand Up @@ -87,7 +88,7 @@
"typescript": "^4.0.2"
},
"dependencies": {
"@balancer-labs/sor": "4.1.1-beta.13",
"@balancer-labs/sor": "^4.1.1-beta.16",
"@ethersproject/abi": "^5.4.0",
"@ethersproject/abstract-signer": "^5.4.0",
"@ethersproject/address": "^5.4.0",
Expand Down
Loading

0 comments on commit fad3b49

Please sign in to comment.