Hybrid Smart Contracts

Omar MK
Hybrid Smart Contracts
5 min readJan 3, 2018

--

Implementation of Hybrid Smart Contracts

Currently Speaking (January 2, 2018) there are 2 proposed implementations of smart contracts, ERC20 and ERC223. ERC20 Documentation can be found Here, and ERC223 Documentation can be found Here. Before we begin our discussion about the hybrid token design we need to first familiarize ourselves with the ERC20 and ERC223 implementations.

Both the ERC20 and ERC223 are written in a software language called Solidity. Solidity is a is a contract-oriented programming language for writing smart contracts, it is used to implement those smart contracts on various Blockchains.

ERC20

The ERC20 was the first proposed implementation, an ERC20’s basic interface functions are the following:

function allowance(address owner, address spender) public view returns (uint256);  
function transferFrom(address from, address to, uint256 value) public returns (bool);
function approve(address spender, uint256 value) public returns (bool);

These following functions explain the transfer aspect of assets using the ERC20 implementation. The first function, the “allowance” gives permission of a following address to take from the owner a certain amount of tokens. The allowance function looks like the following:

function allowance (address _owner, address _spender) public constant onlyOwner returns (uint){
return _allowance[_owner][_spender];}

where _allowance[_owner][_spender] is a mapping of how much each address is allowed to take from a given address.

The second function “transferfrom” is the movement of assets from one address to another. It is implemented as such:

function transferfrom(address _from, address _to, uint _value) public onlyOwner returns (bool){
if (_allowance[_from][msg.sender] > 0 && _value > 0 &&
_allowance[_from][msg.sender] >= _value && _balances[_from] >= _value){
_balances[_from] = _balances[_from].sub(_value);
_balances[_to] = _balances[_to].add(_value);
return true;
}
return false;

before the amount of assets move from one account to another, we have to make sure that what is sent is more than zero, and that the amount that is moving from one address is another is smaller or equal to the amount that is allowed. The last function that is implemented in a basic ERC20 is the “approve” function and is implemented as such:

function approve (address from, address _spender, uint _value) public onlyOwner returns (bool){
if (_allowance[msg.sender][_spender] == _value){
_allowance[msg.sender][_spender].add(_value);
return true;}}

what this function does is that it makes sure that the amount of value that the spender can take from a given address is equal to the value and once that is true, the following value is added.

As you might have already noticed, there are many commands set to do a simple transaction. This becomes an issue when you have many transactions occurring because of the gas cost. The gas cost is the amount of Wei that is needed to compute a transaction onto the blockchain, once there are many functions occurring for that transaction, the gas cost will increase. This is where the ERC223 comes in. However before we start talking about the ERC223, we have to understand a major issue in the ERC20 that promoted developers to come up with another implementation and that is the lost of tokens when the sender sends ether to a contract. There was no safety net from the sender’s side to know that the money that he/she is sending is going to a valid address.

ERC223

The ERC223 has been proposed as a updated implementation for writing smart contracts using solidity. It was meant to fix some of the issues that were encountered in the earlier implementation (ERC20). A basic ERC223 has the following functions in its interface:

function transfer(address to, uint value, bytes data);

As you can see, there is only one function needed to transfer assets between different addresses. This make the transaction much faster and cost less gas to execute. The transfer function is implemented as such:

function transfer(address sender,address receiver, uint256 amount, bytes data) public onlyOwner returns(bool) {
if(balanceOf(sender) < amount)
{
revert();
}
else if (receiver == 0x0)
{
revert();
}
else if(receiver==address(0))
{
revert();
}
else
{
//subtrack the amount of tokens from sender
_balances[sender] = _balances[sender].sub(amount);
//Add those tokens to reciever
_balances[receiver] = _balances[receiver].add(amount);
//If reciever is a contract ...
if (isContract(receiver)) {
Receiver Receiverontract = Receiver(receiver);
Receiverontract.tokenFallback(sender, amount, data);
}
return true;
}
}

In this code script, we use the “balanceof” function which takes in a following address and gives back the balance. The “balanceof” function is integrated in both ERC20 and ERC223 implementations so its not necessary to focus on it but the whole function implementation will be inserted at the end of the article. In the rest of the code we have to run some security steps to ensure that the address that is being sent is valid, if the address is not valid (runs true in the if statement) the revert function is used. The revert function is predefined in solidity. A list of all solidity functions and documentation can be seen here. One the revert function is activated, the transaction does not go through and gas is not used. In previous solidity documentation, the function “throw” was used which plays a similar role but uses up gas.

If the address does not run true through any of the if statements, the code in the “else” section of the code will be executed. This part of the code does the adding and the subtraction of the tokens from the accounts (please refer to the comments to gain a better understanding). The ERC223 implementation also uses a function called the “tokenfallback”, the token fall back function is used when a token is not expecting certain tokens and will revert, returning all the tokens back to the owner.

Hybrid Smart Contract

As times goes on and the new ERC223 implementation is used, we also have to be aware of the already implemented ERC20 tokens. To create something that works with both, we need to integrate both implementations. To create a hybrid contract, we need to incorporate both ERC20 and ERC223 functions into our interface and contracts. Our updated interface contract will look like the following:

function balanceOf(address addr) public view returns(uint256);
function totalSupply() public view returns(uint256);
function getOwner() public returns(address);
function transfer(address sender,address receiver, uint256 amount, bytes data) public returns(bool);
function transfer(address sender,address receiver, uint256 amount) public returns(bool);
function transferfrom(address _from, address _to, uint _value) public returns (bool);
function approve (address _from, address _spender, uint _value) public returns (bool);
function allowance (address _owner, address _spender) public constant returns (uint);

the top code will look very similar to a hybrid token interface that incorporates both ERC20 and ERC223 implementations. As you can see, most of the functions discussed earlier in the article are displayed above. However there are some new functions that have not been talked about yet, the “balanceof”, “totalSupply”, and “getOwner”. Those functions are used to get the balance of a certain address, the totalSupply of the tokens (used in a crowdsale) and the “getOwner” is used for getting the address of a specific owner.

Conclusion

If you are looking to develop smart contracts, you need to be aware of both implementations that are being used at the moment and find a way to incorporate both so that it will be applicable to most tokens. The following implementation of the hybrid token is illustrated through out the article.

--

--