Skip to content

Commit

Permalink
add send txns tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YanhuiJessica committed Oct 12, 2024
1 parent b3f1e84 commit 2f77e8a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ jobs:
pip install git+https://github.com/YanhuiJessica/cheb3.git#egg=cheb3
- name: Python ${{ matrix.python-version }} test utils
run: |
python3 -m pytest
python3 -m pytest tests/utils
- name: Python ${{ matrix.python-version }} test integration
run: |
python3 -m pytest tests/integration
61 changes: 61 additions & 0 deletions tests/integration/test_send_transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import pytest
from web3 import Web3, EthereumTesterProvider

from cheb3 import Connection
from cheb3.utils import compile_file, decode_data, encode_with_signature

# set up the keyfile account with a known address
KEYFILE_ACCOUNT_PKEY = "0x58d23b55bc9cdce1f18c2500f40ff4ab7245df9a89505e9b1fa4851f623d241d"
KEYFILE_ACCOUNT_ADDRESS = "0xdC544d1AA88Ff8bbd2F2AeC754B1F1e99e1812fd"

# For testing purposes
class ConnectionMock(Connection):
def __init__(self) -> None:
self.w3 = Web3(EthereumTesterProvider())


@pytest.fixture(scope="module")
def setup():
conn = ConnectionMock()
conn.w3.eth.default_account = conn.w3.eth.accounts[0]

conn.w3.provider.ethereum_tester.add_account(KEYFILE_ACCOUNT_PKEY)

# fund the account
conn.w3.eth.send_transaction(
{
"from": conn.w3.eth.default_account,
"to": KEYFILE_ACCOUNT_ADDRESS,
"value": conn.w3.to_wei(1, "ether"),
"gas": 21000,
"gasPrice": 10**9,
}
)
return conn


@pytest.fixture(scope="module")
def account(setup):
return setup.account(KEYFILE_ACCOUNT_PKEY)


@pytest.fixture(scope="module")
def token_contract(setup, account):
abi, bytecode = compile_file("tests/integration/contracts/MockWETH.sol", solc_version="0.8.20")["MockWETH"]
contract = setup.contract(account, abi=abi, bytecode=bytecode)
contract.deploy()
return contract


def test_contract_send_transaction(token_contract, account):
balance_before = token_contract.caller.balanceOf(account.address)
token_contract.functions.deposit().send_transaction(value=10)
balance_after = token_contract.caller.balanceOf(account.address)
assert balance_after == balance_before + 10


def test_account_send_transaction(account, token_contract):
balance_before = token_contract.caller.balanceOf(account.address)
account.send_transaction(token_contract.address, 10, encode_with_signature("deposit()"))
balance_after = token_contract.caller.balanceOf(account.address)
assert balance_after == balance_before + 10

0 comments on commit 2f77e8a

Please sign in to comment.