Skip to content

Commit

Permalink
Merge pull request #247 from balancer-labs/develop
Browse files Browse the repository at this point in the history
Release 0.1.41
  • Loading branch information
johngrantuk authored Dec 15, 2022
2 parents 788e875 + 3d7d15b commit a6e426c
Show file tree
Hide file tree
Showing 36 changed files with 980 additions and 117 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/beta-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- uses: actions/checkout@v3
with:
ref: develop
token: ${{ secrets.RELEASE_PAT }}
persist-credentials: false
- uses: actions/setup-node@v3
with:
node-version: 18
Expand All @@ -32,21 +32,22 @@ jobs:
if: steps.cache.outputs.cache-hit != 'true'
- env:
SIGNING_KEY: ${{ secrets.SIGNING_KEY }}
NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.RELEASE_PAT }}
run: |
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 AFFC8986D78B522F2999BAE053C748C476381000
git config gpg.program /usr/bin/gpg
git checkout develop
yarn version --prerelease --preid beta --no-git-tag-version
export NEW_VERSION=$(jq -r '.version' package.json)
git commit -S -am "chore: version bump v$NEW_VERSION"
git tag "v$NEW_VERSION"
git remote set-url origin "https://johngrantuk:[email protected]/balancer-labs/balancer-sdk"
git push
yarn build
yarn publish --non-interactive --tag beta
git push
env:
CI: true
NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
51 changes: 51 additions & 0 deletions balancer-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,57 @@ async getSpotPrice(

[Example](./examples/spotPrice.ts)

## Simulating pool joins and exists

The Balancer Vault provides a [method to simulate join or exit calls to a pool](https://github.com/balancer-labs/balancer-v2-monorepo/blob/master/pkg/standalone-utils/contracts/BalancerQueries.sol#L91).
These function allows you to perform a dry run before sending an actual transaction, without checking the sender / recipient or token balances / approvals. Note that this function is not 'view' (due to implementation details): the client code must explicitly execute `eth_call` instead of `eth_sendTransaction`.

### Simulating joins

There are two ways to join a pool:

1. `joinExactIn`: Joining the pool with known token amounts. This is the most commonly used method.
2. `joinExactOut`: Asking the pool for the expected liquidity when we know how much BPT we want back.

In this documentation, we will focus on the first method (`joinExactIn`) for joining a pool with known token amounts.

```js
const pool = await sdk.pools.find(poolId)
const maxAmountsIn = pool.tokenList.map((t) => forEachTokenSpecifyAmountYouWantToJoinWith)
const queryParams = pool.buildQueryJoinExactIn({ maxAmountsIn })
const response = await balancerContracts.balancerHelpers.queryJoin(...queryParams)
const { bptOut, amountsIn } = response
```

`response` will return:

* `bptOut`: The expected pool token amount returned by the pool.
* `amountsIn`: The same as maxAmountsIn

### Simulating exits

There are three ways to join a pool:

1. `exitToSingleToken`: Exiting liquidity to a single underlying token is the simplest method. However, if the amount of liquidity being exited is a significant portion of the pool's total liquidity, it may result in price slippage.
2. `exitProportionally`: Exiting liquidity proportionally to all pool tokens. This is the most commonly used method. However `ComposableStable` pool type doesn't support it.
3. `exitExactOut`: Asking the pool for the expected pool token amount when we know how much token amounts we want back.

In this example, we will focus on the first method (`exitProportionally`).

```js
const pool = await sdk.pools.find(poolId)
const queryParams = pool.buildQueryJoinExactIn({ bptIn })
const response = await balancerContracts.balancerHelpers.queryJoin(...queryParams)
const { bptIn, amountsOut } = response
```

`response` will return:

* `amountsOut`: Token amounts returned by the pool.
* `bptIn`: The same as intput bptIn

More examples: https://github.com/balancer-labs/balancer-sdk/blob/master/balancer-js/examples/pools/queries.ts

## Joining Pools

### Joining with pool tokens
Expand Down
26 changes: 26 additions & 0 deletions balancer-js/examples/data/emissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* yarn examples:run ./examples/data/emissions.ts
*/
import { BalancerSDK } from '@/.';
import { balEmissions } from '@/modules/data';

const sdk = new BalancerSDK({
network: 1,
rpcUrl: 'https://eth-rpc.gateway.pokt.network'
})

const { data } = sdk;

const now = Math.round(new Date().getTime() / 1000)
const totalBalEmissions = balEmissions.between(now, now + 365 * 86400)

const main = async () => {
if (data.liquidityGauges) {
const gauge = await data.liquidityGauges.findBy('poolId', '0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d');
if (gauge) {
console.log(`yearly emissions share: ${totalBalEmissions * gauge.relativeWeight} BAL`);
}
}
}

main()
2 changes: 1 addition & 1 deletion balancer-js/examples/data/gauge-shares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ const { gaugeShares } = sdk.data;

})();

// npm run examples:run -- ./examples/data/gauge-shares.ts
// yarn examples:run ./examples/data/gauge-shares.ts
5 changes: 3 additions & 2 deletions balancer-js/examples/data/token-yields.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Display APRs for pool ids hardcoded under `const ids`
* Display token yields
* Run command: yarn examples:run ./examples/data/token-yields.ts
*/
import { BalancerSDK } from '../../src/modules/sdk.module';
Expand All @@ -10,9 +10,10 @@ const { data } = sdk;

const tokens = [
yieldTokens[1].waDAI,
'0x3a58a54c066fdc0f2d55fc9c89f0415c92ebf3c4', // stMatic
'0xae7ab96520de3a18e5e111b5eaab095312d7fe84', // stETH
'0xac3e018457b222d93114458476f3e3416abbe38f', // sfrxETH
'0x3a58a54c066fdc0f2d55fc9c89f0415c92ebf3c4', // stMatic (polygon)
'0xfa68fb4628dff1028cfec22b4162fccd0d45efb6', // maticX (polygon)
]

const main = async () => {
Expand Down
4 changes: 2 additions & 2 deletions balancer-js/examples/pools/aprs.polygon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Display APRs for pool ids hardcoded under `const ids`
* Display APRs
* Run command: yarn examples:run ./examples/pools/aprs.polygon.ts
*/
import dotenv from 'dotenv';
Expand All @@ -19,7 +19,7 @@ const { pools } = sdk;

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

if (pool) {
Expand Down
11 changes: 2 additions & 9 deletions balancer-js/examples/pools/aprs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Run command: yarn examples:run ./examples/pools/aprs.ts
*/
import dotenv from 'dotenv';
import { BalancerSDK } from '../../src/modules/sdk.module';
import { BalancerSDK } from '@/.';

dotenv.config();

Expand All @@ -15,14 +15,7 @@ const sdk = new BalancerSDK({
const { pools } = sdk;

const main = async () => {
const list = (
await pools.where(
(pool) =>
pool.poolType != 'Element' &&
pool.poolType != 'AaveLinear' &&
pool.poolType != 'LiquidityBootstrapping'
)
)
const list = (await pools.all())
// .filter((p) => p.id === '0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d')
.sort((a, b) => parseFloat(b.totalLiquidity) - parseFloat(a.totalLiquidity))
.slice(0, 30);
Expand Down
2 changes: 1 addition & 1 deletion balancer-js/examples/pools/calculateLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TOKENS.forEach((token) => {
});

const pools = new Map<string, Pool>();
POOLS.forEach((pool) => pools.set(pool.id, pool as Pool));
POOLS.forEach((pool) => pools.set(pool.id, {...pool, poolTypeVersion: 1, protocolYieldFeeCache: '0'} as Pool));
const poolProvider = findable<Pool>(pools);
const tokenPriceProvider = new StaticTokenPriceProvider(tokenPrices);

Expand Down
20 changes: 20 additions & 0 deletions balancer-js/examples/pools/fees.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BalancerSDK } from '@/.';

const sdk = new BalancerSDK({
network: 1,
rpcUrl: 'https://eth-rpc.gateway.pokt.network',
});

(() => {
[
'0xa5533a44d06800eaf2daad5aad3f9aa9e1dc36140002000000000000000001b8',
].forEach(async (poolId) => {
const pool = await sdk.pools.find(poolId);
if (pool) {
const fees = await sdk.pools.fees(pool);
console.log(fees);
}
})
})();

// yarn examples:run ./examples/pools/fees.ts
62 changes: 62 additions & 0 deletions balancer-js/examples/pools/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Shows how to query balancer helper contracts for
* expected amounts when providing or exiting liquidity from pools
*
* yarn examples:run ./examples/pools/queries.ts
*/

import { parseEther, formatEther } from '@ethersproject/units'
import { BalancerSDK, PoolWithMethods } from '@/.'

const sdk = new BalancerSDK({
network: 1,
rpcUrl: 'https://eth-rpc.gateway.pokt.network',
})

const { pools, balancerContracts: contracts } = sdk;

// Joining with a single token
const queryJoin = async (pool: PoolWithMethods) => {
const token = pool.tokensList[0]
const joinExactInQuery = pool.buildQueryJoinExactIn({
maxAmountsIn: pool.tokensList.map((t) => parseEther((t === token) ? '1' : '0'))
})

const response = await contracts.balancerHelpers.queryJoin(...joinExactInQuery)

console.log(`Joining ${pool.poolType}`)
console.table({
tokens: pool.tokensList.map((t) => `${t.slice(0, 6)}...${t.slice(38, 42)}`),
amountsIn: response.amountsIn.map(formatEther),
bptOut: formatEther(response.bptOut),
});
}

// Exiting to single token
const queryExit = async (pool: PoolWithMethods) => {
const exitToSingleToken = pool.buildQueryExitToSingleToken({
bptIn: parseEther('1'),
tokenOut: pool.tokensList[0]
})

const response = await contracts.balancerHelpers.queryExit(...exitToSingleToken)

console.log(`Exiting ${pool.poolType}`)
console.table({
tokens: pool.tokensList.map((t) => `${t.slice(0, 6)}...${t.slice(38, 42)}`),
amountsOut: response.amountsOut.map(formatEther),
bptIn: formatEther(response.bptIn),
})
}


(async () => {
const composableStable = await pools.find('0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d')
const weighted = await pools.find('0x25accb7943fd73dda5e23ba6329085a3c24bfb6a000200000000000000000387')
const metaStable = await pools.find('0x32296969ef14eb0c6d29669c550d4a0449130230000200000000000000000080')

for (const pool of [composableStable, weighted, metaStable]) {
await queryJoin(pool!)
await queryExit(pool!)
}
})()
6 changes: 4 additions & 2 deletions balancer-js/examples/priceImpact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ async function getPriceImpact() {

const priceImpactWBTCWETH = await pool.calcPriceImpact(
['100000000000000000000', '100000000'],
'99430576622436571714692'
'99430576622436571714692',
true
);
console.log(priceImpactWBTCWETH);

Expand All @@ -43,7 +44,8 @@ async function getPriceImpact() {

const priceImpactStaBal3 = await pool2.calcPriceImpact(
['100000000000000000000', '100000000', '190000000'],
'376972471880969684010'
'376972471880969684010',
true
);
console.log(priceImpactStaBal3);
}
Expand Down
4 changes: 2 additions & 2 deletions balancer-js/examples/swapSor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function getAndProcessSwaps(
swapInfo,
pools,
wallet.address,
balancer.contracts.relayer!.address,
balancer.contracts.relayerV3!.address,
balancer.networkConfig.addresses.tokens.wrappedNativeAsset,
slippage,
undefined
Expand All @@ -71,7 +71,7 @@ async function getAndProcessSwaps(
// console.log(wallet.address);
// console.log(await balancer.sor.provider.getBlockNumber());
// console.log(relayerCallData.data);
const result = await balancer.contracts.relayer
const result = await balancer.contracts.relayerV3
?.connect(wallet)
.callStatic.multicall(relayerCallData.rawCalls);
console.log(result);
Expand Down
4 changes: 2 additions & 2 deletions balancer-js/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "@balancer-labs/sdk",
"version": "0.1.40",
"version": "0.1.41",
"description": "JavaScript SDK for interacting with the Balancer Protocol V2",
"license": "GPL-3.0-only",
"homepage": "https://github.com/balancer-labs/balancer-sdk/balancer-js#readme",
"homepage": "https://github.com/balancer-labs/balancer-sdk#readme",
"repository": {
"type": "git",
"url": "https://github.com/balancer-labs/balancer-sdk",
Expand Down
8 changes: 8 additions & 0 deletions balancer-js/src/lib/constants/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const BALANCER_NETWORK_CONFIG: Record<Network, BalancerNetworkConfig> = {
contracts: {
vault: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
multicall: '0xeefba1e63905ef1d7acba5a8513c70307c1ce441',
balancerHelpers: '0x5aDDCCa35b7A0D07C74063c48700C8590E87864E',
lidoRelayer: '0xdcdbf71A870cc60C6F9B621E28a7D3Ffd6Dd4965',
relayerV3: '0x886A3Ec7bcC508B8795990B60Fa21f85F9dB7948',
relayerV4: '0x2536dfeeCB7A0397CF98eDaDA8486254533b1aFA',
Expand Down Expand Up @@ -57,6 +58,7 @@ export const BALANCER_NETWORK_CONFIG: Record<Network, BalancerNetworkConfig> = {
multicall: '0xa1B2b503959aedD81512C37e9dce48164ec6a94d',
relayerV3: '0xcf6a66E32dCa0e26AcC3426b851FD8aCbF12Dac7',
relayerV4: '0x28A224d9d398a1eBB7BA69BCA515898966Bb1B6b',
balancerHelpers: '0x239e55F427D44C3cc793f49bFB507ebe76638a2b',
},
tokens: {
bal: '0x9a71012b13ca4d3d0cdc72a177df3ef03b0e76a3',
Expand All @@ -81,6 +83,7 @@ export const BALANCER_NETWORK_CONFIG: Record<Network, BalancerNetworkConfig> = {
multicall: '0x269ff446d9892c9e19082564df3f5e8741e190a1',
relayerV3: '0x42E49B48573c725ee32d2579060Ed06894f97002',
relayerV4: '0x5bf3B7c14b10f16939d63Bd679264A1Aa951B4D5',
balancerHelpers: '0x77d46184d22CA6a3726a2F500c776767b6A3d6Ab',
},
tokens: {
bal: '0x040d1edc9569d4bab2d15287dc5a4f10f56a56b8',
Expand All @@ -105,6 +108,7 @@ export const BALANCER_NETWORK_CONFIG: Record<Network, BalancerNetworkConfig> = {
multicall: '0x2cc8688C5f75E365aaEEb4ea8D6a480405A48D2A',
veBal: '0x16ba924752EF283C7946db8A122a6742AA35C1DC',
veBalProxy: '0x98D0d0a65cBeCCaa647a5a95cf27Cf2f00E1231C',
balancerHelpers: '0x94905e703fEAd7f0fD0eEe355D267eE909784e6d',
},
tokens: {
wrappedNativeAsset: '0xdFCeA9088c8A88A76FF74892C1457C17dfeef9C1',
Expand All @@ -124,6 +128,7 @@ export const BALANCER_NETWORK_CONFIG: Record<Network, BalancerNetworkConfig> = {
contracts: {
vault: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
multicall: '0x53c43764255c17bd724f74c4ef150724ac50a3ed',
balancerHelpers: '',
},
tokens: {
wrappedNativeAsset: '0xdFCeA9088c8A88A76FF74892C1457C17dfeef9C1',
Expand All @@ -142,6 +147,7 @@ export const BALANCER_NETWORK_CONFIG: Record<Network, BalancerNetworkConfig> = {
contracts: {
vault: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
multicall: '0x42ad527de7d4e9d9d011ac45b31d8551f8fe9821',
balancerHelpers: '0x5aDDCCa35b7A0D07C74063c48700C8590E87864E',
},
tokens: {
wrappedNativeAsset: '0xdFCeA9088c8A88A76FF74892C1457C17dfeef9C1',
Expand All @@ -166,6 +172,7 @@ export const BALANCER_NETWORK_CONFIG: Record<Network, BalancerNetworkConfig> = {
gaugeController: '0xBB1CE49b16d55A1f2c6e88102f32144C7334B116',
veBal: '0x33A99Dcc4C85C014cf12626959111D5898bbCAbF',
veBalProxy: '0xA1F107D1cD709514AE8A914eCB757E95f9cedB31',
balancerHelpers: '0x5aDDCCa35b7A0D07C74063c48700C8590E87864E',
},
tokens: {
wrappedNativeAsset: '0xdFCeA9088c8A88A76FF74892C1457C17dfeef9C1',
Expand All @@ -189,6 +196,7 @@ export const BALANCER_NETWORK_CONFIG: Record<Network, BalancerNetworkConfig> = {
multicall: '0x2dc0e2aa608532da689e89e237df582b783e552c',
relayerV3: '0x195CcCBE464EF9073d1f7A1ba1C9Bf0f56dfFFff',
relayerV4: '0x1a58897Ab366082028ced3740900ecBD765Af738',
balancerHelpers: '0x8E9aa87E45e92bad84D5F8DD1bff34Fb92637dE9',
},
tokens: {
wrappedNativeAsset: '0x4200000000000000000000000000000000000006',
Expand Down
Loading

0 comments on commit a6e426c

Please sign in to comment.