npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

erc-payable-token

v5.4.0

Published

ERC-1363 Payable Token Implementation

Downloads

299

Readme

ERC-1363 Payable Token

NPM Package CI Coverage Status MIT licensed


ERC-1363 is an extension interface for ERC-20 tokens that supports executing code on a recipient contract after transfer or transferFrom, or code on a spender contract after approve, in a single transaction.

There is no way to execute code on a receiver/spender contract after ERC-20 transfers or approvals so, to perform an action, it is required to send another transaction. This introduces complexity in UI development and friction on adoption as users must wait for the first transaction to be executed and then submit the second one. They must also pay GAS twice.

ERC-1363 makes tokens capable of performing actions more easily and working without the use of any off-chain listener. It allows to make a callback on receiver/spender contracts, after transfers or approvals, in a single transaction.

The following are functions and callbacks introduced by ERC-1363:

  • transferAndCall and transferFromAndCall will call an onTransferReceived on a ERC1363Receiver contract.
  • approveAndCall will call an onApprovalReceived on a ERC1363Spender contract.

ERC-1363 tokens can be used for specific utilities in all cases that require a callback to be executed after a transfer or an approval received. ERC-1363 is also useful for avoiding token loss or token locking in contracts by verifying the recipient contract's ability to handle tokens.

[!IMPORTANT] This repo contains the reference implementation of the official ERC-1363.

Install

npm install erc-payable-token

Usage

pragma solidity ^0.8.20;

import "erc-payable-token/contracts/token/ERC1363/ERC1363.sol";

contract MyToken is ERC1363 {
    constructor(string memory name, string memory symbol) ERC20(name, symbol) {
        // your stuff
    }

  // your stuff
}

Code

IERC1363

IERC1363.sol

Interface of the ERC-1363 standard as defined in the ERC-1363.

interface IERC1363 is IERC20, IERC165 {
    function transferAndCall(address to, uint256 value) external returns (bool);
    function transferAndCall(address to, uint256 value, bytes calldata data) external returns (bool);
    function transferFromAndCall(address from, address to, uint256 value) external returns (bool);
    function transferFromAndCall(address from, address to, uint256 value, bytes calldata data) external returns (bool);
    function approveAndCall(address spender, uint256 value) external returns (bool);
    function approveAndCall(address spender, uint256 value, bytes calldata data) external returns (bool);
}

IERC1363Receiver

IERC1363Receiver.sol

Interface for any contract that wants to support transferAndCall or transferFromAndCall from ERC-1363 token contracts.

interface IERC1363Receiver {
    function onTransferReceived(address spender, address sender, uint256 amount, bytes calldata data) external returns (bytes4);
}

IERC1363Spender

IERC1363Spender.sol

Interface for any contract that wants to support approveAndCall from ERC-1363 token contracts.

interface IERC1363Spender {
    function onApprovalReceived(address sender, uint256 amount, bytes calldata data) external returns (bytes4);
}

IERC1363Errors

IERC1363Errors.sol

Interface of the ERC-1363 custom errors following the ERC-6093 rationale.

ERC1363

ERC1363.sol

Implementation of the ERC-1363 interface.

The reference implementation of ERC-1363 that extends ERC-20 and adds support for executing code after transfers and approvals on recipient contracts.

[!IMPORTANT] transferAndCall, transferFromAndCall and approveAndCall revert if the recipient/spender is an EOA address. To transfer tokens to an EOA or approve it to spend tokens, use the ERC-20 transfer, transferFrom or approve methods.

Examples

[!WARNING] The example contracts are for testing purpose only. When inheriting or copying from these contracts, you must include a way to use the received tokens, otherwise they will be stuck into the contract.

ERC1363Guardian

ERC1363Guardian.sol

As example: a contract that allows to accept ERC-1363 callbacks after transfers or approvals.

It emits a TokensReceived event to notify the transfer received by the contract.

It also implements a _transferReceived function that can be overridden to make your stuff within your contract after a onTransferReceived.

It emits a TokensApproved event to notify the approval received by the contract.

It also implements a _approvalReceived function that can be overridden to make your stuff within your contract after a onApprovalReceived.

ERC1363MethodCallReceiver

ERC1363MethodCallReceiver.sol

As example: a contract that allows to test passing methods via abi encoded function call.

It executes the method passed via data. Methods emit a MethodCall event.

Documentation

Code Analysis

Development

Install dependencies

npm install

Compile

npm run compile

Test

npm test

Code Coverage

npm run coverage

Linter

Check Solidity files

npm run lint:sol

Check JS/TS files

npm run lint:js

Fix JS and Solidity files

npm run lint:fix

License

Code released under the MIT License.