Skip to content

Commit 00a97bd

Browse files
committed
fix: changed to using new names in updated web3py versions
1 parent 38b7870 commit 00a97bd

4 files changed

Lines changed: 31 additions & 27 deletions

File tree

‎uniswap/cli.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def _coerce_to_checksum(addr: str) -> str:
2828
raise ValueError(
2929
"token was not an address, and a shorthand was not found in the token db"
3030
)
31-
if Web3.isChecksumAddress(addr):
31+
if Web3.is_checksum_address(addr):
3232
return addr
3333
else:
34-
return Web3.toChecksumAddress(addr)
34+
return Web3.to_checksum_address(addr)
3535

3636

3737
@click.group()

‎uniswap/tokens.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
tokens_mainnet: Dict[str, ChecksumAddress] = {
8-
k: Web3.toChecksumAddress(v)
8+
k: Web3.to_checksum_address(v)
99
for k, v in {
1010
"ETH": "0x0000000000000000000000000000000000000000",
1111
"WETH": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
@@ -18,7 +18,7 @@
1818
}
1919

2020
tokens_rinkeby: Dict[str, ChecksumAddress] = {
21-
k: Web3.toChecksumAddress(v)
21+
k: Web3.to_checksum_address(v)
2222
for k, v in {
2323
"ETH": "0x0000000000000000000000000000000000000000",
2424
"DAI": "0x2448eE2641d78CC42D7AD76498917359D961A783",
@@ -27,7 +27,7 @@
2727
}
2828

2929
tokens_arbitrum: Dict[str, ChecksumAddress] = {
30-
k: Web3.toChecksumAddress(v)
30+
k: Web3.to_checksum_address(v)
3131
for k, v in {
3232
"ETH": "0x0000000000000000000000000000000000000000",
3333
"WETH": "0x82af49447d8a07e3bd95bd0d56f35241523fbab1",

‎uniswap/uniswap.py‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from web3 import Web3
99
from web3._utils.abi import map_abi_data
1010
from web3._utils.normalizers import BASE_RETURN_NORMALIZERS
11-
from web3.contract import Contract, ContractFunction
11+
from web3.contract import Contract
12+
from web3.contract.base_contract import BaseContractFunction
1213
from web3.exceptions import BadFunctionCallOutput, ContractLogicError
1314
from web3.types import (
1415
TxParams,
@@ -18,7 +19,6 @@
1819
)
1920
from eth_typing.evm import Address, ChecksumAddress
2021
from hexbytes import HexBytes
21-
2222
from .types import AddressLike
2323
from .token import ERC20Token
2424
from .exceptions import InvalidToken, InsufficientBalance
@@ -1435,12 +1435,12 @@ def _deadline(self) -> int:
14351435
return int(time.time()) + 10 * 60
14361436

14371437
def _build_and_send_tx(
1438-
self, function: ContractFunction, tx_params: Optional[TxParams] = None
1438+
self, function: BaseContractFunction, tx_params: Optional[TxParams] = None
14391439
) -> HexBytes:
14401440
"""Build and send a transaction."""
14411441
if not tx_params:
14421442
tx_params = self._get_tx_params()
1443-
transaction = function.build_transaction(tx_params)
1443+
transaction = function._build_transaction(tx_params)
14441444

14451445
if "gas" not in tx_params:
14461446
# `use_estimate_gas` needs to be True for networks like Arbitrum (can't assume 250000 gas),
@@ -1465,7 +1465,9 @@ def _build_and_send_tx(
14651465
logger.debug(f"nonce: {tx_params['nonce']}")
14661466
self.last_nonce = Nonce(tx_params["nonce"] + 1)
14671467

1468-
def _get_tx_params(self, value: Wei = Wei(0), gas: Optional[Wei] = None) -> TxParams:
1468+
def _get_tx_params(
1469+
self, value: Wei = Wei(0), gas: Optional[Wei] = None
1470+
) -> TxParams:
14691471
"""Get generic transaction parameters."""
14701472
params: TxParams = {
14711473
"from": _addr_to_str(self.address),
@@ -1569,7 +1571,7 @@ def multicall(
15691571
block_identifier="latest"
15701572
)
15711573
decoded_results = [
1572-
self.w3.codec.decode_abi(output_types, multicall_result)
1574+
self.w3.codec.decode(output_types, multicall_result)
15731575
for multicall_result in results
15741576
]
15751577
normalized_results = [
@@ -1669,7 +1671,7 @@ def create_pool_instance(
16691671
)
16701672
receipt = self.w3.eth.wait_for_transaction_receipt(tx)
16711673

1672-
event_logs = self.factory_contract.events.PoolCreated().processReceipt(receipt)
1674+
event_logs = self.factory_contract.events.PoolCreated().process_receipt(receipt)
16731675
pool_address = event_logs[0]["args"]["pool"]
16741676
pool_instance = _load_contract(
16751677
self.w3, abi_name="uniswap-v3/pool", address=pool_address
@@ -1823,27 +1825,27 @@ def get_raw_price(
18231825

18241826
if self.version == 2:
18251827
params: Iterable[Union[ChecksumAddress, Optional[int]]] = [
1826-
self.w3.toChecksumAddress(token_in),
1827-
self.w3.toChecksumAddress(token_out),
1828+
self.w3.to_checksum_address(token_in),
1829+
self.w3.to_checksum_address(token_out),
18281830
]
18291831
pair_token = self.factory_contract.functions.getPair(*params).call()
18301832
token_in_erc20 = _load_contract_erc20(
1831-
self.w3, self.w3.toChecksumAddress(token_in)
1833+
self.w3, self.w3.to_checksum_address(token_in)
18321834
)
18331835
token_in_balance = int(
18341836
token_in_erc20.functions.balanceOf(
1835-
self.w3.toChecksumAddress(pair_token)
1837+
self.w3.to_checksum_address(pair_token)
18361838
).call()
18371839
)
18381840
token_in_decimals = self.get_token(token_in).decimals
18391841
token_in_balance = token_in_balance / (10**token_in_decimals)
18401842

18411843
token_out_erc20 = _load_contract_erc20(
1842-
self.w3, self.w3.toChecksumAddress(token_out)
1844+
self.w3, self.w3.to_checksum_address(token_out)
18431845
)
18441846
token_out_balance = int(
18451847
token_out_erc20.functions.balanceOf(
1846-
self.w3.toChecksumAddress(pair_token)
1848+
self.w3.to_checksum_address(pair_token)
18471849
).call()
18481850
)
18491851
token_out_decimals = self.get_token(token_out).decimals
@@ -1852,15 +1854,15 @@ def get_raw_price(
18521854
raw_price = token_out_balance / token_in_balance
18531855
else:
18541856
params = [
1855-
self.w3.toChecksumAddress(token_in),
1856-
self.w3.toChecksumAddress(token_out),
1857+
self.w3.to_checksum_address(token_in),
1858+
self.w3.to_checksum_address(token_out),
18571859
fee,
18581860
]
18591861
pool_address = self.factory_contract.functions.getPool(*params).call()
18601862
pool_contract = _load_contract(
18611863
self.w3, abi_name="uniswap-v3/pool", address=pool_address
18621864
)
1863-
t0 = pool_contract.functions.token0().call()
1865+
# t0 = pool_contract.functions.token0().call()
18641866
t1 = pool_contract.functions.token1().call()
18651867
if t1.lower() == token_in.lower():
18661868
den0 = self.get_token(token_in).decimals
@@ -1953,7 +1955,9 @@ def _token_address_from_exchange(self, exchange_addr: AddressLike) -> Address:
19531955
@functools.lru_cache()
19541956
@supports([1])
19551957
def _exchange_contract(
1956-
self, token_addr: Optional[AddressLike] = None, ex_addr: Optional[AddressLike] = None
1958+
self,
1959+
token_addr: Optional[AddressLike] = None,
1960+
ex_addr: Optional[AddressLike] = None,
19571961
) -> Contract:
19581962
if not ex_addr and token_addr:
19591963
ex_addr = self._exchange_address_from_token(token_addr)

‎uniswap/util.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import functools
55
import lru
66

7-
from typing import Any, Generator, Sequence, Union, List, Tuple, Type, Dict, cast
7+
from typing import Any, Generator, List, Sequence, Tuple, Union
88

99
from web3 import Web3
1010
from web3.exceptions import NameNotFound
@@ -18,7 +18,7 @@
1818

1919
def _get_eth_simple_cache_middleware() -> Middleware:
2020
return construct_simple_cache_middleware(
21-
cache_class=cast(Type[Dict[Any, Any]], functools.partial(lru.LRU, 256)),
21+
cache=functools.partial(lru.LRU, 256), # type: ignore
2222
rpc_whitelist=SIMPLE_CACHE_RPC_WHITELIST,
2323
)
2424

@@ -37,10 +37,10 @@ def _str_to_addr(s: Union[AddressLike, str]) -> Address:
3737
def _addr_to_str(a: AddressLike) -> str:
3838
if isinstance(a, bytes):
3939
# Address or ChecksumAddress
40-
addr: str = Web3.toChecksumAddress("0x" + bytes(a).hex())
40+
addr: str = Web3.to_checksum_address("0x" + bytes(a).hex())
4141
return addr
4242
elif isinstance(a, str) and a.startswith("0x"):
43-
addr = Web3.toChecksumAddress(a)
43+
addr = Web3.to_checksum_address(a)
4444
return addr
4545

4646
raise NameNotFound(a)
@@ -63,7 +63,7 @@ def _load_abi(name: str) -> str:
6363

6464
@functools.lru_cache()
6565
def _load_contract(w3: Web3, abi_name: str, address: AddressLike) -> Contract:
66-
address = Web3.toChecksumAddress(address)
66+
address = Web3.to_checksum_address(address)
6767
return w3.eth.contract(address=address, abi=_load_abi(abi_name)) # type: ignore
6868

6969

0 commit comments

Comments
 (0)