Skip to content

Commit

Permalink
Merge pull request #1 from CrocoFactory/dev
Browse files Browse the repository at this point in the history
Adding tests
  • Loading branch information
blnkoff authored Sep 29, 2024
2 parents 50eb090 + 0bea594 commit 5438b52
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
Empty file added tests/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

import os
import pytest
from typing import Optional
from ether import Network, Token
from ether import AsyncWallet, Wallet
from web3 import AsyncWeb3


@pytest.fixture(scope="session")
def make_wallet():
def _make_wallet(
network: Network | str,
private_key: Optional[str] = None,
is_async: bool = True
) -> AsyncWallet | Wallet:
if not private_key:
private_key = os.getenv('TEST_PRIVATE_KEY')
return AsyncWallet(private_key, network) if is_async else Wallet(private_key, network)

return _make_wallet


@pytest.fixture(scope="session")
def eth_amount():
amount = AsyncWeb3.to_wei(0.001, 'ether')
return amount


@pytest.fixture(scope="session")
def usdc(make_wallet) -> Token:
wallet = make_wallet('BSC', is_async=False)
return wallet.get_token('0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d')
39 changes: 39 additions & 0 deletions tests/test_async_wallet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest
from dotenv import dotenv_values

dotenv_values = dotenv_values()


@pytest.fixture()
def wallet(make_wallet):
return make_wallet(network='BSC', is_async=True, private_key=dotenv_values.get('TEST_PRIVATE_KEY'))


@pytest.mark.asyncio
async def test_get_balance(wallet):
balance = await wallet.get_balance()
assert isinstance(balance, int)


@pytest.mark.asyncio
async def test_build_tx_params(wallet):
tx_params = await wallet.build_tx_params(0)
assert 'value' in tx_params and 'gasPrice' in tx_params


@pytest.mark.asyncio
async def test_get_balance_of(wallet, usdc):
assert isinstance(await wallet.get_balance_of(usdc), int)


@pytest.mark.asyncio
async def test_transaction(wallet, eth_amount):
recipient = '0xe977Fa8D8AE7D3D6e28c17A868EF04bD301c583f'
params = await wallet.build_tx_params(eth_amount, recipient=recipient)
return await wallet.transact(params)


@pytest.mark.asyncio
async def test_transfer(wallet, eth_amount, usdc):
recipient = '0xe977Fa8D8AE7D3D6e28c17A868EF04bD301c583f'
return await wallet.transfer(usdc, recipient, 10 ** (usdc.decimals - 2))
23 changes: 23 additions & 0 deletions tests/test_wallet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest
from dotenv import dotenv_values

dotenv_values = dotenv_values()


@pytest.fixture
def wallet(make_wallet):
return make_wallet(network='BSC', is_async=False, private_key=dotenv_values.get('TEST_PRIVATE_KEY'))


def test_get_balance(wallet):
balance = wallet.get_balance()
assert isinstance(balance, int)


def test_build_tx_params(wallet):
tx_params = wallet.build_tx_params(0)
assert 'value' in tx_params and 'gasPrice' in tx_params


def test_get_balance_of(wallet, usdc):
assert isinstance(wallet.get_balance_of(usdc), int)
13 changes: 13 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from functools import wraps


def validate_status(func):
@wraps(func)
async def wrapper(*args, **kwargs):
wallet = kwargs['wallet']
tx_hash = await func(*args, **kwargs)
status = bool(wallet.provider.eth.wait_for_transaction_receipt(tx_hash))

assert status

return wrapper

0 comments on commit 5438b52

Please sign in to comment.