-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontract_call.py
More file actions
55 lines (42 loc) · 1.92 KB
/
Copy pathcontract_call.py
File metadata and controls
55 lines (42 loc) · 1.92 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
#!/usr/bin/env python3
"""Example 2 — QVM container call (QCore.py)
Read from and invoke a QAsset token on the Quantova Virtual Machine. The QVM is a
register machine that runs compiled containers. Contracts are written in Quanta and
compiled to a container. You read container state through the gateway with
get_container and get_storage, and you invoke a container by submitting an ML-DSA-65
signed transaction.
Run:
export QUANTOVA_GATEWAY=http://127.0.0.1:8645
python contract_call.py
"""
from _common import banner, connect, use_demo_backend
TOKEN = "Q1QDF7WZ2R9V5C3XM8H6U0AGNE2SKTV4YP9CJ3" # example QAsset container
HOLDER = "Q1GP3FR9CVK3XAJSQGC7DU47WCDUVTFDYMY0H82C" # example holder
def main():
banner("Quantova example: QVM container call (QAsset) (QCore.py)")
use_demo_backend()
q = connect()
print(f"Head height: {q.head()['height']}")
# Confirm the container exists on chain.
container = q.get_container(TOKEN)
print(f"Token container: {TOKEN}")
print(f"code size: {container.get('size', 'n/a')} bytes")
token = q.contract(TOKEN)
# Read a QAsset balance. Secured contract state is a map from a 32 byte key to a
# single word value, so a balance reads back as one word.
bal = token.balance_of(HOLDER)
print(f"\nbalanceOf({HOLDER}) = {bal} (a single word map value)")
# Build and ML-DSA-65 sign an invoke, transfer 25 units.
sender = q.wallet.create()
signed = token.build_transfer(sender, to=HOLDER, amount=25)
print(f"\nInvoke: transfer 25 units of the QAsset, from {sender.address}")
print(f"ML-DSA-65 signature: {signed.signature_hex[:34]}...")
try:
result = q.submit_transaction(signed)
print(f"Submitted. tx hash: {result['hash']}")
tx = q.get_transaction(result["hash"])
print(f"status: {tx.get('status')}")
except Exception as e:
print(f"(submit step: {e})")
if __name__ == "__main__":
main()