def rule_create_lock(self, st_account, st_value, st_lock_duration): unlock_time = (rpc.time() + st_lock_duration * WEEK) // WEEK * WEEK if st_value == 0: with brownie.reverts("dev: need non-zero value"): self.voting_escrow.create_lock(st_value, unlock_time, {'from': st_account}) elif self.voting_balances[st_account]['value'] > 0: with brownie.reverts("Withdraw old tokens first"): self.voting_escrow.create_lock(st_value, unlock_time, {'from': st_account}) elif unlock_time <= rpc.time(): with brownie.reverts("Can only lock until time in the future"): self.voting_escrow.create_lock(st_value, unlock_time, {'from': st_account}) elif unlock_time > rpc.time() + 86400 * 365 * 4: with brownie.reverts("Voting lock can be 4 years max"): self.voting_escrow.create_lock(st_value, unlock_time, {'from': st_account}) else: tx = self.voting_escrow.create_lock(st_value, unlock_time, {'from': st_account}) self.voting_balances[st_account] = { 'value': st_value, 'unlock_time': tx.events['Deposit']['locktime'] }
def test_time_exceptions(monkeypatch): with pytest.raises(TypeError): rpc.sleep("foo") with pytest.raises(TypeError): rpc.sleep(3.0) monkeypatch.setattr('brownie.rpc.is_active', lambda: False) with pytest.raises(SystemError): rpc.time()
def test_time(): assert rpc.time() == int(time.time()) rpc.sleep(25) rpc.snapshot() rpc.sleep(75) assert rpc.time() == int(time.time() + 100) rpc.revert() assert rpc.time() == int(time.time() + 25)
def _check(options, id1, sorted_totals, at_price, get_options): for i in range(len(sorted_totals) - 1): now = int(rpc.time() // 2592000 + 1) * 2592000 rpc.sleep(now - rpc.time() - 5) rpc.mine() assert options.getSortedTotals() == sorted_totals[i] assert options.getTotalOptionsAtPrice(10) == at_price[i] assert options.getOptions(id1) == get_options[i] rpc.sleep(10) rpc.mine() assert options.getSortedTotals() == sorted_totals[i + 1] assert options.getTotalOptionsAtPrice(10) == at_price[i + 1] assert options.getOptions(id1) == get_options[i + 1]
def rule_increase_amount(self, st_account, st_value): if st_value == 0: with brownie.reverts("dev: need non-zero value"): self.voting_escrow.increase_amount(st_value, { 'from': st_account, 'gas': GAS_LIMIT }) elif self.voting_balances[st_account]['value'] == 0: with brownie.reverts("No existing lock found"): self.voting_escrow.increase_amount(st_value, { 'from': st_account, 'gas': GAS_LIMIT }) elif self.voting_balances[st_account]['unlock_time'] <= rpc.time(): with brownie.reverts("Cannot add to expired lock. Withdraw"): self.voting_escrow.increase_amount(st_value, { 'from': st_account, 'gas': GAS_LIMIT }) else: self.voting_escrow.increase_amount(st_value, { 'from': st_account, 'gas': GAS_LIMIT }) self.voting_balances[st_account]['value'] += st_value
def test_time(nft): """Block transfers with range time lock""" nft.mint(accounts[1], 10000, rpc.time() + 20, "0x00", {"from": accounts[0]}) with pytest.reverts(): nft.transfer(accounts[2], 1000, {"from": accounts[1]}) rpc.sleep(21) nft.transfer(accounts[2], 1000, {"from": accounts[1]})
def test_cancel_upgrade_without_enough_original_reserve( goldx, new_paxg, paxg, rpc, accounts): print( f'---------------- start to cancel upgrade without enough origianl reserve----------------\n' ) paxg.approve(goldx.address, max_allowance, {'from': accounts[1]}) goldx.mint(accounts[1], 100.001e18, {'from': accounts[1]}) print(f'previous unit is: {goldx.unit()}') interval = 60 upgradeTime = rpc.time() + interval upgradeToken = new_paxg.address upgradeUint = 1.05e18 upgradeMinMintAmount = 0.001e18 upgradeMinBurnAmount = 0.001e18 goldx.upgradeProtocol(upgradeTime, upgradeToken, upgradeUint, upgradeMinMintAmount, upgradeMinBurnAmount) rpc_delay(rpc, interval + 1) goldx.removeReserve() should_add_new_reserve_amount = goldx.getOutstanding( new_paxg.address, upgradeUint) new_paxg.transfer( goldx.address, (should_add_new_reserve_amount + 10**new_paxg.decimals())) with brownie.reverts('cancelUpgrade: Add more current anchored asset!'): goldx.cancelUpgrade()
def test_cancel_upgrade_without_enough_new_reserve(goldx, new_paxg, paxg, rpc, accounts): print( f'---------------- start to cancel upgrade without enough new reserve ----------------\n' ) paxg.approve(goldx.address, max_allowance, {'from': accounts[1]}) goldx.mint(accounts[1], 100e18, {'from': accounts[1]}) goldx.burn(accounts[1], 50e18, {'from': accounts[1]}) interval = 60 upgradeTime = rpc.time() + interval upgradeToken = new_paxg.address upgradeUint = 1.05e18 upgradeMinMintAmount = 0.001e18 upgradeMinBurnAmount = 0.001e18 goldx.upgradeProtocol(upgradeTime, upgradeToken, upgradeUint, upgradeMinMintAmount, upgradeMinBurnAmount) rpc_delay(rpc, interval + 1) goldx.removeReserve() with brownie.reverts('cancelUpgrade: Add more current anchored asset!'): goldx.cancelUpgrade() print( f'---------------- end to cancel upgrade without enough new reserve ----------------\n' )
def test_confirm_upgrade_when_do_not_reach_time(goldx, new_paxg, paxg, rpc, accounts): print( f'---------------- start to confirm upgrade but does not reach time ----------------\n' ) paxg.approve(goldx.address, max_allowance, {'from': accounts[1]}) goldx.mint(accounts[1], 100e18, {'from': accounts[1]}) goldx.burn(accounts[1], 50e18, {'from': accounts[1]}) with brownie.reverts('confirmUpgrade: Too early to confirm upgrading!'): goldx.confirmUpgrade() interval = 60 upgradeTime = rpc.time() + interval upgradeToken = new_paxg.address upgradeUint = 1.05e18 upgradeMinMintAmount = 0.001e18 upgradeMinBurnAmount = 0.001e18 goldx.upgradeProtocol(upgradeTime, upgradeToken, upgradeUint, upgradeMinMintAmount, upgradeMinBurnAmount) with brownie.reverts('confirmUpgrade: Too early to confirm upgrading!'): goldx.confirmUpgrade() print(f'owner is going to confirm upgrade but does not reach time') print( f'---------------- end to confirm upgrade but does not reach time ----------------\n' )
def test_time_partial(nft): """Partially block a transfer with range time lock""" nft.mint(accounts[1], 10000, 0, "0x00", {"from": accounts[0]}) nft.modifyRanges(102001, 106001, rpc.time() + 20, "0x00", {"from": accounts[0]}) assert nft.getRange(102001)["_stop"] == 106001 nft.transfer(accounts[2], 4000, {"from": accounts[1]}) assert nft.rangesOf(accounts[1]) == ((102001, 106001), (108001, 110001)) rpc.sleep(25) nft.transfer(accounts[2], 6000, {"from": accounts[1]}) assert nft.rangesOf(accounts[2]) == ((100001, 110001),)
def dutch_auction(PetylDutchAuction, auction_factory, auction_token): startDate = rpc.time() +10 endDate = startDate + AUCTION_TIME wallet = accounts[1] tx = auction_token.approve(auction_factory, AUCTION_TOKENS, {'from': accounts[0]}) tx = auction_factory.deployDutchAuction(auction_token, AUCTION_TOKENS, startDate, endDate,ETH_ADDRESS, AUCTION_START_PRICE, AUCTION_RESERVE, wallet, {"from": accounts[0]}) dutch_auction = PetylDutchAuction.at(tx.return_value) assert dutch_auction.clearingPrice() == AUCTION_START_PRICE rpc.sleep(10) return dutch_auction
def rule_increase_unlock_time(self, st_account, st_lock_duration): unlock_time = (rpc.time() + st_lock_duration * WEEK) // WEEK * WEEK if self.voting_balances[st_account]['unlock_time'] <= rpc.time(): with brownie.reverts("Lock expired"): self.voting_escrow.increase_unlock_time( unlock_time, { 'from': st_account, 'gas': GAS_LIMIT }) elif self.voting_balances[st_account]['value'] == 0: with brownie.reverts("Nothing is locked"): self.voting_escrow.increase_unlock_time( unlock_time, { 'from': st_account, 'gas': GAS_LIMIT }) elif unlock_time <= self.voting_balances[st_account]['unlock_time']: with brownie.reverts("Can only increase lock duration"): self.voting_escrow.increase_unlock_time( unlock_time, { 'from': st_account, 'gas': GAS_LIMIT }) elif unlock_time > rpc.time() + 86400 * 365 * 4: with brownie.reverts("Voting lock can be 4 years max"): self.voting_escrow.increase_unlock_time( unlock_time, { 'from': st_account, 'gas': GAS_LIMIT }) else: tx = self.voting_escrow.increase_unlock_time( unlock_time, { 'from': st_account, 'gas': GAS_LIMIT }) self.voting_balances[st_account]['unlock_time'] = tx.events[ 'Deposit']['locktime']
def dutch_auction(DutchSwapAuction, auction_token): startDate = rpc.time() +10 endDate = startDate + AUCTION_TIME wallet = accounts[1] dutch_auction = DutchSwapAuction.deploy({'from': accounts[0]}) dutch_auction.initDutchAuction(auction_token, AUCTION_TOKENS, startDate, endDate, AUCTION_START_PRICE, AUCTION_RESERVE, wallet, {"from": accounts[0]}) auction_token.setMintOperator(dutch_auction, True, {"from": accounts[0]}) assert dutch_auction.auctionPrice() == AUCTION_START_PRICE rpc.sleep(10) return dutch_auction
def rule_withdraw(self, st_account): """ Withdraw tokens from the voting escrow. """ if self.voting_balances[st_account]['unlock_time'] > rpc.time(): # fail path - before unlock time with brownie.reverts("The lock didn't expire"): self.voting_escrow.withdraw({'from': st_account}) else: # success path - specific amount self.voting_escrow.withdraw({'from': st_account}) self.voting_balances[st_account]['value'] = 0
def replace_underlying(goldx, new_paxg, paxg, rpc, accounts): interval = 60 upgradeTime = rpc.time() + interval upgradeToken = new_paxg.address upgradeUint = 1.05e18 upgradeMinMintAmount = 0.001e18 upgradeMinBurnAmount = 0.001e18 goldx.upgradeProtocol(upgradeTime, upgradeToken, upgradeUint, upgradeMinMintAmount, upgradeMinBurnAmount) rpc_delay(rpc, interval + 1) goldx.removeReserve() should_add_new_reserve_amount = goldx.getOutstanding( new_paxg.address, upgradeUint) new_paxg.transfer( goldx.address, (should_add_new_reserve_amount + 10**new_paxg.decimals())) goldx.confirmUpgrade()
def test_cancel_upgrade_when_upgrade_protocol(goldx, new_paxg, paxg, rpc, accounts): print( f'---------------- start to cancel upgrade when upgradeprotocol ----------------\n' ) paxg.approve(goldx.address, max_allowance, {'from': accounts[1]}) goldx.mint(accounts[1], 100e18, {'from': accounts[1]}) goldx.burn(accounts[1], 50e18, {'from': accounts[1]}) interval = 60 upgradeTime = rpc.time() + interval upgradeToken = new_paxg.address upgradeUint = 1.05e18 upgradeMinMintAmount = 0.001e18 upgradeMinBurnAmount = 0.001e18 goldx.upgradeProtocol(upgradeTime, upgradeToken, upgradeUint, upgradeMinMintAmount, upgradeMinBurnAmount) goldx.cancelUpgrade() print( f'---------------- end to cancel upgrade when upgradeprotocol ----------------\n' )
def test_upgrade_protocol_with_upgrade_token_is_zero_address( goldx, new_paxg, paxg, rpc, accounts): print( f'---------------- start to upgrade protocol with tokne is zero address ----------------\n' ) paxg.approve(goldx.address, max_allowance, {'from': accounts[1]}) goldx.mint(accounts[1], 100e18, {'from': accounts[1]}) goldx.burn(accounts[1], 50e18, {'from': accounts[1]}) interval = 60 upgradeTime = rpc.time() + interval upgradeToken = zero_address upgradeUint = 1.05e18 upgradeMinMintAmount = 0.001e18 upgradeMinBurnAmount = 0.001e18 print(f'owner is going to upgrade with tokne is zero address') with brownie.reverts( 'upgradeProtocol: New anchored asset should not be zero address!'): goldx.upgradeProtocol(upgradeTime, upgradeToken, upgradeUint, upgradeMinMintAmount, upgradeMinBurnAmount) print( f'---------------- end to upgrade protocol with tokne is zero address ----------------\n' )
def frames_crowdsale(DreamFramesCrowdsale, frame_token, price_feed, bonus_list): wallet = accounts[9] startDate = rpc.time() endDate = startDate + 50000 minFrames = 1 maxFrames = 1000000 producerFrames = 25000 frameUsd = '100 ether' bonusOffList = 30 bonusOnList = 40 hardCapUsd = '3000000 ether' softCapUsd = '1500000 ether' frames_crowdsale = DreamFramesCrowdsale.deploy({"from": accounts[0]}) frames_crowdsale.init(frame_token, price_feed, wallet, startDate, endDate, minFrames, maxFrames, producerFrames, frameUsd, bonusOffList, bonusOnList, hardCapUsd, softCapUsd, {"from": accounts[0]}) tx = frames_crowdsale.addOperator(accounts[1], {"from": accounts[0]}) assert 'OperatorAdded' in tx.events tx = frame_token.setMinter(frames_crowdsale, {"from": accounts[0]}) tx = frames_crowdsale.setBonusList(bonus_list, {"from": accounts[0]}) return frames_crowdsale
def cptime(): yield rpc.time() + 100
def test_set_checkpoint_time(cp, share): """set a checkpoint - time already passed""" with pytest.reverts("dev: time"): cp.newCheckpoint(share, rpc.time() - 100, {"from": accounts[0]}) with pytest.reverts("dev: time"): cp.newCheckpoint(share, rpc.time(), {"from": accounts[0]})
def test_upgrade_protocol(goldx, new_paxg, paxg, rpc, accounts): print(f'---------------- start to upgrade protocol ----------------\n') paxg.approve(goldx.address, max_allowance, {'from': accounts[1]}) check_diff(goldx, paxg, 'mint', 100.078e18, accounts[0], accounts[1]) check_diff(goldx, paxg, 'burn', 50.56909e18, accounts[0], accounts[1]) check_diff(goldx, new_paxg, 'transfer', 32.098e18, accounts[0], accounts[1], 0, accounts[2]) interval = 60 upgradeTime = rpc.time() + interval upgradeToken = new_paxg.address upgradeUint = 1.05e18 upgradeMinMintAmount = 0.001e18 upgradeMinBurnAmount = 0.001e18 print(f'owner is going to set upgrading config for contract') goldx.upgradeProtocol(upgradeTime, upgradeToken, upgradeUint, upgradeMinMintAmount, upgradeMinBurnAmount) rpc_delay(rpc, interval + 1) before_remove_goldx_amount = goldx.totalSupply() before_remove_owner_balance = paxg.balanceOf(accounts[0]) before_remove_contract_reserve = paxg.balanceOf(goldx.address) transfer_paxg_fee = paxg.getFeeFor(before_remove_contract_reserve) print(f'owner is going to remove reserve of the contract\n') goldx.removeReserve() after_remove_owner_balance = paxg.balanceOf(accounts[0]) after_remove_contract_reserve = paxg.balanceOf(goldx.address) assert after_remove_contract_reserve == 0 # actually paxg and goldx should be the different owner, that is the owner should not get paxg transfering fee, # so it should be: assert after_remove_owner_balance == before_remove_owner_balance + before_remove_contract_reserve should_add_new_reserve_amount = goldx.getOutstanding( new_paxg.address, upgradeUint) decimal_diff = goldx.decimals() - new_paxg.decimals() expected_add_new_reserve_amount = div( rdiv(before_remove_goldx_amount, upgradeUint), 10**decimal_diff) print( f'expected contract adds new reserve amount is: {expected_add_new_reserve_amount}\n' ) assert expected_add_new_reserve_amount == should_add_new_reserve_amount print(f'owner is going to add new reserve') new_paxg.transfer( goldx.address, (should_add_new_reserve_amount + 10**new_paxg.decimals())) print(f'owner is going to confirm upgrade') goldx.confirmUpgrade() new_paxg.approve(goldx.address, max_allowance, {'from': accounts[1]}) mint_amount = 100.123e18 check_diff(goldx, new_paxg, 'mint', mint_amount, accounts[0], accounts[1], upgradeUint) burn_amount = 10.0098e18 check_diff(goldx, new_paxg, 'burn', burn_amount, accounts[0], accounts[1], upgradeUint) transfer_amount = 10.459e18 check_diff(goldx, new_paxg, 'transfer', transfer_amount, accounts[0], accounts[1], upgradeUint, accounts[2]) goldx.approve(accounts[2], 100e18, {'from': accounts[1]}) goldx.transferFrom(accounts[1], accounts[2], 10e18, {'from': accounts[2]}) print(f'---------------- end to upgrade protocol ----------------\n')
def test_upgrade_protocol_without_lower_decimal_reserve( goldx, new_paxg_6, paxg, rpc, accounts): print( f'---------------- start to upgrade protocol with lower decimal reserve ----------------\n' ) paxg.approve(goldx.address, max_allowance, {'from': accounts[1]}) goldx.mint(accounts[1], 100.001e18, {'from': accounts[1]}) interval = 60 upgradeTime = rpc.time() + interval upgradeToken = new_paxg_6.address upgradeUint = 1.05e18 upgradeMinMintAmount = 0.001e6 upgradeMinBurnAmount = 0.001e6 goldx.upgradeProtocol(upgradeTime, upgradeToken, upgradeUint, upgradeMinMintAmount, upgradeMinBurnAmount) rpc_delay(rpc, interval + 1) goldx.removeReserve() should_add_new_reserve_amount = goldx.getOutstanding( new_paxg_6.address, upgradeUint) new_paxg_6.transfer( goldx.address, (should_add_new_reserve_amount + 10**new_paxg_6.decimals())) goldx.confirmUpgrade() new_paxg_6.approve(goldx.address, max_allowance, {'from': accounts[1]}) mint_amount = 100.123e6 check_diff(goldx, new_paxg_6, 'mint', mint_amount, accounts[0], accounts[1], upgradeUint, without_paxg_fee=1e18) burn_amount = 10.0098e6 check_diff(goldx, new_paxg_6, 'burn', burn_amount, accounts[0], accounts[1], upgradeUint, without_paxg_fee=1e18) transfer_amount = 10.459e6 check_diff(goldx, new_paxg_6, 'transfer', transfer_amount, accounts[0], accounts[1], upgradeUint, accounts[2], without_paxg_fee=1e18) goldx.approve(accounts[2], 100e6, {'from': accounts[1]}) goldx.transferFrom(accounts[1], accounts[2], 10.59493e6, {'from': accounts[2]})
def _sleep(months): now = int(rpc.time() // 2592000 + 1) * 2592000 rpc.sleep(now - rpc.time() + 1 + 2592000 * (months - 1)) rpc.mine()
def test_frames_crowdsale_setStartDate(frames_crowdsale): tx = frames_crowdsale.setStartDate(rpc.time()+100, {'from': accounts[0]}) assert 'StartDateUpdated' in tx.events with reverts("dev: Not owner"): frames_crowdsale.setStartDate(rpc.time()+100, {'from': accounts[3]})
def _months(months): return int(rpc.time() // 2592000 + 1) * 2592000 + months * 2592000