-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_json_format.py
More file actions
122 lines (102 loc) · 3.67 KB
/
debug_json_format.py
File metadata and controls
122 lines (102 loc) · 3.67 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
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
#!/usr/bin/env python3
"""
Debug JSON format differences between Python and Rust SDKs
"""
import json
import sys
import os
sys.path.insert(0, os.path.dirname(__file__))
from lightpool_sdk import (
LightPoolClient, Signer, TransactionBuilder, ActionBuilder,
Address, ObjectID, U256,
CreateTokenParams, CreateMarketParams, PlaceOrderParams, CancelOrderParams,
OrderSide, TimeInForce, MarketState, LimitOrderParams, OrderParamsType,
TOKEN_CONTRACT_ADDRESS, SPOT_CONTRACT_ADDRESS, create_limit_order_params
)
def debug_json_format():
"""Debug the JSON format being sent"""
# Create a simple transaction to test
signer = Signer.new()
address = signer.address()
# Create a simple place order action
order_params = create_limit_order_params(
side=OrderSide.SELL,
amount=5_000_000,
limit_price=50_000_000_000,
tif=TimeInForce.GTC
)
# Create action
action = ActionBuilder.place_order(
market_address=SPOT_CONTRACT_ADDRESS,
market_id=ObjectID.random(),
balance_id=ObjectID.random(),
params=order_params
)
# Create transaction
tx = TransactionBuilder.new()\
.sender(address)\
.expiration(0xFFFFFFFFFFFFFFFF)\
.add_action(action)\
.build_and_sign(signer)
# Extract the JSON that would be sent
transaction_dict = {
"sender": list(address.to_bytes()),
"expiration": tx.signed_transaction.transaction.expiration,
"actions": [
{
"inputs": [list(bytes.fromhex(str(obj_id).replace('0x', ''))) for obj_id in action.input_objects],
"contract": list(action.target_address.to_bytes()),
"action": 746789037603618816, # ord_place
"params": list(action.params)
}
]
}
signatures_list = [
{
"part1": list(tx.signed_transaction.signatures[0][:32]),
"part2": list(tx.signed_transaction.signatures[0][32:])
}
]
submit_transaction_params = {
"tx": {
"transaction": transaction_dict,
"signatures": signatures_list
}
}
print("=== JSON Format Debug ===")
print(f"Transaction JSON:")
print(json.dumps(transaction_dict, indent=2))
print(f"\nSubmitTransactionParams:")
print(json.dumps(submit_transaction_params, indent=2))
# Test the action name encoding
print(f"\n=== Action Name Debug ===")
print(f"Action name: {action.action_name}")
# Test the _action_name_to_u64 function
def action_name_to_u64(action_name: str) -> int:
BASE = 32
NAME_LENGTH = 12
if len(action_name) > NAME_LENGTH:
raise ValueError(f"Action name too long: {action_name}")
result = 0
for c in action_name:
if c == '_':
digit = 0
elif '1' <= c <= '5':
digit = ord(c) - ord('1') + 1
elif 'a' <= c <= 'z':
digit = ord(c) - ord('a') + 6
else:
raise ValueError(f"Invalid character in action name: {c}")
result = result * BASE + digit
# 用零填充到12个字符
chars_processed = len(action_name)
while chars_processed < NAME_LENGTH:
result = result * BASE
chars_processed += 1
return result
encoded = action_name_to_u64(action.action_name)
print(f"Encoded action name: {encoded}")
print(f"Expected for 'ord_place': 746789037603618816")
print(f"Match: {encoded == 746789037603618816}")
if __name__ == "__main__":
debug_json_format()