forked from Jaammerr/python_sui_sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
266 lines (219 loc) · 8.02 KB
/
client.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import base64
import uuid
import bip_utils
import hashlib
import nacl
import requests
import pyuseragents
from typing import Optional
from mnemonic import Mnemonic
from .models import SignatureScheme, TransferSuiParams, TransferObjectParams, MoveCallParams, ExecuteType, \
BatchTransactionParams, DryRunTransactionParams, MergeCoinsParams, PayParams, PayAllParams, PublishParams, \
SplitCoinParams, SplitCoinEqualParams
mnemo = Mnemonic("english")
class Client:
def __init__(self, faucet_url: str, rpc_url: str, mnemonic: str = True,
derivation_path="m/44'/784'/0'/0'/0'"):
self.faucet_url = faucet_url
self.rpc_url = rpc_url
self.derivation_path = derivation_path
self.session = requests.Session()
self.headers = {
'authority': 'fullnode.testnet.sui.io',
'accept': '*/*',
'accept-language': 'uk,en;q=0.9,en-GB;q=0.8,en-US;q=0.7',
'origin': 'chrome-extension://opcgpfmipidbgpenhmajoajpbobppdil',
'user-agent': pyuseragents.random(),
}
if mnemonic:
self.mnemonic = mnemonic
self.bip39_seed = bip_utils.Bip39SeedGenerator(self.mnemonic).Generate()
self.bip32_ctx = bip_utils.Bip32Slip10Ed25519.FromSeed(self.bip39_seed)
self.bip32_der_ctx = self.bip32_ctx.DerivePath(derivation_path)
self.private_key: bytes = self.bip32_der_ctx.PrivateKey().Raw().ToBytes()
self.public_key: bytes = self.bip32_der_ctx.PublicKey().RawCompressed().ToBytes()
def get_address(self) -> Optional[str]:
return "0x" + hashlib.sha3_256(self.bip32_der_ctx.PublicKey().RawCompressed().ToBytes()).digest().hex()[:40]
def get_public_key_as_b64_string(self) -> Optional[str]:
return base64.b64encode(self.public_key[1:]).decode()
def batch_transaction(self, tx: BatchTransactionParams):
return self.send_request_to_rpc(
method="sui_batchTransaction",
params=[
self.get_address(),
tx.single_transaction_params,
tx.gas_budget,
tx.gas,
tx.gas_budget
])
def dryrun_transaction(self, tx: DryRunTransactionParams):
return self.send_request_to_rpc(
method='sui_dryRunTransaction',
params=[
tx.tx_bytes
]
)
def mint_example_nft(self) -> Optional[dict]:
response = self.send_request_to_rpc(
method='sui_moveCall',
params=[
self.get_address()[2:],
'0x2',
'devnet_nft',
'mint',
[],
["Example NFT", "An NFT created by Sui Wallet",
"ipfs://QmZPWWy5Si54R3d26toaqRiqvCH7HkGdXkxwUgCm2oKKM2?filename=img-sq-01.png"],
None,
10000,
])
tx_bytes = base64.b64decode(str(response['result']['txBytes']))
return self.sign_and_execute_transaction(tx_bytes)
def mint_wizard_nft(self) -> Optional[dict]:
response = self.send_request_to_rpc(
method='sui_moveCall',
params=[
self.get_address()[2:],
'0x2',
'devnet_nft',
'mint',
[],
["Wizard Land", "Expanding The Magic Land",
"https://gateway.pinata.cloud/ipfs/QmYfw8RbtdjPAF3LrC6S3wGVwWgn6QKq4LGS4HFS55adU2?w=800&h=450&c=crop"],
None,
10000,
])
tx_bytes = base64.b64decode(str(response['result']['txBytes']))
return self.sign_and_execute_transaction(tx_bytes)
def move_call(self, tx: MoveCallParams):
return self.send_request_to_rpc(
method="sui_moveCall",
params=[
self.get_address(),
tx.package_object_id,
tx.module,
tx.function,
tx.type_arguments,
tx.arguments,
tx.gas_payment,
tx.gas_budget,
])
def transfer_object(self, tx: TransferObjectParams):
return self.send_request_to_rpc(
method="sui_transferObject",
params=[
self.get_address(),
tx.object_id,
tx.gas_payment,
tx.gas_budget,
tx.recipient
])
def transfer_sui(self, tx: TransferSuiParams):
return self.send_request_to_rpc(
method="sui_transferObject",
params=[
self.get_address(),
tx.sui_object_id,
tx.gas_budget,
tx.recipient,
tx.amount
])
def merge_coins(self, tx: MergeCoinsParams):
return self.send_request_to_rpc(
method='sui_mergeCoins',
params=[
self.get_address(),
tx.primary_coin,
tx.coin_to_merge,
tx.gas,
tx.gas_budget
]
)
def pay_sui(self, tx: PayParams):
return self.send_request_to_rpc(
method='sui_pay',
params=[
self.get_address(),
tx.input_coins,
tx.recipients,
tx.amounts,
tx.gas,
tx.gas_budget
]
)
def pay_all_sui(self, tx: PayAllParams):
return self.send_request_to_rpc(
method='sui_payAllSui',
params=[
self.get_address(),
tx.input_coins,
tx.recipient,
tx.gas_budget
]
)
def publish(self, tx: PublishParams):
return self.send_request_to_rpc(
method='sui_publish',
params=[
self.get_address(),
tx.compiled_modules,
tx.gas,
tx.gas_budget
]
)
def split_coin(self, tx: SplitCoinParams):
return self.send_request_to_rpc(
method='sui_splitCoin',
params=[
self.get_address(),
tx.coin_object_id,
tx.split_amounts,
tx.gas,
tx.gas_budget
]
)
def split_coin_equal(self, tx: SplitCoinEqualParams):
return self.send_request_to_rpc(
method='sui_splitCoinEqual',
params=[
self.get_address(),
tx.coin_object_id,
tx.split_count,
tx.gas,
tx.gas_budget
]
)
def sign_data(self, data: bytes) -> Optional[bytes]:
return nacl.signing.SigningKey(self.private_key).sign(data)[:64]
def sign_and_execute_transaction(self, tx_bytes: bytes) -> Optional[dict]:
signature_bytes = self.sign_data(tx_bytes)
x_bytes_b64_encoded = base64.b64encode(tx_bytes).decode()
signature_b64_encoded = base64.b64encode(signature_bytes).decode()
pubkey_b64_encoded = self.get_public_key_as_b64_string()
signature_scheme = SignatureScheme.ED25519
execute_type = ExecuteType.WaitForLocalExecution
return self.send_request_to_rpc(method='sui_executeTransaction', params=[
x_bytes_b64_encoded,
signature_scheme,
signature_b64_encoded,
pubkey_b64_encoded,
execute_type
])
def send_request_to_rpc(
self,
method: str,
params: list = None,
request_id: str = None
) -> Optional[dict]:
response = requests.post(
self.rpc_url, json={
"jsonrpc": "2.0",
"method": method,
"params": params or [],
"id": request_id or str(uuid.uuid4()),
})
if response.status_code <= 201:
return response.json()
else:
raise Exception(
f'Failed to send transaction | Response status: {response.status_code} | Details: {response.text}')