Search code examples
pythonblockchainsoliditysmartcontractshedera-hashgraph

Hedera-sdk-py Smart Contract Deployment and Querying


I am creating a DApp on the Hedera Blockchain using Hedera-sdk-py, a python wrapper of Hedera SDK in Java. I want to create a smart contract, deploy and query it, but I can not seem to understand the stateful.json file in the documentation (https://github.com/wensheng/hedera-sdk-py). Is the stateful.json a compiled solidity contact, how can I convert my .sol contract to that format, and how can I store data in it after deploying. I have added a link to the stateful.json file below. Any help will be really appreciated.

import os
import json

from hedera import (
    Hbar,
    FileCreateTransaction,
    ContractCreateTransaction,
    ContractCallQuery,
    ContractExecuteTransaction,
    ContractFunctionParameters,
)
from get_client import client, OPERATOR_KEY

client.setMaxTransactionFee(Hbar(100))
client.setMaxQueryPayment(Hbar(10))

cur_dir = os.path.abspath(os.path.dirname(__file__))
jsonf = open(os.path.join(cur_dir, "stateful.json"))
stateful_json = json.load(jsonf)
jsonf.close()
byteCode = stateful_json['object'].encode()

tran = FileCreateTransaction()
resp = tran.setKeys(OPERATOR_KEY
      ).setContents(byteCode
      ).execute(client)
fileId = resp.getReceipt(client).fileId
print("contract bytecode file: ", fileId.toString())

tran = ContractCreateTransaction()
resp = tran.setGas(500_000
      ).setBytecodeFileId(fileId
      ).setConstructorParameters(
            ContractFunctionParameters().addString("hello from hedera!")
      ).execute(client)
contractId = resp.getReceipt(client).contractId
print("new contract id: ", contractId.toString())

# 600 < gas fee < 1000
result = (ContractCallQuery()
      .setGas(50000)
      .setContractId(contractId)
      .setFunction("get_message")
      .setQueryPayment(Hbar(1))
      .execute(client))

if result.errorMessage:
    exit("error calling contract: ", result.errorMessage)

message = result.getString(0)
print("contract returned message: ", message)

resp = (ContractExecuteTransaction()
    .setGas(200_000)
    .setContractId(contractId)
    .setFunction("set_message",
                 ContractFunctionParameters().addString("hello from hedera again!")
                )
    .setMaxTransactionFee(Hbar(2))
    .execute(client))

# if this doesn't throw, then we know the contract executed successfully
receipt = resp.getReceipt(client)

# now query contract
result = (ContractCallQuery()
      .setGas(50000)
      .setContractId(contractId)
      .setFunction("get_message")
      .setQueryPayment(Hbar(1))
      .execute(client))

if result.errorMessage:
   exit("error calling contract: ", result.errorMessage)

message = result.getString(0)
print("contract returned message: ", message)

Stateful.json https://github.com/wensheng/hedera-sdk-py/blob/main/examples/stateful.json


Solution

  • You can use Remix IDE to get that info. If you compile a contract, you will find an {Contract Name}.json in contracts/artifacts. Open it and find "object". You will see what you want.