Solidity – Simple Record Keeping Ethereum Smart Contract

Ethereum

The following is a very simple smart contract, written in Solidity, which allows the user to record strings in an array on the blockchain.

  • The creator of the contract is the initial contract owner
  • Only the owner of the smart contract may add new records
  • Ownership of the contract may be transferred to another address only the current contract owner
  • Anybody may view records

With Ethereum, to update records on the blockchain will cost money in the form of Ether or ‘gas’. Reading values from the blockchain costs nothing.

If you are new to Solidity, the IDE, compiler, and dummy network environment at https://remix.ethereum.org will allow you to test this contract out.

pragma solidity >0.4.0 <=0.7.0;

contract Storage{
       
    //the array where records are kept (string)    
    string[] records;
    
    //the address of the contract owner
    address public owner;
    
    constructor() public{
        //set the original owner as the contract creator
        owner = msg.sender;
    }
    
    function reassignOwner(address newOwner) public{
        require(
            msg.sender == owner,
            "Only the contract owner may reassign ownership of the contract."
        );
        
        //reassign the contract owner
        owner = newOwner;
        
    }
    
    function addRecord(string memory newRecord) public{
        require(
            msg.sender == owner,
            "Only the contract owner may add new records."
        );
        
        //add the record
        records.push(newRecord);
    }
    
    //get a record by its array key
    function getRecord(uint key) public view returns(string memory) {
        return records[key];
    }
    
}

Leave a Reply