Observed since 20 Oct, 23:03 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
JavaScript SDK for interacting with the Balancer Protocol V2
A JavaScript SDK which provides commonly used utilties for interacting with Balancer Protocol V2.
In order to run the examples provided, you need to follow the next steps:
git clone https://github.com/balancer-labs/balancer-sdk.git
cd balancer-sdk
cd balancer-js
Create a .env file in the balancer-js folder
In the .env file you will need to define and initialize the following variables
We have defined both Alchemy and Infura, because some of the examples use Infura, others use Alchemy. However, feel free to modify accordingly and use your favourite one.
ALCHEMY_URL=[ALCHEMY HTTPS ENDPOINT]
INFURA=[Infura API KEY]
TRADER_KEY=[MetaMask PRIVATE KEY]
Some examples also require the following Tenderly config parameters to be defined:
TENDERLY_ACCESS_KEY=[TENDERLY API ACCESS KEY]
TENDERLY_PROJECT=[TENDERLY PROJECT NAME]
TENDERLY_USER=[TENDERLY USERNAME]
Run 'npm run node', this runs a local Hardhat Network
Open a new terminal
cd to balancer-js
Install ts-node using: npm install ts-node
Install tsconfig-paths using: npm install --save-dev tsconfig-paths
Generate contracts using: npm run typechain:generate
Run one of the provided examples (eg: npm run examples:run -- examples/join.ts)
import { BalancerSDK, BalancerSdkConfig, Network } from '@balancer-labs/sdk';
const config: BalancerSdkConfig = {
network: Network.MAINNET,
rpcUrl: `https://mainnet.infura.io/v3/${process.env.INFURA}`,
};
const balancer = new BalancerSDK(config);
In some examples we present a way to make end to end trades against mainnet state. To run them you will need to setup a localhost test node using tools like ganache, hardhat, anvil.
Installation instructions for:
To start a MAINNET forked node:
ALCHEMY_URL=[ALCHEMY HTTPS ENDPOINT for MAINNET]npm run nodeTo start a GOERLI forked node:
ALCHEMY_URL_GOERLI=[ALCHEMY HTTPS ENDPOINT for GOERLI]npm run node:goerliAnvil - use with caution, still experimental.
To start a forked node:
anvil -f FORKABLE_RPC_URL (optional pinned block: --fork-block-number XXX)
Exposes complete functionality for token swapping. An example of using the module with data fetched from the subgraph:
// Uses SOR to find optimal route for a trading pair and amount
const route = balancer.swaps.findRouteGivenIn({
tokenIn,
tokenOut,
amount,
gasPrice,
maxPools,
});
// Prepares transaction attributes based on the route
const transactionAttributes = balancer.swaps.buildSwap({
userAddress,
swapInfo: route,
kind: 0, // 0 - givenIn, 1 - givenOut
deadline,
maxSlippage,
});
// Extract parameters required for sendTransaction
const { to, data, value } = transactionAttributes;
// Execution with ethers.js
const transactionResponse = await signer.sendTransaction({ to, data, value });
The SwapsService provides function to query and make swaps using Balancer V2 liquidity.
const swaps = new swapService({
network: Network;
rpcUrl: string;
});
You can run each example with npm run examples:run -- examples/swaps/swap.ts
Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 0.0.1Registry latest | 23 Feb 2024 | 63 |
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 23:03 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:7beee95d16e73c9289d78468619b5dd1e957869b4f5133d81d22f31a022e8609