def test_buy_tokens(open_crowdsale: Contract, token: Contract, customer: str, beneficiary: str, web3: Web3): """Sending ETH successfully buys tokens.""" web3.eth.sendTransaction({ "from": customer, "to": open_crowdsale.address, "value": to_wei(20, "ether"), "gas": 250000, }) # We get ERC-20 event events = token.pastEvents("Transfer").get() assert len(events) == 1 e = events[0] assert e["args"]["to"] == customer assert e["args"]["from"] == beneficiary assert e["args"]["value"] == 24000 # We get crowdsale event events = open_crowdsale.pastEvents("FundTransfer").get() assert len(events) == 1 e = events[0] assert e["args"]["backer"] == customer assert e["args"]["amount"] == to_wei(20, "ether") assert e["args"]["amountRaised"] == to_wei(20, "ether")
def test_buy_one_investor(chain: TestRPCChain, web3: Web3, ico: Contract, uncapped_token: Contract, customer: str, preico_token_price, preico_starts_at, team_multisig): """Can buy when crowdsale is running.""" original_balance = web3.eth.getBalance(team_multisig) wei_value = to_wei(1, "ether") buys_tokens = wei_value // preico_token_price assert buys_tokens > 0 time_travel(chain, preico_starts_at + 1) assert ico.call().getState() == CrowdsaleState.Funding assert ico.call().investorCount() == 0 assert ico.call().investedAmountOf(customer) == 0 ico.transact({"from": customer, "value": wei_value}).buy() # # See everything was correctly credited # # Tokens on every account assert uncapped_token.call().balanceOf(customer) == buys_tokens assert uncapped_token.call().totalSupply() == buys_tokens assert ico.call().tokensSold() == buys_tokens assert ico.call().investorCount() == 1 # Ether on every account assert ico.call().weiRaised() == wei_value assert ico.call().investedAmountOf(customer) == wei_value balance_diff = web3.eth.getBalance(team_multisig) - original_balance assert balance_diff == wei_value # Investors assert ico.call().investorCount() == 1 # # Events # # Crowdsale events = ico.pastEvents("Invested").get() assert len(events) == 1 e = events[0] assert e["args"]["investor"] == customer assert e["args"]["weiAmount"] == wei_value assert e["args"]["tokenAmount"] == buys_tokens # ERC-20 events = uncapped_token.pastEvents("Transfer").get() assert len(events) == 1 e = events[0] assert e["args"]["from"] == "0x0000000000000000000000000000000000000000" assert e["args"]["to"] == customer assert e["args"]["value"] == buys_tokens
def test_buy_reach_goal(chain: TestRPCChain, flat_pricing, ico: Contract, customer: str, preico_starts_at, preico_ends_at, preico_funding_goal): """Goal can be reached with a sufficient investment.""" time_travel(chain, preico_starts_at + 1) wei_value = preico_funding_goal # Check that we don't have issues with our pricing assert flat_pricing.call().calculatePrice(wei_value, 0, 0, customer, 0) > 0 ico.transact({"from": customer, "value": wei_value}).buy() # We got investment event events = ico.pastEvents("Invested").get() assert len(events) == 1 e = events[0] assert e["args"]["investor"] == customer assert e["args"]["weiAmount"] == wei_value assert ico.call().weiRaised() == wei_value assert ico.call().weiRaised() == ico.call().minimumFundingGoal() assert ico.call().isMinimumGoalReached() time_travel(chain, preico_ends_at + 1) assert ico.call().getState() == CrowdsaleState.Success
def test_buy_too_many_tokens(open_crowdsale: Contract, token: Contract, customer: str, beneficiary: str, web3: Web3): """One cannot out buy the maximum token allocation.""" buy_everything_amount = open_crowdsale.call().getPrice( ) * open_crowdsale.call().maxGoal() web3.eth.sendTransaction({ "from": customer, "to": open_crowdsale.address, "value": buy_everything_amount, "gas": 200000, }) events = token.pastEvents("Transfer").get() assert len(events) == 1 e = events[0] assert e["args"]["to"] == customer assert e["args"]["from"] == beneficiary assert e["args"]["value"] == open_crowdsale.call().maxGoal() # Cannot buy a single token more with pytest.raises(TransactionFailed): web3.eth.sendTransaction({ "from": customer, "to": open_crowdsale.address, "value": open_crowdsale.call().getPrice(), # Minimum value to buy one token "gas": 200000, })
def test_buy_two_investors(chain: TestRPCChain, web3: Web3, ico: Contract, uncapped_token: Contract, customer: str, customer_2, preico_token_price, preico_starts_at, team_multisig): """Two different customers buy in.""" original_balance = web3.eth.getBalance(team_multisig) wei_value = to_wei(1, "ether") buys_tokens = wei_value // preico_token_price assert buys_tokens > 0 time_travel(chain, preico_starts_at + 1) # Buy twice ico.transact({"from": customer, "value": wei_value}).buy() ico.transact({"from": customer_2, "value": wei_value}).buy() # # See everything was correctly credited # # Tokens on every account assert uncapped_token.call().balanceOf(customer) == buys_tokens assert uncapped_token.call().totalSupply() == buys_tokens * 2 assert ico.call().tokensSold() == buys_tokens * 2 # Ether on every account assert ico.call().weiRaised() == wei_value * 2 assert ico.call().investedAmountOf(customer) == wei_value balance_diff = web3.eth.getBalance(team_multisig) - original_balance assert balance_diff == wei_value * 2 # Investors assert ico.call().investorCount() == 2 # # Events # # Crowdsale events = ico.pastEvents("Invested").get() assert len(events) == 2 # ERC-20 events = uncapped_token.pastEvents("Transfer").get() assert len(events) == 2
def test_buy_more_tokens(open_crowdsale: Contract, token: Contract, customer: str, beneficiary: str, web3: Web3): """User can buy more tokens.""" initial_balance = token.call().balanceOf(beneficiary) web3.eth.sendTransaction({ "from": customer, "to": open_crowdsale.address, "value": to_wei(20, "ether"), "gas": 250000, }) web3.eth.sendTransaction({ "from": customer, "to": open_crowdsale.address, "value": to_wei(20, "ether"), "gas": 250000, }) events = token.pastEvents("Transfer").get() assert len(events) == 2 for e in events: assert e["args"]["to"] == customer assert e["args"]["from"] == beneficiary assert e["args"]["value"] == 24000 assert token.call().balanceOf(beneficiary) == initial_balance - 24000 * 2 assert token.call().balanceOf(customer) == 24000 * 2 # We get crowdsale event events = open_crowdsale.pastEvents("FundTransfer").get() assert len(events) == 2 e = events[-1] assert e["args"]["backer"] == customer assert e["args"]["amount"] == to_wei(20, "ether") assert e["args"]["amountRaised"] == to_wei(20, "ether") * 2
def assert_last_tokens_minted(contract: Contract, recipient, tokens): event = contract.pastEvents('TokensMinted').get()[-1]['args'] assert event['recipient'] == recipient assert event['tokens'] == tokens