Skip to content

Commit

Permalink
Merge pull request #411 from balancer/develop
Browse files Browse the repository at this point in the history
Release 1.0.4
  • Loading branch information
johngrantuk authored Apr 13, 2023
2 parents b871bb8 + a1993bf commit 571eef2
Show file tree
Hide file tree
Showing 23 changed files with 1,509 additions and 35 deletions.
25 changes: 17 additions & 8 deletions balancer-js/examples/helpers/print-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const decodeLog = async (log: any, abi: any) => {
};

export const decodeLogs = async (logs: any[]) => {
const decodedLogs = [];
let abi;
const decodedLogs: any[] = [];
let abi: any;

for (const log of logs) {
abi = abis.get(log.address);
Expand Down Expand Up @@ -69,21 +69,30 @@ 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 printTransfer = (log: any) => {
console.log(log.address);
const { from, to, value, src, dst, wad } = log.args;
console.log('\x1b[32m%s\x1b[0m', 'From: ', from || src);
console.log('\x1b[32m%s\x1b[0m', 'To: ', to || dst);
console.log('\x1b[32m%s\x1b[0m', 'Value:', formatEther(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) => {
decodedLogs.map((log: any) => {
console.log('-'.repeat(80));
console.log(log.name);
if (log.name === 'Swap') {
printSwap(log);
} else if (log.name === 'PoolBalanceChanged') {
printPoolBalanceChanged(log);
} else if (log.name === 'InternalBalanceChanged') {
printInternalBalanceChanged(log);
} else if (log.name === 'Transfer') {
printTransfer(log);
}
Expand Down
40 changes: 40 additions & 0 deletions balancer-js/examples/liquidity-managment/migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Migrations module contains methods to migrate liquidity between pools
* Run command: yarn examples:run ./examples/liquidity-managment/migrations.ts
*/
import { BalancerSDK } from '@/.'

const sdk = new BalancerSDK({
network: 1,
rpcUrl: 'http://127.0.0.1:8545', // Using a forked mainnet to be able to approve the relayer
})

const { provider, migrationService } = sdk

if (!migrationService) {
throw new Error('No migrationService present')
}

const main = async () => {
const user = '0x783596B9504Ef2752EFB2d4Aed248fDCb0d9FDab'
const from = '0x32296969ef14eb0c6d29669c550d4a0449130230000200000000000000000080'
const to = '0x32296969ef14eb0c6d29669c550d4a0449130230000200000000000000000080'
const balance = await sdk.contracts.ERC20('0x32296969ef14eb0c6d29669c550d4a0449130230', provider).balanceOf(user)

// To be able to perform a migration and a static call, user needs to approve the relayer first
await provider.send('hardhat_impersonateAccount', [user])
const signer = provider.getSigner(user)
await sdk.contracts.vault.connect(signer).setRelayerApproval(user, migrationService.relayerAddress, true)

// Query for the minimum amount of BPT to receive
const peek = await migrationService.pool2pool(user, from, to, balance)
const peekResult = await provider.call({ ...peek, from: user, gasLimit: 8e6 });
const expectedBptOut = migrationService.getMinBptOut(peekResult);
console.log('expectedBptOut', expectedBptOut.toString(), 'BPT')

// Build the migration with the minimum amount of BPT to receive
const txParams = await migrationService.pool2pool(user, from, to, balance, expectedBptOut)
console.log(txParams.data)
}

main()
2 changes: 1 addition & 1 deletion balancer-js/examples/pools/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function setUpExample(
rpcUrlLocal: string,
network: Network,
tokens: string[],
slots: number[],
slots: number[] | undefined,
balances: string[],
poolId: string,
blockNo: number
Expand Down
2 changes: 1 addition & 1 deletion balancer-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@balancer-labs/sdk",
"version": "1.0.3",
"version": "1.0.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
1 change: 1 addition & 0 deletions balancer-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ export {
} from '@balancer-labs/sor';
export { SimulationType } from './modules/simulation/simulation.module';
export { BALANCER_NETWORK_CONFIG } from './lib/constants/config';
export { Migrations } from './modules/liquidity-managment/migrations';
5 changes: 4 additions & 1 deletion balancer-js/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const isSameAddress = (address1: string, address2: string): boolean =>
getAddress(address1) === getAddress(address2);

export function insert<T>(arr: T[], index: number, newItem: T): T[] {
if (index < 0 || index >= arr.length) {
if (index < 0 || index > arr.length) {
return arr;
}
return [
Expand All @@ -38,6 +38,9 @@ export function insert<T>(arr: T[], index: number, newItem: T): T[] {
* @param newItem
*/
export function replace<T>(arr: T[], index: number, newItem: T): T[] {
if (index < 0 || index >= arr.length) {
return arr;
}
return [
// part of the array before the specified index
...arr.slice(0, index),
Expand Down
Loading

0 comments on commit 571eef2

Please sign in to comment.