doc/testnet/contract: py rpc example

This commit is contained in:
x
2026-01-02 16:54:06 +00:00
parent e579f5a6ab
commit 29501efe04

View File

@@ -8,9 +8,9 @@ More information about the smart contracts architecture can be found
## Hello World ## Hello World
For the porpuses of this guide we are going to use the smart contract For the purposes of this guide we are going to use the smart contract
template found [here][3]. Its a very simple smart contract simulating a template found [here][3]. It's a very simple smart contract simulating
registration ledger, where users can create their membership keys and a registration ledger, where users can create their membership keys and
register or remove themselves from it. Each user is represented as the register or remove themselves from it. Each user is represented as the
[poseidon hash][4] of their membership public key. [poseidon hash][4] of their membership public key.
@@ -22,7 +22,7 @@ $ git clone https://codeberg.org/darkrenaissance/smart-contract
$ cd smart-contract $ cd smart-contract
``` ```
Here, generate the contract `WASM` bincode and its client by executing: Here, generate the contract WASM bincode and its client by executing:
```shell ```shell
$ make $ make
@@ -47,13 +47,12 @@ return back to your `drk` interactive shell.
## Creating contracts ## Creating contracts
Each contract is controlled by a secret key, from which its Contract ID Each contract is controlled by a secret key, from which its Contract
derives. To deploy a smart contract, we need to generate an authority ID derives. To deploy a smart contract, we first need to generate an
keypair first. The Contract ID shown in the outputs is a placeholder authority keypair. The Contract ID shown in the outputs is a placeholder
for the one that will be generated from you. In rest of the guide, use for the one that will be generated from you. In rest of the guide, use
the one you generated by replacing the corresponding placeholder. We the one you generated by replacing the corresponding placeholder. We can
can create our own contract authority by executing the following create our own contract authority by executing the following command:
command:
```shell ```shell
drk> contract generate-deploy drk> contract generate-deploy
@@ -106,7 +105,7 @@ history. We can also export the deployed data by executing:
drk> contract export-data {TX_HASH} > membership.dat drk> contract export-data {TX_HASH} > membership.dat
``` ```
The exported files contains the `WASM` bincode and instruction data The exported files contains the WASM bincode and instruction data
deployed by that transaction, encoded in `base64` as a tuple. deployed by that transaction, encoded in `base64` as a tuple.
## Lock transaction ## Lock transaction
@@ -219,11 +218,11 @@ will not exist in our contract registry.
### Extending the smart contract client ### Extending the smart contract client
The template client is barebones and doesn't provide us with a way to The template client is barebones and doesn't provide us with a way to
view the on chain records of our registry. For that porpuse we can view the on chain records of our registry. For that purpose we can
create a new small program, or extend the client to support this create a new small program, or extend the client to support this
functionality. Following you will find examplatory code for retrieving functionality. Following you will find example code for retrieving
a smart contract records from `darkfid` [JSON-RPC][5], which we can use a smart contract's on-chain records from the [JSON-RPC][5] server in
to list our registry records: `darkfid` which we can use to list our registry records:
{{#tabs }} {{#tabs }}
{{#tab name="Rust" }} {{#tab name="Rust" }}
@@ -264,7 +263,28 @@ if members.is_empty() {
{{#endtab }} {{#endtab }}
{{#tab name="Python" }} {{#tab name="Python" }}
```python ```python
print("TODO") import json
import socket
payload = json.dumps({
"jsonrpc": "2.0",
"method": "blockchain.get_contract_state",
"params": [contract_id, "smart-contract-members"],
"id": 1,
})
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((host, port))
sock.sendall((payload + "\n").encode("utf-8"))
response = b""
while True:
chunk = sock.recv(1)
if not chunk or chunk == b"\n":
break
response += chunk
print(json.loads(response.decode("utf-8")))
``` ```
{{#endtab }} {{#endtab }}
{{#endtabs }} {{#endtabs }}