Smart Contract Life Cycle

Rubix WASM based smart contracts for Wider Adoption

Rubix protocol is committed to improve adaptability of blockchain technology. Along with its revolutionary proof of pledge protocol aided by zero gas fee transactions , Rubix focuses on making dApp deployment and execution easier for our ecosystem. With WebAssembly(WASM) based smart contracts, existing web2 codebases and developers can migrate their codebase and knowledge into Rubix with ease. WebAssembly (WASM) is a binary instruction format that allows code to be executed at near-native speed in a safe , sandboxed and deterministic manner across different platforms. Smart contracts can be written in languages that compile to WebAssembly, such as Rust and C/C++, and then executed on a blockchain platform that supports WASM. Here are the steps in executing WASM in Rubix.

  1. Writing Smart Contracts:

    Smart contracts are written in high-level programming languages like Rust , GoLang or C/C++. These languages offer the flexibility and expressive power of high-level languages while compiling down to WebAssembly bytecode.
  2. Compiling to WebAssembly:

    Once the smart contract code is written, it is compiled to WebAssembly bytecode. Compilers like rustc for Rust or Emscripten for C/C++ can be used to generate WebAssembly binaries from the source code.
    1. About generate and subscribe contract

    2. Generatecontract

      The first step after generating the wasm binaries is to create Smart contract Token (programmable NFTs) that is used to track the proof of execution of the contract. The unique Token ID generated is shared by the owner to peer nodes to participate in contract operations.
      Sample API:
    3. curl --location 'http://localhost:20000/api/generate-smart-contract' \
      --form 'did="User DID"' \
      --form 'binaryCodePath=@""' \
      --form 'rawCodePath=@""' \
      --form 'schemaFilePath=@""'
  3. Deployment on Blockchain:

    The compiled WebAssembly code is then deployed onto a blockchain platform. The contract code, along with any necessary metadata, is stored on the blockchain.
    1. About Deploy and Subscribe contract

    2. DeployContract

      Generate is an offline setup event that happen before a contract enters the network. Once SmartContract Token is created the owner deploy contract to Rubix network by calling —-deployContract—- This creates the first element in tokenchain and pledges the genesis state of token. Sample API:
    3. curl --location 'http://localhost:20000/api/deploy-smart-contract' \
      --header 'Content-Type: application/json' \
      --data '{ "comment": "Your input Comment",
      "deployerAddr": "PeerId.DID of the user who is deploying the smart contract",
      "quorumType": 2,
      "rbtAmount": 1,
      "smartContractToken": "The smart contract token hash generated in generate-smart-contract api" }'
    4. SubscribeContract

      Nodes can subscribe to contract events by using command "subscribe" . This keeps them in sync with contract state changes. Sample API:
    5. curl --location 'http://localhost:20000/api/subscribe-smart-contract' \
      --header 'Content-Type: application/json' \
      --data '{
      "smartContractToken": "smart contract token hash generated from generate-smart-contract api"
      }'
  4. Execution

    When a user or another contract interacts with the deployed smart contract, the contract’s functions are called via transactions. These transactions contain input data that specifies which function of the contract to execute and with what parameters.
    1. About execute contract

    2. Executecontract

      Nodes that want to execute a contract call this API with details on which function they want to run and the corresponding input parameters. Every smart contract will have a schema file that lists all available function names and data type of parameters. Sample API:
    3. curl --location 'http://localhost:20000/api/execute-smart-contract' \
      --header 'Content-Type: application/json' \
      --data '{ "comment": "Your input Comment",
      "executorAddr": "PeerId.DID of the user executingwho is the smart contract",
      "quorumType": 2,
      "smartContractData": "Smart contract input which should be written to the token chain",
      "smartContractToken": "The smart contract token hash generated in to which the node has subscribed" }'
  5. Validation and Execution:

    The transaction is validated by the blockchain nodes to ensure it follows the rules of the blockchain protocol. Once validated, the transaction and the associated smart contract function call are processed by the nodes.
    1. About Publish contract

      1. PublishContract

        Once executeContract state change is approved via consensus , the node who initiated the state change call publishContract to publish the state update to all peers. ExecuteContract and DeployContract is always followed by the PublishContract API.
  6. State Changes and Output:

    Smart contracts can read data from the blockchain’s state and modify it as per the logic defined in their functions. Smart contracts can also produce output data, which is typically returned to the caller after the contract function execution is complete.

    The smart contract is executed on the DApp side. The DApp should have an api endpoint which must be passed as a parameter to register-callback-url api. This api registers the api endpoint in the node. Once the endpoint is registered, each time an execution happens on the token chain, as per the logic in the smart contract deployed, the states in each of the subscribed nodes get updated.

    Sample API :

    curl --location 'http://localhost:20000/api/register-callback-url' \
    --header 'Content-Type: application/json' \
    --data '{ "CallBackURL": "API endpoint url defined on the dapp side",
    "SmartContractToken": "The smart contract hash generated"
    }'
  7. Immutable Execution:

    Once deployed, the logic of a smart contract, represented by its WebAssembly bytecode, is immutable. This means it cannot be changed. If you need to update the contract’s logic, a new version of the contract needs to be deployed.

If you have questions or feedback, please DM us at @rubixchain.