forked from safe-global/safe-eth-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gp_cli.py
75 lines (63 loc) · 2.53 KB
/
gp_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from gnosis.eth.utils import fast_keccak
def confirm_prompt(question: str) -> bool:
reply = None
while reply not in ("y", "n"):
reply = input(f"{question} (y/n): ").lower()
return reply == "y"
if __name__ == "__main__":
import argparse
import os
import sys
import time
from gnosis.eth import EthereumNetwork
from gnosis.eth.constants import NULL_ADDRESS
from gnosis.protocol import GnosisProtocolAPI, Order, OrderKind
PRIVATE_KEY = os.environ.get("PRIVATE_KEY")
if not PRIVATE_KEY:
print("Set PRIVATE_KEY as an environment variable")
sys.exit(1)
parser = argparse.ArgumentParser(description="Place orders on Gnosis Protocol V2")
parser.add_argument(
"--network",
default=EthereumNetwork.RINKEBY.name,
help="Mainnet, Rinkeby or xDAI",
)
parser.add_argument("--from-token", required=True) # TODO Check checksummed address
parser.add_argument("--to-token", required=True)
parser.add_argument("--amount-wei", required=True, type=int)
parser.add_argument("--require-full-fill", action="store_true", default=False)
args = parser.parse_args()
from_token = args.from_token
to_token = args.to_token
amount_wei = args.amount_wei
gnosis_protocol_api = GnosisProtocolAPI(EthereumNetwork[args.network.upper()])
buy_amount_response = gnosis_protocol_api.get_estimated_amount(
from_token, to_token, OrderKind.SELL, amount_wei
)
buy_amount = int(buy_amount_response.get("amount", "0"))
if not buy_amount:
print("Cannot calculate amount to receive, maybe token is not supported")
sys.exit(1)
order = Order(
sellToken=from_token,
buyToken=to_token,
receiver=NULL_ADDRESS,
sellAmount=amount_wei,
buyAmount=buy_amount,
validTo=int(time.time()) + (60 * 60), # Valid for 1 hour
appData=fast_keccak(text="gp-cli"),
feeAmount=0,
kind="sell", # `sell` or `buy`
partiallyFillable=not args.require_full_fill,
sellTokenBalance="erc20", # `erc20`, `external` or `internal`
buyTokenBalance="erc20", # `erc20` or `internal`
)
order.feeAmount = gnosis_protocol_api.get_fee(order)
if confirm_prompt(
f"Exchanging {amount_wei} {from_token} to {buy_amount} {to_token}?"
):
result = gnosis_protocol_api.place_order(order, PRIVATE_KEY)
if isinstance(result, dict):
print(f"Cannot place order {result}")
else:
print(f"Placed order with UUID {result}")