Skip to content

Instantly share code, notes, and snippets.

@wadealexc
Last active August 30, 2020 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wadealexc/12ee22438e8028f5439c5f0faaf9b7f7 to your computer and use it in GitHub Desktop.
Save wadealexc/12ee22438e8028f5439c5f0faaf9b7f7 to your computer and use it in GitHub Desktop.
pragma solidity ^0.5.0;
import "./BPool.sol";
contract AToken is BToken {
constructor () public {
_mint(INIT_POOL_SUPPLY);
_push(msg.sender, INIT_POOL_SUPPLY);
}
}
contract CtrlConsts {
uint constant BONE = 10**18;
uint constant INIT_SUPPLY = BONE * 100;
uint constant MAX_UINT = uint(-1);
uint constant MAX_FEE = BONE / 10;
}
contract Ctrl is CtrlConsts {
uint amtAIn;
uint balanceA;
uint balanceB;
uint weightA;
uint weightB;
// BPool, tA, and tB for SwapExactAmount method
BPool pool_SEA;
AToken tA_SEA;
AToken tB_SEA;
// Amount of token B received from swapExactAmountIn
uint public bOut_SEA;
// BPool, tA, and tB for JoinExitSwap method
BPool pool_JES;
AToken tA_JES;
AToken tB_JES;
// Amount of token B received after joinswapExternAmountIn + exitswapPoolAmountIn
uint public bOut_JES;
/**
* Store weights and balances of tokens for bind process. Store amtAIn for swaps.
* @param _wA Weight of token A (in BONEs)
* @param _wB Weight of token B (in BONEs)
* @param _bA Pool's initial balance of token A (in BONEs)
* @param _bB Pool's initial balance of token B (in BONEs)
* @param _aI Amount of token A that will be traded in both methods (in BONEs)
*/
function setupTests(uint _wA, uint _wB, uint _bA, uint _bB, uint _aI) public {
weightA = _wA * BONE;
weightB = _wB * BONE;
balanceA = _bA * BONE;
balanceB = _bB * BONE;
amtAIn = _aI * BONE;
}
/**
* Run SwapExactAmount test. Deploy -> bind -> setSwapFee -> finalize
* Then trade amtAIn tA_SEA for tB_SEA
*/
function run_SEA() public {
// Set up SwapExactAmount pool
(pool_SEA, tA_SEA, tB_SEA) = deploy();
pool_SEA.bind(address(tA_SEA), balanceA, weightA);
pool_SEA.bind(address(tB_SEA), balanceB, weightB);
pool_SEA.setSwapFee(MAX_FEE);
pool_SEA.finalize();
//
// Call pool_SEA.swapExactAmountIn, trading amtAIn for token tB
// I set minAmountOut to 0 and maxPrice to MAX_UINT, since these are irrelevant
(bOut_SEA, ) = pool_SEA.swapExactAmountIn(address(tA_SEA), amtAIn, address(tB_SEA), 0, MAX_UINT);
}
/**
* Run JoinExitSwap test. Deploy -> bind -> setSwapFee -> finalize
* Trade amtAIn tA_JES for pool tokens, then for tB_JES
*/
function run_JES() public {
// Set up JoinExitSwap pool
(pool_JES, tA_JES, tB_JES) = deploy();
pool_JES.bind(address(tA_JES), balanceA, weightA);
pool_JES.bind(address(tB_JES), balanceB, weightB);
pool_JES.setSwapFee(MAX_FEE);
pool_JES.finalize();
//
// Call pool_JES.joinswapExternAmountIn, trading amtAIn for pool tokens
uint poolOut = pool_JES.joinswapExternAmountIn(address(tA_JES), amtAIn, 0);
// Then call pool_JES.exitswapPoolAmountIn, trading all pool tokens for token tB
bOut_JES = pool_JES.exitswapPoolAmountIn(address(tB_JES), poolOut, 0);
}
// Deploy instances of BPool and AToken. Approve all this contract's tokens to pool
function deploy() internal returns (BPool pool, AToken a, AToken b) {
pool = new BPool();
a = new AToken();
b = new AToken();
// approve all tokens to pool
a.approve(address(pool), uint(-1));
b.approve(address(pool), uint(-1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment