-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdexter.py
174 lines (134 loc) · 5.53 KB
/
dexter.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import cmd
import os
from xrpl.clients import JsonRpcClient
from xrpl.models.amounts import IssuedCurrencyAmount
from xrpl.models.transactions \
import AccountSet, AccountSetFlag, TrustSet, Payment, OfferCreateFlag
from xrpl.models.transactions.offer_create import OfferCreate
from xrpl.transaction \
import safe_sign_and_autofill_transaction, send_reliable_submission
from xrpl.utils import xrp_to_drops
from xrpl.wallet import Wallet, generate_faucet_wallet
TESTNET_URL = "https://s.altnet.rippletest.net:51234/"
# override the default by providing XRPL_JSONRPC_URL in the environment
XRPL_JSONRPC_URL = os.getenv("XRPL_JSONRPC_URL", TESTNET_URL)
client = JsonRpcClient(XRPL_JSONRPC_URL)
class Dexter(cmd.Cmd):
intro = "Welcome to Dexter!"
prompt = '(dexter) '
# exit EOF
def _close(self):
return True
def do_EOF(self, arg):
"""Exit with ^D"""
return self._close()
do_exit = do_EOF
def do_issue(self, arg):
"""Issue a new testnet currency
Parameters:
- token (for example, USD)
- amount (for example, 1000)
- receiver seed (generate on XRPL TestNet Faucet)
"""
print("Issuing currency")
args = arg.split()
token = args[0]
amount = args[1]
receiver_seed = args[2]
if len(token) > 3:
token_bytes = token.encode("ASCII").hex().upper()
token_symbol = '{:<040s}'.format(token_bytes)
else:
token_symbol = token.upper()
receiver = Wallet(receiver_seed, 0)
issuer = generate_faucet_wallet(client)
account_set = AccountSet(account=issuer.classic_address,
set_flag=AccountSetFlag.ASF_DEFAULT_RIPPLE)
signed_account_set = safe_sign_and_autofill_transaction(
account_set, issuer, client)
send_reliable_submission(signed_account_set, client)
trust_set = TrustSet.from_dict(
{
"account": receiver.classic_address,
"limit_amount": {
"issuer": issuer.classic_address,
"currency": token_symbol,
"value": amount
}
}
)
signed_trust_set = safe_sign_and_autofill_transaction(
trust_set, receiver, client)
send_reliable_submission(signed_trust_set, client)
usd_amount = IssuedCurrencyAmount(value=amount,
issuer=issuer.classic_address,
currency=token_symbol)
payment = Payment(account=issuer.classic_address,
destination=receiver.classic_address,
amount=usd_amount)
signed_payment = safe_sign_and_autofill_transaction(
payment,
issuer,
client)
send_reliable_submission(signed_payment, client)
print("Issued " + amount + " " + token + "." + issuer.classic_address)
def do_liquidity(self, arg):
"""Create orders on DEX
Parameters:
- token_with_address, format <token.issuer_address> (for example, USD.r4MHpHsaZnQ9L1F2Qh4YhXD1CaRA7SUz8v)
- amount (for example, 100)
- mid price (for example, 0.87)
- steps (for example, 3)
- interval - in bps of mid price - for example, 20
- receiver seed (use the same as in issuance)
"""
print("Creating liquidity on DEX")
args = arg.split()
token, issuer_address = args[0].split(".")
token_amount = args[1]
mid_price = float(args[2]) # Price of one XRP in token units
steps = int(args[3])
interval = float(args[4])
receiver_seed = args[5]
if len(token) > 3:
token_bytes = token.encode("ASCII").hex().upper()
token_symbol = '{:<040s}'.format(token_bytes)
else:
token_symbol = token.upper()
amount_in_token = IssuedCurrencyAmount(value=token_amount,
issuer=issuer_address,
currency=token_symbol)
receiver = Wallet(receiver_seed, 0)
for i in range(1, steps+1):
ask_price = mid_price * (1 + (interval * i) / 10000)
ask_xrp_amount = float(token_amount) / ask_price
place_order(amount_in_token,
xrp_to_drops(float(ask_xrp_amount)),
receiver,
None)
bid_price = mid_price * (1 - (interval * i) / 10000)
bid_xrp_amount = float(token_amount) / bid_price
place_order(xrp_to_drops(float(bid_xrp_amount)),
amount_in_token,
receiver,
OfferCreateFlag.TF_SELL)
print("Orders are created")
def place_order(amount_to_buy, amount_to_sell, receiver, flags):
if flags is None:
offer_create = \
OfferCreate(account=receiver.classic_address,
taker_gets=amount_to_sell,
taker_pays=amount_to_buy)
else:
offer_create = \
OfferCreate(account=receiver.classic_address,
taker_gets=amount_to_sell,
taker_pays=amount_to_buy,
flags=flags)
signed_offer_create = safe_sign_and_autofill_transaction(
offer_create,
receiver,
client)
send_reliable_submission(signed_offer_create, client)
if __name__ == '__main__':
Dexter().cmdloop()