Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions balancer-js/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BALANCER_SUBGRAPH_URL=https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-v2
4 changes: 3 additions & 1 deletion balancer-js/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ node_modules
yarn-error.log
.DS_Store
.env
dist/
.idea/
dist/
src/subgraph/generated/
6 changes: 2 additions & 4 deletions balancer-js/examples/queryBatchSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import {
Network,
SwapType,
BatchSwapStep,
ConfigSdk,
SUBGRAPH_URLS,
BalancerSdkConfig,
} from '../src/index';

dotenv.config();

async function runQueryBatchSwap() {
const config: ConfigSdk = {
const config: BalancerSdkConfig = {
network: Network.MAINNET,
rpcUrl: `https://kovan.infura.io/v3/${process.env.INFURA}`,
subgraphUrl: SUBGRAPH_URLS[Network.MAINNET],
};
const balancer = new BalancerSDK(config);

Expand Down
67 changes: 46 additions & 21 deletions balancer-js/examples/queryBatchSwapWithSor.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,71 @@
import dotenv from 'dotenv';
import { parseFixed, BigNumber } from '@ethersproject/bignumber';
import { BalancerSDK, Network, ConfigSdk, SUBGRAPH_URLS, SwapType } from '../src/index';
import {
BalancerSDK,
BalancerSdkConfig,
Network,
SwapType,
} from '../src/index';
import { AAVE_DAI, AAVE_USDC, AAVE_USDT, STABAL3PHANTOM } from './constants';

dotenv.config();

async function runQueryBatchSwapWithSor() {
const config: ConfigSdk = {
const config: BalancerSdkConfig = {
network: Network.KOVAN,
rpcUrl: `https://kovan.infura.io/v3/${process.env.INFURA}`,
subgraphUrl: SUBGRAPH_URLS[Network.KOVAN]
}
console.log(config.subgraphUrl);
};
const balancer = new BalancerSDK(config);

const poolsFetched = await balancer.swaps.fetchPools([], false);
if(!poolsFetched){
const poolsFetched = await balancer.swaps.fetchPools();
if (!poolsFetched) {
console.log(`Error fetching pools data.`);
return;
}

// Example showing how to join bb-a-usd pool by swapping stables > BPT
let queryResult = await balancer.swaps.queryBatchSwapWithSor({
tokensIn: [AAVE_DAI.address, AAVE_USDC.address, AAVE_USDT.address],
tokensOut: [STABAL3PHANTOM.address, STABAL3PHANTOM.address, STABAL3PHANTOM.address],
tokensOut: [
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
],
swapType: SwapType.SwapExactIn,
amounts: [parseFixed('100', 18).toString(), parseFixed('100', 6).toString(), parseFixed('100', 6).toString()],
amounts: [
parseFixed('100', 18).toString(),
parseFixed('100', 6).toString(),
parseFixed('100', 6).toString(),
],
fetchPools: {
fetchPools: false, // Because pools were previously fetched we can reuse to speed things up
fetchOnChain: false
}
fetchOnChain: false,
},
});
console.log(`\n******* stables > BPT ExactIn`);
console.log(queryResult.swaps);
console.log(queryResult.assets);
console.log(queryResult.deltas.toString());
console.log(queryResult.returnAmounts.toString());
console.log(queryResult.returnAmounts.toString());

// Example showing how to exit bb-a-usd pool by swapping BPT > stables
queryResult = await balancer.swaps.queryBatchSwapWithSor({
tokensIn: [STABAL3PHANTOM.address, STABAL3PHANTOM.address, STABAL3PHANTOM.address],
tokensIn: [
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
],
tokensOut: [AAVE_DAI.address, AAVE_USDC.address, AAVE_USDT.address],
swapType: SwapType.SwapExactIn,
amounts: [parseFixed('1', 18).toString(), parseFixed('1', 18).toString(), parseFixed('1', 18).toString()],
amounts: [
parseFixed('1', 18).toString(),
parseFixed('1', 18).toString(),
parseFixed('1', 18).toString(),
],
fetchPools: {
fetchPools: false,
fetchOnChain: false
}
fetchOnChain: false,
},
});
console.log(`\n******* BPT > stables ExactIn`);
console.log(queryResult.swaps);
Expand All @@ -55,14 +74,20 @@ async function runQueryBatchSwapWithSor() {
console.log(queryResult.returnAmounts.toString());

queryResult = await balancer.swaps.queryBatchSwapWithSor({
tokensIn: [STABAL3PHANTOM.address, STABAL3PHANTOM.address, STABAL3PHANTOM.address],
tokensIn: [
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
],
tokensOut: [AAVE_DAI.address, AAVE_USDC.address, AAVE_USDT.address],
swapType: SwapType.SwapExactOut,
amounts: queryResult.returnAmounts.map(amt => BigNumber.from(amt).abs().toString()),
amounts: queryResult.returnAmounts.map((amt) =>
BigNumber.from(amt).abs().toString()
),
fetchPools: {
fetchPools: false,
fetchOnChain: false
}
fetchOnChain: false,
},
});
console.log(`\n******* BPT > stables Exact Out`);
console.log(queryResult.swaps);
Expand All @@ -72,4 +97,4 @@ async function runQueryBatchSwapWithSor() {
}

// TS_NODE_PROJECT='tsconfig.testing.json' ts-node ./examples/queryBatchSwapWithSor.ts
runQueryBatchSwapWithSor();
runQueryBatchSwapWithSor();
10 changes: 4 additions & 6 deletions balancer-js/examples/relayerExitPoolAndBatchSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { JsonRpcProvider } from '@ethersproject/providers';
import { Contract } from '@ethersproject/contracts';
import {
BalancerSDK,
BalancerSdkConfig,
Network,
ConfigSdk,
SUBGRAPH_URLS,
StablePoolEncoder,
} from '../src/index';
import { AAVE_DAI, AAVE_USDT, STABAL3PHANTOM } from './constants';
Expand All @@ -22,10 +21,9 @@ User must approve relayer.
Vault must have approvals for tokens.
*/
async function relayerExitPoolAndBatchSwap() {
const config: ConfigSdk = {
const config: BalancerSdkConfig = {
network: Network.KOVAN,
rpcUrl: `https://kovan.infura.io/v3/${process.env.INFURA}`,
subgraphUrl: SUBGRAPH_URLS[Network.KOVAN],
};

const provider = new JsonRpcProvider(config.rpcUrl);
Expand Down Expand Up @@ -67,8 +65,8 @@ async function relayerExitPoolAndBatchSwap() {
slippage: '50000000000000000', // Slippage for swap 5%
fetchPools: {
fetchPools: true,
fetchOnChain: false
}
fetchOnChain: false,
},
});

const relayerContract = new Contract(
Expand Down
9 changes: 3 additions & 6 deletions balancer-js/examples/relayerSwapUnwrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import { Contract } from '@ethersproject/contracts';
import {
BalancerSDK,
Network,
ConfigSdk,
SUBGRAPH_URLS,
AaveHelpers,
BalancerSdkConfig,
} from '../src/index';
import { FundManagement } from '../src/swapsService/types';
import {
Expand All @@ -31,10 +30,9 @@ User must approve relayer
Vault must have approvals for tokens
*/
async function runRelayerSwapUnwrapExactIn() {
const config: ConfigSdk = {
const config: BalancerSdkConfig = {
network: Network.KOVAN,
rpcUrl: `https://kovan.infura.io/v3/${process.env.INFURA}`,
subgraphUrl: SUBGRAPH_URLS[Network.KOVAN],
};

const provider = new JsonRpcProvider(config.rpcUrl);
Expand Down Expand Up @@ -112,10 +110,9 @@ User must approve relayer
Vault must have approvals for tokens
*/
async function runRelayerSwapUnwrapExactOut() {
const config: ConfigSdk = {
const config: BalancerSdkConfig = {
network: Network.KOVAN,
rpcUrl: `https://kovan.infura.io/v3/${process.env.INFURA}`,
subgraphUrl: SUBGRAPH_URLS[Network.KOVAN],
};

const provider = new JsonRpcProvider(config.rpcUrl);
Expand Down
151 changes: 82 additions & 69 deletions balancer-js/package.json
Original file line number Diff line number Diff line change
@@ -1,71 +1,84 @@
{
"name": "@balancer-labs/sdk",
"version": "0.0.14",
"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",
"repository": {
"type": "git",
"url": "https://github.com/balancer-labs/balancer-sdk",
"directory": "balancer-js"
},
"bugs": {
"url": "https://github.com/balancer-labs/balancer-sdk/issues"
},
"main": "dist/index.js",
"module": "dist/index.esm.js",
"browser": "dist/index.umd.js",
"typings": "dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"test": "TS_NODE_PROJECT='tsconfig.testing.json' mocha --extension test.ts -r ts-node/register --recursive",
"lint": "eslint ./src --ext .ts --max-warnings 0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-typescript": "^8.2.1",
"@typechain/ethers-v5": "^7.0.1",
"@types/chai": "^4.2.12",
"@types/lodash": "^4.14.177",
"@types/mocha": "^8.0.3",
"@types/node": "^15.12.4",
"@typescript-eslint/eslint-plugin": "^4.1.1",
"@typescript-eslint/parser": "^4.1.1",
"chai": "^4.2.0",
"dotenv": "^10.0.0",
"eslint": "^7.9.0",
"eslint-plugin-mocha-no-only": "^1.1.1",
"eslint-plugin-prettier": "^3.1.4",
"mocha": "^8.2.1",
"prettier": "^2.1.2",
"rollup": "^2.52.8",
"rollup-plugin-dts": "^3.0.2",
"tiny-invariant": "^1.1.0",
"ts-node": "^10.0.0",
"typechain": "^5.1.1",
"typescript": "^4.0.2"
},
"dependencies": {
"@balancer-labs/sor": "^2.1.0-beta.4",
"@ethersproject/abi": "^5.5.0",
"@ethersproject/abstract-signer": "^5.5.0",
"@ethersproject/contracts": "^5.5.0",
"@ethersproject/providers": "^5.5.0",
"lodash": "^4.17.21"
},
"peerDependencies": {
"@ethersproject/abi": "^5.4.0",
"@ethersproject/abstract-signer": "^5.4.0",
"@ethersproject/address": "^5.4.0",
"@ethersproject/bignumber": "^5.4.0",
"@ethersproject/bytes": "^5.4.0",
"@ethersproject/constants": "^5.4.0",
"@ethersproject/contracts": "^5.4.0"
}
"name": "@balancer-labs/sdk",
"version": "0.0.14",
"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",
"repository": {
"type": "git",
"url": "https://github.com/balancer-labs/balancer-sdk",
"directory": "balancer-js"
},
"bugs": {
"url": "https://github.com/balancer-labs/balancer-sdk/issues"
},
"main": "dist/index.js",
"module": "dist/index.esm.js",
"browser": "dist/index.umd.js",
"typings": "dist/index.d.ts",
"files": [
"dist/"
],
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"test": "TS_NODE_PROJECT='tsconfig.testing.json' mocha --extension test.ts -r ts-node/register --recursive",
"lint": "eslint ./src --ext .ts --max-warnings 0",
"generate": "graphql-codegen --config src/subgraph/codegen.yml -r dotenv/config"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-typescript": "^8.2.1",
"@typechain/ethers-v5": "^7.0.1",
"@types/chai": "^4.2.12",
"@types/lodash": "^4.14.177",
"@types/mocha": "^8.0.3",
"@types/node": "^15.12.4",
"@typescript-eslint/eslint-plugin": "^4.1.1",
"@typescript-eslint/parser": "^4.1.1",
"chai": "^4.2.0",
"dotenv": "^10.0.0",
"eslint": "^7.9.0",
"eslint-plugin-mocha-no-only": "^1.1.1",
"eslint-plugin-prettier": "^3.1.4",
"mocha": "^8.2.1",
"prettier": "^2.1.2",
"rollup": "^2.52.8",
"rollup-plugin-dts": "^3.0.2",
"tiny-invariant": "^1.1.0",
"ts-node": "^10.0.0",
"typechain": "^5.1.1",
"typescript": "^4.0.2",
"@graphql-codegen/add": "^3.1.0",
"@graphql-codegen/cli": "^2.3.0",
"@graphql-codegen/introspection": "^2.1.0",
"@graphql-codegen/schema-ast": "^2.4.0",
"@graphql-codegen/typescript": "2.4.0",
"@graphql-codegen/typescript-document-nodes": "^2.2.0",
"@graphql-codegen/typescript-graphql-request": "^4.3.0",
"@graphql-codegen/typescript-operations": "^2.2.0",
"@graphql-codegen/typescript-resolvers": "2.4.1"
},
"dependencies": {
"@balancer-labs/sor": "3.0.0-beta.0",
"@ethersproject/abi": "^5.5.0",
"@ethersproject/abstract-signer": "^5.5.0",
"@ethersproject/contracts": "^5.5.0",
"@ethersproject/providers": "^5.5.0",
"lodash": "^4.17.21",
"axios": "^0.24.0",
"graphql": "^15.6.1",
"graphql-request": "^3.5.0"
},
"peerDependencies": {
"@ethersproject/abi": "^5.4.0",
"@ethersproject/abstract-signer": "^5.4.0",
"@ethersproject/address": "^5.4.0",
"@ethersproject/bignumber": "^5.4.0",
"@ethersproject/bytes": "^5.4.0",
"@ethersproject/constants": "^5.4.0",
"@ethersproject/contracts": "^5.4.0"
}
}
Loading