AIOU Chain Docs

AIOU Chain Docs

  • Docs
  • Languages iconEnglish
    • 中文

›Smart Contract

Getting started

  • Overview
  • Quickstart

AIOU Design and Concepts

  • Account
  • Economic model

Smart Contract

  • Smart Contract Quick Start
  • AIOU Blockchain API
  • Update Contract
  • Generate Receipt in Smart Contract
  • Create IRC20 Token

Running AIOU node

  • Join AIOU Network
  • Become Servi Node

Reference

  • API
  • System Contract
  • Economic Contract
  • Token Contract
  • Gas Charge Table

AIOU Javascript SDK

  • AIOU
  • Blockchain
  • KeyPair
  • Transaction

AIOU Tech Internals

  • VM
  • Database
  • Network layer

Update Contract

Features

After the contract is deployed to the blockchain, developers may face the need to update the contract, such as fixing bugs, version upgrades, etc.

We provide a complete contract update mechanism that allows developers to easily update smart contract by sending a transaction. More importantly, we provide very flexible update permission control to meet any permission requirements.

In order to update the smart contract, you need to implement a function in the smart contract:

can_update(data) {
}

When receiving a request to update the contract, the system will first call the can_update(data) function of the contract. data is an optional input parameter of type string. If the function returns true, the contract update is executed. Otherwise, a Update Refused error is returned.

By properly writing this function, you can implement any permission management requirements, such as:only update when two people authorize at the same time, or some people vote to decide whether to update the contract, etc.

If the function is not implemented in the contract, the contract is not allowed to update by default.

Hello BlockChain

Below we take a simple smart contract as an example to illustrate the process of contract update.

Create a new contract file helloContract.js with following content

class helloContract
{
    init() {
    }
    hello() {
        return "hello world";
    }
    can_update(data) {
        return blockchain.requireAuth(blockchain.contractOwner(), "active");
    }
};
module.exports = helloContract;

Look at the can_update() function implementation in the contract file, which allows the contract to be updated only when using the contract owner account authorization.

Publish Contract

$ export AIOU_ACCOUNT=admin # replace with your own account name here
$ iwallet compile hello.js
$ iwallet --account $AIOU_ACCOUNT publish hello.js hello.js.abi
...
The contract id is: ContractEg5zFjJrSPdgCR5mYXQLfHXripq64q17MuJoaWKTaaax

Call the Contract First Time

Now you call the hello function inside the contract you just uploaded, you will get 'hello world' as return.

$ iwallet --account $AIOU_ACCOUNT -v call ContractEg5zFjJrSPdgCR5mYXQLfHXripq64q17MuJoaWKTaaax hello "[]"
...
    "statusCode": "SUCCESS",
    "message": "",
    "returns": [
        "[\"hello world\"]"
    ],
    "receipts": [
    ]
}

Update Contract

First edit the contract file helloContract.js to generate a new contract code as follows:

class helloContract
{
    init() {
    }
    hello() {
        return "hello aiou";
    }
    can_update(data) {
        return blockchain.requireAuth(blockchain.contractOwner(), "active");
    }
};
module.exports = helloContract;

We modified the implementation of the hello() function to change the return from 'hello world' to 'hello aiou'.

Use the following command to upgrade your smart contract:

iwallet --account $AIOU_ACCOUNT publish --update hello.js hello.js.abi ContractEg5zFjJrSPdgCR5mYXQLfHXripq64q17MuJoaWKTaaax

Call the Contract Second Time

After the transaction is confirmed, you can call the hello() function via iwallet again and find that the return changes from 'hello world' to 'hello aiou'

$ iwallet --account $AIOU_ACCOUNT -v call ContractEg5zFjJrSPdgCR5mYXQLfHXripq64q17MuJoaWKTaaax hello "[]"
...
    "statusCode": "SUCCESS",
    "message": "",
    "returns": [
        "[\"hello aiou\"]"
    ],
    "receipts": [
    ]
}
← AIOU Blockchain APIGenerate Receipt in Smart Contract →
  • Features
  • Hello BlockChain
    • Publish Contract
    • Call the Contract First Time
    • Update Contract
    • Call the Contract Second Time
AIOU Chain Docs
Community
BlogGitHubStar
Facebook Open Source
Copyright © 2021 Your Name or Your Company Name