def setup(): deployer = accounts[0] alice = accounts[1] bob = accounts[2] worker = accounts[9] alpha = interface.IAny('0xa1faa113cbE53436Df28FF0aEe54275c13B40975') proxy_admin = ProxyAdminImpl.deploy({'from': deployer}) staking_impl = AlphaStaking.deploy({'from': deployer}) staking = TransparentUpgradeableProxyImpl.deploy( staking_impl, proxy_admin, staking_impl.initialize.encode_input(alpha, deployer), {'from': deployer}) staking = interface.IAny(staking) # set deployer as worker staking.setWorker(worker, {'from': deployer}) # mint tokens mint_tokens(alpha, alice) mint_tokens(alpha, bob) mint_tokens(alpha, deployer) mint_tokens(alpha, worker) # approve alpha.approve(staking, 2**256 - 1, {'from': alice}) alpha.approve(staking, 2**256 - 1, {'from': bob}) alpha.approve(staking, 2**256 - 1, {'from': deployer}) alpha.approve(staking, 2**256 - 1, {'from': worker}) return deployer, alice, bob, worker, alpha, proxy_admin, staking_impl, staking
def test_curve_spell(bank, curve_spell): alice = accounts[1] comptroller = interface.IAny('0xAB1c342C7bf5Ec5F02ADEA1c2270670bCa144CbB') cream = '0x6d5a7597896a703fe8c85775b23395a48f971305' comptroller._setCreditLimit(bank, 2**256 - 1, {'from': cream}) dai = interface.IAny('0x6B175474E89094C44Da98b954EedeAC495271d0F') usdc = interface.IAny('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48') usdt = interface.IAny('0xdAC17F958D2ee523a2206206994597C13D831ec7') lp = interface.IAny('0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490') mint_tokens(dai, alice) mint_tokens(usdc, alice) mint_tokens(usdt, alice) dai.approve(bank, 2**256 - 1, {'from': alice}) usdc.approve(bank, 2**256 - 1, {'from': alice}) usdt.approve(bank, 2**256 - 1, {'from': alice}) bank.execute( 0, curve_spell, curve_spell.addLiquidity3.encode_input( lp, [10 * 10**18, 10 * 10**6, 10 * 10**6], 0, [10**18, 10**6, 10**6], 0, 0, 0, 0), {'from': alice})
def main(): deployer = accounts.at('0xB593d82d53e2c187dc49673709a6E9f806cdC835', force=True) # deployer = accounts.load('gh') weth = interface.IAny('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2') dai = interface.IAny('0x6B175474E89094C44Da98b954EedeAC495271d0F') usdc = interface.IAny('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48') usdt = interface.IAny('0xdAC17F958D2ee523a2206206994597C13D831ec7') werc20 = WERC20.at('0xe28D9dF7718b0b5Ba69E01073fE82254a9eD2F98') wmas = WMasterChef.at('0x373ae78a14577682591E088F2E78EF1417612c68') wliq = WLiquidityGauge.at('0xfdB4f97953150e47C8606758C13e70b5a789a7ec') wsindex = WStakingRewards.at('0x713df2DDDA9C7d7bDa98A9f8fCd82c06c50fbd90') wsperp = WStakingRewards.at('0xC4635854480ffF80F742645da0310e9e59795c63') # upgrade bank's implementation bank_impl = HomoraBank.deploy({'from': deployer}) proxy_admin = Contract.from_explorer( '0x090eCE252cEc5998Db765073D07fac77b8e60CB2') bank = HomoraBank.at('0x5f5Cd91070960D13ee549C9CC47e7a4Cd00457bb') proxy_admin.upgrade(bank, bank_impl, {'from': deployer}) # re-deploy spells uniswap_spell = UniswapV2SpellV1.deploy( bank, werc20, '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', {'from': deployer}) sushiswap_spell = SushiswapSpellV1.deploy( bank, werc20, '0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F', wmas, {'from': deployer}) balancer_spell = BalancerSpellV1.deploy(bank, werc20, weth, {'from': deployer}) curve_spell = CurveSpellV1.deploy(bank, werc20, weth, wliq, {'from': deployer}) # set whitelist spells in bank bank.setWhitelistSpells( [uniswap_spell, sushiswap_spell, balancer_spell, curve_spell], [True] * 4, {'from': deployer}) # set whitelist tokens in bank bank.setWhitelistTokens([weth, dai, usdc, usdt], [True] * 4, {'from': deployer}) # set whitelist lp tokens in spells uniswap_spell.setWhitelistLPTokens( ['0xd3d2E2692501A5c9Ca623199D38826e513033a17'], [True], {'from': deployer}) sushiswap_spell.setWhitelistLPTokens( ['0x795065dCc9f64b5614C407a6EFDC400DA6221FB0'], [True], {'from': deployer}) balancer_spell.setWhitelistLPTokens( ['0xF54025aF2dc86809Be1153c1F20D77ADB7e8ecF4'], [True], {'from': deployer}) curve_spell.setWhitelistLPTokens( ['0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490'], [True], {'from': deployer})
def main(): deployer = accounts[0] alice = accounts[1] bob = accounts[2] alpha = interface.IAny('0xa1faa113cbE53436Df28FF0aEe54275c13B40975') proxy_admin = ProxyAdminImpl.deploy({'from': deployer}) staking_impl = AlphaStaking.deploy({'from': deployer}) staking = TransparentUpgradeableProxyImpl.deploy( staking_impl, proxy_admin, staking_impl.initialize.encode_input(alpha, deployer), {'from': deployer}) staking = interface.IAny(staking) # mint tokens alpha.mint(alice, 10**30) alpha.mint(bob, 10**30) alpha.mint(deployer, 10**30) # approve alpha.approve(staking, 2**256 - 1, {'from': alice}) alpha.approve(staking, 2**256 - 1, {'from': bob}) alpha.approve(staking, 2**256 - 1, {'from': deployer}) #################################################################################### print('===============================================') print('1. set worker & reward') assert staking.worker( ) == '0x0000000000000000000000000000000000000000', 'worker should initially be 0' staking.setWorker(alice, {'from': deployer}) assert staking.worker() == alice, 'incorrect worker' staking.setWorker(bob, {'from': deployer}) assert staking.worker() == bob, 'incorrect worker' staking.stake(10**18, {'from': alice}) try: staking.reward(0, {'from': alice}) assert False except VirtualMachineError: pass try: staking.reward(10**18, {'from': alice}) assert False except VirtualMachineError: pass staking.reward(0, {'from': deployer}) staking.reward(0, {'from': bob}) staking.reward(10**18, {'from': deployer}) staking.reward(10**18, {'from': bob})
def deploy_pools(deployer, bank, add_strat, liq_strat, rem_strat, bank_config, goblin_config, oracle, pools): wbnb = interface.IAny(wbnb_address) prices = [] tokens0 = [] tokens1 = [] registry = {} for pool in pools: fToken = interface.IAny(pool['token']) if fToken.address < wbnb_address: tokens0.append(fToken) tokens1.append(wbnb) price = (10**18 * wbnb.balanceOf(pool['lp'])) // fToken.balanceOf( pool['lp']) prices.append(price) else: tokens0.append(wbnb) tokens1.append(fToken) price = (10**18 * fToken.balanceOf(pool['lp'])) // wbnb.balanceOf( pool['lp']) prices.append(price) if pool['pid'] == 1: goblin = PancakeswapPool1Goblin.deploy(bank, chef_address, router_address, add_strat, liq_strat, 300, {'from': deployer}) else: goblin = PancakeswapGoblin.deploy(bank, chef_address, router_address, pool['pid'], add_strat, liq_strat, 300, {'from': deployer}) goblin_config.setConfigs([goblin], [pool['goblinConfig']], {'from': deployer}) add_strat_2 = StrategyAddTwoSidesOptimal.deploy( router_address, goblin, fToken, {'from': deployer}) goblin.setStrategyOk([add_strat_2, rem_strat], True, {'from': deployer}) bank_config.setGoblins([goblin], [goblin_config], {'from': deployer}) registry[pool['name']] = {'goblin': goblin, 'two_side': add_strat_2} # set oracle prices oracle.setPrices(tokens0, tokens1, prices, {'from': deployer}) return registry
def test_cake_2(bank, registry): alice = accounts[1] prevBNBBal = alice.balance() bank.work( 0, registry['sashimi']['goblin'], 0, 0, eth_abi.encode_abi(['address', 'bytes'], [ registry['sashimi']['two_side'].address, eth_abi.encode_abi(['address', 'uint256', 'uint256'], [sashimi_address, 0, 0]) ]), { 'from': alice, 'value': '1 ether' }) curBNBBal = alice.balance() print('∆ ht alice', curBNBBal - prevBNBBal) print('alice pos', bank.positionInfo(1)) assert almostEqual(curBNBBal - prevBNBBal, -10**18), 'incorrect BNB input amount' # test reinvest chain.mine(10) goblin = interface.IAny(registry['sashimi']['goblin']) goblin.reinvest({'from': alice})
def main(): deployer = accounts.at('0xCB8cF8E6f61A3a0E383A3dcd9625d7ACe7436De4', force=True) user = accounts.at('0x435Fd10EA3814583eE5fDDfe830330DD4d6Eb4CF', force=True) # deployer = accounts.load('staking-kovan') # user = accounts.load('kovan-tester') token = MockERC20.deploy('mock', 'MOCK', 18, {'from': deployer}) proxy_admin = ProxyAdminImpl.deploy({'from': deployer}) staking_impl = AlphaStaking.deploy({'from': deployer}) staking = TransparentUpgradeableProxyImpl.deploy( staking_impl, proxy_admin, staking_impl.initialize.encode_input(token, deployer), {'from': deployer}) staking = interface.IAny(staking) staking.setWorker(deployer, {'from': deployer}) # mint tokens to deployer token.mint(deployer, 10**12 * 10**18, {'from': deployer}) token.approve(staking, 2**256-1, {'from': deployer}) staking.stake(1000 * 10**18, {'from': deployer}) token.mint(user, 10**12 * 10**18, {'from': user}) token.approve(staking, 2**256-1, {'from': user}) staking.stake(10 * 10**18, {'from': user})
def deploy(deployer): triple_slope_model = TripleSlopeModel.deploy({'from': deployer}) # min debt 0.2 BNB at 10 gwei gas price (killBps 5% -> at least 0.01BNB bonus) # reserve pool bps 1000 (10%) # kill bps 500 (5%) bank_config = ConfigurableInterestBankConfig.deploy( 2 * 10**17, 1000, 500, triple_slope_model, {'from': deployer}) proxy_admin = ProxyAdminImpl.deploy({'from': deployer}) bank_impl = Bank.deploy({'from': deployer}) bank = TransparentUpgradeableProxyImpl.deploy( bank_impl, proxy_admin, bank_impl.initialize.encode_input(bank_config), {'from': deployer}) bank = interface.IAny(bank) oracle = SimplePriceOracle.deploy({'from': deployer}) # strats add_strat = StrategyAllBNBOnly.deploy(router_address, {'from': deployer}) liq_strat = StrategyLiquidate.deploy(router_address, {'from': deployer}) rem_strat = StrategyWithdrawMinimizeTrading.deploy(router_address, {'from': deployer}) goblin_config = PancakeswapGoblinConfig.deploy(oracle, {'from': deployer}) print('bank', bank.address) print('bank_config', bank_config.address) print('all bnb', add_strat.address) print('liq', liq_strat.address) print('withdraw', rem_strat.address) return bank, add_strat, liq_strat, rem_strat, bank_config, goblin_config, oracle
def main(): deployer = accounts.at('0xB593d82d53e2c187dc49673709a6E9f806cdC835', force=True) # deployer = accounts.load('gh') alpha = interface.IAny('0xa1faa113cbE53436Df28FF0aEe54275c13B40975') proxy_admin = ProxyAdminImpl.at('0x090eCE252cEc5998Db765073D07fac77b8e60CB2') staking_impl = AlphaStaking.deploy({'from': deployer, 'gas_price': gas_strategy}) staking = TransparentUpgradeableProxyImpl.deploy( staking_impl, proxy_admin, staking_impl.initialize.encode_input(alpha, deployer), {'from': deployer, 'gas_price': gas_strategy}) staking = interface.IAny(staking) # approve staking contract stake_amt = 10 * 10**18 alpha.approve(staking, stake_amt, {'from': deployer, 'gas_price': gas_strategy}) # stake 10 ALPHA staking.stake(stake_amt, {'from': deployer, 'gas_price': gas_strategy})
def main(): safebox_eth = SafeBoxETH.at('0xeEa3311250FE4c3268F8E684f7C87A82fF183Ec1') safebox_usdt = SafeBox.at('0x020eDC614187F9937A1EfEeE007656C6356Fb13A') safebox_usdc = SafeBox.at('0x08bd64BFC832F1C2B3e07e634934453bA7Fa2db2') safebox_dai = SafeBox.at('0xee8389d235E092b2945fE363e97CDBeD121A0439') cy_eth = interface.IAny(safebox_eth.cToken()) cy_usdt = interface.IAny(safebox_usdt.cToken()) cy_usdc = interface.IAny(safebox_usdc.cToken()) cy_dai = interface.IAny(safebox_dai.cToken()) print('eth', safebox_eth.totalSupply() * cy_eth.exchangeRateStored() / 1e36) print('usdt', safebox_usdt.totalSupply() * cy_usdt.exchangeRateStored() / 1e24) print('usdc', safebox_usdc.totalSupply() * cy_usdc.exchangeRateStored() / 1e24) print('dai', safebox_dai.totalSupply() * cy_dai.exchangeRateStored() / 1e36)
def pure_staking(a, alpha, deployer, alice, bob, worker, proxy_admin, AlphaStaking, TransparentUpgradeableProxyImpl): ''' Staking contract without side-effect eg. setWorker, mint_tokens, approve ''' staking_impl = AlphaStaking.deploy({'from': deployer}) contract = TransparentUpgradeableProxyImpl.deploy( staking_impl, proxy_admin, staking_impl.initialize.encode_input(alpha, deployer), {'from': deployer}) contract = interface.IAny(contract) return contract
def test_uniswap_spell(bank, uniswap_spell): alice = accounts[1] comptroller = interface.IAny('0xAB1c342C7bf5Ec5F02ADEA1c2270670bCa144CbB') cream = '0x6d5a7597896a703fe8c85775b23395a48f971305' comptroller._setCreditLimit(bank, 2**256 - 1, {'from': cream}) uni = interface.IAny('0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984') weth = interface.IAny('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2') mint_tokens(uni, alice) mint_tokens(weth, alice, amount=1000 * 10**18) uni.approve(bank, 2**256 - 1, {'from': alice}) weth.approve(bank, 2**256 - 1, {'from': alice}) bank.execute( 0, uniswap_spell, uniswap_spell.addLiquidityWERC20.encode_input( uni, weth, [10 * 10**18, 10 * 10**18, 0, 0, 10**18, 0, 0, 0]), {'from': alice})
def test_sushiswap_spell(bank, sushiswap_spell): alice = accounts[1] comptroller = interface.IAny('0xAB1c342C7bf5Ec5F02ADEA1c2270670bCa144CbB') cream = '0x6d5a7597896a703fe8c85775b23395a48f971305' comptroller._setCreditLimit(bank, 2**256 - 1, {'from': cream}) sushi = interface.IAny('0x6B3595068778DD592e39A122f4f5a5cF09C90fE2') weth = interface.IAny('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2') mint_tokens(sushi, alice) mint_tokens(weth, alice, amount=1000 * 10**18) sushi.approve(bank, 2**256 - 1, {'from': alice}) weth.approve(bank, 2**256 - 1, {'from': alice}) bank.execute( 0, sushiswap_spell, sushiswap_spell.addLiquidityWMasterChef.encode_input( sushi, weth, [10 * 10**18, 10 * 10**18, 0, 0, 10**18, 0, 0, 0], 12), {'from': alice})
def print_uni_sushi_data(pos_id, interface): bank = interface.IAny('0xba5eBAf3fc1Fcca67147050Bf80462393814E54B') _, collToken, collId, collSize = bank.getPositionInfo(pos_id) pool = interface.IAny(collToken).getUnderlyingToken(collId) r0, r1, _ = interface.IAny(pool).getReserves() supply = interface.IAny(pool).totalSupply() token0 = interface.IAny(pool).token0() token1 = interface.IAny(pool).token1() print(f'{interface.IAny(token0).symbol()} amount:', r0 * collSize // supply / 10**interface.IAny(token0).decimals()) print(f'{interface.IAny(token1).symbol()} amount:', r1 * collSize // supply / 10**interface.IAny(token1).decimals())
def test_balancer_spell(bank, balancer_spell): alice = accounts[1] comptroller = interface.IAny('0xAB1c342C7bf5Ec5F02ADEA1c2270670bCa144CbB') cream = '0x6d5a7597896a703fe8c85775b23395a48f971305' comptroller._setCreditLimit(bank, 2**256 - 1, {'from': cream}) perp = interface.IAny('0xbC396689893D065F41bc2C6EcbeE5e0085233447') usdc = interface.IAny('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48') lp = interface.IAny('0xF54025aF2dc86809Be1153c1F20D77ADB7e8ecF4') wstaking = interface.IAny('0xC4635854480ffF80F742645da0310e9e59795c63') mint_tokens(perp, alice) mint_tokens(usdc, alice) perp.approve(bank, 2**256 - 1, {'from': alice}) usdc.approve(bank, 2**256 - 1, {'from': alice}) bank.execute( 0, balancer_spell, balancer_spell.addLiquidityWStakingRewards.encode_input( lp, [10 * 10**18, 10 * 10**6, 0, 0, 10**6, 0, 0], wstaking), {'from': alice})
def mint_tokens(token, to, interface=None, amount=None): if interface is None: interface = globals()['interface'] token = interface.IAny(token) if amount is None: # default is 1M tokens amount = 10**12 * 10**token.decimals() if token == ALPHA: owner = token.owner() token.mint(amount, {'from': owner}) token.transfer(to, amount, {'from': owner}) else: raise Exception('tokens not supported')
def check_replace_v2_oracle(band_oracle, link_oracle, simple_oracle, agg_oracle, deployer): bank_v2 = HomoraBank.at('0x5f5Cd91070960D13ee549C9CC47e7a4Cd00457bb') core_oracle = CoreOracle.at('0x1E5BDdd0cDF8839d6b27b34927869eF0AD7bf692') simple_oracle_v2 = interface.IAny('0x487e0ae63Bfd8364a11e840900BaAD92D5aF7C42') v2_tokens = [ Tokens.WETH, Tokens.DAI, Tokens.USDC, Tokens.USDT, Tokens.UNI, Tokens.PERP, Tokens.SUSHI ] # set route core_oracle.setRoute(v2_tokens, [agg_oracle] * len(v2_tokens), {'from': deployer})
def staking(a, alpha, deployer, alice, bob, worker, proxy_admin, AlphaStaking, TransparentUpgradeableProxyImpl): staking_impl = AlphaStaking.deploy({'from': deployer}) contract = TransparentUpgradeableProxyImpl.deploy( staking_impl, proxy_admin, staking_impl.initialize.encode_input(alpha, deployer), {'from': deployer}) contract = interface.IAny(contract) contract.setWorker(worker, {'from': deployer}) alpha.mint(alice, 10**30) alpha.mint(bob, 10**30) alpha.mint(deployer, 10**30) alpha.mint(worker, 10**30) alpha.approve(contract, 2**256 - 1, {'from': alice}) alpha.approve(contract, 2**256 - 1, {'from': bob}) alpha.approve(contract, 2**256 - 1, {'from': deployer}) alpha.approve(contract, 2**256 - 1, {'from': worker}) return contract
def mint_tokens(token, to, interface=None, amount=None): if interface is None: interface = globals()['interface'] token = interface.IAny(token) if amount is None: # default is 1M tokens amount = 10**12 * 10**token.decimals() if token == CAKE: owner = token.owner() token.mint(to, amount, {'from': owner}) elif token == WBNB: token.deposit({'from': accounts[9], 'value': amount}) token.transfer(to, amount, {'from': accounts[9]}) elif token == ALPHA or token == BUSD: owner = token.owner() token.mint(amount, {'from': owner}) token.transfer(to, amount, {'from': owner}) else: raise Exception('tokens not supported')
def main(): deployer = accounts.at('0xB593d82d53e2c187dc49673709a6E9f806cdC835', force=True) # deployer = accounts.load('gh') simple_oracle = interface.IAny('0x05e7b38931948e10171e643e5f3004dcd0bef22b') band_oracle = BandAdapterOracle.at('0xb35E6a063CC00c66408284d60765c52e70394772') link_oracle = ChainlinkAdapterOracle.at('0x9A42660eebFf100B9D88cab82b3049Ae2a33712f') agg_oracle = AggregatorOracle.at('0x636478DcecA0308ec6b39e3ab1e6b9EBF00Cd01c') ######################################################################## # check token prices # check_token_prices(band_oracle, link_oracle, simple_oracle, agg_oracle, deployer) ######################################################################## # try replacing in v1 bank check_replace_v1_oracle(band_oracle, link_oracle, simple_oracle, agg_oracle, deployer) ######################################################################## # try replacing in old v2 bank check_replace_v2_oracle(band_oracle, link_oracle, simple_oracle, agg_oracle, deployer)
def deploy(deployer): triple_slope_model = TripleSlopeModel.deploy({ 'from': deployer, 'gas_price': 1000000000 }) # min debt 1 BNB at 10 gwei gas price (avg gas fee = ~0.006 BNB) (killBps 1% -> at least 0.01BNB bonus) # reserve pool bps 2000 (20%) # kill bps 100 (1%) print('部署 bank_config...') bank_config = ConfigurableInterestBankConfig.deploy( 10 * 17, 2000, 100, triple_slope_model, { 'from': deployer, 'gas_price': 1000000000 }) print('部署 ProxyAdminImpl...') proxy_admin = ProxyAdminImpl.deploy({ 'from': deployer, 'gas_price': 1000000000 }) print('部署 Bank...') bank_impl = Bank.deploy({'from': deployer, 'gas_price': 1000000000}) print('部署 TransparentUpgradeableProxyImpl...') bank = TransparentUpgradeableProxyImpl.deploy( bank_impl, proxy_admin, bank_impl.initialize.encode_input(bank_config), { 'from': deployer, 'gas_price': 1000000000 }) bank = interface.IAny(bank) print('部署 SimplePriceOracle...') # oracle = SimplePriceOracle.deploy({'from': deployer, 'gas_price':1000000000}) oracle = SimplePriceOracle.at('0xf3e2206a87014A3acE8def4Bd65BE84CC9B2b388') # strats print('部署 StrategyAllHTOnly... router_address =', router_address) add_strat = StrategyAllHTOnly.deploy(router_address, { 'from': deployer, 'gas_price': 1000000000 }) print('部署 StrategyLiquidate...') liq_strat = StrategyLiquidate.deploy(router_address, { 'from': deployer, 'gas_price': 1000000000 }) print('部署 StrategyWithdrawMinimizeTrading...') rem_strat = StrategyWithdrawMinimizeTrading.deploy(router_address, { 'from': deployer, 'gas_price': 1000000000 }) print('部署 PancakeswapGoblinConfig...') goblin_config = PancakeswapGoblinConfig.deploy(oracle, { 'from': deployer, 'gas_price': 1000000000 }) print('bank', bank.address) print('bank_config', bank_config.address) print('all ht', add_strat.address) print('liq', liq_strat.address) print('withdraw', rem_strat.address) # bank 0x87b8D8337A8086385326fC23b0c95A39cCA2D45D # bank_config 0x86e2Cd710a6fC950506c718b81A8E791b0F24884 # all ht 0x13e60be414955aF08261b9B2B01C012c9B2E1D0E # liq 0x272fFA2cf090815e5cac7B89aD5e3266800f0fF4 # withdraw 0x1d151C966B875188583E35500B3473eB84E75d71 return bank, add_strat, liq_strat, rem_strat, bank_config, goblin_config, oracle
def main(): admin = accounts[0] alice = accounts[1] bob = accounts[2] triple_slope_model = TripleSlopeModel.deploy({'from': admin}) # min debt 0.2 BNB at 10 gwei gas price (killBps 5% -> at least 0.01BNB bonus) # reserve pool bps 1000 (10%) # kill bps 500 (5%) bank_config = ConfigurableInterestBankConfig.deploy( 2 * 10**17, 1000, 500, triple_slope_model, {'from': admin}) proxy_admin = ProxyAdminImpl.deploy({'from': admin}) bank_impl = Bank.deploy({'from': admin}) bank = TransparentUpgradeableProxyImpl.deploy( bank_impl, proxy_admin, bank_impl.initialize.encode_input(bank_config), {'from': admin}) bank = interface.IAny(bank) cake = interface.IAny('0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82') wbnb = interface.IAny('0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c') chef = '0x73feaa1ee314f8c655e354234017be2193c9e24e' router = '0x05ff2b0db69458a0750badebc4f9e13add608c7f' # pancake router lp = interface.IAny('0xA527a61703D82139F8a06Bc30097cC9CAA2df5A6') r0, r1, _ = lp.getReserves() cake_price = r1 * 10**18 // r0 # in bnb oracle = SimplePriceOracle.deploy({'from': admin}) oracle.setPrices([cake], [wbnb], [cake_price], {'from': admin}) # strats add_strat = StrategyAllBNBOnly.deploy(router, {'from': admin}) liq_strat = StrategyLiquidate.deploy(router, {'from': admin}) rem_strat = StrategyWithdrawMinimizeTrading.deploy(router, {'from': admin}) # goblin goblin = PancakeswapPool1Goblin.deploy(bank, chef, router, add_strat, liq_strat, 300, {'from': admin}) # new strat add_strat_2 = StrategyAddTwoSidesOptimal.deploy(router, goblin, cake, {'from': admin}) goblin.setStrategyOk([add_strat_2], True, {'from': admin}) # set whitelist cake add_strat.setWhitelistTokens([cake], [True], {'from': admin}) liq_strat.setWhitelistTokens([cake], [True], {'from': admin}) rem_strat.setWhitelistTokens([cake], [True], {'from': admin}) # mint tokens mint_tokens(cake, alice) mint_tokens(wbnb, alice) # approve tokens cake.approve(add_strat_2, 2**256 - 1, {'from': alice}) # set goblin in bank config # work factor 72.5% # kill factor 80% # max price diff 1.1x (11000) goblin_config = PancakeswapGoblinConfig.deploy(oracle, {'from': admin}) goblin_config.setConfigs([goblin], [[True, 7250, 8000, 11000]], {'from': admin}) bank_config.setGoblins([goblin], [goblin_config], {'from': admin}) # add some BNB to bank bank.deposit({'from': bob, 'value': '1000 ether'}) ############################################################################################ print('==============') print('Case 0. set up initial borrow') alice = accounts[1] bob = accounts[2] bank.deposit({'from': bob, 'value': '2 ether'}) prevBNBBal = alice.balance() bank.work( 0, goblin, 10**18, 0, eth_abi.encode_abi(['address', 'bytes'], [ add_strat_2.address, eth_abi.encode_abi(['address', 'uint256', 'uint256'], [cake.address, 0, 0]) ]), { 'from': alice, 'value': '1 ether' }) ########################################################### # work (no borrow) with 0 cake print('==============================================================') print('Case 1. work (no borrow) with 0 CAKE') deposit_amt = 10**18 prevBNBBal = alice.balance() bank.work( 0, goblin, 0, 0, eth_abi.encode_abi(['address', 'bytes'], [ add_strat_2.address, eth_abi.encode_abi(['address', 'uint256', 'uint256'], [cake.address, 0, 0]) ]), { 'from': alice, 'value': deposit_amt }) curBNBBal = alice.balance() print('∆ bnb alice', curBNBBal - prevBNBBal) print('alice pos', bank.positionInfo(2)) pos_health, pos_debt = bank.positionInfo(2) assert almostEqual( pos_health, deposit_amt), 'position health should be ~1 BNB (swap fee 0.2%)' assert pos_debt == 0, 'position debt should be 0' ############################################################ # work (borrow) with 0 cake print('==============================================================') print('Case 2. work 2x (borrow) with 0 CAKE') deposit_amt = 1 * 10**18 borrow_amt = 1 * 10**18 prevBNBBal = alice.balance() bank.work( 0, goblin, borrow_amt, 0, eth_abi.encode_abi(['address', 'bytes'], [ add_strat_2.address, eth_abi.encode_abi(['address', 'uint256', 'uint256'], [cake.address, 0, 0]) ]), { 'from': alice, 'value': deposit_amt }) curBNBBal = alice.balance() print('∆ bnb alice', curBNBBal - prevBNBBal) print('alice pos', bank.positionInfo(3)) pos_health, pos_debt = bank.positionInfo(3) assert almostEqual(curBNBBal - prevBNBBal, -deposit_amt), 'incorrect deposit amt' assert almostEqual(pos_debt, borrow_amt), 'debt != borrow amount' assert almostEqual( pos_health, deposit_amt + borrow_amt), 'position health should be ~ deposited + borrow amt' ########################################################### # work (no borrow) with cake print('==============================================================') print('Case 3. work (no borrow) with CAKE') deposit_amt = 10**18 cake_amt = 2 * 10**18 prevBNBBal = alice.balance() bank.work( 0, goblin, 0, 0, eth_abi.encode_abi(['address', 'bytes'], [ add_strat_2.address, eth_abi.encode_abi(['address', 'uint256', 'uint256'], [cake.address, cake_amt, 0]) ]), { 'from': alice, 'value': deposit_amt }) curBNBBal = alice.balance() print('∆ bnb alice', curBNBBal - prevBNBBal) print('alice pos', bank.positionInfo(4)) pos_health, pos_debt = bank.positionInfo(4) assert almostEqual( pos_health, deposit_amt + cake_amt * cake_price // 10**18), 'position health should be ~1 BNB (swap fee 0.2%)' assert pos_debt == 0, 'position debt should be 0' ############################################################ # work (borrow) with cake print('==============================================================') print('Case 4. work 2x (borrow) with CAKE') deposit_amt = 1 * 10**18 borrow_amt = 1 * 10**18 cake_amt = 2 * 10**18 prevBNBBal = alice.balance() prevCakeBal = cake.balanceOf(alice) bank.work( 0, goblin, borrow_amt, 0, eth_abi.encode_abi(['address', 'bytes'], [ add_strat_2.address, eth_abi.encode_abi(['address', 'uint256', 'uint256'], [cake.address, cake_amt, 0]) ]), { 'from': alice, 'value': deposit_amt }) curBNBBal = alice.balance() curCakeBal = cake.balanceOf(alice) print('∆ bnb alice', curBNBBal - prevBNBBal) print('∆ cake alice', curCakeBal - prevCakeBal) print('alice pos', bank.positionInfo(5)) pos_health, pos_debt = bank.positionInfo(5) assert almostEqual(curBNBBal - prevBNBBal, -deposit_amt), 'incorrect deposit amt' assert almostEqual(pos_debt, borrow_amt), 'debt != input + borrow' assert almostEqual( pos_health, deposit_amt + borrow_amt + cake_amt * cake_price // 10**18), 'position health should be ~ deposited + borrow amt'
def check_replace_v1_oracle(band_oracle, link_oracle, simple_oracle, agg_oracle, deployer): bank_v1 = interface.IAny('0x67B66C99D3Eb37Fa76Aa3Ed1ff33E8e39F0b9c7A') goblin_config = interface.IAny('0x61858a3d3d8fDbC622a64a9fFB5b77Cc57beCB98') goblin_config.setOracle(agg_oracle, {'from': deployer})
def main(): bank = interface.IAny('0x5f5Cd91070960D13ee549C9CC47e7a4Cd00457bb') fake_credit_limit(bank)
def fake_credit_limit(bank): comptroller = interface.IAny('0xab1c342c7bf5ec5f02adea1c2270670bca144cbb') comptroller_admin = comptroller.admin() comptroller._setCreditLimit(bank, 2**256 - 1, {'from': comptroller_admin})
def main(): # deployer = accounts[0] # deployer = accounts.at('0x1dcEf12e93b0aBF2d36f723e8B59Cc762775d513', force=True) deployer = accounts.add( 'eb555556ca1b0a95142fa46019afa8451eb247dee035992742d60aa44316252f') alice = accounts.add( 'a8060afe2390bd0c00c7ef800f545d466b55cc84d4ad6d01e03220af03e97982') bob = accounts.add( '7b9009958a83807bbe38bf35f451ff0c4bf4d926cee63dd07658762db58ceba4') # =====开始:重新部署合约=============================== # print('1 部署 银行配置合约...') # print(' 部署3段式利率模型 triple_slope_model...') # triple_slope_model = TripleSlopeModel.deploy({'from': deployer}) # print(' 部署可配银行配置合约 ConfigurableInterestBankConfig...') # print(' min debt 0.2 HT at 10 gwei gas price (avg gas fee = ~0.006 HT) (killBps 1% -> at least 0.01BNB bonus)') # print(' reserve pool bps 2000 (20%)') # print(' kill bps 100 (1%)') # bank_config = ConfigurableInterestBankConfig.deploy( # 2 * 10*17, 2000, 100, triple_slope_model, {'from': deployer}) # print('2 部署 银行合约...') # print(' 部署 ProxyAdminImpl...') # proxy_admin = ProxyAdminImpl.deploy({'from': deployer}) # print(' 部署 Bank...') # bank_impl = Bank.deploy({'from': deployer}) # print(' 部署 TransparentUpgradeableProxyImpl...') # bank = TransparentUpgradeableProxyImpl.deploy( # bank_impl, proxy_admin, bank_impl.initialize.encode_input(bank_config), {'from': deployer}) # bank = interface.IAny(bank) # print('3 部署 测试用的预言机合约...') # print(' 部署 SimplePriceOracle...') # # oracle = SimplePriceOracle.deploy({'from': deployer}) # oracle = SimplePriceOracle.at('0xf3e2206a87014A3acE8def4Bd65BE84CC9B2b388') # # strats # print('4 部署 策略合约...') # print(' 部署 StrategyAllHTOnly... router_address =',router_address) # add_strat = StrategyAllHTOnly.deploy(router_address, {'from': deployer}) # print(' 部署流动性策略 StrategyLiquidate...') # liq_strat = StrategyLiquidate.deploy(router_address, {'from': deployer}) # print(' 部署提款策略 StrategyWithdrawMinimizeTrading...') # rem_strat = StrategyWithdrawMinimizeTrading.deploy(router_address, {'from': deployer}) # print(' 部署goblin的配置,传入oracle合约地址 PancakeswapGoblinConfig...') # goblin_config = PancakeswapGoblinConfig.deploy(oracle, {'from': deployer}) # =====结束:重新部署合约=============================== # ==================================== # 或者关联到已部署合约 bank_config = ConfigurableInterestBankConfig.at( '0x50D73FF608c6a66b1c36f4a051363cC8a359621d') bank = TransparentUpgradeableProxyImpl.at( '0xF1FB892E5072cE129Af13a795f211598A8931132') bank = interface.IAny(bank) oracle = SimplePriceOracle.at('0xf3e2206a87014A3acE8def4Bd65BE84CC9B2b388') add_strat = StrategyAllHTOnly.at( '0x7Bc24bB9da86C808cf8cBC9ED935AC61068FCD34') liq_strat = StrategyLiquidate.at( '0x04492d6C6bB5439e5916CC792b570DEd6a3563f6') rem_strat = StrategyWithdrawMinimizeTrading.at( '0x5158e8E7a7C45050Cd039802530473b01570f1f2') goblin_config = PancakeswapGoblinConfig.at( '0x21952994097720Db2356B73617F7d03ee9662Eaa') # ==================================== print(' 部署后合约地址汇总:') print(' bank', bank.address) print(' bank_config', bank_config.address) print(' all ht', add_strat.address) print(' liq', liq_strat.address) print(' withdraw', rem_strat.address) print(' goblin_config', goblin_config.address) pools = [{ 'name': 'sashimi', 'token': sashimi_address, 'lp': sashimi_lp_address, 'pid': 1, 'goblinConfig': [True, 6250, 7000, 11000] }] wht = interface.IAny(wht_address) print(' WHT的地址是:', wht_address) # =====开始:重新配置交易池=============================== # registry = {} # print('5 部署交易池...') # for pool in pools: # print(' ==============================') # print(' 部署交易对:wht -',pool['name'],'地址是:',pool['token']) # fToken = interface.IAny(pool['token']) # if pool['pid'] == 1: # # reinvest 0.3% (avg gas fee ~0.006 BNB) # print(' 部署 pid=1 的交易对的gobilin PancakeswapPool1Goblin ... reinvest 0.3%') # goblin = PancakeswapPool1Goblin.deploy( # ank, # chef_address, # router_address, # add_strat, # liq_strat, # 30, # {'from': deployer}) # else: # # reinvest 0.3% (avg gas fee ~0.006 BNB) # print(' 部署 pid>1 的交易对的gobilin PancakeswapGoblin ... reinvest 0.3%') # goblin = PancakeswapGoblin.deploy( # bank, # chef_address, # router_address, # pool['pid'], # add_strat, # liq_strat, # 30, # {'from': deployer}) # print(' 配置goblin的配置数据到配置合约中 goblin_config.setConfigs ...',pool['goblinConfig']) # goblin_config.setConfigs([goblin], [pool['goblinConfig']], {'from': deployer}) # print(' 部署双边策略合约:add_strat_2 StrategyAddTwoSidesOptimal ...') # add_strat_2 = StrategyAddTwoSidesOptimal.deploy( # router_address, goblin, fToken, {'from': deployer}) # print(' 配置双边策略add_strat_2到 goblin goblin.setStrategyOk ...') # goblin.setStrategyOk([add_strat_2, rem_strat], True, {'from': deployer}) # print(' 配置goblin到银行配置合约 bank_config.setGoblins ...') # bank_config.setGoblins([goblin], [goblin_config], {'from': deployer}) # # re-assign two side strat as add strat for pool 1 goblin # if pool['pid'] == 1: # print(' 对于pid=1的交易池 配置更严格的策略add_strat_2到goblin ...') # goblin.setCriticalStrategies(add_strat_2, liq_strat, {'from': deployer}) # print(' 配置add_strat到goblin goblin.setStrategyOk ...') # goblin.setStrategyOk([add_strat], False, {'from': deployer}) # unset add_strat # registry[pool['name']] = {'goblin': goblin, # 'two_side': add_strat_2, # 'token': fToken.address} # print(' 交易池配置完成!! registry[',pool["name"],']:',registry[pool['name']]) # =====结束:重新配置交易池=============================== # 或者使用已部署的池子 registry = {} goblin = PancakeswapPool1Goblin.at( '0xB67e7267fc3180679AC2C4A0C13442C15321670D') add_strat_2 = StrategyAddTwoSidesOptimal.at( '0xe00ff084fe9d67CA6736052aFeb00F2870315F91') registry['sashimi'] = { 'goblin': goblin, 'two_side': add_strat_2, 'token': '0xc2037C1c13dd589e0c14C699DD2498227d2172cC' } print('registry[ sashimi ]:', registry['sashimi']) # ==================================== wht = interface.IAny(wht_address) sashimi = interface.IAny(sashimi_address) lp = interface.IAny(sashimi_lp_address) r0, r1, _ = lp.getReserves() sashimi_price = r1 * 10**18 // r0 # in bnb oracle.setPrices([sashimi], [wht], [sashimi_price], {'from': deployer}) print('6 配置交易对的价格 sashimi对wht的价格的价格是', sashimi_price / 10**18) print(' lptoken 总供应量是:', lp.totalSupply() / 10**18) print(' 当前bank存款:', bank.totalSupply() / 10**18) # set whitelist tokens to add_strat (no sashimi) add_fTokens = list(map(lambda pool: pool['token'], pools[1:])) print('add_fTokens:', add_fTokens) print('add_strat.setWhitelistTokens ...') add_strat.setWhitelistTokens(add_fTokens, [True] * len(add_fTokens), { 'from': deployer, 'gas_price': 1000000000 }) # set whitelist tokens to liq_strat, rem_strat fTokens = list(map(lambda pool: pool['token'], pools)) print('fTokens:', fTokens) print('liq_strat.setWhitelistTokens ...') liq_strat.setWhitelistTokens(fTokens, [True] * len(fTokens), {'from': deployer}) print('rem_strat.setWhitelistTokens ...') rem_strat.setWhitelistTokens(fTokens, [True] * len(fTokens), {'from': deployer}) # mint tokens # mint_tokens(sashimi, alice) # approve tokens sashimi.approve(add_strat_2, 2**256 - 1, {'from': alice}) wht.approve(bank, 2**256 - 1, {'from': alice}) ######################################################################### # test work print(' 开始测试token: sashimi ...') print(' bank.nextPositionID=', bank.nextPositionID()) print(' 接收债务吗? ', bank_config.acceptDebt(registry['sashimi']['goblin'])) print(' minDebtSize= ', bank_config.minDebtSize()) print( ' minDebtSize.workFactor= ', bank_config.workFactor(registry['sashimi']['goblin'], bank_config.minDebtSize())) print(' workFactor= ', bank_config.workFactor(registry['sashimi']['goblin'], 10**17)) # ======================== prevBNBBal = alice.balance() bank.work( 0, registry['sashimi']['goblin'], 10**17, 0, eth_abi.encode_abi(['address', 'bytes'], [ registry['sashimi']['two_side'].address, eth_abi.encode_abi(['address', 'uint256', 'uint256'], [registry['sashimi']['token'], 0, 0]) ]), { 'from': alice, 'value': '0.05 ether', 'gas_price': 1000000000 }) curBNBBal = alice.balance() print(' bank.nextPositionID=', bank.nextPositionID()) pos_id = bank.nextPositionID() - 1 print(' goblin.health=', goblin.health(pos_id)) print(' goblin.shares=', goblin.shares(pos_id)) print(' goblin.shareToBalance(goblin.shares)=', goblin.shareToBalance(goblin.shares(pos_id))) print(' ∆ ht alice', curBNBBal - prevBNBBal) print(' alice pos', bank.positionInfo(pos_id))
def main(): admin = accounts[0] alice = accounts[1] minter = accounts[9] alpha = interface.IAny(alpha_address) pancake_router = interface.IAny(router_address) triple_slope_model = TripleSlopeModel.deploy({'from': admin}) # min debt 0.2 BNB at 10 gwei gas price (killBps 5% -> at least 0.01BNB bonus) # reserve pool bps 1000 (10%) # kill bps 500 (5%) bank_config = ConfigurableInterestBankConfig.deploy( 2 * 10**17, 1000, 500, triple_slope_model, {'from': admin}) proxy_admin = ProxyAdminImpl.deploy({'from': admin}) bank_impl = Bank.deploy({'from': admin}) bank = TransparentUpgradeableProxyImpl.deploy( bank_impl, proxy_admin, bank_impl.initialize.encode_input(bank_config), {'from': admin}) bank = interface.IAny(bank) # create pair factory = interface.IAny(interface.IAny(router_address).factory()) factory.createPair(alpha_address, bank, {'from': minter}) # deploy ibBNBRouter router = IbBNBRouter.deploy(router_address, bank, alpha_address, {'from': admin}) # get ibBNB + ALPHA to minter bank.deposit({'from': minter, 'value': '100000 ether'}) mint_tokens(alpha_address, minter) # mint ibBNB + ALPHA to alice bank.transfer(alice, 1000 * 10**18, {'from': minter}) mint_tokens(alpha_address, alice) # minter approve pancake router alpha.approve(router_address, 2**256-1, {'from': minter}) bank.approve(router_address, 2**256-1, {'from': minter}) # alice approve ibBNB router alpha.approve(router, 2**256-1, {'from': alice}) bank.approve(router, 2**256-1, {'from': alice}) lp = interface.IAny(router.lpToken()) lp.approve(router, 2**256-1, {'from': alice}) # setup liquidity in ibBNB + ALPHA pool price = 1000 # 1 ibBNB = 1000 alpha bnb_supply = 1000 * 10**18 pancake_router.addLiquidity(bank, alpha_address, bnb_supply, bnb_supply * price, 0, 0, minter, 2**256-1, {'from': minter}) ############################################################################################## # Case 1. add & remove liquidity alpha + bnb print('======================================================================') print('Case 1. add liquidity & remove alpha + bnb') prevBNBBal = alice.balance() # add liquidity router.addLiquidityBNB(10**18, 0, 0, alice, 2**256-1, {'from': alice, 'value': 10**18}) curBNBBal = alice.balance() delta1 = curBNBBal - prevBNBBal assert lp.balanceOf(alice) > 0 prevBNBBal = alice.balance() # remove liquidity router.removeLiquidityBNB(lp.balanceOf(alice), 0, 0, alice, 2**256-1, {'from': alice}) curBNBBal = alice.balance() delta2 = curBNBBal - prevBNBBal assert almostEqual(delta1, -10**18 // price), 'incorrect BNB input amount' assert almostEqual(delta2, 10**18 // price), 'incorrect BNB output amount' ############################################################################################## # Case 2. add liquidity two sides optimal print('======================================================================') print('Case 2. add liquidity two sides optimal') prevIbBNBBal = bank.balanceOf(alice) prevAlphaBal = alpha.balanceOf(alice) router.addLiquidityTwoSidesOptimal(10**18, 0, 0, alice, 2**256-1, {'from': alice}) curIbBNBBal = bank.balanceOf(alice) curAlphaBal = alpha.balanceOf(alice) assert almostEqual(curIbBNBBal - prevIbBNBBal, -10**18), 'incorrect ibBNB input amount' prevIbBNBBal = bank.balanceOf(alice) prevAlphaBal = alpha.balanceOf(alice) router.addLiquidityTwoSidesOptimal(0, 10**18, 0, alice, 2**256-1, {'from': alice}) curIbBNBBal = bank.balanceOf(alice) curAlphaBal = alpha.balanceOf(alice) assert almostEqual(curAlphaBal - prevAlphaBal, -10**18), 'incorrect ALPHA input amount' prevIbBNBBal = bank.balanceOf(alice) prevAlphaBal = alpha.balanceOf(alice) router.addLiquidityTwoSidesOptimal(2 * 10**18, 10**18, 0, alice, 2**256-1, {'from': alice}) curIbBNBBal = bank.balanceOf(alice) curAlphaBal = alpha.balanceOf(alice) assert almostEqual(curIbBNBBal - prevIbBNBBal, -2 * 10**18), 'incorrect ibBNB input amount' assert almostEqual(curAlphaBal - prevAlphaBal, -10**18), 'incorrect ALPHA input amount' ############################################################################################## # Case 3. add liquidity two sides optimal bnb print('======================================================================') print('Case 3. add liquidity two sides optimal bnb') prevIbBNBBal = bank.balanceOf(alice) prevAlphaBal = alpha.balanceOf(alice) router.addLiquidityTwoSidesOptimalBNB(10**18, 0, alice, 2**256-1, {'from': alice}) curIbBNBBal = bank.balanceOf(alice) curAlphaBal = alpha.balanceOf(alice) assert almostEqual(curAlphaBal - prevAlphaBal, -10**18), 'incorrect ALPHA input amount' prevIbBNBBal = bank.balanceOf(alice) prevBNBBal = alice.balance() prevAlphaBal = alpha.balanceOf(alice) router.addLiquidityTwoSidesOptimalBNB( 0, 0, alice, 2**256-1, {'from': alice, 'value': '1 ether'}) curIbBNBBal = bank.balanceOf(alice) curBNBBal = alice.balance() curAlphaBal = alpha.balanceOf(alice) assert almostEqual(curBNBBal - prevBNBBal, -10**18), 'incorrect ibBNB input amount' prevIbBNBBal = bank.balanceOf(alice) prevBNBBal = alice.balance() prevAlphaBal = alpha.balanceOf(alice) router.addLiquidityTwoSidesOptimalBNB( 2 * 10**18, 0, alice, 2**256-1, {'from': alice, 'value': '1 ether'}) curIbBNBBal = bank.balanceOf(alice) curBNBBal = alice.balance() curAlphaBal = alpha.balanceOf(alice) assert almostEqual(curBNBBal - prevBNBBal, -10**18), 'incorrect ibBNB input amount' assert almostEqual(curAlphaBal - prevAlphaBal, -2 * 10**18), 'incorrect ALPHA input amount' ############################################################################################## # Case 4. remove liquidity alpha + bnb print('======================================================================') print('Case 4. remove liquidity alpha + bnb') r0, r1, _ = lp.getReserves() supply = lp.totalSupply() prevLPBal = lp.balanceOf(alice) prevIbBNBBal = bank.balanceOf(alice) prevBNBBal = alice.balance() prevAlphaBal = alpha.balanceOf(alice) router.removeLiquidityBNB(lp.balanceOf(alice) // 10, 0, 0, alice, 2**256-1, {'from': alice}) curLPBal = lp.balanceOf(alice) curIbBNBBal = bank.balanceOf(alice) curBNBBal = alice.balance() curAlphaBal = alpha.balanceOf(alice) assert almostEqual(curLPBal - prevLPBal, -prevLPBal // 10), 'incorrect LP withdraw amount' assert almostEqual(curAlphaBal - prevAlphaBal, r0 * prevLPBal // 10 / supply), 'incorrect ALPHA received' assert almostEqual(curBNBBal - prevBNBBal, r1 * prevLPBal // 10 / supply), 'incorrect BNB received' assert prevIbBNBBal == curIbBNBBal, 'ibBNB not equal' ############################################################################################## # Case 5. remove liquidity all alpha print('======================================================================') print('Case 5. remove liquidity all alpha') r0, r1, _ = lp.getReserves() supply = lp.totalSupply() prevLPBal = lp.balanceOf(alice) prevIbBNBBal = bank.balanceOf(alice) prevBNBBal = alice.balance() prevAlphaBal = alpha.balanceOf(alice) router.removeLiquidityAllAlpha(lp.balanceOf(alice) // 10, 0, alice, 2**256-1, {'from': alice}) curLPBal = lp.balanceOf(alice) curIbBNBBal = bank.balanceOf(alice) curBNBBal = alice.balance() curAlphaBal = alpha.balanceOf(alice) assert almostEqual(curLPBal - prevLPBal, -prevLPBal // 10), 'incorrect LP withdraw amount' assert almostEqual(curAlphaBal - prevAlphaBal, 2 * r0 * prevLPBal // 10 / supply), 'incorrect ALPHA received' assert curBNBBal == prevBNBBal, 'BNB not equal' assert prevIbBNBBal == curIbBNBBal, 'ibBNB not equal' ############################################################################################## # Case 6. swap exact bnb for alpha print('======================================================================') print('Case 6. swap exact bnb for alpha') prevIbBNBBal = bank.balanceOf(alice) prevBNBBal = alice.balance() prevAlphaBal = alpha.balanceOf(alice) router.swapExactBNBForAlpha(0, alice, 2**256-1, {'from': alice, 'value': '1 ether'}) curIbBNBBal = bank.balanceOf(alice) curBNBBal = alice.balance() curAlphaBal = alpha.balanceOf(alice) assert almostEqual(curAlphaBal - prevAlphaBal, 10**18 * price), 'incorrect ALPHA received' assert curBNBBal - prevBNBBal == -10**18, 'BNB not equal' assert prevIbBNBBal == curIbBNBBal, 'ibBNB not equal' ############################################################################################## # Case 7. swap alpha for exact bnb print('======================================================================') print('Case 7. swap alpha for exact bnb') prevIbBNBBal = bank.balanceOf(alice) prevBNBBal = alice.balance() prevAlphaBal = alpha.balanceOf(alice) router.swapAlphaForExactBNB(10**16, 2000 * 10**16, alice, 2**256-1, {'from': alice}) curIbBNBBal = bank.balanceOf(alice) curBNBBal = alice.balance() curAlphaBal = alpha.balanceOf(alice) assert almostEqual(curAlphaBal - prevAlphaBal, -10**16 * price), 'incorrect ALPHA received' assert curBNBBal - prevBNBBal == 10**16, 'BNB not equal' assert prevIbBNBBal == curIbBNBBal, 'ibBNB not equal' ############################################################################################## # Case 8. swap exact alpha for bnb print('======================================================================') print('Case 8. swap exact alpha for bnb') prevIbBNBBal = bank.balanceOf(alice) prevBNBBal = alice.balance() prevAlphaBal = alpha.balanceOf(alice) router.swapExactAlphaForBNB(10**18, 0, alice, 2**256-1, {'from': alice}) curIbBNBBal = bank.balanceOf(alice) curBNBBal = alice.balance() curAlphaBal = alpha.balanceOf(alice) assert curAlphaBal - prevAlphaBal == -10**18, 'incorrect ALPHA received' assert almostEqual(curBNBBal - prevBNBBal, 10**18 // price), 'BNB not equal' assert prevIbBNBBal == curIbBNBBal, 'ibBNB not equal' ############################################################################################## # Case 9. swap bnb for exact alpha print('======================================================================') print('Case 9. swap bnb for exact alpha') prevIbBNBBal = bank.balanceOf(alice) prevBNBBal = alice.balance() prevAlphaBal = alpha.balanceOf(alice) router.swapBNBForExactAlpha(10**18, alice, 2**256-1, {'from': alice, 'value': '1 ether'}) curIbBNBBal = bank.balanceOf(alice) curBNBBal = alice.balance() curAlphaBal = alpha.balanceOf(alice) assert curAlphaBal - prevAlphaBal == 10**18, 'incorrect ALPHA received' assert almostEqual(curBNBBal - prevBNBBal, -10**18 // price), 'BNB not equal' assert prevIbBNBBal == curIbBNBBal, 'ibBNB not equal'
def main(): # deployer = accounts[0] # deployer = accounts.at('0x1dcEf12e93b0aBF2d36f723e8B59Cc762775d513', force=True) deployer = accounts.add( 'eb555556ca1b0a95142fa46019afa8451eb247dee035992742d60aa44316252f') # ==================================== # 部署相关合约 # bank, add_strat, liq_strat, rem_strat, bank_config, goblin_config, oracle = deploy(deployer) # 或者关联到已部署合约 bank_config = ConfigurableInterestBankConfig.at( '0x50D73FF608c6a66b1c36f4a051363cC8a359621d') bank = TransparentUpgradeableProxyImpl.at( '0xF1FB892E5072cE129Af13a795f211598A8931132') bank = interface.IAny(bank) oracle = SimplePriceOracle.at('0xf3e2206a87014A3acE8def4Bd65BE84CC9B2b388') add_strat = StrategyAllHTOnly.at( '0x7Bc24bB9da86C808cf8cBC9ED935AC61068FCD34') liq_strat = StrategyLiquidate.at( '0x04492d6C6bB5439e5916CC792b570DEd6a3563f6') rem_strat = StrategyWithdrawMinimizeTrading.at( '0x5158e8E7a7C45050Cd039802530473b01570f1f2') goblin_config = PancakeswapGoblinConfig.at( '0x21952994097720Db2356B73617F7d03ee9662Eaa') print('bank', bank.address) print('bank_config', bank_config.address) print('all ht', add_strat.address) print('liq', liq_strat.address) print('withdraw', rem_strat.address) print('goblin_config', goblin_config.address) # ==================================== pools = [ { 'name': 'sashimi', 'token': sashimi_address, 'lp': sashimi_lp_address, 'pid': 1, 'goblinConfig': [True, 6250, 7000, 11000] } # ==================================== # 屏蔽无需测试的token # , # { # 'name': 'busd', # 'token': busd_address, # 'lp': busd_lp_address, # 'pid': 2, # 'goblinConfig': [True, 7000, 8000, 11000] # }, # { # "name": "btcb", # "token": btcb_address, # "lp": btcb_lp_address, # "pid": 15, # "goblinConfig": [True, 7000, 8000, 11000] # }, # { # "name": "eth", # "token": eth_address, # "lp": eth_lp_address, # "pid": 14, # "goblinConfig": [True, 7000, 8000, 11000] # }, # { # "name": "usdt", # "token": usdt_address, # "lp": usdt_lp_address, # "pid": 17, # "goblinConfig": [True, 7000, 8000, 11000] # }, # { # "name": "alpha", # "token": alpha_address, # "lp": alpha_lp_address, # "pid": 16, # "goblinConfig": [True, 6250, 7000, 11000] # } # ==================================== ] # ==================================== # deploy pools # registry = deploy_pools(deployer, bank, add_strat, liq_strat, rem_strat, # bank_config, goblin_config, oracle, pools) # 或者使用已部署的池子 registry = {} goblin = PancakeswapPool1Goblin.at( '0xB67e7267fc3180679AC2C4A0C13442C15321670D') add_strat_2 = StrategyAddTwoSidesOptimal.at( '0xe00ff084fe9d67CA6736052aFeb00F2870315F91') registry['sashimi'] = { 'goblin': goblin, 'two_side': add_strat_2, 'token': '0xc2037C1c13dd589e0c14C699DD2498227d2172cC' } print('registry[ sashimi ]:', registry['sashimi']) # ==================================== wht = interface.IAny(wht_address) fToken = interface.IAny(sashimi_address) # oracle.setPrices([wht], [fToken], [10**18 * 300], {'from': deployer, 'gas_price':1000000000}) # # set whitelist tokens to add_strat (no sashimi) # add_fTokens = list(map(lambda pool: pool['token'], pools[1:])) # print('add_fTokens:', add_fTokens) # print('add_strat.setWhitelistTokens ...') # add_strat.setWhitelistTokens(add_fTokens, [True] * len(add_fTokens), {'from': deployer, 'gas_price':1000000000}) # # set whitelist tokens to liq_strat, rem_strat # fTokens = list(map(lambda pool: pool['token'], pools)) # print('fTokens:', fTokens) # print('liq_strat.setWhitelistTokens ...') # liq_strat.setWhitelistTokens(fTokens, [True] * len(fTokens), {'from': deployer, 'gas_price':1000000000}) # print('rem_strat.setWhitelistTokens ...') # rem_strat.setWhitelistTokens(fTokens, [True] * len(fTokens), {'from': deployer, 'gas_price':1000000000}) ######################################################################### # test work print('开始测试token: sashimi ...') print('bank.nextPositionID=', bank.nextPositionID()) print('goblin.health()=', goblin.health(1)) print('goblin.shares[]=', goblin.shares(1)) print('goblin.shareToBalance(goblin.shares[])=', goblin.shareToBalance(goblin.shares(1)))
def main(): admin = accounts[0] alice = accounts[1] triple_slope_model = TripleSlopeModel.deploy({'from': admin}) # min debt 0.2 BNB at 10 gwei gas price (killBps 5% -> at least 0.01BNB bonus) # reserve pool bps 1000 (10%) # kill bps 500 (5%) bank_config = ConfigurableInterestBankConfig.deploy( 2 * 10**17, 1000, 500, triple_slope_model, {'from': admin}) proxy_admin = ProxyAdminImpl.deploy({'from': admin}) bank_impl = Bank.deploy({'from': admin}) bank = TransparentUpgradeableProxyImpl.deploy( bank_impl, proxy_admin, bank_impl.initialize.encode_input(bank_config), {'from': admin}) bank = interface.IAny(bank) ################################################################### # deposit & withdraw deposit_amt = 10**18 prevBNBBal = alice.balance() prevIbBNBBal = bank.balanceOf(alice) bank.deposit({'from': alice, 'value': deposit_amt}) curBNBBal = alice.balance() curIbBNBBal = bank.balanceOf(alice) print('∆ bnb alice', curBNBBal - prevBNBBal) print('∆ ibBNB alice', curIbBNBBal - prevIbBNBBal) assert curBNBBal - prevBNBBal == -(curIbBNBBal - prevIbBNBBal), 'first depositor should get 1:1' assert curBNBBal - prevBNBBal == -deposit_amt # withdraw 1/3 prevBNBBal = alice.balance() prevIbBNBBal = bank.balanceOf(alice) bank.withdraw(curIbBNBBal // 3, {'from': alice}) curBNBBal = alice.balance() curIbBNBBal = bank.balanceOf(alice) print('∆ bnb alice', curBNBBal - prevBNBBal) print('∆ ibBNB alice', curIbBNBBal - prevIbBNBBal) assert curBNBBal - prevBNBBal == -(curIbBNBBal - prevIbBNBBal), 'first depositor should get 1:1' assert curBNBBal - prevBNBBal == deposit_amt // 3 # withdraw remaining prevBNBBal = alice.balance() prevIbBNBBal = bank.balanceOf(alice) bank.withdraw(curIbBNBBal, {'from': alice}) curBNBBal = alice.balance() curIbBNBBal = bank.balanceOf(alice) print('∆ bnb alice', curBNBBal - prevBNBBal) print('∆ ibBNB alice', curIbBNBBal - prevIbBNBBal) assert curBNBBal - prevBNBBal == -(curIbBNBBal - prevIbBNBBal), 'first depositor should get 1:1' assert curBNBBal - prevBNBBal == deposit_amt - deposit_amt // 3
def deploy_pools(deployer, bank, add_strat, liq_strat, rem_strat, bank_config, goblin_config, oracle, pools): wht = interface.IAny(wht_address) registry = {} for pool in pools: print('==============================') print('deploying pool', pool['name']) fToken = interface.IAny(pool['token']) if pool['pid'] == 1: # reinvest 0.3% (avg gas fee ~0.006 BNB) print('deploying PancakeswapPool1Goblin ...') goblin = PancakeswapPool1Goblin.deploy(bank, chef_address, router_address, add_strat, liq_strat, 30, { 'from': deployer, 'gas_price': 1000000000 }) else: # reinvest 0.3% (avg gas fee ~0.006 BNB) print('deploying PancakeswapGoblin ...') goblin = PancakeswapGoblin.deploy(bank, chef_address, router_address, pool['pid'], add_strat, liq_strat, 30, { 'from': deployer, 'gas_price': 1000000000 }) print('goblin_config.setConfigs ...') goblin_config.setConfigs([goblin], [pool['goblinConfig']], { 'from': deployer, 'gas_price': 1000000000 }) print('deploying StrategyAddTwoSidesOptimal ...') add_strat_2 = StrategyAddTwoSidesOptimal.deploy( router_address, goblin, fToken, { 'from': deployer, 'gas_price': 1000000000 }) print('goblin.setStrategyOk ...') goblin.setStrategyOk([add_strat_2, rem_strat], True, { 'from': deployer, 'gas_price': 1000000000 }) print('bank_config.setGoblins ...') bank_config.setGoblins([goblin], [goblin_config], { 'from': deployer, 'gas_price': 1000000000 }) # re-assign two side strat as add strat for pool 1 goblin if pool['pid'] == 1: print('goblin.setCriticalStrategies ...') goblin.setCriticalStrategies(add_strat_2, liq_strat, { 'from': deployer, 'gas_price': 1000000000 }) print('goblin.setStrategyOk ...') goblin.setStrategyOk([add_strat], False, { 'from': deployer, 'gas_price': 1000000000 }) # unset add_strat registry[pool['name']] = { 'goblin': goblin, 'two_side': add_strat_2, 'token': fToken.address } print('registry[', pool["name"], ']:', registry[pool['name']]) return registry