-
Notifications
You must be signed in to change notification settings - Fork 0
/
soroban_deploy_contract.py
107 lines (87 loc) · 3.75 KB
/
soroban_deploy_contract.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
"""This example shows how to deploy a compiled contract to the Stellar network.
"""
import time
from stellar_sdk import Network, Keypair, TransactionBuilder
from stellar_sdk import xdr as stellar_xdr
from stellar_sdk.soroban import SorobanServer
from stellar_sdk.soroban.soroban_rpc import TransactionStatus
# TODO: You need to replace the following parameters according to the actual situation
secret = "S..." #Put your Account Secret Key here
rpc_server_url = "https://horizon-futurenet.stellar.cash:443/soroban/rpc"
network_passphrase = Network.FUTURENET_NETWORK_PASSPHRASE
contract_file_path = "./soroban_hello_world_contract.wasm"
kp = Keypair.from_secret(secret)
soroban_server = SorobanServer(rpc_server_url)
print("installing contract...")
source = soroban_server.load_account(kp.public_key)
# with open(contract_file_path, "rb") as f:
# contract_bin = f.read()
tx = (
TransactionBuilder(source, network_passphrase)
.set_timeout(300)
.append_install_contract_code_op(
contract=contract_file_path, # the path to the contract, or binary data
source=kp.public_key,
)
.build()
)
simulate_transaction_data = soroban_server.simulate_transaction(tx)
print(f"simulated transaction: {simulate_transaction_data}")
# The footpoint is predictable, maybe we can optimize the code to omit this step
print(f"setting footprint and signing transaction...")
assert simulate_transaction_data.results is not None
tx.set_footpoint(simulate_transaction_data.results[0].footprint)
tx.sign(kp)
send_transaction_data = soroban_server.send_transaction(tx)
print(f"sent transaction: {send_transaction_data}")
while True:
print("waiting for transaction to be confirmed...")
get_transaction_status_data = soroban_server.get_transaction_status(
send_transaction_data.id
)
if get_transaction_status_data.status != TransactionStatus.PENDING:
break
time.sleep(3)
print(f"transaction status: {get_transaction_status_data}")
wasm_id = None
if get_transaction_status_data.status == TransactionStatus.SUCCESS:
result = stellar_xdr.SCVal.from_xdr(get_transaction_status_data.results[0].xdr) # type: ignore
wasm_id = result.obj.bin.hex() # type: ignore
print(f"wasm id: {wasm_id}")
assert wasm_id, "wasm id should not be empty"
print("creating contract...")
source = soroban_server.load_account(
kp.public_key
) # refresh source account, because the current SDK will increment the sequence number by one after building a transaction
tx = (
TransactionBuilder(source, network_passphrase)
.set_timeout(300)
.append_create_contract_op(
wasm_id=wasm_id,
source=kp.public_key,
)
.build()
)
simulate_transaction_data = soroban_server.simulate_transaction(tx)
print(f"simulated transaction: {simulate_transaction_data}")
# The footpoint is predictable, maybe we can optimize the code to omit this step
print(f"setting footprint and signing transaction...")
assert simulate_transaction_data.results is not None
tx.set_footpoint(simulate_transaction_data.results[0].footprint)
tx.sign(kp)
send_transaction_data = soroban_server.send_transaction(tx)
print(f"sent transaction: {send_transaction_data}")
while True:
print("waiting for transaction to be confirmed...")
get_transaction_status_data = soroban_server.get_transaction_status(
send_transaction_data.id
)
if get_transaction_status_data.status != TransactionStatus.PENDING:
break
time.sleep(3)
print(f"transaction status: {get_transaction_status_data}")
wasm_id = None
if get_transaction_status_data.status == TransactionStatus.SUCCESS:
result = stellar_xdr.SCVal.from_xdr(get_transaction_status_data.results[0].xdr) # type: ignore
contract_id = result.obj.bin.hex() # type: ignore
print(f"contract id: {contract_id}")