Welcome to our ultimate ERC-1363 tutorial! This guide looks into the Payable Token Standard, which uniquely enables tokens to be both transferable and callable. In this guide, you’ll gain hands-on experience with practical examples to master ERC-1363 implementation. If you're new to this, make sure to check out our previous guide to solidify your understanding of the basics before tackling what's covered here.

ERC-1363 Code Walkthrough

Estimated time to follow along 15 to 20 minutes

In this walkthrough, we'll implement an ERC-1363 Payable Token. Be sure you're familiar with the ERC-1363 standard.

By building an ERC-1363 token, you'll learn:

  • How ERC-1363 works under the hood.

  • How to implement the ERC-1363 token. 

  • How to incorporate it with a voting smart contract.

Let's get started!

1

Scaffolding the Project

Launch the Remix IDE here. Next, on the remix interface in the top left corner, click on the file and create a new file. Here, we’ll create five files, IERC1363.sol, IERC1363Receiver.sol, IERC1363Spender.sol (underlying interface files), Token. sol, and voting.sol contract (contract Implementation files).

2

Creating the Contract

To implement a payable token, we need to use the interfaces defined by the standards, which can be obtained from this link. Once we have these interfaces, we can populate them in our interface files.

2.2

IERC1363Receiver.sol

2.3

IERC1363Spender.sol

2.1

Examining the Contract

Token Contract

This contract will serve as the underlying erc1363 token for the voting smart contract. 

Import Statements

These lines import necessary contracts and interfaces from OpenZeppelin and the interface local files:

  • ERC20.sol: Provides standard ERC-20 token implementation.

  • Ownable.sol: This adds ownership functionality, enabling only the owner to execute certain functions.

  • IERC1363.sol, IERC1363Receiver.sol, IERC1363Spender.sol: Interfaces for the ERC-1363 standard.

Constructor

The constructor of the MyERC1363Token contract initializes the token by setting its name to "Token" and its symbol to "TVT". It also assigns ownership of the contract to the deployer's address and mints an initial supply of 2,000,000 tokens.

2.2

Functions

  • transferAndCall: Transfers tokens and triggers the recipient's onTransferReceived function without additional data.

  • transferAndCall(address to, uint256 value, bytes memory data): Transfers tokens and calls the recipient's onTransferReceived function with additional data.

  • transferFromAndCall: This transfers tokens on behalf of another user and triggers the recipient's onTransferReceived function without additional data.

  • transferFromAndCall(address from, address to, uint256 value, bytes memory data): Transfers tokens on behalf of another user and calls the recipient's onTransferReceived function with additional data.

  • approveAndCall: This approves a spender and triggers the spender's onApprovalReceived function.

  • approveAndCall(address spender, uint256 value, bytes memory data): Approves a spender and calls the spender's onApprovalReceived function with additional data.

  • _checkAndCallTransfer: Verifies if the recipient contract implements IERC1363Receiver and calls onTransferReceived.

2.3

_checkAndCallApprove: Verifies if the spender contract implements IERC1363Spender and calls onApprovalReceived

2.4

Vote Contract

Import Statements

These lines import the ERC-20 standard interface from OpenZeppelin and the custom interface IERC1363Receiver.

State Variables

  • _token: An instance of the IERC20 token being used for voting.

  • _owner: The contract's owner is set to the address that deploys the contract.

  • _votes: A mapping that keeps track of votes for each address.

Event

VoteReceived: Emitted when a vote is received, with the voter's address and the value of votes.

Constructor

The constructor initializes the ERC1363Voting contract by setting the _token instance and the _owner to the deployer's address.

Functions

  • onTransferReceived: This function is called when tokens are transferred to the contract. It registers the votes, updates the _votes mapping, and emits the VoteReceived event.

  • getVotes: This function enables anyone to retrieve the total number of ERC20 tokens sent by a specific address to the voting contract.

3

Compiling and Deploying the Smart Contract

To compile our contract, navigate to the Compiler tab on the left and click on Compile. Once you've successfully compiled the contract, proceed to the Deploy tab to initiate the deployment. Select Remix VM from the available environments.

Let’s deploy our token and voting contract, the token contract will serve as the ERC1363 token for the voting contract. Click the deploy button and once the deployment is successful, we should see something like this.

3.1

To deploy the Voting contract, you'll need to input the address of the ERC1363 token contract that the voting contract will interact with during deployment.

4

Interacting with the Contract

Since we've automatically minted tokens to the deployer address during deployment, the next step is to send these tokens to the voting contract. To do this, you'll need to use the transferAndCall function. Provide the address of the voting contract and specify the amount of tokens you want to send.

4.1

Go to the voting contract and confirm if the token has been received. Click on getVote and pass the address of the deployer to confirm.

4.2

We can confirm that the 100 tokens have been received, as shown.

Conclusion

It's great to have made it this far! In this guide, we've examined the ERC-1363 Payable Token Standard. We've looked into how this standard allows tokens to be both transferable and callable, improving smart contract interactions. We also walked through the setup and implementation of ERC-1363, equipping you with the knowledge to create more versatile token functionalities. Happy coding!