ERC20 + Comment/Message/Input data

Hello,

Most banking apps have a comment text field when sending a payment.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/Context.sol";

abstract contract ERC20Comment is Context, ERC20 {
    /**
     * @dev Emitted by {transferWithComment} or {transferWithCommentFrom}.
     */
    event Comment(
        string indexed commment,
        address indexed from,
        address indexed to,
        uint256 value
    );

    /**
     * @dev See {IERC20-transfer}.
     *
     * - `commment` is additional field for custom message.
     */
    function transferWithComment(
        address to,
        uint256 amount,
        string memory commment
    ) public virtual returns (bool) {
        address owner = _msgSender();
        _transfer(owner, to, amount);
        emit Comment(commment, owner, to, amount);
        return true;
    }

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * - `commment` is additional field for custom message.
     */
    function transferWithCommentFrom(
        address from,
        address to,
        uint256 amount,
        string memory commment
    ) public virtual returns (bool) {
        address spender = _msgSender();
        _spendAllowance(from, spender, amount);
        _transfer(from, to, amount);
        emit Comment(commment, from, to, amount);
        return true;
    }
}

The user added 5 items to the cart for the amount of 20 USDT.
The user paid 20 USDT to the Merchant's wallet.
A transaction in a mempool.
The user added 2 items to the cart in the amount of 20 USDT.
The user paid 20 USDT to the Merchant's wallet.

One transaction completed successfully, and one failed.

Now the question is, which product group should the merchant send to the user? It's impossible to know.

That is why it would be very nice to have a custom text field, as it is now in many banking applications.

Backend:

const secret = hash(itemIds, "aa1efcb7221a61");
const comment = hash(from, to, value, secret);

After the transaction, I check the Transfer event and make sure that the buyer has paid for the item.

if (comment === hash(event.from, event.to, event.value, secret)) {
    return "success";
}

Thanks to the comment, I can know exactly which set of goods the user paid for.

How about using _beforeTokenTransfer and _afterTokenTransfer hook?

I think it will not be possible to use, due to the fact that an additional field string memory commment is needed.

1 Like