All Products
Search
Document Center

Blockchain as a Service:Solidity Development Startup

Last Updated:Dec 03, 2021

This topic uses a simple contract code as an example to describe the code structure.

Sample code

The following code shows a simple credit management project on the contract platform of Ant Blockchain, including the methods of issuance, transferring, and querying credits.

pragma solidity ^0.4.0;

contract CreditManager {
    int256 creditLimit = 1000000000;   // the issue limit 
    int256 issueAmount = 0;           // the issue total amount 
    identity admin;                    // the administrator in contract 

    mapping(identity=>int256) credit; 

    event IssueEvent(identity indexed to, int256 indexed value);
    event TransferEvent(identity indexed from, identity indexed to, int256 indexed value);

    function CreditManager() {
        admin = msg.sender;
    }

    // modifier
    modifier onlyAdmin() {
        require(msg.sender == admin,"Permission denied");
        _;
    }

    // issue credit and only admin can 
    function Issue(identity account, int256 value) public onlyAdmin returns(bool) {
        // the value should bigger than 0, and issueAmount add value should small than issueAmount
        require(issueAmount + value <= creditLimit && issueAmount + value > issueAmount && value > 0, "Invalid value!") ;
        credit[account] += value;
        issueAmount += value;
        IssueEvent(account,value);
        return true;
    }

    function Transfer(identity account,int256 value) public returns(bool) {
        require(credit[msg.sender] >= value, "balance not enough!") ;
        require(value > 0 && value <= creditLimit, "Invalid value!") ;
        credit[msg.sender] -= value;
        credit[account] += value;
        TransferEvent(msg.sender,account,value);
        return true;
    }

    function Query(identity account) public returns(int256) {
        return credit[account];
    }
}

Code analysis

Declare the contract version

First, the contract version needs to be declared, starting with the declaration of the keyword contract.

As shown in the following code, the contract platform of Ant Blockchain is modified and redesigned based on Solidity version 0.4.24. Features prior to version 0.4.24 are supported.

pragma solidity ^0.4.0;
contract CreditManager {

Declare variables

In this contract example, two variables are defined to hold the total amount of credits (creditLimit) and the issued amount (issueAmount). The identity data type is used to identify each user. For example, the identity of the contract administrator (admin) is a 32-byte string. Each identity is unique on the blockchain and can be used to label the credits of the user.

int256 creditLimit = 1000000000;   // the isuue limit
int256 issueAmount = 0;           // the issue total amount
identity admin;                   // the administrator in contract
mapping(identity=>int256) credit;

Declare events

In this example, two events are declared to record the execution of corresponding methods: IssueEvent and TransferEvent. IssueEvent records the issuance of credits and TransferEvent records the transfer of credits. For more information about logs, see chapter six.

event IssueEvent(identity indexed to, int256 indexed value);
event TransferEvent(identity indexed from, identity indexed to, int256 indexed value);

Constructor functions

In the constructor function, set the contract administrator (admin) account ID and use the keyword modifier to set administrator permissions.( Click to see the description of keywords)

function CreditManager() {
    admin = msg.sender;
}

// modifier, when mas.sender ! = admin, the contract will show "Permission denied" in output
modifier onlyAdmin() {
    require(msg.sender == admin,"Permission denied");
    _;
}

Define implementation methods

In this example, the contract contains implementation methods of issuing, transferring, and querying credits.

As shown in the following code, when you call the corresponding method to issue credits, the onlyAdmin field is used to ensure that this operation can only be performed by the administrator. Then the credits are added to the account passed in by the user. Avoid credit overflow during the issuance process. After the credits are issued, the credit issuance event is triggered, and the user can view the event record in the log.

// issue credit and only admin can 
    function Issue(identity account, int256 value) public onlyAdmin returns(bool) {
        // the value should bigger than 0, and issueAmount add value should small than issueAmount
        require(issueAmount + value <= creditLimit && issueAmount + value > issueAmount && value > 0, "Invalid value!") ;
        credit[account] += value;
        issueAmount += value;
        IssueEvent(account,value);
        return true;
    }

The transfer operation is similar to the issuance operation. However, the transfer and query operations do not require administrator permissions.