-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from CrocoFactory/dev
Adding tests
- Loading branch information
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |