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
Exemple #2
0
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})
Exemple #3
0
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 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})
Exemple #5
0
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})
Exemple #6
0
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 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():
    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
Exemple #9
0
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