-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrade.py
66 lines (57 loc) · 2.06 KB
/
trade.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
import json
from xrpl.models import XRP, IssuedCurrency
from xrpl.utils import xrp_to_drops
from xrpl.clients import JsonRpcClient
from xrpl.models.transactions import OfferCreate
from xrpl.wallet import Wallet
from xrpl.transaction import submit
from xrpl.asyncio.transaction import autofill_and_sign
from xrpl.transaction import submit_and_wait, sign_and_submit
# Load a wallet from a file
def load_wallet_from_file(filename):
with open(filename, 'r') as f:
wallet_dict = json.load(f)
print(wallet_dict)
# create a Wallet object from the loaded details
return Wallet(wallet_dict["public_key"], wallet_dict["private_key"])
wallet = load_wallet_from_file('wallets/wallet_mm.json')
# Define the network client
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
def build_tx(side, amount):
# Define the proposed trade.
tst_tx = {
"currency": IssuedCurrency(
currency="TST",
issuer="rP9jPyP5kyvFRb6ZiRghAGw5u8SGAmU4bd"
),
"value": amount,
}
xrp_txt = {
"currency": XRP(),
# 25 TST * 10 XRP per TST * 15% financial exchange (FX) cost
"value_buy": xrp_to_drops(amount * amount/2.5 * 1.15),
"value_sell": xrp_to_drops(amount * amount/2.5 * 0.4),
}
if side == 'buy':
buy_tx = OfferCreate(
account=wallet.address,
taker_gets=xrp_txt["value_buy"],
taker_pays=tst_tx["currency"].to_amount(tst_tx["value"]),
)
return buy_tx
if side == 'sell':
sell_tx = OfferCreate(
account=wallet.address,
taker_gets=tst_tx["currency"].to_amount(tst_tx["value"]),
taker_pays=xrp_txt["value_sell"],
)
return sell_tx
def trade_tst(side, amount):
if side == 'sell':
sell_tx = build_tx(side, amount)
return sign_and_submit(sell_tx, client, wallet)
if side == 'buy':
buy_tx = build_tx(side, amount)
return sign_and_submit(buy_tx, client, wallet)
# Buy: You pay 8.7XRP per TST (0.115)
# Sell: You get 8.5 XRP per TST (0.1176)