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

safe-global / safe-eth-py / 10527345804

23 Aug 2024 02:20PM UTC coverage: 93.892% (-0.01%) from 93.903%
10527345804

push

github

web-flow
Add addresses 1.4.1 for chain BEVM_TESTNET (#1297)

* Add new master copy address 0x41675C099F32341bf84BFc5382aF534df5C7461a

* Add new master copy address 0x29fcB43b46531BcA003ddC8FCB67FFE91900C762

* Add new proxy address 0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec67

* Apply linter fixes

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

8670 of 9234 relevant lines covered (93.89%)

3.76 hits per line

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

35.14
/gnosis/eth/oracles/cowswap.py
1
import logging
4✔
2
from typing import Optional
4✔
3

4
from gnosis.cowsap import CowSwapAPI, OrderKind
4✔
5

6
from .. import EthereumClient, EthereumNetworkNotSupported
4✔
7
from .exceptions import CannotGetPriceFromOracle
4✔
8
from .oracles import PriceOracle
4✔
9
from .utils import get_decimals
4✔
10

11
logger = logging.getLogger(__name__)
4✔
12

13

14
class CowswapOracle(PriceOracle):
4✔
15
    """
16
    CowSwap Oracle implementation
17

18
    https://docs.cow.fi/off-chain-services/api
19
    """
20

21
    def __init__(
4✔
22
        self,
23
        ethereum_client: EthereumClient,
24
    ):
25
        """
26
        :param ethereum_client:
27
        """
28
        self.ethereum_client = ethereum_client
×
29
        self.w3 = ethereum_client.w3
×
30
        self.api = CowSwapAPI(ethereum_client.get_network())
×
31

32
    @classmethod
4✔
33
    def is_available(
4✔
34
        cls,
35
        ethereum_client: EthereumClient,
36
    ) -> bool:
37
        """
38
        :param ethereum_client:
39
        :return: `True` if CowSwap is available for the EthereumClient provided, `False` otherwise
40
        """
41
        try:
×
42
            CowSwapAPI(ethereum_client.get_network())
×
43
            return True
×
44
        except EthereumNetworkNotSupported:
×
45
            return False
×
46

47
    def get_price(
4✔
48
        self, token_address_1: str, token_address_2: Optional[str] = None
49
    ) -> float:
50
        token_address_2 = token_address_2 or self.api.weth_address
×
51
        if token_address_1 == token_address_2:
×
52
            return 1.0
×
53

54
        token_1_decimals = get_decimals(token_address_1, self.ethereum_client)
×
55
        try:
×
56
            result = self.api.get_estimated_amount(
×
57
                token_address_1, token_address_2, OrderKind.SELL, 10**token_1_decimals
58
            )
59
            if "buyAmount" in result:
×
60
                # Decimals needs to be adjusted
61
                token_2_decimals = get_decimals(token_address_2, self.ethereum_client)
×
62
                return (
×
63
                    float(result["buyAmount"])
64
                    / result["sellAmount"]
65
                    * 10 ** (token_1_decimals - token_2_decimals)
66
                )
67

68
            exception = None
×
69
        except IOError as exc:
×
70
            exception = exc
×
71
            result = {}
×
72

73
        message = (
×
74
            f"Cannot get price from CowSwap {result} "
75
            f"for token-1={token_address_1} to token-2={token_address_2}"
76
        )
77
        logger.debug(message)
×
78
        raise CannotGetPriceFromOracle(message) from exception
×
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