Solidity
如何使用 erc777 tokensreceived 掛鉤已部署的 erc20 代幣,如 BUSD?
嗨,我正在嘗試建立一個接收像 BUSD 這樣的 erc20 代幣並將其轉移到一個地址的合約,但它需要兩次確認。所以我決定使用approve 和transferFrom 功能,但是這個需要兩次確認,所以我認為ERC777 是最好的方法。這是我的程式碼:
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); } }
我複制了 BUSD 程式碼並部署並使用了它的地址作為建構子,但它沒有部署我需要這個或新解決方案的幫助。
它沒有部署,因為您的建構子正在呼叫您所在鏈上不存在的契約(ERC1820 系統資料庫)。除此之外,您嘗試做的事情是行不通的,BUSD 是 ERC20,而不是 ERC777,因此在將代幣轉移到您的合約時它不會呼叫 tokenReceived() 函式。