Solidity – How to Implement ERC777 TokensReceived Hook for Deployed ERC20 Tokens Like BUSD

erc-20erc-777solidity

hi I'm trying to build a contract that receives erc20 token like BUSD and transfer it to an address but it needs two confirmations. so I decided to use approve and transferFrom function but this one needs two confirms so I think ERC777 is the best way to do this. here is my code:

pragma solidity ^0.8.14;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC777/IERC777.sol";
import "https://github.com/fractional-company/contracts/blob/master/src/OpenZeppelin/introspection/IERC1820Registry.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC777/IERC777Recipient.sol";

contract Simple777Recipient is IERC777Recipient {

    IERC1820Registry private _erc1820 = IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);
    bytes32 constant private TOKENS_RECIPIENT_INTERFACE_HASH = keccak256("ERC777TokensRecipient");

    IERC777 private _token;

    event DoneStuff(address operator, address from, address to, uint256 amount, bytes userData, bytes operatorData);

    constructor (address token) public {
        _token = IERC777(token);

        _erc1820.setInterfaceImplementer(address(this), TOKENS_RECIPIENT_INTERFACE_HASH, address(this));
    }

    function tokensReceived(
        address operator,
        address from,
        address to,
        uint256 amount,
        bytes calldata userData,
        bytes calldata operatorData
    ) external override {
        require(msg.sender == address(_token), "Simple777Recipient: Invalid token");
        emit DoneStuff(operator, from, to, amount, userData, operatorData);
    }
}

I copied BUSD code and deployed and used its address for constructor but it's not deploying i need help for this or a new solution.

Best Answer

It's not deploying because your constructor is calling a contract (the ERC1820 registry) which doesnt exist on the chain your on. Besides that, what you're trying to do won't work, BUSD is ERC20, it's not ERC777, so it won't call the tokenReceived() function when transferring tokens to your contract.