Solidity Example Subcurrency

Solidity Example Subcurrency Main Tips

  • In this tutorial, we will show you how to create the most simple form of cryptocurrency, using Solidity.
  • To try out the code shown in this tutorial, it is suggested to use Remix.

Solidity Example Subcurrency

This time, we will create a contract, which implements a simple form of cryptocurrency.

That being said, it is completely possible to create coins out of thin air.

However, only the creator of the contract can do that.

In addition to that, anyone can send coins to each other – they simply need an Ethereum keypair.

So, here’s the whole code we will need to create a contract implementing our subcurrency.


pragma solidity ^0.4.0;

contract Subcurrency {
    // The "public" keyword allows external accounts to read the variable
    address public minter;
    mapping (address => uint) public coinBalances;

    // Light clients can react on changes efficiently thanks to events.
    event Sent(address from, address to, uint sum);

    // The code of this constructor is run only once the contract is created.
    function Coin() {
        minter = msg.sender;

    function mint(address receiver, uint sum) {
        if (msg.sender != minter) return;
        coinBalances[receiver] += sum;

    function send(address receiver, uint amount) {
        if (coinBalances[msg.sender] < sum) return;
        coinBalances[msg.sender] -= sum;
        coinBalances[receiver] += sum;
        Sent(msg.sender, receiver, sum);


Try on Remix Try live on Hosting

This examples introduces a few new concepts, that we will now explain in the context of this example.

Solidity Example Subcurrency Address

Look at the fifth line of the code example above.

It reads address public minter;

This line creates an address variable called minter.

Firstly, the address keyword is used to specify that this is a state variable of a publicly accessible type address.

Addresses are 160 bit variables and arithmetic operations cannot be performed with them.

It is used for storing addresses, as well as external keypairs.

Second of all, the public keyword.

It is important because it makes its current state accessible as well as alliwing other contracts to access it.

Solidity Example Subcurrency Mapping

The following line of code creates a mapping.

First, the type mapping is used for mapping address variables to unsigned integers.

The mapping that is created is a lot like a hash table. Hash tables are much like associative arrays, storing keys in indexed slots.

The main similarity between mappings and hash tables is that every possible key is mapped to a value, the byte representation of which is all zeros.

However, it is not possible to obtain every key or make a complete list of values inside a mapping.

For this reason, it is important that you do one of the two when using mappings:

  • keep in mind what you add to the mapping
  • use it in a context where you don’t need to obtain all values inside the mapping

In addition to that, it creates a bit more complex getter functions (read about getter functions in later chapters):


function coinBalances(address _account) returns (uint) {
    return coinBalances[_account];


Try on Remix Try live on Hosting

Solidity Example Subcurrency Events

The following line of code creates an event called Sent, which is called in the end of the send() function, after the transaction is sent.

Events are used to store arguments in the transaction log.

This is because events allow you to conveniently use EVM loging facilities, which lets you listen “listen” to the transactions.

The code you might use to listen to the event gets a little complicated. Look at this example and note how the function balances(), which is generated automatically, is called from the user interface:


Coin.Sent().watch({}, '', function(error, result) {
    if (!error) {
        console.log("Coin transfer: " + result.args.amount +
            " coins were sent from " + result.args.from +
            " to " + + ".");
        console.log("Coin balances now:\n" +
            "Sender: " + +
            "Receiver: " +;


Try on Remix Try live on Hosting

Solidity Example Subcurrency Coin Constructor

The function Coin() (on the 12th line of the code example above) is special – it is a coin constructor, which is run during the contract’s creation and cannot be called afterward.

It permanently stores the creator’s address in the magic global variable called msg, along with tx and block.

This variable is accessible from the blockchain.

The current external function call always comes from msg.sender .

Solidity Example Subcurrency Result

The only two functions that can be called after the contract has been initiated are mint and send.

mint will only work if called by the account which created the contract itself.

send, on the other hand, will work for anyone who has the coins of the subcurrency that was created, to send it.

It should be also noted that the transaction is only stored in the data storage of the coin contract.

Using events created by the send() function being called to make transactions, you can create a “bitcoin” explorer for tracking the transactions and balancing the new coin.

Read previous post:
JavaScript Where To

As you know, JavaScript is one of the three main front-end development languages. You probably know that CSS properties can...