def test_deposit(w3, ether_token):
    user = w3.eth.accounts[2]
    user_bal = call(ether_token.balance_of(user))
    assert user_bal == 0
    # in normal use cases the user's account would be set, but here we'll just send it
    transact(ether_token.deposit(Web3.toWei(1, 'gwei'), {'from': user}))
    new_user_bal = call(ether_token.balance_of(user))
    assert new_user_bal == Web3.toWei(1, 'gwei')
def test_increase_allowance(w3, ether_token):
    spender = w3.eth.accounts[1]
    old_allowed = call(
        ether_token.allowance(ether_token.account, w3.eth.accounts[1]))
    transact(ether_token.increase_allowance(spender, Web3.toWei(1, 'kwei')))
    new_allowed = call(
        ether_token.allowance(ether_token.account, w3.eth.accounts[1]))
    assert new_allowed == old_allowed + Web3.toWei(1, 'kwei')
def test_withdraw(w3, ether_token, market_token, reserve, listing):
    # Set market token privileges before this test
    priv = call(market_token.has_privilege(reserve.address))
    # It's possible the privilege may have been set already
    if priv == False:
        tx = transact(
            market_token.set_privileged(reserve.address, listing.address))
        # wait for this to be mined
        rct = w3.eth.waitForTransactionReceipt(tx)
        # Check mining succeeded
        assert rct['status'] == 1
        priv = call(market_token.has_privilege(reserve.address))
        assert priv == True
    # Use a new user
    user = w3.eth.accounts[3]
    tx = transact(ether_token.deposit(Web3.toWei(1, 'ether'), {'from': user}))
    rct = w3.eth.waitForTransactionReceipt(tx)
    new_user_bal = call(ether_token.balance_of(user))
    assert new_user_bal == Web3.toWei(1, 'ether')

    # Approve the spend
    tx = transact(
        ether_token.approve(reserve.address,
                            Web3.toWei(1, 'ether'),
                            opts={'from': user}))
    rct = w3.eth.waitForTransactionReceipt(tx)
    assert rct['status'] == 1

    # Call support
    tx = transact(
        reserve.support(new_user_bal, opts={
            'gas': 1000000,
            'from': user
        }))
    rct = w3.eth.waitForTransactionReceipt(tx)
    assert rct['status'] == 1
    cmt_user_bal = call(market_token.balance_of(user))
    # This is around one milliether, but not quite due to spread
    assert cmt_user_bal > 0

    # Call withdraw
    tx = transact(reserve.withdraw(opts={'gas': 1000000, 'from': user}))
    rct = w3.eth.waitForTransactionReceipt(tx)
    assert rct['status'] == 1
    cmt_user_bal = call(market_token.balance_of(user))
    # This should be 0
    assert cmt_user_bal == 0
    cet_user_bal = call(ether_token.balance_of(user))
    # Should be a little under 1 eth
    assert cet_user_bal > 0
def test_transfer_from(w3, ether_token):
    user = w3.eth.accounts[2]
    user_bal = call(ether_token.balance_of(user))
    owner_bal = call(ether_token.balance_of(ether_token.account))
    amt = Web3.toWei(1, 'mwei')
    # this user needs to approve the 'spender' (the owner in this test case)
    transact(ether_token.approve(ether_token.account, amt, {'from': user}))
    # with the allowance, that acct may transfer.
    transact(ether_token.transfer_from(user, ether_token.account, amt))
    new_user_bal = call(ether_token.balance_of(user))
    new_owner_bal = call(ether_token.balance_of(ether_token.account))
    assert new_user_bal == user_bal - amt
    assert new_owner_bal == owner_bal + amt
    # should have 'zeroed' the allowance
    allowed = call(ether_token.allowance(user, ether_token.account))
Exemple #5
0
def market_token(w3, market_token_pre, reserve, listing):
    """
    set the privileged for maket_token
    """
    tx_hash = transact(market_token_pre.set_privileged(reserve.address, listing.address))
    tx_rcpt = w3.eth.waitForTransactionReceipt(tx_hash)
    return market_token_pre
def test_withdraw(w3, ether_token):
    user = w3.eth.accounts[2]
    user_bal = call(ether_token.balance_of(user))
    tx = transact(ether_token.withdraw(user_bal, {'from': user}))
    new_user_bal = call(ether_token.balance_of(user))
    assert new_user_bal == 0
    rct = w3.eth.getTransactionReceipt(tx)
    logs = ether_token.deployed.events.Withdrawn().processReceipt(rct)
    assert logs[0]['args']['to'] == user
    assert logs[0]['args']['amount'] == user_bal
def test_transfer(w3, ether_token):
    user = w3.eth.accounts[3]
    other = w3.eth.accounts[4]
    user_bal = call(ether_token.balance_of(user))
    other_bal = call(ether_token.balance_of(other))
    assert user_bal == 0
    assert other_bal == 0
    # Let's deposit 1 ether token in user's account
    transact(ether_token.deposit(Web3.toWei(1, 'gwei'), {'from': user}))
    # Let's send this to other
    tx = transact(
        ether_token.transfer(other, Web3.toWei(1, 'gwei'), {'from': user}))
    new_user_bal = call(ether_token.balance_of(user))
    new_other_bal = call(ether_token.balance_of(other))
    assert new_user_bal == 0
    assert new_other_bal == Web3.toWei(1, 'gwei')
    # event published...
    rct = w3.eth.getTransactionReceipt(tx)
    logs = ether_token.deployed.events.Transfer().processReceipt(rct)
    assert logs[0]['args']['amount'] == Web3.toWei(1, 'gwei')
def test_approve_and_allowance(w3, ether_token):
    # exact gas amounts are known
    gas = ether_token.get_gas('approve')
    assert gas == 37763 + GAS_BUFFER  # looked up manually in the abi
    spender = w3.eth.accounts[1]
    # passing empty opts assures all defaults are used
    tx = transact(ether_token.approve(spender, Web3.toWei(1, 'gwei')))
    allowed = call(
        ether_token.allowance(ether_token.account, w3.eth.accounts[1]))
    assert allowed == Web3.toWei(1, 'gwei')
    # check the event
    rct = w3.eth.getTransactionReceipt(tx)
    logs = ether_token.deployed.events.Approval().processReceipt(rct)
    assert logs[0]['args']['amount'] == Web3.toWei(1, 'gwei')
def test_support(w3, ether_token, market_token, reserve, listing):
    # Set market token privileges before this test
    priv = call(market_token.has_privilege(reserve.address))
    # May have been set previously
    if priv == False:
        tx = transact(
            market_token.set_privileged(reserve.address, listing.address))
        # wait for this to be mined
        rct = w3.eth.waitForTransactionReceipt(tx)
        # Check mining succeeded
        assert rct['status'] == 1
        priv = call(market_token.has_privilege(reserve.address))
        assert priv == True

    user = w3.eth.accounts[2]
    user_bal = call(ether_token.balance_of(user))
    assert user_bal == 0

    # Deposit ETH in EtherToken
    tx = transact(ether_token.deposit(Web3.toWei(1, 'ether'), {'from': user}))
    rct = w3.eth.waitForTransactionReceipt(tx)
    new_user_bal = call(ether_token.balance_of(user))
    assert new_user_bal == Web3.toWei(1, 'ether')
    assert rct['status'] == 1

    # Approve the spend
    old_allowance = call(ether_token.allowance(user, reserve.address))
    assert old_allowance == 0
    tx = transact(
        ether_token.approve(reserve.address,
                            Web3.toWei(1, 'ether'),
                            opts={'from': user}))
    rct = w3.eth.waitForTransactionReceipt(tx)
    assert rct['status'] == 1
    new_allowance = call(ether_token.allowance(user, reserve.address))
    assert new_allowance == Web3.toWei(1, 'ether')

    # Perform pre-checks for support
    support_price = call(reserve.get_support_price())
    assert new_user_bal >= support_price
    assert new_allowance >= new_user_bal
    minted = (new_user_bal // support_price) * 10**9
    assert minted == 10**6 * Web3.toWei(1, 'gwei')
    priv = call(market_token.has_privilege(reserve.address))
    assert priv == True
    total_supply = call(market_token.total_supply())
    assert total_supply == Web3.toWei(2, 'ether')

    # Call support
    tx = transact(
        reserve.support(new_user_bal, opts={
            'gas': 1000000,
            'from': user
        }))
    rct = w3.eth.waitForTransactionReceipt(tx)
    assert rct['status'] == 1
    logs = reserve.deployed.events.Supported().processReceipt(rct)
    cmt_user_bal = call(market_token.balance_of(user))
    assert cmt_user_bal == Web3.toWei(1, 'milliether')
    new_supply = call(market_token.total_supply())
    assert new_supply == total_supply + cmt_user_bal
Exemple #10
0
def datatrust(w3, datatrust_pre, listing):
    tx_hash = transact(datatrust_pre.set_privileged(listing.address))
    tx_rcpt = w3.eth.waitForTransactionReceipt(tx_hash)
    return datatrust_pre
Exemple #11
0
def voting(w3, voting_pre, parameterizer, reserve, datatrust_pre, listing):
    tx_hash = transact(voting_pre.set_privileged(parameterizer.address, datatrust_pre.address, listing.address))
    tx_rcpt = w3.eth.waitForTransactionReceipt(tx_hash)
    return voting_pre
Exemple #12
0
def test_reparameterize(w3, ether_token, market_token, voting, parameterizer,
                        reserve):
    # Set market token privileges before this test
    priv = call(market_token.has_privilege(reserve.address))
    assert priv == True

    user = w3.eth.accounts[2]
    user_bal = call(ether_token.balance_of(user))
    assert user_bal == 0

    # Deposit ETH in EtherToken
    tx = transact(ether_token.deposit(Web3.toWei(10, 'ether'), {'from': user}))
    rct = w3.eth.waitForTransactionReceipt(tx)
    new_user_bal = call(ether_token.balance_of(user))
    assert new_user_bal == Web3.toWei(10, 'ether')
    assert rct['status'] == 1

    # Approve the spend
    old_allowance = call(ether_token.allowance(user, reserve.address))
    assert old_allowance == 0
    tx = transact(
        ether_token.approve(reserve.address,
                            Web3.toWei(10, 'ether'),
                            opts={'from': user}))
    rct = w3.eth.waitForTransactionReceipt(tx)
    assert rct['status'] == 1
    new_allowance = call(ether_token.allowance(user, reserve.address))
    assert new_allowance == Web3.toWei(10, 'ether')

    # Perform pre-checks for support
    support_price = call(reserve.get_support_price())
    assert new_user_bal >= support_price
    assert new_allowance >= new_user_bal
    minted = (new_user_bal // support_price) * 10**9
    assert minted == 10**7 * Web3.toWei(1, 'gwei')
    priv = call(market_token.has_privilege(reserve.address))
    assert priv == True
    total_supply = call(market_token.total_supply())
    assert total_supply == Web3.toWei(2, 'ether')

    # Call support
    tx = transact(
        reserve.support(new_user_bal, opts={
            'gas': 1000000,
            'from': user
        }))
    rct = w3.eth.waitForTransactionReceipt(tx)
    assert rct['status'] == 1
    logs = reserve.deployed.events.Supported().processReceipt(rct)
    cmt_user_bal = call(market_token.balance_of(user))
    assert cmt_user_bal == Web3.toWei(10, 'milliether')
    new_supply = call(market_token.total_supply())
    assert new_supply == total_supply + cmt_user_bal

    # Approve the market token allowance
    old_mkt_allowance = call(market_token.allowance(user, voting.address))
    assert old_mkt_allowance == 0
    tx = transact(
        market_token.approve(voting.address,
                             Web3.toWei(10, 'milliether'),
                             opts={'from': user}))
    rct = w3.eth.waitForTransactionReceipt(tx)
    assert rct['status'] == 1
    new_mkt_allowance = call(market_token.allowance(user, voting.address))
    assert new_mkt_allowance == Web3.toWei(10, 'milliether')

    stake = call(parameterizer.get_stake())
    print("stake")
    print(stake)
    assert stake <= new_mkt_allowance

    tx = transact(
        parameterizer.reparameterize(PLURALITY,
                                     51,
                                     opts={
                                         'gas': 500000,
                                         'from': user
                                     }))
    rct = w3.eth.waitForTransactionReceipt(tx)
    assert rct['status'] == 1
    logs = parameterizer.deployed.events.ReparamProposed().processReceipt(rct)
    hash = call(parameterizer.get_hash(PLURALITY, 51))
    # should see an event with the calculated hash for those vals
    assert logs[0]['args']['hash'] == hash