Beispiel #1
0
def check_transfer(contract, sender, receiver, amount):
    sender_balance = call_function(contract, 'balanceOf', [sender])
    receiver_balance = call_function(contract, 'balanceOf', [receiver])

    tx_hash = transact_function(sender, contract, 'transfer',
                                [receiver, amount])
    contract.web3.eth.waitForTransactionReceipt(tx_hash)

    new_sender_balance = call_function(contract, 'balanceOf', [sender])
    new_receiver_balance = call_function(contract, 'balanceOf', [receiver])

    # check that balances are correct after transfer
    if sender != receiver:
        assert new_sender_balance == sender_balance - amount, \
            wrong('balance of sender {} after transfer', new_sender_balance, sender_balance - amount)

        assert new_receiver_balance == receiver_balance + amount, \
            wrong('balance of receiver {} after transfer'.format(receiver), new_receiver_balance,
                  receiver_balance + amount)
    else:
        assert sender_balance == new_sender_balance, \
            wrong('balance of address {} after transferring to itself'.format(sender), new_sender_balance,
                  sender_balance)

    # check that Transfer event is emitted correctly
    check_event(contract, tx_hash, 'Transfer', {
        'from': sender,
        'to': receiver,
        'tokens': amount
    })
Beispiel #2
0
def test_enable_transfers(contract, accounts, approvers, spenders):
    print('check that enabling transfers works correctly: ', end='')

    # non owner account should not be able to enable transfers
    owner = call_function(contract, 'owner')
    not_owners = [account for account in accounts if account != owner]
    for account in not_owners:
        assert reverts(transact_function, [account, contract, 'enableTransfers']), \
            'non owner account {} should not be able to enable transfers'.format(account)

    # owner should be able to enable transfers
    tx_hash = transact_function(owner, contract, 'enableTransfers')
    check_event(contract, tx_hash, 'TransfersEnabled')

    # check that transfer works after enabling transfers
    senders = accounts[:2]
    receivers = accounts[1:3]
    for sender in senders:
        for receiver in receivers:
            check_transfer(contract, sender, receiver, 1)

    # check that transferFrom works after enabling transfers
    for approver in approvers:
        for spender in spenders:
            receiver = accounts[3]
            check_transfer_from(contract, approver, spender, receiver, 1)

    # check that triggering enableTransfers one more time fails
    assert reverts(transact_function, [owner, contract, 'enableTransfers']), \
        'owner should not be able to enable transfers when it is already enabled'

    print('SUCCESS')
Beispiel #3
0
def test_disable_transfers(contract, accounts, approvers, spenders):
    print('check that disabling transfers works correctly: ', end='')

    # non owner account should not be able to enable transfers
    owner = call_function(contract, 'owner')
    not_owners = [account for account in accounts if account != owner]
    for account in not_owners:
        assert reverts(transact_function, [account, contract, 'disableTransfers']), \
            'non owner account {} should not be able to disable transfers'.format(account)

    # owner should be able to enable transfers
    tx_hash = transact_function(owner, contract, 'disableTransfers')
    check_event(contract, tx_hash, 'TransfersDisabled')

    # check that transfer reverts
    senders = accounts[:2]
    receivers = accounts[1:3]
    for sender in senders:
        for receiver in receivers:
            assert reverts(transact_function, [sender, contract, 'transfer', [receiver, 1]]), \
                '{} should not be able to transfer when transferring is disabled'.format(sender)

    # check that transferFrom reverts
    for approver in approvers:
        for spender in spenders:
            receiver = accounts[3]
            assert reverts(
                transact_function,
                [spender, contract, 'transferFrom', [approver, receiver, 1]])

    # check that triggering disableTransfers one more time fails
    assert reverts(transact_function, [owner, contract, 'disableTransfers']), \
        'owner should not be able to disable transfers when it is already disabled'

    print('SUCCESS')
Beispiel #4
0
def check_approve(contract, approver, spender, amount):
    tx_hash = transact_function(approver, contract, 'approve',
                                [spender, amount])
    contract.web3.eth.waitForTransactionReceipt(tx_hash)

    allowance = call_function(contract, 'allowance', [approver, spender])
    assert allowance == amount, wrong(
        '{} allowance for {}'.format(approver, spender), allowance, amount)

    # check that Approval event is emitted correctly
    check_event(contract, tx_hash, 'Approval', {
        'tokenOwner': approver,
        'spender': spender,
        'tokens': amount
    })
Beispiel #5
0
def check_transfer_from(contract, approver, spender, receiver, amount):
    approver_balance = call_function(contract, 'balanceOf', [approver])
    receiver_balance = call_function(contract, 'balanceOf', [receiver])
    spender_balance = call_function(contract, 'balanceOf', [spender])
    allowance = call_function(contract, 'allowance', [approver, spender])

    tx_hash = transact_function(spender, contract, 'transferFrom',
                                [approver, receiver, amount])
    contract.web3.eth.waitForTransactionReceipt(tx_hash)

    new_approver_balance = call_function(contract, 'balanceOf', [approver])
    new_receiver_balance = call_function(contract, 'balanceOf', [receiver])
    new_spender_balance = call_function(contract, 'balanceOf', [spender])
    new_allowance = call_function(contract, 'allowance', [approver, spender])

    assert new_allowance == allowance - amount, \
        wrong('{} allowance to {}'.format(approver, spender), new_allowance, allowance - amount)

    # check that balances are correct after transferFrom
    if approver != receiver:
        assert new_approver_balance == approver_balance - amount, \
            wrong('balance of approver {} after transfer', new_approver_balance, approver_balance - amount)

        assert new_receiver_balance == receiver_balance + amount, \
            wrong('balance of receiver {} after transfer'.format(receiver), new_receiver_balance,
                  receiver_balance + amount)
    else:
        assert new_approver_balance == approver_balance, \
            wrong('balance of approver {} after transfer to itself'.format(approver), new_approver_balance,
                  approver_balance)

    if spender != receiver and spender != spender:
        assert new_spender_balance == spender_balance, \
            wrong('balance of spender {} after transfer', new_spender_balance, spender_balance)

    # check that Transfer event is emitted correctly
    check_event(contract, tx_hash, 'Transfer', {
        'from': approver,
        'to': receiver,
        'tokens': amount
    })
Beispiel #6
0
def check_adjust_approval(contract, approver, spender, amount):
    allowance = call_function(contract, 'allowance', [approver, spender])

    if amount >= 0:
        function_name = 'increaseApproval'
    else:
        function_name = 'decreaseApproval'

    tx_hash = transact_function(approver, contract, function_name,
                                [spender, abs(amount)])
    contract.web3.eth.waitForTransactionReceipt(tx_hash)

    new_allowance = call_function(contract, 'allowance', [approver, spender])
    assert new_allowance == allowance + amount, wrong(
        '{} allowance for {}'.format(approver, spender), allowance, amount)

    # check that Approval event is emitted correctly
    check_event(contract, tx_hash, 'Approval', {
        'tokenOwner': approver,
        'spender': spender,
        'tokens': new_allowance
    })