-
Notifications
You must be signed in to change notification settings - Fork 40
/
pgql_s_ptb.py
216 lines (187 loc) · 6.67 KB
/
pgql_s_ptb.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
# Copyright Frank V. Castellucci
# SPDX-License-Identifier: Apache-2.0
# -*- coding: utf-8 -*-
"""Sample module for pysui beta Transaction Builder leveraging Sui GraphQL."""
import base64
from pysui import SuiConfig, SyncGqlClient, PysuiConfiguration
from pysui.sui.sui_clients.common import SuiRpcResult
import pysui.sui.sui_pgql.pgql_query as qn
import pysui.sui.sui_pgql.pgql_types as pgql_type
from pysui.sui.sui_pgql.pgql_sync_txn import SuiTransaction
def handle_result(result: SuiRpcResult) -> SuiRpcResult:
"""."""
if result.is_ok():
if hasattr(result.result_data, "to_json"):
print(result.result_data.to_json(indent=2))
return result.result_data
print(result.result_data)
else:
print(result.result_string)
if result.result_data and hasattr(result.result_data, "to_json"):
print(result.result_data.to_json(indent=2))
else:
print(result.result_data)
return result
def transaction_inspect(txb: SuiTransaction):
"""Uses defaults for DryRunTransaction and just the TransactionKind"""
# Print the TransactionType BCS (pre-serialized) structure
print(txb.raw_kind().to_json(indent=2))
# Execute the dry run
handle_result(
txb.client.execute_query_node(
with_node=qn.DryRunTransactionKind(tx_bytestr=txb.build_for_dryrun())
)
)
def transaction_dryrun(txer: SuiTransaction):
"""Uses fully built TransactionData for DryRunTransaction"""
raw_kind = txer.transaction_data()
# Print the TransactionData BCS (pre-serialized) structure
print(raw_kind.to_json(indent=2))
# Execute the dry run
handle_result(
txer.client.execute_query_node(
with_node=qn.DryRunTransaction(tx_bytestr=txer.build())
)
)
def transaction_dryrun_with_gas(txer: SuiTransaction, coin_ids: list[str]):
"""Uses fully built TransactionData for DryRunTransaction"""
raw_kind = txer.transaction_data(use_gas_objects=coin_ids)
# Print the TransactionData BCS (pre-serialized) structure
print(raw_kind.to_json(indent=2))
# Execute the dry run
handle_result(
txer.client.execute_query_node(
with_node=qn.DryRunTransaction(tx_bytestr=txer.build(coin_ids))
)
)
def transaction_execute(txer: SuiTransaction):
"""Uses fully built and serialized TransactionData for ExecuteTransaction."""
# Execute the transaction
handle_result(
txer.client.execute_query_node(
with_node=qn.ExecuteTransaction(**txer.build_and_sign())
)
)
def demo_tx_split(client: SyncGqlClient):
"""Demonstrate GraphQL Beta PTB with split and transfer."""
txb = SuiTransaction(client=client)
scoin = txb.split_coin(
coin=txb.gas,
amounts=[100000000],
)
txb.transfer_objects(transfers=[scoin], recipient=client.config.active_address)
#### Uncomment the action to take
transaction_inspect(txb)
# transaction_dryrun(txb)
# transaction_dryrun_with_gas(
# txb,
# [
# "<ENTER ONE OR MORE COIN IDS TO PAY",
# ],
# )
# transaction_execute(txb)
def demo_tx_split_equal(client: SyncGqlClient):
"""Demonstrate GraphQL Beta PTB with split coin to equal parts and keeps in owner."""
txb = SuiTransaction(client=client)
scoin = txb.split_coin_equal(
coin="<ENTER COID ID TO SPLIT STRING>",
split_count=3,
)
#### Uncomment the action to take
# transaction_inspect(txb)
transaction_dryrun(txb)
# transaction_dryrun_with_gas(
# txb,
# [
# "<ENTER ONE OR MORE COIN IDS TO PAY",
# ],
# )
# transaction_execute(txb)
def demo_tx_unstake(client: SyncGqlClient):
"""Demonstrate GraphQL Beta PTB with unstaking 1 coin if found."""
owner = client.config.active_address
skblk: pgql_type.SuiStakedCoinsGQL = handle_result(
client.execute_query_node(with_node=qn.GetDelegatedStakes(owner=owner))
)
# Only execute if staked coin found
if skblk.staked_coins:
txb = SuiTransaction(client=client)
txb.unstake_coin(staked_coin=skblk.staked_coins[0])
transaction_inspect(txb)
# transaction_dryrun(txb)
# transaction_dryrun_with_gas(
# txb,
# [
# "<ENTER ONE OR MORE COIN IDS TO PAY",
# ],
# )
# transaction_execute(txb)
else:
print("No staked coins found")
def demo_tx_transfer_sui(client: SyncGqlClient):
"""Demonstrate GraphQL Beta PTB with transferring sui."""
txb = SuiTransaction(client=client)
txb.transfer_sui(
recipient="<ENTER RECIPIENT ADDRESS STRING>",
from_coin=txb.gas,
amount=100000000,
)
transaction_inspect(txb)
# transaction_dryrun(txb)
# transaction_dryrun_with_gas(
# txb,
# [
# "<ENTER ONE OR MORE COIN IDS TO PAY",
# ],
# )
# transaction_execute(txb)
def demo_tx_public_transfer(client: SyncGqlClient):
"""Demonstrate GraphQL Beta PTB with public transfer object."""
txb = SuiTransaction(client=client)
txb.public_transfer_object(
object_to_send="<ENTER OBJECT ID TO SEND STRING>",
recipient="<ENTER RECIPIENT ADDRESS STRING>",
object_type="<ENTER OBJECT TYPE STRING>",
)
# transaction_inspect(txb)
transaction_dryrun(txb)
# transaction_dryrun_with_gas(
# txb,
# [
# "<ENTER ONE OR MORE COIN IDS TO PAY",
# ],
# )
# transaction_execute(txb)
def demo_tx_publish(client: SyncGqlClient):
"""Demonstrate publishing a package."""
txb = SuiTransaction(client=client)
upg_cap = txb.publish(project_path="<ENTER SUI MOVE PROJECT PATH>")
txb.transfer_objects(transfers=[upg_cap], recipient=client.config.active_address)
transaction_inspect(txb)
# transaction_dryrun(txb)
# transaction_dryrun_with_gas(
# txb,
# [
# "<ENTER ONE OR MORE COIN IDS TO PAY",
# ],
# )
# transaction_execute(txb)
if __name__ == "__main__":
client_init = SyncGqlClient(
write_schema=False,
pysui_config=PysuiConfiguration(
group_name=PysuiConfiguration.SUI_GQL_RPC_GROUP
),
)
print(f"Default schema base version '{client_init.base_schema_version}'")
print(f"Default schema build version '{client_init.schema_version()}'")
try:
# print()
demo_tx_split(client_init)
# demo_tx_split_equal(client_init)
# demo_tx_public_transfer(client_init)
# demo_tx_unstake(client_init)
# demo_tx_transfer_sui(client_init)
# demo_tx_publish(client_init)
except ValueError as ve:
print(ve.args)