AnyLedger Wallet and ERC20 Tokens

Lorenzo Pieri
Anyl
Published in
5 min readNov 9, 2018

AnyLedger just added support for ERC20 tokens! To our knowledge this is the first library in the world that allows embedded devices to interact with token smart contracts.

In the last blogpost we outlined that, besides basic capabilities such as sending transaction, AnyLedger wallet is also able to perform more complex actions such as executing smart contract calls. As you may know, an ERC20 Token is just a very simple smart contract and also a pretty popular one! (more than 130000 of them!)

Tokens, tokens, tokens….

We thought that this topic could be interesting to many projects, which now will be able send their tokens directly from embedded IoT devices. Do you need to tokenize a physical item or to create some game theoretic real world incentive mechanism? Here you go!

To my mind, this can be interesting to projects like VeChain Foundation, OmiseGO, 0x, Golem Project, Aurorachain, ChainLink, æternity, Pundi X (writers), Waltonchain_EN, civic, Angel Versetti (Ambrosus), Raiden Network, SONM, IoTeX, Modum, IoTChain, XY Oracle Network, OriginTrail. Did I miss some projects which could benefit from real world token interaction? Let me know in the comments!

By the way, this article is part of a series of technical updates from AnyLedger and it was written together with Jozef Henzl. You can follow and contribute to the project on the Github repository (yes, it’s all open source!).

A (very) short primer on Tokens

A token itself is a simple unitary representation of “something”. It can be a unit of currency, a single voting right, the digital twin of a physical object and so on. Each blockchain address, for instance each Ethereum address, is mapped to a certain balance of tokens, with the default value usually set to zero by the token contract creator. Tokens are appealing since they live on the blockchain and every transfer will be immutably recorded in a block.

In principle a token can be implemented in many ways, but to allow interoperability between applications, exchanges and wallets supporting the tokens it’s a good practice to follow a standard recipe. The most famous recipe is the ERC20 Token standard. Every ERC20 token must implement the following interface:

// -----------------------------------------------------------------
// ERC Token Standard #20 Interface
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
// -----------------------------------------------------------------
contract ERC20Interface {
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}

As expected we find a function to transfer tokens between addresses and a function to check the balance. For a more in depth explanation of ERC20 tokens you can check the ERC20 token standard ethereum wiki page or the article Understanding ERC-20 Token Contracts. Standards don’t last forever, indeed ERC777 tokens are proposed to solve some of the issues of ERC20 tokens.

From Solidity to C

How do we execute a smart contract method? And in particular how do we do it using the C language? The key lies in the data field of the Ethereum transaction. The contents of this field are arbitrary — the interpretation depends on the code deployed in the smart contract. Since almost all smart contract creators use Solidity, we’ll focus on it for now.

The data passed to the virtual machine consist of a method signature, which is defined as a first four bytes of keccak256 hash of the method name and arguments, and an arbitrary amount of data chunks, encoded depending on the type. The principles are very well explained in the article How To Decipher A Smart Contract Method Call by Howard.

Since it would be tedious to create the method arguments by hand, we have created a simple ABI generator. It takes a JSON file produced by the solc as an input, and translates it to a C function that generates the data block required to call the method.

To make a more concrete example, let’s take a look at how the C ABI for an ERC20 token is generated. First, we will compile .sol code with Solidity compiler

solc ERC20Token.sol — combined-json abi,bin > ERC20Token_abi.json

And then we can generate corresponding C source:

python3 -m abi_compiler — output h — abi ERC20Token_abi.json > erc20_abi.hpython3 -m abi_compiler — output c — abi ERC20Token_abi.json > erc20_abi.c

Let’s compare method from the original solidity source with the generated one:

ERC20Token.sol:function transfer(address _to, uint256 _value) external returns (bool success);erc20_abi.h:int Token_transfer(address_t * _to, uint256_t * _value, data_block_t *out);

By calling this function a data block with appropriately encoded data chunks will be generated and it can be used as a part of the Ethereum transaction.

ERC20 shell commands in Zephyr

In our previous blogpost we have explained the basics of the Zephyr shell and submodules that allow you to test the AnyLedger wallet functionality from the terminal.

Release 0.3.1 of the AnyLedger wallet adds a new feature: ERC20 submodule. Its purpose is to demonstrate Smart contract capabilities of the Wallet and allows checking the balance and doing ERC20 token transfers.

Start by jumping to the shell and sync the wallet nonce

uart:~$ wallet sync

ERC20 functionality is implemented in the erc20 submodule. Let’s add a new token:

uart:~$ erc20 add 0xa1ec42238555cb30f1aa6c8cea7f2683a6ffd435

To verify that the token has been added, we can list known tokens

uart:~$ erc20 list 
0 TKN 18 a1ec42238555cb30f1aa6c8cea7f2683a6ffd435

First number of the output is the index, which we will use to refer to the token.

Check the token balance and total supply:

uart:~$ erc20 balance 0
9999996.927942405962072064
uart:~$ erc20 totalSupply 0
10000000.0

And to transfer the tokens simply specify index, recipient and an amount. A transaction hash is returned as a result:

uart:~$ erc20 transfer 0 0x4d6Bb4ed029B33cF25D0810b029bd8B1A6bcAb7B 12
0x2d34fc84d159a0d5c5de16d4a233943ad6d03a0fd30d52a8e43a91f25b1a3b2d

What’s next!?

We are not finished with smart contracts, there are really a ton of interesting contracts that are ideal for IoT devices. In a future blogpost we will cover the case of access control, in which the device can be selectively managed by authorised blockchain addresses. Until then, stay tuned and happy coding!

--

--

Lorenzo Pieri
Anyl
Editor for

Founder of AnyLedger. PhD in Physics | Science and Tech Enthusiast | Entrepreneur