Skip to content

Commit

Permalink
Merge pull request #501 from balancer/develop
Browse files Browse the repository at this point in the history
Release 1.1.3
  • Loading branch information
johngrantuk authored Jul 28, 2023
2 parents e6f3296 + e727a97 commit 2c9b350
Show file tree
Hide file tree
Showing 112 changed files with 1,936 additions and 677 deletions.
1 change: 0 additions & 1 deletion balancer-js/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules
dist
examples
**/generated
10 changes: 5 additions & 5 deletions balancer-js/examples/contracts/veBAL-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/**
* This example shows how to the adjusted veBAL balance from the active boost delegation contract
*
*
* How to run:
* yarn run example examples/contracts/veBAL-proxy.ts
*/
import { BalancerSDK, Network } from '@balancer-labs/sdk';

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

const { veBalProxy } = sdk.contracts;

async function main() {
const USER = "0x91F450602455564A64207414c7Fbd1F1F0EbB425";
const USER = '0x91F450602455564A64207414c7Fbd1F1F0EbB425';
const balance = await veBalProxy?.getAdjustedBalance(USER);
console.log("User's veBAL adjusted balance", balance);
}
Expand Down
8 changes: 4 additions & 4 deletions balancer-js/examples/contracts/veBAL.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/**
* Shows how to interact with the veBAL contract
*
*
* How to run:
* yarn run example examples/contracts/veBAL.ts
*/
import { BalancerSDK, Network } from '@balancer-labs/sdk';

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

const { veBal } = sdk.contracts;

async function main() {
if (!veBal) throw new Error('veBal address must be defined');

const USER = "0x91F450602455564A64207414c7Fbd1F1F0EbB425";
const USER = '0x91F450602455564A64207414c7Fbd1F1F0EbB425';

const lockInfo = await veBal.getLockInfo(USER);
console.log("veBAL lock info for user", lockInfo);
console.log('veBAL lock info for user', lockInfo);
}

main();
20 changes: 12 additions & 8 deletions balancer-js/examples/data/fee-distributor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* This example shows how to use the FeeDistributor contract to claim rewards
*
*
* How to run:
* yarn example examples/data/fee-distributor.ts
*/
import { BalancerSDK, Network } from "@balancer-labs/sdk";
import { BalancerSDK, Network } from '@balancer-labs/sdk';

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

const { feeDistributor } = sdk.data;
Expand All @@ -22,15 +22,19 @@ const claimableTokens: string[] = [
const userAddress = '0x549c660ce2B988F588769d6AD87BE801695b2be3';

(async function () {
if (!feeDistributor) throw new Error("feeDistributor not defined");
const data = await feeDistributor.getClaimableBalances(userAddress, claimableTokens);
if (!feeDistributor) throw new Error('feeDistributor not defined');
const data = await feeDistributor.getClaimableBalances(
userAddress,
claimableTokens
);
console.table(data);

const callData = feeDistributor.claimBalances(userAddress, claimableTokens);
console.log(`Encoded Callable: ${callData.slice(0, 10)}...${callData.slice(-5)}`);
console.log(
`Encoded Callable: ${callData.slice(0, 10)}...${callData.slice(-5)}`
);
console.log(`
const tx = { to: '${sdk.networkConfig.addresses.contracts.feeDistributor}', data: callData };
const receipt = await (await signer.sendTransaction(tx)).wait();
`)

`);
})();
16 changes: 9 additions & 7 deletions balancer-js/examples/data/gauge-shares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import { BalancerSDK, Network } from '@balancer-labs/sdk';

const sdk = new BalancerSDK({
network: Network.MAINNET,
rpcUrl: ''
rpcUrl: '',
});

const { gaugeShares } = sdk.data;

(async function() {
(async function () {
if (!gaugeShares) throw 'Gauge Subgraph must be initialized';

const USER_ADDR = '0x00676e437f1945b85ec3a3c90aae35e0352115ed';
const GAUGE_ID = '0xc5f8b1de80145e3a74524a3d1a772a31ed2b50cc';
const GAUGESHARE_ID = `${USER_ADDR}-${GAUGE_ID}`;
const GAUGESHARE_ID2 = "0x79c17982020abb9a2214aa952308e104e5840e2d-0xc5f8b1de80145e3a74524a3d1a772a31ed2b50cc";
const GAUGESHARE_ID2 =
'0x79c17982020abb9a2214aa952308e104e5840e2d-0xc5f8b1de80145e3a74524a3d1a772a31ed2b50cc';

let result;

Expand All @@ -31,12 +32,13 @@ const { gaugeShares } = sdk.data;

result = await gaugeShares.findByGauge(GAUGE_ID, 5);
console.log('Gauge shares by gauge (first 5)', result);

result = await gaugeShares.findByGauge(GAUGE_ID, 2, 1);
console.log('Gauge shares by gauge (#2 & #3)', result);

result = await gaugeShares.query({ where: { id_in: [ GAUGESHARE_ID, GAUGESHARE_ID2 ] }});
console.log('Gauge shares subgraph query', result);
result = await gaugeShares.query({
where: { id_in: [GAUGESHARE_ID, GAUGESHARE_ID2] },
});
console.log('Gauge shares subgraph query', result);
// Gauges subgraph : https://thegraph.com/hosted-service/subgraph/balancer-labs/balancer-gauges

})();
12 changes: 8 additions & 4 deletions balancer-js/examples/data/liquidity-gauges.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Example retrieves all the gauges for the pools and shows one example with BAL and one example with BAL and another reward token (LIDO)
*
*
* Run with:
* yarn example ./examples/data/liquidity-gauges.ts
*/
import { BalancerSDK, Network } from "@balancer-labs/sdk";
import { BalancerSDK, Network } from '@balancer-labs/sdk';

const sdk = new BalancerSDK({
network: Network.ARBITRUM,
Expand All @@ -16,6 +16,10 @@ const { liquidityGauges } = sdk.data;
if (!liquidityGauges) throw 'Gauge Subgraph must be initialized';
const gauges = await liquidityGauges.fetch();
console.log(`Gauges: `, gauges.length);
console.log(gauges.find((it) => it.id === '0x914ec5f93ccd6362ba925bedd0bd68107b85d2ca'));
console.log(gauges.find((it) => it.id === '0xcf9f895296f5e1d66a7d4dcf1d92e1b435e9f999'));
console.log(
gauges.find((it) => it.id === '0x914ec5f93ccd6362ba925bedd0bd68107b85d2ca')
);
console.log(
gauges.find((it) => it.id === '0xcf9f895296f5e1d66a7d4dcf1d92e1b435e9f999')
);
})();
30 changes: 16 additions & 14 deletions balancer-js/examples/data/pool-shares.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
/**
* Example of using the poolShares data source
*
*
* Run with:
* yarn example ./examples/data/pool-shares.ts
*/
import { BalancerSDK, Network } from '@balancer-labs/sdk';

const sdk = new BalancerSDK(
{
network: Network.MAINNET,
rpcUrl: ''
});
const sdk = new BalancerSDK({
network: Network.MAINNET,
rpcUrl: '',
});
const { poolShares } = sdk.data;

(async function() {

const POOLSHARE_ID = '0x01abc00e86c7e258823b9a055fd62ca6cf61a163-0x2da1bcb14be26be6812e0e871e8dc4f4c0d92629';
const POOL_ID = '0x01abc00e86c7e258823b9a055fd62ca6cf61a16300010000000000000000003b'
(async function () {
const POOLSHARE_ID =
'0x01abc00e86c7e258823b9a055fd62ca6cf61a163-0x2da1bcb14be26be6812e0e871e8dc4f4c0d92629';
const POOL_ID =
'0x01abc00e86c7e258823b9a055fd62ca6cf61a16300010000000000000000003b';
const USER_ADDR = '0xba12222222228d8ba445958a75a0704d566bf2c8';

let result;

result = await poolShares.find(POOLSHARE_ID);
Expand All @@ -35,9 +35,11 @@ const { poolShares } = sdk.data;

result = await poolShares.findByPool(POOL_ID, 2, 1);
console.log('Pool shares by pool (#2 & #3)', result);

result = await poolShares.query({ where: { poolId: POOL_ID, balance_gt: '0' }, first: 3 });

result = await poolShares.query({
where: { poolId: POOL_ID, balance_gt: '0' },
first: 3,
});
console.log('Pool shares subgraph query', result);
// Balancer subgraph : https://thegraph.com/hosted-service/subgraph/balancer-labs/balancer-v2

})();
30 changes: 17 additions & 13 deletions balancer-js/examples/data/pools.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { BalancerSDK, Network } from '@balancer-labs/sdk';

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

const { pools, poolsOnChain } = sdk.data;

async function main() {

const POOL_ID1 = '0x2d011adf89f0576c9b722c28269fcb5d50c2d17900020000000000000000024d';
const POOL_ID2 = '0x4aa462d59361fc0115b3ab7e447627534a8642ae000100000000000000000158';
const POOL_IDs = [ POOL_ID1, POOL_ID2 ];
const POOL_ID1 =
'0x2d011adf89f0576c9b722c28269fcb5d50c2d17900020000000000000000024d';
const POOL_ID2 =
'0x4aa462d59361fc0115b3ab7e447627534a8642ae000100000000000000000158';
const POOL_IDs = [POOL_ID1, POOL_ID2];

let result;

Expand All @@ -21,18 +22,21 @@ async function main() {
result = await pools.all();
console.log('Fetch all pools', result);

result = await pools.where(pool => POOL_IDs.includes(pool.id));
result = await pools.where((pool) => POOL_IDs.includes(pool.id));
console.log('Filter pools by attributes', result);

// Fefetch on-chain balances for a given pool
// Refetch on-chain balances for a given pool
const pool = await pools.find(POOL_ID1);
for (const idx in pool!.tokens) {
pool!.tokens[idx].balance = '0';
if (!pool) {
throw new Error('Pool not found');
}
for (const idx in pool.tokens) {
pool.tokens[idx].balance = '0';
}
const onchain = await poolsOnChain.refresh(pool!);
const onchain = await poolsOnChain.refresh(pool);
console.log('onchain pool', onchain);
}

main();

// yarn example ./examples/data/pools.ts
// yarn example ./examples/data/pools.ts
13 changes: 12 additions & 1 deletion balancer-js/examples/data/token-prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ const tetuBal = '0x7fc9e0aa043787bfad28e29632ada302c790ce33';

(async () => {
// It will be just one request to coingecko
const ps = [eth, weth, dai, ohm, tetuBal, matic, eth, dai, tetuBal, matic].map((t) => data.tokenPrices.find(t));
const ps = [
eth,
weth,
dai,
ohm,
tetuBal,
matic,
eth,
dai,
tetuBal,
matic,
].map((t) => data.tokenPrices.find(t));
const price = await Promise.all(ps);

console.log(price);
Expand Down
44 changes: 22 additions & 22 deletions balancer-js/examples/helpers/erc20.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { hexlify, zeroPad } from '@ethersproject/bytes'
import { keccak256 } from '@ethersproject/solidity'
import { BigNumber } from '@ethersproject/bignumber'
import { Contract } from '@ethersproject/contracts'
import { JsonRpcProvider, JsonRpcSigner } from '@ethersproject/providers'
import { hexlify, zeroPad } from '@ethersproject/bytes';
import { keccak256 } from '@ethersproject/solidity';
import { BigNumber } from '@ethersproject/bignumber';
import { Contract } from '@ethersproject/contracts';
import { JsonRpcProvider, JsonRpcSigner } from '@ethersproject/providers';

/**
* Set token balance for a given account
Expand All @@ -23,14 +23,16 @@ export const setTokenBalance = async (
isVyperMapping = false
): Promise<void> => {
// Get storage slot index
const slotFormat = isVyperMapping ? [slot, account] : [account, slot]
const slotValue = keccak256(['uint256', 'uint256'], slotFormat)
const slotFormat = isVyperMapping ? [slot, account] : [account, slot];
const slotValue = keccak256(['uint256', 'uint256'], slotFormat);

// Manipulate local balance (needs to be bytes32 string)
const value = hexlify(zeroPad(BigNumber.from(String(BigInt(balance))).toHexString(), 32))
const value = hexlify(
zeroPad(BigNumber.from(String(BigInt(balance))).toHexString(), 32)
);

await provider.send('hardhat_setStorageAt', [token, slotValue, value])
}
await provider.send('hardhat_setStorageAt', [token, slotValue, value]);
};

/**
* Approve token balance for vault contract
Expand All @@ -48,17 +50,15 @@ export const approveToken = async (
): Promise<boolean> => {
const iERC20 = [
'function approve(address spender, uint256 amount) external returns (bool)',
]
const erc20 = new Contract(token, iERC20, signer)
const txReceipt = await (
await erc20.approve(spender, amount)
).wait()
return txReceipt.status === 1
}
];
const erc20 = new Contract(token, iERC20, signer);
const txReceipt = await (await erc20.approve(spender, amount)).wait();
return txReceipt.status === 1;
};

/**
* Get ERC20 token balance for a given account
*
*
* @param token Token address to get balance of
* @param account Account to get balance for
* @param provider JsonRpcProvider
Expand All @@ -71,7 +71,7 @@ export const getTokenBalance = async (
): Promise<string> => {
const iERC20 = [
'function balanceOf(address account) external view returns (uint256)',
]
const erc20 = new Contract(token, iERC20, provider)
return erc20.balanceOf(account)
}
];
const erc20 = new Contract(token, iERC20, provider);
return erc20.balanceOf(account);
};
14 changes: 9 additions & 5 deletions balancer-js/examples/helpers/forked-utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { JsonRpcProvider } from '@ethersproject/providers'
import { JsonRpcProvider } from '@ethersproject/providers';

/**
* Resets the fork to a given block number
*
* @param provider JsonRpcProvider
* @param blockNumber Block number to reset fork to
*/
export const reset = (provider: JsonRpcProvider, blockNumber: number, jsonRpcUrl = 'https://rpc.ankr.com/eth'): Promise<void> =>
export const reset = (
provider: JsonRpcProvider,
blockNumber?: number,
jsonRpcUrl = 'https://rpc.ankr.com/eth'
): Promise<void> =>
provider.send('hardhat_reset', [
{
forking: {
jsonRpcUrl,
blockNumber
}
}
blockNumber,
},
},
]);
Loading

0 comments on commit 2c9b350

Please sign in to comment.