mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-07 22:04:03 -05:00
doc/testnet/contract: py rpc example
This commit is contained in:
@@ -8,9 +8,9 @@ More information about the smart contracts architecture can be found
|
||||
|
||||
## Hello World
|
||||
|
||||
For the porpuses of this guide we are going to use the smart contract
|
||||
template found [here][3]. Its a very simple smart contract simulating a
|
||||
registration ledger, where users can create their membership keys and
|
||||
For the purposes of this guide we are going to use the smart contract
|
||||
template found [here][3]. It's a very simple smart contract simulating
|
||||
a registration ledger, where users can create their membership keys and
|
||||
register or remove themselves from it. Each user is represented as the
|
||||
[poseidon hash][4] of their membership public key.
|
||||
|
||||
@@ -22,7 +22,7 @@ $ git clone https://codeberg.org/darkrenaissance/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
|
||||
$ make
|
||||
@@ -47,13 +47,12 @@ return back to your `drk` interactive shell.
|
||||
|
||||
## Creating contracts
|
||||
|
||||
Each contract is controlled by a secret key, from which its Contract ID
|
||||
derives. To deploy a smart contract, we need to generate an authority
|
||||
keypair first. The Contract ID shown in the outputs is a placeholder
|
||||
Each contract is controlled by a secret key, from which its Contract
|
||||
ID derives. To deploy a smart contract, we first need to generate an
|
||||
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
|
||||
the one you generated by replacing the corresponding placeholder. We
|
||||
can create our own contract authority by executing the following
|
||||
command:
|
||||
the one you generated by replacing the corresponding placeholder. We can
|
||||
create our own contract authority by executing the following command:
|
||||
|
||||
```shell
|
||||
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
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
## Lock transaction
|
||||
@@ -219,11 +218,11 @@ will not exist in our contract registry.
|
||||
### Extending the smart contract client
|
||||
|
||||
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
|
||||
functionality. Following you will find examplatory code for retrieving
|
||||
a smart contract records from `darkfid` [JSON-RPC][5], which we can use
|
||||
to list our registry records:
|
||||
functionality. Following you will find example code for retrieving
|
||||
a smart contract's on-chain records from the [JSON-RPC][5] server in
|
||||
`darkfid` which we can use to list our registry records:
|
||||
|
||||
{{#tabs }}
|
||||
{{#tab name="Rust" }}
|
||||
@@ -264,7 +263,28 @@ if members.is_empty() {
|
||||
{{#endtab }}
|
||||
{{#tab name="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 }}
|
||||
{{#endtabs }}
|
||||
|
||||
Reference in New Issue
Block a user