• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

safe-global / safe-eth-py / 10793540350

10 Sep 2024 01:31PM UTC coverage: 93.551% (-0.3%) from 93.892%
10793540350

push

github

falvaradorodriguez
Fix cowswap test

8777 of 9382 relevant lines covered (93.55%)

3.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.64
/safe_eth/eth/tests/ethereum_test_case.py
1
import logging
4✔
2
import os
4✔
3

4
from eth_account import Account
4✔
5
from eth_account.signers.local import LocalAccount
4✔
6
from eth_typing import ChecksumAddress, HexAddress, HexStr
4✔
7
from web3 import Web3
4✔
8
from web3.contract import Contract
4✔
9
from web3.types import TxParams, Wei
4✔
10

11
from ..ethereum_client import EthereumClient, get_auto_ethereum_client
4✔
12
from ..multicall import Multicall
4✔
13
from .utils import deploy_erc20, send_tx
4✔
14

15
logger = logging.getLogger(__name__)
4✔
16

17

18
_cached_data = {
4✔
19
    "ethereum_client": None,  # Prevents initializing again
20
}
21

22

23
class EthereumTestCaseMixin:
4✔
24
    ethereum_client: EthereumClient
4✔
25
    ethereum_node_url: str
4✔
26
    w3: Web3
4✔
27
    ethereum_test_account: LocalAccount
4✔
28
    multicall: Multicall
4✔
29

30
    @classmethod
4✔
31
    def setUpClass(cls):
4✔
32
        super().setUpClass()
4✔
33

34
        cls.ethereum_test_account = cls.get_ethereum_test_account()
4✔
35
        # Caching ethereum_client to prevent initializing again
36
        cls.ethereum_client = _cached_data["ethereum_client"]
4✔
37

38
        if not cls.ethereum_client:
4✔
39
            cls.ethereum_client = get_auto_ethereum_client()
4✔
40
            Multicall.deploy_contract(cls.ethereum_client, cls.ethereum_test_account)
4✔
41
            _cached_data["ethereum_client"] = cls.ethereum_client
4✔
42

43
        cls.ethereum_node_url = cls.ethereum_client.ethereum_node_url
4✔
44

45
        cls.w3 = cls.ethereum_client.w3
4✔
46
        cls.multicall = cls.ethereum_client.multicall
4✔
47
        assert cls.multicall, "Multicall must be defined"
4✔
48

49
    @classmethod
4✔
50
    def get_ethereum_test_account(cls):
4✔
51
        try:
4✔
52
            from django.conf import settings
4✔
53

54
            key = settings.ETHEREUM_TEST_PRIVATE_KEY
4✔
55
        except ModuleNotFoundError:
×
56
            key = os.environ.get(
×
57
                "ETHEREUM_TEST_PRIVATE_KEY",
58
                "b0057716d5917badaf911b193b12b910811c1497b5bada8d7711f758981c3773",  # Ganache account 9
59
            )
60
        return Account.from_key(key)
4✔
61

62
    @property
4✔
63
    def gas_price(self):
4✔
64
        return self.w3.eth.gas_price
4✔
65

66
    def send_tx(self, tx: TxParams, account: LocalAccount) -> bytes:
4✔
67
        return send_tx(self.w3, tx, account)
4✔
68

69
    def send_ether(self, to: str, value: int) -> bytes:
4✔
70
        return send_tx(
4✔
71
            self.w3, {"to": to, "value": Wei(value)}, self.ethereum_test_account
72
        )
73

74
    def create_and_fund_account(
4✔
75
        self, initial_ether: float = 0, initial_wei: int = 0
76
    ) -> LocalAccount:
77
        account = Account.create()
4✔
78
        if initial_ether > 0.0 or initial_wei > 0:
4✔
79
            self.send_tx(
4✔
80
                {
81
                    "to": account.address,
82
                    "value": Wei(
83
                        self.w3.to_wei(initial_ether, "ether") + Wei(initial_wei)
84
                    ),
85
                },
86
                self.ethereum_test_account,
87
            )
88
        return account
4✔
89

90
    def deploy_erc20(
4✔
91
        self,
92
        name: str,
93
        symbol: str,
94
        owner: str,
95
        amount: int,
96
        decimals: int = 18,
97
    ) -> Contract:
98
        return deploy_erc20(
×
99
            self.w3,
100
            self.ethereum_test_account,
101
            name,
102
            symbol,
103
            ChecksumAddress(HexAddress(HexStr(owner))),
104
            amount,
105
            decimals=decimals,
106
        )
107

108
    def deploy_example_erc20(self, amount: int, owner: str) -> Contract:
4✔
109
        return deploy_erc20(
4✔
110
            self.w3,
111
            self.ethereum_test_account,
112
            "Uxio",
113
            "UXI",
114
            ChecksumAddress(HexAddress(HexStr(owner))),
115
            amount,
116
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc