• ETH 合约常用方法


    contract Test {
        
        event NFTReceived(address operator, address from, uint256 tokenId, bytes data);
        function onERC721Received(address operator, address from, uint256 tokenId, bytes memory data) public returns (bytes4) {
            //only receive the _nft staff
            if(address(this) != operator) {
                //invalid from nft
                return 0;
            }
            
            //success
            emit NFTReceived(operator, from, tokenId, data);
            return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
        }
        
    // 从合约中转进账户 ERC20
       function erc20income(IERC20 asset, address account, uint256 balance) external {
            asset.transferFrom(account, address(this), balance);
        }
    
    // 从账户转进合约中 ERC20
       function erc20withdraw(IERC20 asset, address account) external returns (uint256 balance) {
            balance = asset.balanceOf(address(this));
            asset.transfer(account, balance);
        }
    
    // 从账户转到合约
        function nftWithdraw(IERC721 token, address account, uint256 tokenId) external {
            token.safeTransferFrom(address(this), account, tokenId);
        }
    
    // 从账户转到合约
    	function nft2Contract(IERC721 token, address account, uint256 tokenId) external {
    		token.safeTransferFrom(account, address(this), tokenId);
    	}
    	
    	
    // 从合约中转出去 ETH
        function ethWithdraw(address payable account) external  {
            uint256 _currentBalance =  address(this).balance;
            account.transfer(_currentBalance);
        }
        
        
    // 从合约中转出去 ETH
        function ethIncome() external payable {
    
        }
        
    // 可以直接转账 ETH
        function () external payable {}
        
    }
    
  • 相关阅读:
    23种设计模式(12):策略模式
    23种设计模式(11):责任链模式
    23种设计模式(10):命令模式
    23种设计模式(9):访问者模式
    23种设计模式(8):观察者模式
    23种设计模式(7):中介者模式
    23种设计模式(6):模版方法模式
    创建型模式总结
    23种设计模式(5):原型模式
    leetcode6
  • 原文地址:https://www.cnblogs.com/ShaoYinling/p/14977312.html
Copyright © 2020-2023  润新知