Skip to content

Commit

Permalink
Merge commit '50107999c837f6324ca3fddb4ac18f8fe9d4c923' into query-im…
Browse files Browse the repository at this point in the history
…provements
  • Loading branch information
Luiz Gustavo Abou Hatem De Liz committed Jul 7, 2023
2 parents 6a17d8f + 5010799 commit ae8cfb5
Show file tree
Hide file tree
Showing 78 changed files with 1,577 additions and 768 deletions.
20 changes: 14 additions & 6 deletions balancer-js/examples/data/pools.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Network } from '../../src/index';
import { BalancerSDK } from '../../src/modules/sdk.module';
import { BalancerSDK, Network } from '@balancer-labs/sdk';

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

const { pools, poolsOnChain } = sdk.data;

async function main() {

Expand All @@ -23,6 +23,14 @@ async function main() {

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
const pool = await pools.find(POOL_ID1);
for (const idx in pool!.tokens) {
pool!.tokens[idx].balance = '0';
}
const onchain = await poolsOnChain.refresh(pool!);
console.log('onchain pool', onchain);
}

main();
Expand Down
24 changes: 14 additions & 10 deletions balancer-js/examples/helpers/print-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,23 @@ export const printLogs = async (logs: any[]) => {
};

const printInternalBalanceChanged = (log: any) => {
const { user, token, delta } = log.args
console.log('\x1b[32m%s\x1b[0m', 'User: ', user)
console.log('\x1b[32m%s\x1b[0m', 'Token:', token)
console.log('\x1b[32m%s\x1b[0m', 'Delta:', formatEther(delta))
}
const { user, token, delta } = log.args;
console.log('\x1b[32m%s\x1b[0m', 'User: ', user);
console.log('\x1b[32m%s\x1b[0m', 'Token:', token);
console.log('\x1b[32m%s\x1b[0m', 'Delta:', formatEther(delta));
};

const printTransfer = (log: any) => {
console.log(log.address);
const { from, to, value, src, dst, wad, _to, _from, _value } = log.args
console.log('\x1b[32m%s\x1b[0m', 'From: ', from || _from || src)
console.log('\x1b[32m%s\x1b[0m', 'To: ', to || _to || dst)
console.log('\x1b[32m%s\x1b[0m', 'Value:', formatEther(value || _value || wad))
}
const { from, to, value, src, dst, wad, _to, _from, _value } = log.args;
console.log('\x1b[32m%s\x1b[0m', 'From: ', from || _from || src);
console.log('\x1b[32m%s\x1b[0m', 'To: ', to || _to || dst);
console.log(
'\x1b[32m%s\x1b[0m',
'Value:',
formatEther(value || _value || wad)
);
};

decodedLogs.map((log: any) => {
console.log('-'.repeat(80));
Expand Down
21 changes: 10 additions & 11 deletions balancer-js/examples/pools/create/create-linear-pool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Linear - Create. (Linear Pools are initialized upon creation and can be joined immediately after creation using swaps)
*
*
* Run command:
* yarn example ./examples/pools/create/create-linear-pool.ts
*/
Expand All @@ -10,21 +10,21 @@ import {
Network,
PoolType,
ProtocolId,
} from '@balancer-labs/sdk'
import { parseEther } from '@ethersproject/units'
} from '@balancer-labs/sdk';
import { parseEther } from '@ethersproject/units';

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

// Setup join parameters
const signer = balancer.provider.getSigner()
const ownerAddress = await signer.getAddress()
const dai = '0x6b175474e89094c44da98b954eedeac495271d0f'
const sape = '0x7966c5bae631294d7cffcea5430b78c2f76db6fa'
const poolTokens = [dai, sape]
const signer = balancer.provider.getSigner();
const ownerAddress = await signer.getAddress();
const dai = '0x6b175474e89094c44da98b954eedeac495271d0f';
const sape = '0x7966c5bae631294d7cffcea5430b78c2f76db6fa';
const poolTokens = [dai, sape];

const poolParameters: LinearCreatePoolParameters = {
name: 'My-Test-Pool-Name',
Expand All @@ -49,9 +49,8 @@ async function createLinearPool() {
await signer.sendTransaction({
to,
data,
gasLimit: 30000000,
})
).wait()
).wait();

// Check logs of creation receipt to get new pool ID and address
const { poolAddress, poolId } =
Expand Down
60 changes: 39 additions & 21 deletions balancer-js/examples/pools/create/create-weighted-pool.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Weighted - Create and do an initial join.
*
*
* Run command:
* yarn example ./examples/pools/create/create-weighted-pool.ts
*/
import { BalancerSDK, Network, PoolType } from '@balancer-labs/sdk'
import { reset, setTokenBalance, approveToken } from 'examples/helpers'
import { AddressZero } from '@ethersproject/constants'
import { parseFixed } from '@ethersproject/bignumber'
import { BalancerSDK, Network, PoolType } from '@balancer-labs/sdk';
import { reset, setTokenBalance, approveToken } from 'examples/helpers';
import { AddressZero } from '@ethersproject/constants';
import { parseFixed } from '@ethersproject/bignumber';

async function createAndInitJoinWeightedPool() {
const balancer = new BalancerSDK({
Expand All @@ -16,26 +16,46 @@ async function createAndInitJoinWeightedPool() {
});

// Setup join parameters
const signer = balancer.provider.getSigner()
const ownerAddress = await signer.getAddress()
const usdc = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
const usdt = '0xdac17f958d2ee523a2206206994597c13d831ec7'
const poolTokens = [usdc, usdt]
const signer = balancer.provider.getSigner();
const ownerAddress = await signer.getAddress();
const usdc = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
const usdt = '0xdac17f958d2ee523a2206206994597c13d831ec7';
const poolTokens = [usdc, usdt];
const amountsIn = [
parseFixed('1000000000', 6).toString(),
parseFixed('1000000000', 6).toString(),
];

// Prepare local fork for simulation
await reset(balancer.provider, 17347414)
await setTokenBalance(balancer.provider, ownerAddress, poolTokens[0], amountsIn[0], 9)
await setTokenBalance(balancer.provider, ownerAddress, poolTokens[1], amountsIn[1], 2)
await approveToken(poolTokens[0], balancer.contracts.vault.address, amountsIn[0], signer)
await approveToken(poolTokens[1], balancer.contracts.vault.address, amountsIn[1], signer)
await reset(balancer.provider, 17347414);
await setTokenBalance(
balancer.provider,
ownerAddress,
poolTokens[0],
amountsIn[0],
9
);
await setTokenBalance(
balancer.provider,
ownerAddress,
poolTokens[1],
amountsIn[1],
2
);
await approveToken(
poolTokens[0],
balancer.contracts.vault.address,
amountsIn[0],
signer
);
await approveToken(
poolTokens[1],
balancer.contracts.vault.address,
amountsIn[1],
signer
);

const weightedPoolFactory = balancer.pools.poolFactory.of(
PoolType.Weighted
)
const weightedPoolFactory = balancer.pools.poolFactory.of(PoolType.Weighted);

const poolParameters = {
name: 'My-Test-Pool-Name',
Expand All @@ -59,9 +79,8 @@ async function createAndInitJoinWeightedPool() {
from: ownerAddress,
to,
data,
gasLimit: 30000000,
})
).wait()
).wait();

// Check logs of creation receipt to get new pool ID and address
const { poolAddress, poolId } =
Expand All @@ -86,7 +105,6 @@ async function createAndInitJoinWeightedPool() {
await signer.sendTransaction({
to: initJoinParams.to,
data: initJoinParams.data,
gasLimit: 30000000,
});

// Check that pool balances are as expected after join
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* This example shows how to use the SDK generalisedJoin method.
*
*
* It depends on a forked mainnet node running on localhost:8545
*
*
* Use the following command to start a forked mainnet node:
* anvil --fork-url https://rpc.ankr.com/eth --fork-block-number 16411000 --fork-chain-id 1
* or
Expand All @@ -18,7 +18,7 @@
*
* The example joins the USD stable pool with DAI for decimals convinience.
* However the pool can be joined with any other token or composition of tokens.
*
*
* Expected frontend (FE) flow:
* 1. User selects tokens and amounts to join a pool
* 2. FE calls joinGeneralised with simulation type Tenderly or VaultModel
Expand All @@ -28,27 +28,28 @@
* 6. SDK calculates expectedAmountOut that is 100% accurate
* 7. SDK returns joinGeneralised transaction data with proper minAmountsOut limits in place
* 8. User is now able to submit a safe transaction to the blockchain
*
*
* Run with:
* yarn example ./examples/pools/join/join-composable-stable-with-underlying.ts
*/
import { BalancerSDK, Relayer, SimulationType } from '@balancer-labs/sdk';
import { parseEther } from '@ethersproject/units';
import {
BalancerSDK,
Relayer,
SimulationType
} from '@balancer-labs/sdk'
import { parseEther } from '@ethersproject/units'
import { approveToken, printLogs, reset, setTokenBalance } from 'examples/helpers'
approveToken,
printLogs,
reset,
setTokenBalance,
} from 'examples/helpers';

// Joining bbaUSD2 pool with DAI
const poolId =
'0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d'
const dai = '0x6B175474E89094C44Da98b954EedeAC495271d0F'
const amount = parseEther('1000').toString()
'0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d';
const dai = '0x6B175474E89094C44Da98b954EedeAC495271d0F';
const amount = parseEther('1000').toString();

const balancer = new BalancerSDK({
network: 1,
rpcUrl: 'http://localhost:8545',
rpcUrl: 'http://127.0.0.1:8545',
subgraphQuery: {
args: {
where: {
Expand All @@ -62,31 +63,31 @@ const balancer = new BalancerSDK({
},
attrs: {},
},
})
});

const { provider, contracts } = balancer
const signer = provider.getSigner()
const { provider, contracts } = balancer;
const signer = provider.getSigner();

/**
* Get some DAI to the signer and approve the vault to move it on our behalf.
* This is only needed for the example to work, in a real world scenario the signer
* would already have DAI and the vault would already be approved.
*/
async function setup(address: string) {
await reset(provider, 17000000)
await setTokenBalance(provider, address, dai, amount, 2)
await approveToken(dai, contracts.vault.address, amount, signer)
await reset(provider, 17000000);
await setTokenBalance(provider, address, dai, amount, 2);
await approveToken(dai, contracts.vault.address, amount, signer);
}

async function join() {
const address = await signer.getAddress()
const address = await signer.getAddress();

setup(address)
setup(address);

// Here we join with DAI, but we could join with any other token or combination of tokens
const tokensIn = [dai]
const amountsIn = [amount]
const slippage = '100' // 100 bps = 1%
const tokensIn = [dai];
const amountsIn = [amount];
const slippage = '100'; // 100 bps = 1%

// Use SDK to create join using either Tenderly or VaultModel simulation
// Note that this does not require authorisation to be defined
Expand All @@ -98,19 +99,18 @@ async function join() {
slippage,
signer,
SimulationType.VaultModel
)
);

// User reviews expectedAmountOut
console.log('Expected BPT out - VaultModel: ', expectedOut);


// Need to sign the approval only once per relayer
const relayerAuth = await Relayer.signRelayerApproval(
contracts.relayer.address,
address,
signer,
contracts.vault
)
);

// Use SDK to create join callData
const query = await balancer.pools.generalisedJoin(
Expand All @@ -122,7 +122,7 @@ async function join() {
signer,
SimulationType.VaultModel,
relayerAuth
)
);

// Join
const joinReciept = await (
Expand All @@ -131,9 +131,9 @@ async function join() {
data: query.encodedCall,
gasLimit: 8e6,
})
).wait()
).wait();

await printLogs(joinReciept.logs)
await printLogs(joinReciept.logs);
}

join()
join();
Loading

0 comments on commit ae8cfb5

Please sign in to comment.