In this tutorial we'll be demonstrating how to call a function of a smart contract on the Ethereum blockchain.

Functions are sections of code that execute certain bits of logic. For example, in the CryptoHunt ICO we implemented a requestRefund function which people could call to get their investment back on the ICO's failure. Original instructions are still available here but in this tutorial we'll be calling the function of a completely unrelated project in two different ways.

Functions and Smart Contracts

When a smart contract gets compiled (translated into machine language so that Ethereum can understand it), it's hashed – compressed into an unreadable format.

Because of this, a special bit of code called ABI is required to interact with a smart contract. ABI is Application Binary Interface and it's used to call the unreadable functions from the Ethereum address (i.e. the contract's address). You can compare ABI to a power adapter of a laptop – a typical power socket provides 200+V while a laptop usually needs 12V. The adapter is there to facilitate “communication” between the two standards.

ABIs are generally generated during compilation and will be placed into a separate file next to the Solidity source code or in a tool which does the compiling (e.g. Remix).

It's important to note that an ABI does not need to match the contract's source code 100%. If a contract has 10 functions of which only one is myFunction, then an ABI with just that one function defined is enough to call that one function, ignoring the rest.

So how do we find a contract's ABI? There's several ways:

  1. The author of the smart contract generally publishes the ABI alongside his contract. For example, the ABI of our raffle contract is here.
  2. If the author published the source code of the smart contract, the ABI can be generated with tools like Truffle, Solc, or even Remix as we'll see later.

Example

Let's go through an example of executing a function on the Ethereum blockchain. We recently created a raffle for the popular Croatian blockchain conference Blocksplit:

BlockSplit

The cut-off date is now in the past so it's time to call the draw function twice in order to get two winners.

How to Generate Solidity ABI

If the source code of our raffle's smart contract is as follows:

pragma solidity ^0.4.20;

contract Blocksplit {
    
    address[] public players;
    mapping (address => bool) public uniquePlayers;
    address[] public winners;
    
    address public charity = 0xc39eA9DB33F510407D2C77b06157c3Ae57247c2A;
    
    uint256 drawnBlock = 0;
    
    function() external payable {
        play(msg.sender);
    }
    
    function play(address _participant) payable public {
        require (winners.length < 2);        
        require (msg.value >= 1000000000000000 && msg.value <= 100000000000000000);
        require (uniquePlayers[_participant] == false);
        
        players.push(_participant);
        uniquePlayers[_participant] = true;
    }
    
    function draw() external {
        require (now > 1522908000);
        require (block.number != drawnBlock);
        require (winners.length < 2);
        
        drawnBlock = block.number;
        
        uint256 winningIndex = randomGen();
        address winner = players[winningIndex];
        winners.push(winner);
        
        players[winningIndex] = players[players.length - 1];
        players.length--;
        
        if (winners.length == 2) {
            charity.transfer(address(this).balance);
        }
    }
    
    function randomGen() constant internal returns (uint256 randomNumber) {
        uint256 seed = uint256(block.blockhash(block.number - 200));
        return(uint256(keccak256(block.blockhash(block.number-1), seed )) % players.length);
    }
    
}

... it's enough to just paste it into an empty Remix file and activate compilation by clicking "Start to Compile", then click on the Details button.

Compile + Details
Compile + Details

The fourth frame from the top is the ABI frame and there'll be a little "copy" icon next to the word "ABI" which lets you copy the whole ABI into the clipboard for later use.

ABI copy
ABI copy

For demonstration purposes, we'll run the draw function from both MyEtherWallet and Remix, once for each winner.

Running Ethereum functions from MyEtherWallet

One of the easiest way to execute Ethereum functions is via MyEtherWallet.

We open the "Contracts" screen and under address we put the address of the contract we're executing functions on, while the ABI field gets the ABI. The right side dropdown menu can be ignored - that's just there for the contracts the MEW team decided to keep around indefinitely.

We need to add 0x4127ab8d4a407b9fdcf1358f8d32b5686cb0b887 into the address field and the ABI should be the one we got from the process in Remix before, or copied straight from here. Then, we need to click "Access".

"Read / Write Contract" will appear, giving us a menu through which we can interact with the contract. If, for example, we select the players function and input the number 4 into the uint256 field, we'll get the 5th player's address on screen after clicking Read: 0x5a8e3cabc6533958959892eee1419f3c1170fe72.

Player 4
Player 4

How exactly this works and why the 5th player is in 4th place - find out in this post.

Now let's select the draw function.

Seeing as the draw function is a "write" function (a function which changes the blockchain's state and therefore needs a transaction), MyEtherWallet asks us which wallet we'll be unlocking to access our account and send that transaction. Seeing as we're logged in with MetaMask, we pick that option.

Wallet choice
Wallet choice

After clicking "Connect" the wallet selection disappears, leaving only the "Write" button.

Write
Write

Clicking "Write", MyEtherWallet recommends some values: the amount of Ether to send (0 because we're not sending Ether to anyone) and maximum amount of gas we're willing to spend on executing this transaction.

Ether and Gas limit
Ether and Gas limit

A few seconds after sending the transaction (in our case 22), the transaction should be executed. If everything went fine, the execution of the winners function in our contract should be showing an address at the 0 position (first winner):

Winner 0
Winner 0

The first winner is 0x58d092125a6945d45e18bc03d985c082dcab1a46, congrats!

Running Ethereum functions from Remix

Now let's go through the same procedure with Remix.

Remix does not have the ability to directly interface with a contract via an ABI, but can access it if you have the contract's source code which then generates the ABI and uses that for interfacing with the contract. The process is as follows:

  1. Paste the source code (above in this post) into an empty file in Remix.
  2. Click the "Run" tab
  3. Pick "Injected Web3" in the list of Environments
  4. Pick our contract in the selection below
  5. Leave Create empty and input the raffle's address into "At Address"
  6. Click the blue "At Address" button.
Remix contract load
Remix contract load

That'll create an interface for interacting with the raffle's contract. Other than being able to read any value from the contract for free with the blue buttons, we can also execute the draw function by clicking its red button.

Draw
Draw

Remix then asks us to confirm execution, suggesting a price of 2 GWei per gas. That's five times less than the default suggested price of MetaMask, because Remix gets its average prices from Ether Gas Station Info, a site for analyzing the Ethereum network's throughput and busyness.

Cheaper gas suggestion
Cheaper gas suggestion

After confirming, MetaMask will once again pop up asking for another confirmation and offering to change gas price. The total maximum price of this transaction - despite being identical to the previous one - is a forgettable 0.07 USD.

MetaMask confirm
MetaMask confirm

After execution, the new winner will have been drawn. This can be checked in both MyEtherWallet and Remix:

Checking in MyEtherWallet
Checking in MyEtherWallet
Checking in Remix
Checking in Remix

According to rules defined in the smart contract, when 2 winners have been selected, the money should be sent to our charity address. Let's check if that happened. The address we should inspect is 0xc39eA9DB33F510407D2C77b06157c3Ae57247c2A and we should look at "Internal Transactions" because the transaction was internal to the Ethereum blockchain - it wasn't an interaction from a user to a contract but rather from a contract to an endpoint. Sure enough, the money arrived and we got 0.5 Ether for charity!

Internal Transactions
Internal Transactions

Congratulations to winner 2: 0xbc00d4391a507e36e8d62d3c37148860df2b9636! Information on how to grab the coupon will be published shortly!

Conclusion

Executing an Ethereum function is very simple if we have the address of the contract and its ABI. This method can be used to execute different functionality in different Ethereum applications living on the blockchain.

Because of the relative complexity of this procedure and its inaccessibility to non-technical users, Web3.js was created. Web3.js is a set of JavaScript libraries meant to simplify interaction with Ethereum contracts and that's what we'll cover in a future post.


If you found this article useful or interesting, please consider donating to keep our operation running.

LEAVE A REPLY

Please enter your comment!
Please enter your name here