Files
protocol/python-packages/contract_wrappers/test/test_erc20_wrapper.py
2019-03-25 19:25:41 -04:00

81 lines
2.3 KiB
Python

"""Tests for ERC20Token wrapper."""
from decimal import Decimal
import pytest
from zero_ex.contract_wrappers import ERC20Token, TxParams
MAX_ALLOWANCE = int("{:.0f}".format(Decimal(2) ** 256 - 1))
@pytest.fixture(scope="module")
def erc20_wrapper(ganache_provider):
"""Get an instance of ERC20Token wrapper class for testing."""
return ERC20Token(ganache_provider)
def test_erc20_wrapper__balance_of(
accounts,
erc20_wrapper, # pylint: disable=redefined-outer-name
weth_address,
weth_instance, # pylint: disable=redefined-outer-name
):
"""Test getting baance of an account for an ERC20 token."""
acc1_original_weth_balance = erc20_wrapper.balance_of(
weth_address, accounts[0]
)
acc2_original_weth_balance = erc20_wrapper.balance_of(
weth_address, accounts[1]
)
expected_difference = 1 * 10 ** 18
weth_instance.functions.deposit().transact(
{"from": accounts[0], "value": expected_difference}
)
weth_instance.functions.deposit().transact(
{"from": accounts[1], "value": expected_difference}
)
acc1_weth_balance = erc20_wrapper.balance_of(weth_address, accounts[0])
acc2_weth_balance = erc20_wrapper.balance_of(weth_address, accounts[1])
assert (
acc1_weth_balance - acc1_original_weth_balance == expected_difference
)
assert (
acc2_weth_balance - acc2_original_weth_balance == expected_difference
)
def test_erc20_wrapper__approve(
accounts,
erc20_proxy_address,
erc20_wrapper, # pylint: disable=redefined-outer-name
weth_address, # pylint: disable=redefined-outer-name
):
"""Test approving one account to spend balance from another account."""
erc20_wrapper.approve(
weth_address,
erc20_proxy_address,
MAX_ALLOWANCE,
tx_params=TxParams(from_=accounts[0]),
)
erc20_wrapper.approve(
weth_address,
erc20_proxy_address,
MAX_ALLOWANCE,
tx_params=TxParams(from_=accounts[1]),
)
acc_1_weth_allowance = erc20_wrapper.allowance(
weth_address, accounts[0], erc20_proxy_address
)
acc_2_weth_allowance = erc20_wrapper.allowance(
weth_address, accounts[1], erc20_proxy_address
)
assert acc_1_weth_allowance == MAX_ALLOWANCE
assert acc_2_weth_allowance == MAX_ALLOWANCE