Skip to content

Commit

Permalink
Merge pull request #144 from balancer-labs/develop
Browse files Browse the repository at this point in the history
Merge new SOR to master.
  • Loading branch information
TomAFrench authored Sep 16, 2021
2 parents 2fabc24 + 9602fa4 commit 7b7ff63
Show file tree
Hide file tree
Showing 270 changed files with 5,388,597 additions and 22,090 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/node:11
- image: cimg/node:14.16.1
working_directory: ~/balancer
steps:
- checkout
Expand Down
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +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"],
},
};
67 changes: 18 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,60 +31,29 @@ There are two types of swap available:
or
**swapExactOut** - i.e. You want to receive exactly 1 BAL and SOR will calculate X amount of ETH you must input.

The SOR will return totalReturn/totalInput as well as a list swaps to achieve the total. Swaps can be through direct pools, i.e. A > POOL1 > B, or via a multihop pool, i.e. A > POOL1 > C > POOL2 > B. The swaps can be executed directly on-chain or with something like the [ExchangeProxy](https://github.com/balancer-labs/balancer-registry/blob/master/contracts/ExchangeProxy.sol).

Example Output:

```js
// Following is output for 1USDC->WETH swapExactIn
[
swaps,
amountOut,
] = await SOR.getSwaps(....

console.log(
`USDC>WETH, SwapExactIn, 1USDC, Total WETH Return: ${amountOut.toString()}`
);
// USDC>WETH, SwapExactIn, 1USDC, Total WETH Return: 3090385829490120 - This is the total amount of WETH received for 1USDC
console.log(`Swaps: `);
console.log(swaps);

/*
This demonstrates a multihop swap going:
USDC -> BTC++ via pool 0x75286...
Then BTC++ -> WETH via pool 0xd4dbf...
*/
[
// Multihop swap
[
// First sequence in swap
{
pool: '0x75286e183d923a5f52f52be205e358c5c9101b09',
tokenIn: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
tokenOut: '0x0327112423f3a68efdf1fcf402f6c5cb9f7c33fd',
swapAmount: '1000000',
limitReturnAmount: '0',
maxPrice: '115792089237316195423570985008687907853269984665640564039457584007913129639935'
},
// Second sequence in swap
{
pool: '0xd4dbf96db2fdf8ed40296d8d104b371adf7dee12',
tokenIn: '0x0327112423f3a68efdf1fcf402f6c5cb9f7c33fd',
tokenOut: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
swapAmount: '89403274570637',
limitReturnAmount: '0',
maxPrice:'115792089237316195423570985008687907853269984665640564039457584007913129639935'
}
]
]
```
The SOR will return totalReturn/totalInput as well as a list swaps to achieve the total. Swaps can be through direct pools, i.e. A > POOL1 > B, or via a multihop pool, i.e. A > POOL1 > C > POOL2 > B. The swaps are returned in a format that can be directly to the Vault to execute the trade.

The file: [example-swapExactIn.ts](test/testScripts/example-swapExactIn.ts), shows full examples with comments for various swaps.
The example file `swapExample.ts` in: [./testScripts](test/testScripts/), demonstrates full examples with comments.

To Run:

Create a .env file in root dir with your infura provider key: `INFURA=your_key`

Install dependencies: `$ yarn install`

Run example: `$ ts-node ./test/testScripts/example-swapExactIn.ts`
Run example: `$ ts-node ./test/testScripts/swapExample.ts`

## Environment Variables

Optional config values can be set in the .env file:

PRICE_ERROR_TOLERANCE - how close we expect prices after swap to be in SOR suggested paths. Defaults 0.00001.

INFINITESIMAL - Infinitesimal is an amount that's used to initialize swap amounts so they are not zero or the path's limit. Defaults 0.000001.

Example:

```
PRICE_ERROR_TOLERANCE=0.00001
INFINITESIMAL=0.000001
```
40 changes: 28 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"version": "1.1.0",
"license": "GPL-3.0-only",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"prepublish": "tsc",
"test": "nyc mocha -r ts-node/register test/*.spec.ts",
"coverage": "nyc report --reporter=text-lcov | coveralls"
"build": "rollup -c",
"prepack": "yarn build",
"test": "TS_NODE_PROJECT='tsconfig.testing.json' nyc mocha -r ts-node/register test/*.spec.ts",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint ./src ./test --ext .ts --max-warnings 0"
},
"husky": {
"hooks": {
Expand All @@ -24,28 +26,42 @@
],
"devDependencies": {
"@ethersproject/wallet": "^5.0.7",
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@rollup/plugin-typescript": "^8.2.5",
"@types/chai": "^4.2.10",
"@types/lodash.clonedeep": "^4.5.6",
"@types/mocha": "^7.0.2",
"@types/node": "^14.0.20",
"@typescript-eslint/eslint-plugin": "^4.29.2",
"@typescript-eslint/parser": "^4.29.2",
"chai": "^4.2.0",
"coveralls": "^3.0.9",
"dotenv": "^8.2.0",
"eslint": "^7.32.0",
"eslint-plugin-mocha-no-only": "^1.1.1",
"eslint-plugin-prettier": "^3.4.1",
"husky": "^4.2.1",
"mocha": "^7.1.0",
"mocha": "8.2.1",
"nyc": "^15.0.0",
"prettier": "^1.19.1",
"perf_hooks": "^0.0.1",
"prettier": "^2.3.2",
"pretty-quick": "^2.0.1",
"ts-node": "^8.6.2",
"typescript": "^3.8.3"
"rollup": "^2.56.3",
"rollup-plugin-dts": "^4.0.0",
"ts-node": "^10.0.0",
"typescript": "^4.3.5"
},
"dependencies": {
"@ethersproject/address": "^5.0.5",
"@ethersproject/constants": "^5.0.5",
"@ethersproject/contracts": "^5.0.5",
"@ethersproject/providers": "5.0.12",
"@ethersproject/solidity": "^5.0.5",
"@ethersproject/units": "^5.0.6",
"bignumber.js": "^9.0.0",
"isomorphic-fetch": "^2.2.1"
"@georgeroman/balancer-v2-pools": "^0.0.5",
"bignumber.js": "^9.0.1",
"isomorphic-fetch": "^2.2.1",
"lodash.clonedeep": "^4.5.0",
"lodash.set": "^4.3.2"
}
}
35 changes: 35 additions & 0 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import typescript from '@rollup/plugin-typescript';
import dts from 'rollup-plugin-dts';

import pkg from './package.json';

const external = [
'@ethersproject/abi',
'@ethersproject/abstract-signer',
'@ethersproject/address',
'@ethersproject/constants',
'@ethersproject/contracts',
'@ethersproject/providers',
'isomorphic-fetch',
'bignumber.js',
];

export default [
{
input: 'src/index.ts',
output: [
{ file: pkg.main, format: 'cjs', sourcemap: true },
{ file: pkg.module, format: 'es', sourcemap: true },
],
plugins: [nodeResolve(), json(), commonjs(), typescript()],
external,
},
{
input: 'src/index.ts',
output: [{ file: 'dist/index.d.ts', format: 'es' }],
plugins: [dts()],
},
];
Loading

0 comments on commit 7b7ff63

Please sign in to comment.