All Products
Search
Document Center

Solidity Development Startup

Last Updated: May 24, 2019

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.

  1. pragma solidity ^0.4.0;
  2. contract CreditManager {
  3. int256 creditLimit = 1000000000; // the issue limit
  4. int256 issueAmount = 0; // the issue total amount
  5. identity admin; // the administrator in contract
  6. mapping(identity=>int256) credit;
  7. event IssueEvent(identity indexed to, int256 indexed value);
  8. event TransferEvent(identity indexed from, identity indexed to, int256 indexed value);
  9. function CreditManager() {
  10. admin = msg.sender;
  11. }
  12. // modifier
  13. modifier onlyAdmin() {
  14. require(msg.sender == admin,"Permission denied");
  15. _;
  16. }
  17. // issue credit and only admin can
  18. function Issue(identity account, int256 value) public onlyAdmin returns(bool) {
  19. // the value should bigger than 0, and issueAmount add value should small than issueAmount
  20. require(issueAmount + value <= creditLimit && issueAmount + value > issueAmount && value > 0, "Invalid value!") ;
  21. credit[account] += value;
  22. issueAmount += value;
  23. IssueEvent(account,value);
  24. return true;
  25. }
  26. function Transfer(identity account,int256 value) public returns(bool) {
  27. require(credit[msg.sender] >= value, "balance not enough!") ;
  28. require(value > 0 && value <= creditLimit, "Invalid value!") ;
  29. credit[msg.sender] -= value;
  30. credit[account] += value;
  31. TransferEvent(msg.sender,account,value);
  32. return true;
  33. }
  34. function Query(identity account) public returns(int256) {
  35. return credit[account];
  36. }
  37. }

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.

  1. pragma solidity ^0.4.0;
  2. 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.

  1. int256 creditLimit = 1000000000; // the isuue limit
  2. int256 issueAmount = 0; // the issue total amount
  3. identity admin; // the administrator in contract
  4. 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.

  1. event IssueEvent(identity indexed to, int256 indexed value);
  2. 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)

  1. function CreditManager() {
  2. admin = msg.sender;
  3. }
  4. // modifier,when mas.sender ! = admin, the contract will show "Permission denied" in output
  5. modifier onlyAdmin() {
  6. require(msg.sender == admin,"Permission denied");
  7. _;
  8. }

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.

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

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