-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from balancer-labs/develop
0.1.17
- Loading branch information
Showing
64 changed files
with
3,605 additions
and
2,029 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import dotenv from 'dotenv'; | ||
import { Wallet } from '@ethersproject/wallet'; | ||
import { JsonRpcProvider } from '@ethersproject/providers'; | ||
import { BalancerSDK, Network, PoolModel } from '../src/index'; | ||
import { formatFixed } from '@ethersproject/bignumber'; | ||
import { forkSetup } from '../src/test/lib/utils'; | ||
import { ADDRESSES } from '../src/test/lib/constants'; | ||
|
||
dotenv.config(); | ||
|
||
const { ALCHEMY_URL: jsonRpcUrl } = process.env; | ||
|
||
// Slots used to set the account balance for each token through hardhat_setStorageAt | ||
// Info fetched using npm package slot20 | ||
const wBTC_SLOT = 0; | ||
const wETH_SLOT = 3; | ||
const slots = [wBTC_SLOT, wETH_SLOT]; | ||
const initialBalances = ['1000000000', '100000000000000000000']; | ||
|
||
// Public test account with 10000 ETH | ||
// publicKey = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'; | ||
const privateKey = | ||
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'; | ||
const wallet = new Wallet(privateKey); | ||
|
||
/* | ||
Example showing how to use Pools module to join pools. | ||
*/ | ||
async function join() { | ||
const network = Network.MAINNET; | ||
const rpcUrl = 'http://127.0.0.1:8545'; | ||
const provider = new JsonRpcProvider(rpcUrl, network); | ||
|
||
const poolId = | ||
'0xa6f548df93de924d73be7d25dc02554c6bd66db500020000000000000000000e'; // 50/50 WBTC/WETH Pool | ||
const tokensIn = [ | ||
ADDRESSES[network].WBTC?.address, | ||
ADDRESSES[network].WETH?.address, | ||
]; // Tokens that will be provided to pool by joiner | ||
const amountsIn = ['10000000', '1000000000000000000']; | ||
const slippage = '100'; // 100 bps = 1% | ||
|
||
const sdkConfig = { | ||
network, | ||
rpcUrl, | ||
}; | ||
const balancer = new BalancerSDK(sdkConfig); | ||
|
||
// Sets up local fork granting signer initial balances and token approvals | ||
await forkSetup( | ||
balancer, | ||
provider, | ||
tokensIn as string[], | ||
slots, | ||
initialBalances, | ||
jsonRpcUrl as string | ||
); | ||
|
||
// Use SDK to find pool info | ||
const pool: PoolModel | undefined = await balancer.poolsProvider.find(poolId); | ||
if (!pool) throw new Error('Pool not found'); | ||
|
||
// Checking balances to confirm success | ||
const bptContract = balancer.contracts.ERC20(pool.address, provider); | ||
const bptBalanceBefore = await bptContract.balanceOf(wallet.address); | ||
|
||
// Use SDK to create join | ||
const { to, data, minBPTOut } = await pool.buildJoin( | ||
wallet.address, | ||
tokensIn as string[], | ||
amountsIn, | ||
slippage | ||
); | ||
|
||
// Submit join tx | ||
const transactionResponse = await wallet.connect(provider).sendTransaction({ | ||
to, | ||
data, | ||
// gasPrice: '6000000000', // gas inputs are optional | ||
// gasLimit: '2000000', // gas inputs are optional | ||
}); | ||
|
||
const transactionReceipt = await transactionResponse.wait(); | ||
|
||
const bptBalanceAfter = await bptContract.balanceOf(wallet.address); | ||
console.log( | ||
'BPT Balance before joining pool: ', | ||
formatFixed(bptBalanceBefore, 18) | ||
); | ||
console.log( | ||
'BPT Balance after joining pool: ', | ||
formatFixed(bptBalanceAfter, 18) | ||
); | ||
console.log( | ||
'Minimum BPT balance expected after join: ', | ||
formatFixed(minBPTOut, 18) | ||
); | ||
} | ||
|
||
// yarn examples:run ./examples/join.ts | ||
join(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect } from 'chai'; | ||
import { parseFixed } from './math'; | ||
|
||
describe('utils/math', () => { | ||
describe('parseFixed', () => { | ||
it('Should work with simple integers', () => { | ||
const result = parseFixed('15'); | ||
expect(result.toString()).to.be.eq('15'); | ||
}); | ||
|
||
it('Should work with decimal strings', () => { | ||
const result = parseFixed('15.123', 3); | ||
expect(result.toString()).to.be.eq('15123'); | ||
}); | ||
|
||
it('Should work with decimal strings that have too many decimals', () => { | ||
const result = parseFixed('15.123456', 3); | ||
expect(result.toString()).to.be.eq('15123'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
BigNumber, | ||
BigNumberish, | ||
parseFixed as _parseFixed, | ||
} from '@ethersproject/bignumber'; | ||
|
||
export function parseFixed(value: string, decimals?: BigNumberish): BigNumber { | ||
const valueWithTrimmedDecimals = new RegExp(`[0-9]+\\.?[0-9]{0,${decimals}}`); | ||
const result = value.match(valueWithTrimmedDecimals); | ||
let parsedValue = value; | ||
if (result) { | ||
parsedValue = result[0]; | ||
} | ||
|
||
return _parseFixed(parsedValue, decimals); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { expect } from 'chai'; | ||
import { BigNumber } from 'ethers'; | ||
import { subSlippage, addSlippage } from './slippageHelper'; | ||
|
||
describe('slippage helper', () => { | ||
const amount = BigNumber.from('100'); | ||
const slippageAsBasisPoints = BigNumber.from('100'); // 1% | ||
const slippageAsPercentage = BigNumber.from('1'); // 1% | ||
|
||
describe('subSlippage', () => { | ||
context('when slippage input as basis points', () => { | ||
const result = subSlippage(amount, slippageAsBasisPoints).toString(); | ||
it('should work', () => { | ||
expect(result).to.be.equal('99'); | ||
}); | ||
}); | ||
context('when slippage input as percentage', () => { | ||
const result = subSlippage(amount, slippageAsPercentage).toString(); | ||
it('should fail', () => { | ||
expect(result).to.be.not.equal('99'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('addSlippage', () => { | ||
context('when slippage input as basis points', () => { | ||
const result = addSlippage(amount, slippageAsBasisPoints).toString(); | ||
it('should work', () => { | ||
expect(result).to.be.equal('101'); | ||
}); | ||
}); | ||
context('when slippage input as percentage', () => { | ||
const result = addSlippage(amount, slippageAsPercentage).toString(); | ||
it('should fail', () => { | ||
expect(result).to.be.not.equal('101'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { BigNumber } from '@ethersproject/bignumber'; | ||
|
||
const bpsPerOne = BigNumber.from('10000'); // number of basis points in 100% | ||
|
||
/** | ||
* Multiplies input by slippage amount | ||
* | ||
* @param {BigNumber} amount Input amount (not parsed) | ||
* @param {BigNumber} slippage Slippage value in bps - i.e. 50 = 0.5% | ||
* @returns Result delta from multiplying amount and slippage | ||
*/ | ||
export const mulSlippage = ( | ||
amount: BigNumber, | ||
slippage: BigNumber | ||
): BigNumber => { | ||
const delta = amount.mul(slippage).div(bpsPerOne); | ||
return delta; | ||
}; | ||
|
||
/** | ||
* Reduce input amount by slippage factor | ||
* | ||
* @param {BigNumber} amount Input in EVM amounts | ||
* @param {BigNumber} slippage Slippage value in bps - i.e. 50 = 0.5% | ||
* @returns Result amount subtracting slippage | ||
*/ | ||
export const subSlippage = ( | ||
amount: BigNumber, | ||
slippage: BigNumber | ||
): BigNumber => { | ||
const delta = mulSlippage(amount, slippage); | ||
return amount.sub(delta); | ||
}; | ||
|
||
/** | ||
* Increase input amount by slippage factor | ||
* | ||
* @param {BigNumber} amount Input in EVM amounts | ||
* @param {BigNumber} slippage Slippage value in bps - i.e. 50 = 0.5% | ||
* @returns Result amount adding slippage | ||
*/ | ||
export const addSlippage = ( | ||
amount: BigNumber, | ||
slippage: BigNumber | ||
): BigNumber => { | ||
const delta = mulSlippage(amount, slippage); | ||
return amount.add(delta); | ||
}; |
Oops, something went wrong.