def try_update_price_agent(self, new_price_agent): if (self.state == self.states["PreFunding"] or self.state == self.states["Funding"]): succeeds("Update price agent succeeds", super().update_price_agent(new_price_agent)) self.priceAgent = new_price_agent else: fails("Update price agent fails", super().update_price_agent(new_price_agent))
def try_set_minimum_buy_value(self, new_minimum): if (self.state == self.states["PreFunding"] or self.state == self.states["Funding"]): succeeds("Set minimum buy value succeeds", super().set_minimum_buy_value(new_minimum)) self.minimum_buy_value = new_minimum else: fails("Set minimum buy value fails", super().set_minimum_buy_value(new_minimum))
def try_update_eurs_per_eth(self, milieurs_amount, price_agent): if (self.state == self.states["PreFunding"] or self.state == self.states["Funding"]) and self.priceAgent == price_agent: succeeds("Update eurs per eth succeeds", super().update_eurs_per_eth(milieurs_amount)) self.milieurs_per_eth = milieurs_amount else: fails("Update eurs per eth fails", super().update_eurs_per_eth(milieurs_amount))
def try_set_starting_time(self, starting_time): if self.state == self.states["PreFunding"] and int(round(time.time( ))) < starting_time and starting_time < super().ends_at(): succeeds("Set starting time succeeds", super().set_starting_time(starting_time)) self.startsAt = starting_time else: fails("Set starting time fails", super().set_starting_time(starting_time))
def try_set_ending_time(self, ending_time): if (self.state == self.states["PreFunding"] or self.state == self.states["Funding"]) and int(round(time.time( ))) < ending_time and super().starts_at() < ending_time: succeeds("Set starting time succeeds", super().set_ending_time(ending_time)) self.endsAt = ending_time else: fails("Set starting time fails", super().set_ending_time(ending_time))
def try_finalize(self): if self.state == self.states["Success"] and not self.halted: succeeds("Finalization of Crowdsale succeeds", super().finalize()) self.state = self.states["Finalized"] sold = self.tokens_sold_var + self.bounties_tokens to_share = (sold * 18) // 82 self.multisig_tokens += to_share print(self.multisig_tokens) print(self.token_balance(self.multisig_wallet())) self.check_all_end_balances() else: fails("Finalization of Crowdsale fails", super().finalize())
def try_preallocate(self): for investor in self.investors: tx_receipt = super().preallocate(investor.address, self.tokens_to_preallocate, self.wei_price_of_preallocation) if self.state == self.states[ "PreFunding"] or self.state == self.states["Funding"]: succeeds("Preallocate succeeds", tx_receipt) token_amount = self.tokens_to_preallocate * (10**18) self.token_balances[investor.address] += token_amount self.tokens_sold_var += token_amount else: fails("Preallocate fails", tx_receipt)
def buy_with_customer_id(self, buyer): tx_receipt = super().buy_with_customer_id(buyer.customerId, buyer.address, self.investment) if self.halted: fails("Buying using buyOnBehalfWithCustomerId fails if halted", tx_receipt) elif buyer.customerId == 0: fails( "Buying using buyOnBehalfWithCustomerId fails with invalid customer ID", tx_receipt) elif (self.states["PreFunding"] == self.state and not buyer.whitelisted): fails( "Buying using buyOnBehalfWithCustomerId fails if in PreFunding state and buyer isn't whitelisted", tx_receipt) elif (self.states["PreFunding"] != self.state and self.states["Funding"] != self.state): fails( "Buying using buyOnBehalfWithCustomerId fails if not in PreFunding nor Funding state", tx_receipt) else: succeeds("Buying using buyOnBehalfWithCustomerId succeeds", tx_receipt) (wei_amount, token_amount) = self.calculate_token_amount( self.investment * (10**18), buyer.address) self.multisig_wei += wei_amount print(self.multisig_wei) print(self.web3.eth.getBalance(super().multisig_wallet())) self.token_balances[buyer.address] += token_amount self.tokens_sold_var += token_amount
def send_ether(self, buyer): tx_receipt = super().send_ether_to_crowdsale(buyer.address, self.investment) if self.halted: fails("Sending ether to crowdsale fails if halted", tx_receipt) elif self.requiredCustomerId: fails("Sending ether to crowdsale fails with requiredCustomerId", tx_receipt) elif (self.states["PreFunding"] == self.state and not buyer.whitelisted): fails( "Sending ether to crowdsale fails if in PreFunding state and buyer isn't whitelisted", tx_receipt) elif (self.states["PreFunding"] != self.state and self.states["Funding"] != self.state): fails( "Sending ether to crowdsale fails if not in PreFunding nor Funding state", tx_receipt) else: succeeds("Sending ether to crowdsale succeeds", tx_receipt) (wei_amount, token_amount) = self.calculate_token_amount( self.investment * (10**18), buyer.address) self.multisig_wei += wei_amount print(self.multisig_wei) print(self.web3.eth.getBalance(super().multisig_wallet())) self.token_balances[buyer.address] += token_amount self.tokens_sold_var += token_amount
def try_configuration_crowdsale(self): if self.state == self.states["PendingConfiguration"]: succeeds("Configuration of Crowdsale succeeds", super().configuration_crowdsale()) self.state = self.states["PreFunding"] self.startsAt = super().starts_at() self.endsAt = super().ends_at() self.bounties_tokens = self.multisig_tokens = super( ).initial_bounties_tokens() self.token = self.instantiate_token() print(self.multisig_tokens) print(self.token_balance(self.multisig_wallet())) assert self.token_balance( super().multisig_wallet()) == 252 * (10**5) * (10**18) assert self.token_balance( self.contract.address) == 1008 * (10**6) * (10**18) assert self.token_balance( super().multisig_wallet()) == self.params["multisig_supply"] assert self.token_balance( self.contract.address) == self.params["crowdsale_supply"] else: fails("Configuration of Crowdsale fails", super().configuration_crowdsale()) print("ETA for ICO: " + str(super().eta_ico() + 1) + " seconds.")
(token, deployment_hash) = deployer.deploy( "./build/", "ReleasableToken", tx, ) #EXAMPLE succeeds("Unhalt succeeds", haltable_contract.functions.unhalt().transact(tx)) succeeds("ReleasableToken construction shouldn't fail.", deployment_hash) assert token.functions.released().call() == False, "Shouldn't start released" fails( "Shouldn't allow transfers from a non transfer agent", token.functions.transfer(random_receiver(), random_contribution()).transact( tx_from(non_transfer_agent))) fails( "Shouldn't allow extracting funds from a non transfer agent", token.functions.transferFrom(non_transfer_agent, random_receiver(), random_contribution()).transact( tx_from(non_transfer_agent))) fails( "Should fail if non owner tries to set a transfer agent", token.functions.setTransferAgent(transfer_agent, True).transact(tx_from(transfer_agent))) assert token.functions.transferAgents(transfer_agent).call(
receipt = web3.eth.waitForTransactionReceipt(tx_hash) assert receipt.status == 1 functions = mintable_token_contract.functions def get_minting_finished(): return functions.mintingFinished().call() def get_mint_agent(address): return functions.mintAgents(address).call() def mint(receiver, amount, tx_args): return functions.mint(receiver, amount).transact(tx_args) def set_mint_agent(address, state, tx_args): return functions.setMintAgent(address, state).transact(tx_args) tx['from'] = accounts[1] fails("setMintAgent with account 1 and account 1 as sender: fails because it's the wrong owner", set_mint_agent(accounts[1], True, tx)) tx['from'] = accounts[0] succeeds("setMintAgent with account 1 and account 0 as sender: succeeds because it's the right owner", set_mint_agent(accounts[1], True, tx)) fails("mint to account 2 and account 0 as sender: fails because it's the wrong mint agent", mint(accounts[2], to_mint, tx)) tx['from'] = accounts[1] succeeds("mint to account 2 and account 1 as sender: succeeds because it's the right mint agent", mint(accounts[2], to_mint, tx)) tx['from'] = accounts[0] (mintable_token_contract, tx_hash) = deployer.deploy("./build/", "MintableTokenMock", tx, config['multisig_supply'], config['MW_address'], False) receipt = web3.eth.waitForTransactionReceipt(tx_hash) assert receipt.status == 1 functions = mintable_token_contract.functions
def sub_approval(allower, spender, value): old = allowed[allower][spender] if value > old: allowed[allower][spender] = 0 else: allowed[allower][spender] = old - value def test_balances(): assert total_supply == get_total_supply() for x in accounts: assert token_balances[x] == balance_of(x) for n in accounts: for m in accounts: assert allowed[n][m] == allowance(n, m) tx["from"] = accounts[3] fails("Transfer 10000 from account 1 to account 2 with account 3: Fails because account 1 has no tokens", functions.transferFrom(accounts[1], accounts[2], value_transfer).transact(tx)) tx["from"] = accounts[1] fails("Transfer 10000 from account 1 to account 2: Fails because account 1 has no tokens", functions.transfer(accounts[2], value_transfer).transact(tx)) fails("Burn 10000 from account 1: Fails because account 1 has no tokens", functions.burnTokensMock(accounts[1], value_transfer).transact(tx)) succeeds("subApproval of 10000 from account 1 to account 2: Succeeds because it has no restrictions", functions.subApproval(accounts[2], value_transfer).transact(tx)) sub_approval(accounts[1], accounts[2], value_transfer) succeeds("addApproval of 10000 from account 1 to account 2: Succeeds because it has no restrictions", functions.addApproval(accounts[2], value_transfer).transact(tx)) allowed[accounts[1]][accounts[2]] += value_transfer fails("Approve 10000 from account 1 to account 2: Fails because neither value or allowance are zero", functions.approve(accounts[2], value_transfer).transact(tx)) succeeds("Mint 100000 to account 1: Succeeds because mint has no restrictions", functions.mint(accounts[1], value_mint).transact(tx))
"./build/", "LostAndFoundTokenMock", tx, ) receipt = web3.eth.waitForTransactionReceipt(tx_hash) assert receipt.status == 1 functions = lost_and_found_token_contract.functions def get_master(): return functions.master().call() def enable_lost_and_found(agent, tokens, token_contract, tx_args): return functions.enableLostAndFound(agent, tokens, token_contract).transact(tx_args) assert get_master() == sender tx['from'] = accounts[1] fails( "enableLostAndFound with account 1 as sender: fails because it's the wrong master", enable_lost_and_found(agent_to_approve, tokens_to_approve, standard_token_mock_contract.address, tx)) tx['from'] = accounts[0] succeeds( "enableLostAndFound with account 0 as sender: succeeds because it's the right master", enable_lost_and_found(agent_to_approve, tokens_to_approve, standard_token_mock_contract.address, tx))
tx_hash) = deployer.deploy("./build/", "CrowdsaleToken", tx, config["multisig_supply"], config["token_decimals"], config["MW_address"], config["token_retriever_account"]) receipt = web3.eth.waitForTransactionReceipt(tx_hash) assert receipt.status == 1 functions = crowdsale_token_contract.functions def can_upgrade(): return functions.canUpgrade().call() def release_token_transfer(tx_args): return functions.releaseTokenTransfer().transact(tx_args) assert functions.name().call() == "BurgerKoenig" assert functions.symbol().call() == "BK" assert functions.decimals().call() == config["token_decimals"] assert functions.lost_and_found_master().call( ) == config["token_retriever_account"] assert not can_upgrade() fails("releaseTokenTransfer fails because sender isn't the releaseAgent.", release_token_transfer(tx)) succeeds("setReleaseAgent succeeds because the sender is the owner.", functions.setReleaseAgent(accounts[1]).transact(tx)) tx["from"] = accounts[1] succeeds("releaseTokenTransfer succeeds because sender is the releaseAgent.", release_token_transfer(tx)) assert can_upgrade()
def can_upgrade(): return functions.canUpgrade().call() def upgrade_agent(): return functions.upgradeAgent().call() def total_upgraded(): return functions.totalUpgraded().call() print("Testing changeUpgradeMaster") fails("changeUpgradeMaster fails calling with wrong master sender", functions.changeUpgradeMaster(newmaster).transact(txnewmaster)) assert upgrade_master() == master fails( "changeUpgradeMaster fails setting address_zero and correct master sender", functions.changeUpgradeMaster(address_zero).transact(txmaster)) assert upgrade_master() == master succeeds( "changeUpgradeMaster succeeds with correct master sender and setting new master", functions.changeUpgradeMaster(newmaster).transact(txmaster)) assert upgrade_master() == newmaster succeeds( "changeUpgradeMaster succeeds with correct master sender and setting old master", functions.changeUpgradeMaster(master).transact(txnewmaster)) assert upgrade_master() == master assert can_upgrade()