-
Notifications
You must be signed in to change notification settings - Fork 0
/
soroban_invoke_contract_function.py
68 lines (55 loc) · 2.64 KB
/
soroban_invoke_contract_function.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
"""This example shows how to call the Soroban contract.
The current API is in an unstable state.
1. You need to follow [this tutorial](https://soroban.stellar.org/docs/tutorials/deploy-to-local-network)
to deploy the [Hello World contract](https://github.com/stellar/soroban-examples/tree/main/hello_world) first.
2. Install Stellar Python SDK from soroban branch:
pip install git+https://github.com/StellarCN/py-stellar-base.git@soroban
3. Modify the necessary parameters in this script, then run it.
"""
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
from stellar_sdk.soroban.types import Symbol
# TODO: You need to replace the following parameters according to the actual situation
secret = "SDMVWEWUZVYDACEM3G2X2XT4W4BP3OSZHX3KXVOQTJMDIUQQLKAIWK7Z"
rpc_server_url = "https://horizon-futurenet.stellar.cash:443/soroban/rpc"
contract_id = "940985b099a50abf77b8e3a245471d1912f5c1d8502da07128619aa778d90c1f"
network_passphrase = Network.FUTURENET_NETWORK_PASSPHRASE
kp = Keypair.from_secret(secret)
soroban_server = SorobanServer(rpc_server_url)
source = soroban_server.load_account(kp.public_key)
# Let's build a transaction that invokes the `hello` function.
tx = (
TransactionBuilder(source, network_passphrase)
.set_timeout(300)
.append_invoke_contract_function_op(
contract_id=contract_id,
function_name="hello",
parameters=[Symbol("World")],
source=kp.public_key,
)
.build()
)
simulate_transaction_data = soroban_server.simulate_transaction(tx)
print(f"simulated transaction: {simulate_transaction_data}")
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}")
if get_transaction_status_data.status == TransactionStatus.SUCCESS:
result = stellar_xdr.SCVal.from_xdr(get_transaction_status_data.results[0].xdr) # type: ignore
output = [x.sym.sc_symbol.decode() for x in result.obj.vec.sc_vec] # type: ignore
print(f"transaction result: {output}")