How to Implement an ERC-1363 Smart Contract
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
2
Creating the Contract
2.2
2.3
2.1
Examining the Contract
- 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.
2.2
- 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
2.4
Vote Contract
- _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.
- 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
3.1
4
Interacting with the Contract
4.1
4.2
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!