Beispiel #1
0
def test_event_execute(web3: Web3, topped_up_hosted_wallet: HostedWallet,
                       simple_test_contract: Contract):
    """Test calling a contract froma hosted wallet."""

    hosted_wallet = topped_up_hosted_wallet

    # Events of the hosted wallet
    listener, events = create_contract_listener(hosted_wallet.contract)

    # Events of the contract we are calling
    target_listener, target_events = create_contract_listener(
        simple_test_contract)

    # Make gas a huge number so we don't run out of gas.
    # No idea of actual gas consumption.
    gas_amount = 1500000

    balance_before = hosted_wallet.get_balance()

    magic = random.randint(0, 2**30)
    txid = hosted_wallet.execute(simple_test_contract,
                                 "setValue",
                                 args=[magic],
                                 max_gas=gas_amount)
    confirm_transaction(web3, txid)

    balance_after = hosted_wallet.get_balance()

    # Check events from the wallet
    update_count = listener.poll()

    assert update_count == 1
    assert len(events) == 1
    event_name, input_data = events[0]
    assert event_name == "Execute"
    assert input_data["value"] == 0
    # Hmm looks like this network doesn't spend any gas
    # assert input_data["spentGas"] == 100000000000000  # Hardcoded value for private test geth
    assert input_data["to"] == simple_test_contract.address

    # Doing call() doesn't incur any gas cost on the contract
    assert balance_after == balance_before

    # Check events from the testcontract.sol
    update_count = target_listener.poll()
    assert update_count == 1
    assert len(target_events) == 1
    event_name, input_data = target_events[0]
    assert event_name == "Received"
Beispiel #2
0
def test_event_send_tokens(web3, hosted_wallet, token, coinbase):
    """Hosted wallet sends tokens."""

    # BBB
    token = token.contract

    # Top up hosted wallet with tokens
    txid = token.transact().transfer(hosted_wallet.address, 4000)
    confirm_transaction(web3, txid)

    # Prepare event listener
    listener, events = create_contract_listener(token)
    listener.poll()

    # Transfer tokens back
    # Do a withdraw from wallet
    txid = hosted_wallet.execute(token, "transfer", args=[coinbase, 4000])
    confirm_transaction(web3, txid)
    update_count = listener.poll()

    # Check the transfer event arrives
    assert update_count == 1
    assert len(events) == 1
    event_name, input_data = events[0]
    assert event_name == "Transfer"
    assert input_data["value"] == 4000
    assert input_data["to"] == coinbase
    assert input_data["from"] == hosted_wallet.address

    # Check balances
    token.call().balanceOf(coinbase) == 10000
    token.call().balanceOf(hosted_wallet.address) == 0
def test_fund_crowdsale(web3, hosted_wallet, token):
    """When we fund crowdsale tokens get created."""

    contract = token.contract
    test_fund = Decimal(1000)  # ETH

    listener, events = create_contract_listener(contract)

    contract.call().investorCount() == 0

    txid = send_balance_to_contract(token.contract, test_fund, gas=1000000)
    confirm_transaction(web3, txid)
    update_count = listener.poll()

    # receipt = web3.eth.getTransactionReceipt(txid)
    # print("Gas used by funding is ", receipt["cumulativeGas"])

    # Check the transfer event arrives
    assert update_count == 2  # Buy + Transfer
    assert len(events) == 2
    event_name, input_data = events[0]
    assert event_name == "Buy"
    assert input_data["eth"] == to_wei(test_fund, "ether")
    assert input_data["tokens"] == 2000

    # Check balances
    web3.eth.getBalance(MULTISIG) == to_wei(test_fund * 2, "ether")
    contract.call().weiRaised() == to_wei(test_fund, "ether")
    contract.call().investorCount() == 1
Beispiel #4
0
def test_event_withdraw_wallet(web3, topped_up_hosted_wallet, coinbase):
    """Withdraw funds from the wallet and see that we get the event of the deposit."""

    hosted_wallet = topped_up_hosted_wallet
    coinbase_address = coinbase

    listener, events = create_contract_listener(hosted_wallet.contract)

    # Do a withdraw from wallet
    txid = hosted_wallet.withdraw(coinbase_address, TEST_VALUE)
    confirm_transaction(web3, txid)

    # Wallet contract should generate events if the withdraw succeeded or not
    update_count = listener.poll()

    assert update_count == 1
    assert len(events) == 1
    event_name, input_data = events[0]
    assert event_name == "Withdraw"
    assert input_data["value"] == to_wei(TEST_VALUE)
    assert input_data["to"] == coinbase_address

    # Deposit some more, should generate one new event
    txid = hosted_wallet.withdraw(coinbase_address, TEST_VALUE)
    confirm_transaction(web3, txid)

    update_count = listener.poll()
    assert update_count == 1
    assert event_name == "Withdraw"
    assert input_data["value"] == to_wei(TEST_VALUE)
    assert input_data["to"] == coinbase_address
Beispiel #5
0
def test_event_fund_wallet(web3, hosted_wallet):
    """Send some funds int the wallet and see that we get the event of the deposit."""

    listener, events = create_contract_listener(hosted_wallet.contract)

    # value = get_wallet_balance(testnet_wallet_contract_address)
    txid = send_balance_to_contract(hosted_wallet, TEST_VALUE)
    confirm_transaction(web3, txid)

    update_count = listener.poll()

    assert update_count == 1
    assert len(events) == 1
    event_name, input_data = events[0]
    assert event_name == "Deposit"
    assert input_data["value"] == to_wei(TEST_VALUE)

    # Deposit some more, should generate one new event
    txid = send_balance_to_contract(hosted_wallet, TEST_VALUE)
    confirm_transaction(web3, txid)

    update_count = listener.poll()

    assert update_count == 1
    assert len(events) == 2
    event_name, input_data = events[1]
    assert event_name == "Deposit"
    assert input_data["value"] == to_wei(TEST_VALUE)
Beispiel #6
0
def test_event_claim_fees(web3, topped_up_hosted_wallet, coinbase):
    """We correctly can claim transaction fees from the hosted wallet contract."""

    hosted_wallet = topped_up_hosted_wallet
    coinbase_address = coinbase

    # Do a withdraw to cause some fees
    listener, events = create_contract_listener(hosted_wallet.contract)
    assert hosted_wallet.get_balance() > TEST_VALUE
    txid = hosted_wallet.withdraw(coinbase_address, TEST_VALUE)
    confirm_transaction(web3, txid)

    # Claim fees for the withdraw operation
    claim_txid, price = hosted_wallet.claim_fees(txid)
    confirm_transaction(web3, claim_txid)

    # We should have event for withdraw + claims
    update_count = listener.poll()
    assert update_count == 2
    assert len(events) == 2
    event_name, input_data = events[-1]  # Fee claim event

    assert event_name == "ClaimFee"
    assert input_data["txid"] == txid_to_bin(
        txid)  # This was correctly targeted to original withdraw
    assert input_data["value"] == to_wei(price)  # We claimed correct amount
Beispiel #7
0
def test_event_send_tokens(web3, hosted_wallet, token, coinbase):
    """Hosted wallet sends tokens."""

    # BBB
    token = token.contract
    listener, events = create_contract_listener(token)

    # Top up hosted wallet with tokens
    txid = token.transact().transfer(hosted_wallet.address, 4000)
    # Prepare event listener
    confirm_transaction(web3, txid)
    update_count = listener.poll()
    assert update_count == 1
    assert events[0][
        0] == "Transfer"  # [('Transfer', {'from': '0x41e76d4aabeb54a90ca67c59374236314ac3ecd4', 'value': 4000, 'to': '0xdde544e991dfb43808e8f1c4a750eaff3cb17ef4'})]

    # Prepare event listener
    listener, events = create_contract_listener(token)
    listener.poll()

    wallet_listener, wallet_events = create_contract_listener(
        hosted_wallet.contract)

    # Transfer tokens back
    # Do a withdraw from wallet
    txid = hosted_wallet.execute(token, "transfer", args=[coinbase, 4000])
    receipt = confirm_transaction(web3, txid)
    update_count = listener.poll()
    wallet_update_count = wallet_listener.poll()

    # See hosted wallet processed transfer() call
    assert wallet_update_count == 1
    assert wallet_events[0][0] != "FailedExecute"

    # Check the transfer event arrives
    assert update_count == 1
    assert len(events) == 1
    event_name, input_data = events[0]
    assert event_name == "Transfer"
    assert input_data["value"] == 4000
    assert input_data["to"] == coinbase
    assert input_data["from"] == hosted_wallet.address

    # Check balances
    token.call().balanceOf(coinbase) == 10000
    token.call().balanceOf(hosted_wallet.address) == 0
Beispiel #8
0
def test_event_withdraw_wallet_too_much(web3: Web3, topped_up_hosted_wallet,
                                        coinbase):
    """Try to withdraw more than the wallet has."""

    hosted_wallet = topped_up_hosted_wallet
    coinbase_address = coinbase

    listener, events = create_contract_listener(hosted_wallet.contract)

    too_much = Decimal(99999999)

    txid = hosted_wallet.withdraw(coinbase_address, too_much)
    confirm_transaction(web3, txid)

    update_count = listener.poll()

    # XXX:
    assert update_count == 1
    assert len(events) == 1
    event_name, input_data = events[0]
    assert event_name == "ExceededWithdraw"
    assert input_data["value"] == to_wei(too_much)
Beispiel #9
0
def test_event_receive_tokens(web3, hosted_wallet, token, coinbase):
    """A hosted wallet receive tokens."""

    # BBB
    token = token.contract

    listener, events = create_contract_listener(token)

    txid = token.transact().transfer(hosted_wallet.address, 4000)
    confirm_transaction(web3, txid)
    update_count = listener.poll()

    # Check the transfer event arrives
    assert update_count == 1
    assert len(events) == 1
    event_name, input_data = events[0]
    assert event_name == "Transfer"
    assert input_data["value"] == 4000
    assert input_data["from"] == coinbase
    assert input_data["to"] == hosted_wallet.address

    # Check balances
    token.call().balanceOf(coinbase) == 6000
    token.call().balanceOf(hosted_wallet.address) == 400
Beispiel #10
0
def test_event_receive_tokens(web3, hosted_wallet, token, coinbase):
    """A hosted wallet receive tokens."""

    # BBB
    token = token.contract

    listener, events = create_contract_listener(token)

    txid = token.transact().transfer(hosted_wallet.address, 4000)
    confirm_transaction(web3, txid)
    update_count = listener.poll()

    # Check the transfer event arrives
    assert update_count == 1
    assert len(events) == 1
    event_name, input_data = events[0]
    assert event_name == "Transfer"
    assert input_data["value"] == 4000
    assert input_data["from"] == coinbase
    assert input_data["to"] == hosted_wallet.address

    # Check balances
    token.call().balanceOf(coinbase) == 6000
    token.call().balanceOf(hosted_wallet.address) == 400