-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit_order.py
More file actions
87 lines (75 loc) · 2.94 KB
/
limit_order.py
File metadata and controls
87 lines (75 loc) · 2.94 KB
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
import os
import asyncio
from eth_account import Account
from eth_account.messages import encode_defunct
import httpx
from dotenv import load_dotenv
from predict_sdk import (
OrderBuilder,
ChainId,
Side,
MarketHelperInput,
MarketHelperValueInput,
LimitHelperInput,
BuildOrderInput
)
def approve():
# Load environment variables
load_dotenv()
# Create a wallet to sign the message (must be the orders' `maker`)
private_key = os.getenv("WALLET_PRIVATE_KEY")
signer = Account.from_key(private_key)
builder = OrderBuilder.make(ChainId.BNB_MAINNET, signer)
# Calculate order amounts for a LIMIT order
amounts = builder.get_limit_order_amounts(
LimitHelperInput(
side=Side.BUY,
price_per_share_wei=10000000000000000, # 0.01 USDT per share
quantity_wei=100000000000000000000, # 100 shares
)
)
print(f"Maker Amount: {amounts.maker_amount}") # 1 USDT # 买方需要支付的USDT in wei
print(f"Taker Amount: {amounts.taker_amount}") # 买方将获得的份额 in wei
print(f"Last Price: {amounts.last_price}") # 0.01 USDT
print(f"Price Per Share: {amounts.price_per_share}") # 0.01 USDT
# - maker_amount: 买方需要支付的USDT(wei)
# - taker_amount: 买方将获得的份额(wei)
# - price_per_share: 加权平均价格
# - last_price: 最后一笔成交的价格
# Build an order
order = builder.build_order(
"LIMIT",
BuildOrderInput(
side=Side.BUY,
# Get from GET /markets endpoint
token_id="51923869364888026720633707255758662304830403989354809616621058202583057583874",
maker_amount=str(amounts.maker_amount),
taker_amount=str(amounts.taker_amount),
fee_rate_bps=200, # Get from GET /markets endpoint
),
)
# Now you can sign orders and interact with contracts
typed_data = builder.build_typed_data(
order,
is_neg_risk=False, # Get from GET /markets endpoint
is_yield_bearing=False, # Get from GET /markets endpoint
)
signed_order = builder.sign_typed_data_order(typed_data) # w3w is responsible for this
# Print all signed_order fields
print("\n=== Signed Order Fields ===")
print(f"salt: {signed_order.salt}")
print(f"maker: {signed_order.maker}")
print(f"signer: {signed_order.signer}")
print(f"taker: {signed_order.taker}")
print(f"token_id: {signed_order.token_id}")
print(f"maker_amount: {signed_order.maker_amount}")
print(f"taker_amount: {signed_order.taker_amount}")
print(f"expiration: {signed_order.expiration}")
print(f"nonce: {signed_order.nonce}")
print(f"fee_rate_bps: {signed_order.fee_rate_bps}")
print(f"side: {signed_order.side}")
print(f"signature_type: {signed_order.signature_type}")
print(f"signature: {signed_order.signature}")
print(f"hash: {signed_order.hash}")
if __name__ == '__main__':
approve()