Skip to content

Smart Contract Examples

This section describes common Smart Contract examples that are used in application development

Factory Pattern

The AssetFactory contract is used to create individual assets and allows for easier creation of assets. Assets that are created by the contract are "owned" by the factory, and can only be called by the factory. Assets may point back to the factory. Addtionally, the factory checks user permissions. With the Factory Pattern, the code is uploaded once which makes subsequent asset creations cheap.

contract AssetFactory {
  function createAsset(string[] assetData) returns (address) {
    Asset a = new Asset(assetData);
    return address(a);
  }

  function updateAsset(address a, string[] updates) {
    Asset(a).update(updates);
  }
}

contract Asset {
  AssetFactory owner;
  string[] assetData;

  constructor(string[] _assetData) {
    owner = msg.sender;
    assetData = _assetData;
  }

  function update(string[] _updates) {
    assert(msg.sender == owner);
    assetData = _updates;
  }
}

Arrays of Structs as Contracts

Arrays, structs, and mappings are not indexed by the Cirrus API. Instead of storing data in these data structures, store the data in a separate contract as shown below.

Store a pointer to the contract in the original data structure. In the contract containing the data, include some information about the containing contract. With this contract design, the data in the Item contracts will be indexed by the Cirrus API. Another example of this can be seen in Joining Multiple Tables

contract Object {
  Item[] items;
  mapping (string => uint) itemMap;

  function addItem(string key, string value) {
    Item i = new Item(items.length, key, value);
    items.push(i);
    itemMap[key] = items.length;
  }
}

contract Item {
  Object owner;
  uint index;
  string key;
  string value;

  constructor(uint _index, string _key, string _value) {
    owner = msg.sender;
    index = _index;
    key = _key;
    value = _value;
  }
}

Node Information in Contracts

Node Information that is stored in contracts include the following: 1. Enode Adress 2. Node Identifiers (URL, number, etc.)

Node contracts can be linked by organization information so that all nodes operated by a particular org can be linked together. Additionally, private chain governance can be done at the org level by querying all nodes for an org.

contract Org {
  string orgName;
  Node[] nodes;


  function addNode(string enodeAddress) {
    Node node = new Node(orgName, enodeAddress);
    nodes.push(node);
  }
}

contract Node {
  string enodeAddress;
  string orgName;

  constructor(_orgName, _enodeAddress) {
    orgName = _orgName;
    enodeAddress = _enodeAddress;
  }
}

SolidVM String Operators

SolidVM allows for string concatenation: 1. Use the + operator to concatenate two strings 2. Use the += operator to append a string to another string

It is no longer necessary to hand-write string concatenation functions or use third-party libraries

contract StringUtils {
  // concatenate(“hello”, “world”) returns “hello world”
  function concatenate(string s1, string s2) returns (string) {
    return s1 + “ “ + s2;
  }

  // append(“world”) returns “hello world”
  function append(string s) returns (string) {
    string s0 = “hello “;
    s0 += s;
    return s0;
  }
}