Skip to content

Commit

Permalink
Merge pull request #85 from balancer-labs/develop
Browse files Browse the repository at this point in the history
Version 0.1.12
  • Loading branch information
johngrantuk committed Jun 2, 2022
2 parents ba4e492 + ba46d58 commit 6cd88df
Show file tree
Hide file tree
Showing 96 changed files with 7,617 additions and 4,383 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/balancer-js.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ jobs:
run: yarn subgraph:generate
- name: Compile
run: yarn build
- name: Run node in background for integration tests
run: npx hardhat node --hostname 127.0.0.1 &
- name: Test
run: yarn test

env:
CI: true
INFURA: ${{ secrets.INFURA }}
ALCHEMY_URL: ${{ secrets.ALCHEMY_URL }}
18 changes: 9 additions & 9 deletions balancer-js/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'prettier', 'mocha-no-only'],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
rules: {
'comma-spacing': ['error', { before: false, after: true }],
'prettier/prettier': 'error',
'mocha-no-only/mocha-no-only': ['error'],
},
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'prettier', 'mocha-no-only'],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
rules: {
'comma-spacing': ['error', { before: false, after: true }],
'prettier/prettier': 'error',
'mocha-no-only/mocha-no-only': ['error'],
},
};
3 changes: 2 additions & 1 deletion balancer-js/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ yarn-error.log
.env
.idea/
dist/
src/subgraph/generated/
src/subgraph/generated/
cache/
18 changes: 9 additions & 9 deletions balancer-js/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"printWidth": 80,
"tabWidth": 4,
"singleQuote": true,
"bracketSpacing": true,
"trailingComma": "es5",
"semi": true,
"newline-before-return": true,
"no-duplicate-variable": [true, "check-parameters"],
"no-var-keyword": true
"printWidth": 80,
"tabWidth": 2,
"singleQuote": true,
"bracketSpacing": true,
"trailingComma": "es5",
"semi": true,
"newline-before-return": true,
"no-duplicate-variable": [true, "check-parameters"],
"no-var-keyword": true
}
49 changes: 49 additions & 0 deletions balancer-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,55 @@ const config: BalancerSdkConfig = {
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:

* [Hardhat](https://hardhat.org/getting-started/#installation)

To start a forked node:
```
npm run node
```

* [Anvil](https://github.com/foundry-rs/foundry/tree/master/anvil#installation) - use with caution, still experimental.

To start a forked node:
```
anvil -f FORKABLE_RPC_URL (optional pinned block: --fork-block-number XXX)
```

## Swaps Module

Exposes complete functionality for token swapping. An example of using the module with data fetched from the subgraph:

```js
// 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 })
```


## SwapsService

The SwapsService provides function to query and make swaps using Balancer V2 liquidity.
Expand Down
108 changes: 55 additions & 53 deletions balancer-js/examples/batchSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,65 @@ const SENDER_WALLET_ADDRESS = '0x35f5a330FD2F8e521ebd259FA272bA8069590741';
Example showing how to encode and send a batch swap transaction.
*/
async function runBatchSwap() {
console.log('PRIVATE_KEY', TRADER_KEY);
console.log('PRIVATE_KEY', TRADER_KEY);

const encodedBatchSwapData = Swaps.encodeBatchSwap({
kind: SwapType.SwapExactIn,
swaps: [
// First pool swap: 10000 USDC => ? DAI
{
poolId: '0x0cdab06b07197d96369fea6f3bea6efc7ecdf7090002000000000000000003de',
// USDC
assetInIndex: 0,
// DAI
assetOutIndex: 1,
amount: '10000',
userData: '0x',
},
// Second pool swap: 10000 DAI => ? USDC
{
poolId: '0x17018c2f7c345add873474879ff0ed98ebd6346a000200000000000000000642',
// DAI
assetInIndex: 1,
// USDC
assetOutIndex: 0,
amount: '10000',
userData: '0x',
},
],
assets: [
// USDC
'0xc2569dd7d0fd715b054fbf16e75b001e5c0c1115',
// DAI
'0x04df6e4121c27713ed22341e7c7df330f56f289b',
],
funds: {
fromInternalBalance: false,
// These can be different addresses!
recipient: RECIPIENT_WALLET_ADDRESS,
sender: SENDER_WALLET_ADDRESS,
toInternalBalance: false,
},
limits: ['0', '0'], // No limits
deadline: '999999999999999999', // Infinity
});
const encodedBatchSwapData = Swaps.encodeBatchSwap({
kind: SwapType.SwapExactIn,
swaps: [
// First pool swap: 10000 USDC => ? DAI
{
poolId:
'0x0cdab06b07197d96369fea6f3bea6efc7ecdf7090002000000000000000003de',
// USDC
assetInIndex: 0,
// DAI
assetOutIndex: 1,
amount: '10000',
userData: '0x',
},
// Second pool swap: 10000 DAI => ? USDC
{
poolId:
'0x17018c2f7c345add873474879ff0ed98ebd6346a000200000000000000000642',
// DAI
assetInIndex: 1,
// USDC
assetOutIndex: 0,
amount: '10000',
userData: '0x',
},
],
assets: [
// USDC
'0xc2569dd7d0fd715b054fbf16e75b001e5c0c1115',
// DAI
'0x04df6e4121c27713ed22341e7c7df330f56f289b',
],
funds: {
fromInternalBalance: false,
// These can be different addresses!
recipient: RECIPIENT_WALLET_ADDRESS,
sender: SENDER_WALLET_ADDRESS,
toInternalBalance: false,
},
limits: ['0', '0'], // No limits
deadline: '999999999999999999', // Infinity
});

const provider = new InfuraProvider(Network.KOVAN, process.env.INFURA);
const wallet = new Wallet(TRADER_KEY as string, provider);
const provider = new InfuraProvider(Network.KOVAN, process.env.INFURA);
const wallet = new Wallet(TRADER_KEY as string, provider);

const tx = await wallet.sendTransaction({
data: encodedBatchSwapData,
to: balancerVault,
/**
* The following gas inputs are optional,
**/
// gasPrice: '6000000000',
// gasLimit: '2000000',
});
const tx = await wallet.sendTransaction({
data: encodedBatchSwapData,
to: balancerVault,
/**
* The following gas inputs are optional,
**/
// gasPrice: '6000000000',
// gasLimit: '2000000',
});

console.log(tx);
console.log(tx);
}

// yarn examples:run ./examples/batchSwap.ts
Expand Down
44 changes: 22 additions & 22 deletions balancer-js/examples/constants.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
interface TestToken {
address: string;
decimals: number;
address: string;
decimals: number;
}

// Kovan version
export const bbausd: TestToken = {
address: '0x8fd162f338b770f7e879030830cde9173367f301',
decimals: 18,
address: '0x8fd162f338b770f7e879030830cde9173367f301',
decimals: 18,
};

// Kovan version
export const AAVE_USDT: TestToken = {
address: '0x13512979ade267ab5100878e2e0f485b568328a4',
decimals: 6,
address: '0x13512979ade267ab5100878e2e0f485b568328a4',
decimals: 6,
};

// Kovan version
export const AAVE_USDC: TestToken = {
address: '0xe22da380ee6b445bb8273c81944adeb6e8450422',
decimals: 6,
address: '0xe22da380ee6b445bb8273c81944adeb6e8450422',
decimals: 6,
};

// Kovan version
export const AAVE_DAI: TestToken = {
address: '0xff795577d9ac8bd7d90ee22b6c1703490b6512fd',
decimals: 18,
address: '0xff795577d9ac8bd7d90ee22b6c1703490b6512fd',
decimals: 18,
};

// Kovan version
export const WRAPPED_AAVE_USDT: TestToken = {
address: '0xe8191aacfcdb32260cda25830dc6c9342142f310',
decimals: 6,
address: '0xe8191aacfcdb32260cda25830dc6c9342142f310',
decimals: 6,
};

// Kovan version
export const WRAPPED_AAVE_USDC: TestToken = {
address: '0x0fbddc06a4720408a2f5eb78e62bc31ac6e2a3c4',
decimals: 6,
address: '0x0fbddc06a4720408a2f5eb78e62bc31ac6e2a3c4',
decimals: 6,
};

// Kovan version
export const WRAPPED_AAVE_DAI: TestToken = {
address: '0x4811a7bb9061a46753486e5e84b3cd3d668fb596',
decimals: 18,
address: '0x4811a7bb9061a46753486e5e84b3cd3d668fb596',
decimals: 18,
};

// Kovan version
export const USDC: TestToken = {
address: '0xc2569dd7d0fd715b054fbf16e75b001e5c0c1115',
decimals: 6,
address: '0xc2569dd7d0fd715b054fbf16e75b001e5c0c1115',
decimals: 6,
};

// Kovan version
export const USDT: TestToken = {
address: '0xcc08220af469192c53295fdd34cfb8df29aa17ab',
decimals: 6,
address: '0xcc08220af469192c53295fdd34cfb8df29aa17ab',
decimals: 6,
};

// Kovan version
export const DAI: TestToken = {
address: '0x04df6e4121c27713ed22341e7c7df330f56f289b',
decimals: 6,
address: '0x04df6e4121c27713ed22341e7c7df330f56f289b',
decimals: 6,
};
Loading

0 comments on commit 6cd88df

Please sign in to comment.