Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit13a77ab

Browse files
committed
refactor: massive refactor
1 parent266dea8 commit13a77ab

File tree

9 files changed

+259
-203
lines changed

9 files changed

+259
-203
lines changed

‎tests/test_cli.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ def test_get_token():
3939
print_result(result)
4040
assertresult.exit_code==0
4141

42-
out=json.loads(result.stdout.replace("'",'"'))
43-
assertout["symbol"]=="WETH"
44-
assertout["decimals"]==18
45-
4642

4743
deftest_get_tokendb():
4844
runner=CliRunner(mix_stderr=False)

‎uniswap/cli.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
fromweb3importWeb3
77

88
from .uniswapimportUniswap,AddressLike,_str_to_addr
9+
from .tokenimportBaseToken,Token
910
from .tokensimporttokens
1011

1112

@@ -65,14 +66,14 @@ def price(
6566
quantity:int=None,
6667
)->None:
6768
"""Returns the price of ``quantity`` tokens of ``token_in`` quoted in ``token_out``."""
68-
uni=ctx.obj["UNISWAP"]
69+
uni:Uniswap=ctx.obj["UNISWAP"]
6970
ifquantityisNone:
70-
quantity=10**uni.get_token(token_in)["decimals"]
71+
quantity=10**uni.get_token(token_in).decimals
7172
price=uni.get_token_token_input_price(token_in,token_out,qty=quantity)
7273
ifraw:
7374
print(price)
7475
else:
75-
decimals=uni.get_token(token_out)["decimals"]
76+
decimals=uni.get_token(token_out).decimals
7677
print(price/10**decimals)
7778

7879

@@ -81,7 +82,7 @@ def price(
8182
@click.pass_context
8283
deftoken(ctx:click.Context,token:AddressLike)->None:
8384
"""Show metadata for token"""
84-
uni=ctx.obj["UNISWAP"]
85+
uni:Uniswap=ctx.obj["UNISWAP"]
8586
t1=uni.get_token(token)
8687
print(t1)
8788

@@ -91,12 +92,11 @@ def token(ctx: click.Context, token: AddressLike) -> None:
9192
@click.pass_context
9293
deftokendb(ctx:click.Context,metadata:bool)->None:
9394
"""List known token addresses"""
94-
uni=ctx.obj["UNISWAP"]
95+
uni:Uniswap=ctx.obj["UNISWAP"]
9596
forsymbol,addrintokens.items():
9697
ifmetadataandaddr!="0x0000000000000000000000000000000000000000":
9798
data=uni.get_token(_str_to_addr(addr))
98-
data["address"]=addr
99-
assertdata["symbol"].lower()==symbol.lower()
99+
assertdata.symbol.lower()==symbol.lower()
100100
print(data)
101101
else:
102-
print({"symbol":symbol,"address":addr})
102+
print(BaseToken(symbol,addr))

‎uniswap/constants.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
ETH_ADDRESS="0x0000000000000000000000000000000000000000"
2+
WETH9_ADDRESS="0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
3+
4+
# see: https://chainid.network/chains/
5+
_netid_to_name= {
6+
1:"mainnet",
7+
3:"ropsten",
8+
4:"rinkeby",
9+
56:"binance",
10+
97:"binance_testnet",
11+
100:"xdai",
12+
}
13+
14+
_factory_contract_addresses_v1= {
15+
"mainnet":"0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95",
16+
"ropsten":"0x9c83dCE8CA20E9aAF9D3efc003b2ea62aBC08351",
17+
"rinkeby":"0xf5D915570BC477f9B8D6C0E980aA81757A3AaC36",
18+
"kovan":"0xD3E51Ef092B2845f10401a0159B2B96e8B6c3D30",
19+
"görli":"0x6Ce570d02D73d4c384b46135E87f8C592A8c86dA",
20+
}
21+
22+
23+
# For v2 the address is the same on mainnet, Ropsten, Rinkeby, Görli, and Kovan
24+
# https://uniswap.org/docs/v2/smart-contracts/factory
25+
_factory_contract_addresses_v2= {
26+
"mainnet":"0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
27+
"ropsten":"0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
28+
"rinkeby":"0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
29+
"görli":"0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
30+
"xdai":"0xA818b4F111Ccac7AA31D0BCc0806d64F2E0737D7",
31+
}
32+
33+
_router_contract_addresses_v2= {
34+
"mainnet":"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
35+
"ropsten":"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
36+
"rinkeby":"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
37+
"görli":"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
38+
"xdai":"0x1C232F01118CB8B424793ae03F870aa7D0ac7f77",
39+
}

‎uniswap/decorators.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
importfunctools
2+
fromtypingimportCallable,Any,List,Dict,TYPE_CHECKING
3+
4+
from .constantsimportETH_ADDRESS
5+
6+
ifTYPE_CHECKING:
7+
from .uniswapimportUniswap
8+
9+
10+
defcheck_approval(method:Callable)->Callable:
11+
"""Decorator to check if user is approved for a token. It approves them if they
12+
need to be approved."""
13+
14+
@functools.wraps(method)
15+
defapproved(self:Any,*args:Any,**kwargs:Any)->Any:
16+
# Check to see if the first token is actually ETH
17+
token=args[0]ifargs[0]!=ETH_ADDRESSelseNone
18+
token_two=None
19+
20+
# Check second token, if needed
21+
ifmethod.__name__=="make_trade"ormethod.__name__=="make_trade_output":
22+
token_two=args[1]ifargs[1]!=ETH_ADDRESSelseNone
23+
24+
# Approve both tokens, if needed
25+
iftoken:
26+
is_approved=self._is_approved(token)
27+
# logger.warning(f"Approved? {token}: {is_approved}")
28+
ifnotis_approved:
29+
self.approve(token)
30+
iftoken_two:
31+
is_approved=self._is_approved(token_two)
32+
# logger.warning(f"Approved? {token_two}: {is_approved}")
33+
ifnotis_approved:
34+
self.approve(token_two)
35+
returnmethod(self,*args,**kwargs)
36+
37+
returnapproved
38+
39+
40+
defsupports(versions:List[int])->Callable:
41+
defg(f:Callable)->Callable:
42+
iff.__doc__isNone:
43+
f.__doc__=""
44+
f.__doc__+="""\n\n
45+
Supports Uniswap
46+
"""+", ".join(
47+
"v"+str(ver)forverinversions
48+
)
49+
50+
@functools.wraps(f)
51+
defcheck_version(self:"Uniswap",*args:List,**kwargs:Dict)->Any:
52+
ifself.versionnotinversions:
53+
raiseException(
54+
f"Function{f.__name__} does not support version{self.version} of Uniswap passed to constructor"
55+
)
56+
returnf(self,*args,**kwargs)
57+
58+
returncheck_version
59+
60+
returng

‎uniswap/exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
fromtypingimportAny
2+
3+
4+
classInvalidToken(Exception):
5+
"""Raised when an invalid token address is used."""
6+
7+
def__init__(self,address:Any)->None:
8+
Exception.__init__(self,f"Invalid token address:{address}")
9+
10+
11+
classInsufficientBalance(Exception):
12+
"""Raised when the account has insufficient balance for a transaction."""
13+
14+
def__init__(self,had:int,needed:int)->None:
15+
Exception.__init__(self,f"Insufficient balance. Had{had}, needed{needed}")

‎uniswap/token.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fromdataclassesimportdataclass
2+
from .typesimportAddressLike
3+
4+
5+
@dataclass
6+
classBaseToken:
7+
symbol:str
8+
address:AddressLike
9+
10+
def__repr__(self)->str:
11+
returnf"BaseToken({self.symbol},{self.address!r})"
12+
13+
14+
@dataclass
15+
classToken(BaseToken):
16+
name:str
17+
decimals:int
18+
19+
def__repr__(self)->str:
20+
returnf"Token({self.symbol},{self.address!r},{self.decimals})"

‎uniswap/types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fromtypingimportUnion
2+
fromweb3.ethimportContract# noqa: F401
3+
fromweb3.typesimportAddress,ChecksumAddress,ENS
4+
5+
6+
# TODO: Consider dropping support for ENS altogether and instead use AnyAddress
7+
AddressLike=Union[Address,ChecksumAddress,ENS]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp