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_tokens_too_early(early_crowdsale: Contract, token: Contract, customer: str, beneficiary: str, web3: Web3): """One cannot participate to the crowdsale too early.""" with pytest.raises(TransactionFailed): web3.eth.sendTransaction({ "from": customer, "to": early_crowdsale.address, "value": to_wei(20, "ether"), "gas": 250000, })
def test_refund(open_crowdsale: Contract, token: Contract, customer: str, beneficiary: str, multisig: str, empty_address: str, web3: Web3, end: int): """Refunding failed ICO gives ETH back correctly.""" customer_original_balance = web3.eth.getBalance(customer) multisig_original_balance = web3.eth.getBalance(multisig) web3.eth.sendTransaction({ "from": customer, "to": open_crowdsale.address, "value": to_wei(20, "ether"), "gas": 250000, }) assert open_crowdsale.call().balanceOf(customer) == to_wei(20, "ether") assert web3.eth.getBalance(multisig) > multisig_original_balance + to_wei( 20, "ether") - 500000 # ICO ends open_crowdsale.transact().setCurrent(end + 1) finished_crowdsale = open_crowdsale # We did not finish finished_crowdsale.transact().checkGoalReached() assert not finished_crowdsale.call().fundingGoalReached() assert web3.eth.getBalance(customer) < customer_original_balance # Load balance back to the crowdsale web3.eth.sendTransaction({ "from": multisig, "to": finished_crowdsale.address, "value": web3.eth.getBalance(multisig) - 250000, "gas": 250000, }) # Customer 1 demands refund finished_crowdsale.transact({"from": customer}).safeWithdrawal() assert open_crowdsale.call().balanceOf(customer) == 0 assert web3.eth.getBalance(customer) > customer_original_balance - 500000
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 test_check_goal_not_reached(open_crowdsale: Contract, token: Contract, customer: str, beneficiary: str, web3: Web3, end: int): """Crowdsale may not reach its minimum funding goal.""" # Buy some tokens web3.eth.sendTransaction({ "from": customer, "to": open_crowdsale.address, "value": to_wei(20, "ether"), "gas": 250000, }) open_crowdsale.transact().setCurrent(end + 1) finished_crowdsale = open_crowdsale finished_crowdsale.transact().checkGoalReached() assert finished_crowdsale.call().fundingGoalReached() == False
def test_no_transfer_before_close(open_crowdsale: Contract, token: Contract, customer: str, beneficiary: str, empty_address: str, web3: Web3, end: int): """Buyer cannot transfer tokens before ICO is over.""" web3.eth.sendTransaction({ "from": customer, "to": open_crowdsale.address, "value": to_wei(20, "ether"), "gas": 250000, }) amount = 4000 with pytest.raises(TransactionFailed): token.transact({"from": customer}).transfer(empty_address, amount) token.transact().setCurrent(end+1) token.transact({"from": customer}).transfer(empty_address, amount) assert token.call().balanceOf(empty_address) == amount
def test_invalid_to_wei_values(value, unit): with pytest.raises(ValueError): to_wei(value, unit) with pytest.raises(ValueError): from_wei(value, unit)
def test_to_wei(value, expected): assert to_wei(*value) == decimal.Decimal(expected)
def test_conversion_rount_trip(amount_in_wei, intermediate_unit): intermediate_amount = from_wei(amount_in_wei, intermediate_unit) result_amount = to_wei(intermediate_amount, intermediate_unit) assert result_amount == amount_in_wei