Smart Contract Guide - HcashOrg/Hcash GitHub Wiki

In HCash, every smart contract is a legal bytecode registered in HCash blockchain.

Contract has some APIs and offline APIs for use. User can call contract's API/offline-API with arguments. The difference between contract's API and offline-API is whether the operation is broadcast on the chain.

Every contract has its own independent state store called 'storage'. Contracts can query data from storage and save data to storage. The storage exists as data-diff commits on the HCash blockchain.

A very very simple contract looks like:

type Storage = {
  name: string,
  age: int,
  users: Array<string>
}

var M = Contract<Storage>()

function M:init()
  -- this is a comment after symbol '--'
  -- save some storage to blockchain
  self.storage.name = "hello"
  self.storage.age = 100
  self.storage.users = []
end

function M:on_deposit(amount: int)
  -- on_deposit function will be called when user transfer asset to this contract
end

-- this is a contract-API. user can call it with argument
function M:sayHello(arg: string)
  print("sayHello arg: ", arg)
  table.append(self.storage.users, caller_address)  -- caller_address is the address of operator user
  self.storage.age = self.storage.age + 1
end

-- this is a contract-offline-API. user can use it to query data from this contract
offline function M:queryUsers(_: string)
  let usersStr = json.dumps(self.storage.users)
  return usersStr
end

return M  -- this indicates which object is the instance of the contract

You can save these contract code to a contract source file, named like 'hello.glua'.

Suppose you have started the HCash blockchain program at the command line.

The next steps are:

  • compile contract

compile_contract the-contract-source-path

This will generate a filename.gpc file in the same directory of contract source file

  • register contract to blockchain

register_contract caller-account-name path-of-contract-gpc-file system-asset-symbol gasLimit

the system-asset-symbol is HSR now. and the gasLimt is the maximum contract call costs you allow. gasLimit will not necessarily be fully deducted. The actual gas consumption is based on the contract's execution.

the register_contract command will return a contract address string if no error occurred. The string is the address of the new contract you just create.

  • get contract info

get_contract_info address-of-contract

  • use contract

You can use call_contract to broadcast a contract call to the blockchain. call_contract address-of-contract caller-account-name call-api-name call-api-argument system-asset-symbol gasLimit

You can use call_contract_offline to execute contract offline API locally, without gas usage.

call_contract_offline address-of-contract caller-account-name call-api-name call-api-argument system-asset-symbol gasLimit

call_contract_offline is mainly used to query the contract status

  • transfer asset to the contract if you want

wallet_transfer_to_contract transfer-amount system-asset-symbol from-account-name contract-address-to-receive-asset gasLimit

  • Docs

To see the syntax of the smart contract language and how to call it, visit the URL: https://github.com/yusun/hcashdocument

⚠️ **GitHub.com Fallback** ⚠️