Putri dan Kacang Polong

Sudah bertahun-tahun lamanya seorang pangeran mendambakan putri raja yang sempurna. Akhirnya, dia memutuskan untuk mengelilingi dunia demi menemukan putri raja yang dia inginkan. Sayang, meski sudah…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Ethereum Payable Token and how it works

The story of the ERC1363's birth

As EIP 20 says:

So basically you can use tokens to represent any fungible tradable good: coins, loyalty points, in-game points, etc.

Here the standard ERC20 definition:

Here a basic implementation of thetransfer function.

And below a basic implementation of the approve and transferFrom functions.

So, as you can see, balances is not more than a mapping between tokens holder address and its amount of tokens.

For example, assume that a token contract has two token holders:

The token contract’s balances data structure will contain the following informations:

The balanceOf(...) function will return the following values:

If 0x111...111 wants to transfer 10 tokens to 0x222...222, 0x111...111 will execute the function:

The token contract’s transfer(...) function will alter the balances data structure to contain the following informations:

The balanceOf(...) function will now return the following values:

approve(...) and transferFrom(...) are used when someone wants to authorise another address to spend tokens in its behalf.

Again, allowed is not more than a mapping between tokens holder address, spender address and the amount of tokens approved.

So, if 0x111...111 wants to authorise 0x222...222 to transfer some tokens (30), 0x111...111 will execute the function:

The approve data structure will now contain the following information:

If 0x222...222 wants to later transfer some tokens (20) from 0x111...111 to 0x333...333, 0x222...222 executes the transferFrom(...) function:

The balances data structure will be altered to contain the following information:

And the allowed data structure will now contain the following information:

0x222...222 can still spend 10 tokens from 0x111...111.

The balanceOf(...) function will now return the following values:

Imagine you are sending tokens to pay the ACME#1 invoice so the receiver contract should set your invoice as paid.
How it could be possible? Nobody is notifying nobody else that the contract has received tokens to pay the invoice number ACME#1.

Or imagine that you want your subscription enabled after you approved your tokens to be spent from the subscriptions’ manager contract.

Or imagine… everything else you can do in real life by paying something and having something else in return.

So, actually, solutions maybe:

or

Anyway these solutions expect to be done in at least 2 transactions and it means double spending fee, double waiting time, lots of complexity and more.

And mostly, from my point of view as a Smart Contracts and DAPP developer, it means that I must set and maintain a GETH node with a nodejs service listening for any Transfer event coming from the token contract, call any callback on the receiver contract to set stuffs done, being updated on the gas costs to have my transactions done quickly and not to pay more gas than the received token value, and… oh oh oh… how many things.

So this is the reason because of I decided to build my own implementation of a payable token.

Yeah, I Googled it lots of time, sure. Many people have already explored this problem and there are many other ERC and proposals that have been used by lots of tokens but, there is not a standard yet.

It defines transferAndCall and transferFromAndCall functions that will call an onTransferReceived on a ERC1363Receiver contract.
It defines approveAndCall functions that will call an onApprovalReceived on a ERC1363Spendercontract.

By doing so, contracts can accept ERC-20 payments to create a token payable crowdsale, selling services for tokens, paying invoices, making subscriptions, or use them for a specific utility and many other purposes.

Sounds good?

Every Payable Token compliant contract MUST implement the ERC1363 interface other than ERC20 and ERC165 interfaces.

A contract that wants to accept token payments via transferAndCall or transferFromAndCall MUST implement the ERC1363Receiver interface:

A contract that wants to accept token payments via approveAndCall MUST implement the ERC1363Spender interface:

Here there is my own implementation of an ERC1363 Token using the OpenZeppelin library.

You can use it in your project by installing with npm:

Add a comment

Related posts:

Best Instagram Grid Layouts Guide

Most Instagram users search for information on various products or services. Instagram grid layouts are essential when it comes to your Instagram page success.The article below will look at how one…

WIDELANES FESTIVAL 2018

Kami fikir menempuh perjalanan dari Bandung ke Jakarta akan membuat kami merasa kelelahan ketika sampai di Widelanes Festival 2018 yang diadakan Sabtu (1/9) kemarin. Ternyata tidak sama sekali…

You Can Be 5x More Impactful by Avoiding These 5 Words

It was one time that I had mailed some stories to my dear old English professor Mrs. Das. After a few days, she called and asked, “Is everything fine with you?” That was not what I was expecting, and…