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

Commitd93e86f

Browse files
authored
Merge pull request#106 from ErikBjare/dev/v3-mvp
2 parents09259e0 +0eebc04 commitd93e86f

File tree

15 files changed

+1111
-461
lines changed

15 files changed

+1111
-461
lines changed

‎.github/workflows/test.yml

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3-
4-
name:test
1+
name:Test
52

63
on:
74
push:
@@ -10,30 +7,90 @@ on:
107
branches:[ master ]
118

129
jobs:
13-
build:
10+
test:
11+
name:test (v${{ matrix.uniswap-version }})
1412
runs-on:ubuntu-latest
13+
strategy:
14+
fail-fast:false
15+
matrix:
16+
uniswap-version:[1, 2, 3]
1517

1618
steps:
1719
-uses:actions/checkout@v2
18-
-name:Set up Python 3.8
20+
-name:Set up Python
1921
uses:actions/setup-python@v1
2022
with:
2123
python-version:3.8
2224
-name:Set up Node
2325
uses:actions/setup-node@v2-beta
2426
with:
2527
node-version:'12'
26-
-name:Install dependencies
28+
29+
# Set up poetry cache, from https://github.com/python-poetry/poetry/blob/45a9b8f20384591d0a33ae876bcf23656f928ec0/.github/workflows/main.yml
30+
-name:Get full python version
31+
id:full-python-version
32+
run:|
33+
echo ::set-output name=version::$(python -c "import sys; print('-'.join(str(v) for v in sys.version_info[:3]))")
34+
-name:Set up poetry
2735
run:|
2836
python -m pip install --upgrade pip poetry
37+
poetry config virtualenvs.in-projecttrue
38+
-name:Set up cache
39+
uses:actions/cache@v2
40+
id:cache
41+
with:
42+
path:.venv
43+
key:venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
44+
-name:Ensure cache is healthy
45+
if:steps.cache.outputs.cache-hit == 'true'
46+
run:timeout 10s poetry run pip --version || rm -rf .venv
47+
48+
-name:Install dependencies
49+
run:|
2950
poetry install
3051
npm install -g ganache-cli
31-
-name:Typecheck
32-
run:|
33-
make typecheck
3452
-name:Test
3553
env:
3654
PROVIDER:${{ secrets.MAINNET_PROVIDER }}
3755
MAINNET_PROVIDER:${{ secrets.MAINNET_PROVIDER }}
56+
UNISWAP_VERSION:${{ matrix.uniswap-version }}
3857
run:|
3958
make test
59+
60+
typecheck:
61+
name:typecheck
62+
runs-on:ubuntu-latest
63+
64+
steps:
65+
-uses:actions/checkout@v2
66+
-name:Set up Python
67+
uses:actions/setup-python@v1
68+
with:
69+
python-version:3.8
70+
71+
# Set up poetry cache, from https://github.com/python-poetry/poetry/blob/45a9b8f20384591d0a33ae876bcf23656f928ec0/.github/workflows/main.yml
72+
-name:Get full python version
73+
id:full-python-version
74+
run:|
75+
echo ::set-output name=version::$(python -c "import sys; print('-'.join(str(v) for v in sys.version_info[:3]))")
76+
-name:Set up poetry
77+
run:|
78+
python -m pip install --upgrade pip poetry
79+
poetry config virtualenvs.in-projecttrue
80+
-name:Set up cache
81+
uses:actions/cache@v2
82+
id:cache
83+
with:
84+
path:.venv
85+
key:venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
86+
-name:Ensure cache is healthy
87+
if:steps.cache.outputs.cache-hit == 'true'
88+
run:timeout 10s poetry run pip --version || rm -rf .venv
89+
90+
-name:Install dependencies
91+
run:|
92+
python -m pip install --upgrade pip poetry
93+
poetry install
94+
-name:Typecheck
95+
run:|
96+
make typecheck

‎Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ typecheck:
99
lint:
1010
poetry run flake8
1111

12+
format:
13+
black uniswap
14+
15+
format-abis:
16+
npx prettier --write --parser=json uniswap/assets/*/*.abi
17+
1218
precommit:
1319
make typecheck
1420
make lint

‎poetry.lock

Lines changed: 111 additions & 115 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ web3 = "^5.12.0"
2828
click ="^7.1.2"
2929

3030
[tool.poetry.dev-dependencies]
31-
pytest ="^5.4.3"
31+
mypy ="*"
3232
black ="^19.10b0"
33+
pytest ="^5.4.3"
3334
pytest-cov ="^2.10.0"
3435
pytest-dotenv ="^0.5.2"
3536
python-dotenv ="^0.14.0"
3637
flake8 ="^3.8.3"
37-
mypy ="^0.782"
3838
Sphinx ="^3.5.4"
3939
sphinx-book-theme ="^0.1.0"
4040
sphinx-click ="^2.7.1"

‎tests/test_cli.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,46 @@
11
importjson
2+
importsys
23

34
fromclick.testingimportCliRunner
45

56
fromuniswap.cliimportmain
67

78

9+
defprint_result(result):
10+
print(result)
11+
print(result.stdout.strip())
12+
print(result.stderr.strip(),file=sys.stderr)
13+
14+
815
deftest_get_price():
9-
runner=CliRunner()
16+
runner=CliRunner(mix_stderr=False)
1017
result=runner.invoke(main, ["price","weth","dai"])
18+
print_result(result)
1119
assertresult.exit_code==0
1220

1321
# Will break when ETH breaks 10k
14-
assert1000<float(result.output)<10_000
22+
assert1000<float(result.stdout)<10_000
1523

1624
result=runner.invoke(main, ["price","wbtc","dai"])
1725
assertresult.exit_code==0
1826

1927
# Will break when BTC breaks 100k
20-
assert10_000<float(result.output)<100_000
28+
assert10_000<float(result.stdout)<100_000
2129

2230

2331
deftest_get_token():
24-
runner=CliRunner()
32+
runner=CliRunner(mix_stderr=False)
2533
result=runner.invoke(main, ["token","weth"])
34+
print_result(result)
2635
assertresult.exit_code==0
27-
out=json.loads(result.output.replace("'",'"'))
36+
37+
out=json.loads(result.stdout.replace("'",'"'))
2838
assertout["symbol"]=="WETH"
2939
assertout["decimals"]==18
3040

3141

3242
deftest_get_tokendb():
33-
runner=CliRunner()
43+
runner=CliRunner(mix_stderr=False)
3444
result=runner.invoke(main, ["tokendb","--metadata"])
45+
print_result(result)
3546
assertresult.exit_code==0

‎tests/test_uniswap.py

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
importos
33
importsubprocess
44
importshutil
5+
importlogging
56
fromtypingimportGenerator
67
fromcontextlibimportcontextmanager
78
fromdataclassesimportdataclass
@@ -12,20 +13,44 @@
1213
fromuniswapimportUniswap,InvalidToken,InsufficientBalance
1314

1415

16+
logger=logging.getLogger(__name__)
17+
18+
ENV_UNISWAP_VERSION=os.getenv("UNISWAP_VERSION",None)
19+
ifENV_UNISWAP_VERSION:
20+
UNISWAP_VERSIONS= [int(ENV_UNISWAP_VERSION)]
21+
else:
22+
UNISWAP_VERSIONS= [1,2,3]
23+
24+
1525
@dataclass
1626
classGanacheInstance:
1727
provider:str
1828
eth_address:str
1929
eth_privkey:str
2030

2131

22-
@pytest.fixture(scope="module",params=[1,2])
32+
@pytest.fixture(scope="module",params=UNISWAP_VERSIONS)
2333
defclient(request,web3:Web3,ganache:GanacheInstance):
24-
uniswap=Uniswap(
34+
returnUniswap(
2535
ganache.eth_address,ganache.eth_privkey,web3=web3,version=request.param
2636
)
27-
uniswap._buy_test_assets()
28-
returnuniswap
37+
38+
39+
@pytest.fixture(scope="module")
40+
deftest_assets(client:Uniswap):
41+
"""
42+
Buy some DAI and USDC to test with.
43+
"""
44+
tokens=client._get_token_addresses()
45+
46+
fortoken_name,amountin [("DAI",100*10**18), ("USDC",100*10**6)]:
47+
token_addr=tokens[token_name]
48+
price=client.get_eth_token_output_price(token_addr,amount)
49+
logger.info(f"Cost of{amount}{token_name}:{price}")
50+
logger.info("Buying...")
51+
52+
tx=client.make_trade_output(tokens["ETH"],token_addr,amount)
53+
client.w3.eth.waitForTransactionReceipt(tx)
2954

3055

3156
@pytest.fixture(scope="module")
@@ -78,6 +103,7 @@ class TestUniswap(object):
78103
weth=Web3.toChecksumAddress("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2")
79104
bat=Web3.toChecksumAddress("0x0D8775F648430679A709E98d2b0Cb6250d2887EF")
80105
dai=Web3.toChecksumAddress("0x6b175474e89094c44da98b954eedeac495271d0f")
106+
usdc=Web3.toChecksumAddress("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48")
81107

82108
# For Rinkeby
83109
# eth = "0x0000000000000000000000000000000000000000"
@@ -86,10 +112,14 @@ class TestUniswap(object):
86112

87113
# ------ Exchange ------------------------------------------------------------------
88114
deftest_get_fee_maker(self,client:Uniswap):
115+
ifclient.versionnotin [1,2]:
116+
pytest.skip("Tested method not supported in this Uniswap version")
89117
r=client.get_fee_maker()
90118
assertr==0
91119

92120
deftest_get_fee_taker(self,client:Uniswap):
121+
ifclient.versionnotin [1,2]:
122+
pytest.skip("Tested method not supported in this Uniswap version")
93123
r=client.get_fee_taker()
94124
assertr==0.003
95125

@@ -121,19 +151,21 @@ def test_get_token_eth_input_price(self, client, token, qty):
121151
assertr
122152

123153
@pytest.mark.parametrize(
124-
"token0, token1, qty",
154+
"token0, token1, qty, kwargs",
125155
[
126-
(bat,dai,ONE_ETH),
127-
(dai,bat,ONE_ETH),
128-
(bat,dai,2*ONE_ETH),
129-
(weth,dai,ONE_ETH),
130-
(dai,weth,ONE_ETH),
156+
# BAT/DAI has no liquidity in V3
157+
# (bat, dai, ONE_ETH),
158+
# (dai, bat, ONE_ETH),
159+
# (bat, dai, 2 * ONE_ETH),
160+
(dai,usdc,ONE_ETH, {"fee":500}),
161+
(weth,dai,ONE_ETH, {}),
162+
(dai,weth,ONE_ETH, {}),
131163
],
132164
)
133-
deftest_get_token_token_input_price(self,client,token0,token1,qty):
134-
ifnotclient.version==2:
135-
pytest.skip("Tested methodonly supportedonUniswapv2")
136-
r=client.get_token_token_input_price(token0,token1,qty)
165+
deftest_get_token_token_input_price(self,client,token0,token1,qty,kwargs):
166+
ifclient.versionnotin [2,3]:
167+
pytest.skip("Tested methodnot supportedin thisUniswapversion")
168+
r=client.get_token_token_input_price(token0,token1,qty,**kwargs)
137169
assertr
138170

139171
@pytest.mark.parametrize(
@@ -163,19 +195,19 @@ def test_get_token_eth_output_price(self, client, token, qty):
163195
assertr
164196

165197
@pytest.mark.parametrize(
166-
"token0, token1, qty",
198+
"token0, token1, qty, kwargs",
167199
[
168-
(bat,dai,ONE_ETH),
169-
(dai,bat,ONE_ETH),
170-
(bat,dai,2*ONE_ETH),
171-
(weth,dai,ONE_ETH),
172-
(dai,weth,ONE_ETH),
200+
#(bat, dai, ONE_ETH),
201+
(dai,usdc,ONE_ETH, {"fee":500}),
202+
#(bat, dai, 2 * ONE_ETH),
203+
(weth,dai,ONE_ETH, {}),
204+
(dai,weth,ONE_ETH, {}),
173205
],
174206
)
175-
deftest_get_token_token_output_price(self,client,token0,token1,qty):
176-
ifnotclient.version==2:
177-
pytest.skip("Tested methodonly supportedonUniswapv2")
178-
r=client.get_token_token_output_price(token0,token1,qty)
207+
deftest_get_token_token_output_price(self,client,token0,token1,qty,kwargs):
208+
ifclient.versionnotin [2,3]:
209+
pytest.skip("Tested methodnot supportedin thisUniswapversion")
210+
r=client.get_token_token_output_price(token0,token1,qty,**kwargs)
179211
assertr
180212

181213
# ------ ERC20 Pool ----------------------------------------------------------------
@@ -241,11 +273,11 @@ def test_remove_liquidity(
241273
"input_token, output_token, qty, recipient, expectation",
242274
[
243275
# ETH -> Token
244-
(eth,bat,1_000_000_000*ONE_WEI,None,does_not_raise),
276+
(eth,dai,1_000_000_000*ONE_WEI,None,does_not_raise),
245277
# Token -> Token
246-
(bat,dai,1_000_000_000*ONE_WEI,None,does_not_raise),
278+
(dai,usdc,1_000_000_000*ONE_WEI,None,does_not_raise),
247279
# Token -> ETH
248-
(bat,eth,1_000_000*ONE_WEI,None,does_not_raise),
280+
(usdc,eth,1_000_000*ONE_WEI,None,does_not_raise),
249281
# (eth, bat, 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise),
250282
# (bat, eth, 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise),
251283
# (dai, bat, 0.00001 * ONE_ETH, ZERO_ADDRESS, does_not_raise),
@@ -256,6 +288,7 @@ def test_make_trade(
256288
self,
257289
client:Uniswap,
258290
web3:Web3,
291+
test_assets,
259292
input_token,
260293
output_token,
261294
qty:int,
@@ -278,9 +311,9 @@ def test_make_trade(
278311
"input_token, output_token, qty, recipient, expectation",
279312
[
280313
# ETH -> Token
281-
(eth,bat,1_000_000_000*ONE_WEI,None,does_not_raise),
314+
(eth,dai,1_000_000*ONE_WEI,None,does_not_raise),
282315
# Token -> Token
283-
(bat,dai,1_000_000_000*ONE_WEI,None,does_not_raise),
316+
(dai,usdc,1_000_000*ONE_WEI,None,does_not_raise),
284317
# Token -> ETH
285318
(dai,eth,1_000_000*ONE_WEI,None,does_not_raise),
286319
# FIXME: These should probably be uncommented eventually
@@ -301,6 +334,7 @@ def test_make_trade_output(
301334
self,
302335
client:Uniswap,
303336
web3:Web3,
337+
test_assets,
304338
input_token,
305339
output_token,
306340
qty:int,

‎uniswap/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from .uniswapimportUniswap,InvalidToken,InsufficientBalance
1+
from .uniswapimportUniswap,InvalidToken,InsufficientBalance,_str_to_addr
22
from .cliimportmain

‎uniswap/assets/uniswap-v1/exchange.abi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,4 +1002,4 @@
10021002
"type": "function",
10031003
"gas": 1713
10041004
}
1005-
]
1005+
]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp