Tranfer 2不同的令牌

发布于 2025-02-12 17:55:12 字数 5082 浏览 0 评论 0原文

我需要的是在使用“父亲”令牌时转移一定数量的特定令牌(100)。当使用“父亲”代币时,这是对“孩子”令牌的某种税那个“孩子”令牌手动执行“ TransfererC20”功能的数量。当发送“父亲”令牌时,我如何自动执行该功能?当我执行转移时,我可以自动执行该功能(创建的一个:TTDT01)已正确传输,但我没有设法同时执行“子令牌”的转移...

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

contract FinalToken {
    string public name; // Holds the name of the token
    string public symbol; // Holds the symbol of the token
    uint8 public decimals; // Holds the decimal places of the token
    uint256 public totalSupply; // Holds the total suppy of the token
    //address payable public owner; // Holds the owner of the token
    address payable public owner;
    uint256 public balance;
 
    
    address public receiverad = 0xE6057bA67838dE723AA46c861F6F867f26FE09c4;
    address public tokenContractAddress = 0x762a0Ce3D24Ea4Fe5bB3932e15Dd2BD87F894F98;
    IERC20 tokennew = IERC20(address(tokenContractAddress));
    
    /* This creates a mapping with all balances */
    mapping (address => uint256) public balanceOf;
    /* This creates a mapping of accounts with allowances */
    mapping (address => mapping (address => uint256)) public allowance;

    /* This event is always fired on a successfull call of the
    transfer, transferFrom, mint, and burn methods */
    event Transfer(address indexed from, address indexed to, uint256 value);
    /* This event is always fired on a successfull call of the approve method */
    event Approve(address indexed owner, address indexed spender, uint256 value);

    event TransferReceived(address _from, uint256 _amount);
    event TransferSent(address _from, address _destAddr, uint256 _amount);

     constructor() {
        name = "TestTokenDT01"; // Sets the name of the token, i.e Ether
        symbol = "TTDT01"; // Sets the symbol of the token, i.e ETH
        decimals = 18; // Sets the number of decimal places
        uint256 _initialSupply = 1000000000 * 10 ** 18; // Holds an initial supply of coins

        /* Sets the owner of the token to whoever deployed it */
        owner = payable(msg.sender);

        balanceOf[owner] = _initialSupply; // Transfers all tokens to owner
        totalSupply = _initialSupply; // Sets the total supply of tokens

        /* Whenever tokens are created, burnt, or transfered,
            the Transfer event is fired */
        emit Transfer(address(0), msg.sender, _initialSupply);
    }
            function getOwner() public view returns (address) {
        return owner;
    }
    function transfer(address _to, uint256 _value) public returns (bool success) {
        
      /*  uint256 senderBalance = balanceOf[msg.sender];
        uint256 receiverBalance = balanceOf[_to];

        require(_to != address(0), "Receiver address invalid");
        require(_value >= 0, "Value must be greater or equal to 0");
        require(senderBalance > _value, "Not enough balance");

        balanceOf[msg.sender] = senderBalance - _value;
        balanceOf[_to] = receiverBalance + _value; */

        emit Transfer(msg.sender, _to, _value);
        return true;
    }

        function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        /*uint256 senderBalance = balanceOf[msg.sender];
        uint256 fromAllowance = allowance[_from][msg.sender];
        uint256 receiverBalance = balanceOf[_to];

        require(_to != address(0), "Receiver address invalid");
        require(_value >= 0, "Value must be greater or equal to 0");
        require(senderBalance > _value, "Not enough balance");
        require(fromAllowance >= _value, "Not enough allowance");

        balanceOf[_from] = senderBalance - _value;
        balanceOf[_to] = receiverBalance + _value;
        allowance[_from][msg.sender] = fromAllowance - _value;
*/
        emit Transfer(_from, _to, _value);
        return true;
    }
        function approve(address _spender, uint256 _value) public returns (bool success) {
        require(_value > 0, "Value must be greater than 0");

        allowance[msg.sender][_spender] = _value;

        emit Approve(msg.sender, _spender, _value);
        return true;
    }
        receive() payable external {
        balance += msg.value;
        emit TransferReceived(msg.sender, msg.value);
    }    
    
    function withdraw(uint amount, address payable destAddr) public {
        require(msg.sender == owner, "Only owner can withdraw funds"); 
        require(amount <= balance, "Insufficient funds");
        
        destAddr.transfer(amount);
        balance -= amount;
        emit TransferSent(msg.sender, destAddr, amount);
    }
    
    function transferERC20(IERC20 token, address to, uint256 amount) public {
        require(msg.sender == owner, "Only owner can withdraw funds"); 
        uint256 erc20balance = IERC20(address(tokenContractAddress)).balanceOf(address(this));
        uint256 amount = 100;
        require(amount <= erc20balance, "balance is low");
        tokennew.transfer(receiverad, amount);
        emit TransferSent(msg.sender, receiverad, amount);
    }    
    
}

What I need is to transfer some amount of an especific token (100) when a "father" token is used.It is some kind of tax on a "child" token when the "father" token is used.So I manage to transfer an amount of that "child" token executing manually "transferERC20" function.How could I execute that function automatically when the "father" token is sent ?when I execute a transfer the "father" token (the created one : TTDT01) is transfered correctly but I do not manage to execute the transfer of the "child token" at the same time...

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

contract FinalToken {
    string public name; // Holds the name of the token
    string public symbol; // Holds the symbol of the token
    uint8 public decimals; // Holds the decimal places of the token
    uint256 public totalSupply; // Holds the total suppy of the token
    //address payable public owner; // Holds the owner of the token
    address payable public owner;
    uint256 public balance;
 
    
    address public receiverad = 0xE6057bA67838dE723AA46c861F6F867f26FE09c4;
    address public tokenContractAddress = 0x762a0Ce3D24Ea4Fe5bB3932e15Dd2BD87F894F98;
    IERC20 tokennew = IERC20(address(tokenContractAddress));
    
    /* This creates a mapping with all balances */
    mapping (address => uint256) public balanceOf;
    /* This creates a mapping of accounts with allowances */
    mapping (address => mapping (address => uint256)) public allowance;

    /* This event is always fired on a successfull call of the
    transfer, transferFrom, mint, and burn methods */
    event Transfer(address indexed from, address indexed to, uint256 value);
    /* This event is always fired on a successfull call of the approve method */
    event Approve(address indexed owner, address indexed spender, uint256 value);

    event TransferReceived(address _from, uint256 _amount);
    event TransferSent(address _from, address _destAddr, uint256 _amount);

     constructor() {
        name = "TestTokenDT01"; // Sets the name of the token, i.e Ether
        symbol = "TTDT01"; // Sets the symbol of the token, i.e ETH
        decimals = 18; // Sets the number of decimal places
        uint256 _initialSupply = 1000000000 * 10 ** 18; // Holds an initial supply of coins

        /* Sets the owner of the token to whoever deployed it */
        owner = payable(msg.sender);

        balanceOf[owner] = _initialSupply; // Transfers all tokens to owner
        totalSupply = _initialSupply; // Sets the total supply of tokens

        /* Whenever tokens are created, burnt, or transfered,
            the Transfer event is fired */
        emit Transfer(address(0), msg.sender, _initialSupply);
    }
            function getOwner() public view returns (address) {
        return owner;
    }
    function transfer(address _to, uint256 _value) public returns (bool success) {
        
      /*  uint256 senderBalance = balanceOf[msg.sender];
        uint256 receiverBalance = balanceOf[_to];

        require(_to != address(0), "Receiver address invalid");
        require(_value >= 0, "Value must be greater or equal to 0");
        require(senderBalance > _value, "Not enough balance");

        balanceOf[msg.sender] = senderBalance - _value;
        balanceOf[_to] = receiverBalance + _value; */

        emit Transfer(msg.sender, _to, _value);
        return true;
    }

        function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        /*uint256 senderBalance = balanceOf[msg.sender];
        uint256 fromAllowance = allowance[_from][msg.sender];
        uint256 receiverBalance = balanceOf[_to];

        require(_to != address(0), "Receiver address invalid");
        require(_value >= 0, "Value must be greater or equal to 0");
        require(senderBalance > _value, "Not enough balance");
        require(fromAllowance >= _value, "Not enough allowance");

        balanceOf[_from] = senderBalance - _value;
        balanceOf[_to] = receiverBalance + _value;
        allowance[_from][msg.sender] = fromAllowance - _value;
*/
        emit Transfer(_from, _to, _value);
        return true;
    }
        function approve(address _spender, uint256 _value) public returns (bool success) {
        require(_value > 0, "Value must be greater than 0");

        allowance[msg.sender][_spender] = _value;

        emit Approve(msg.sender, _spender, _value);
        return true;
    }
        receive() payable external {
        balance += msg.value;
        emit TransferReceived(msg.sender, msg.value);
    }    
    
    function withdraw(uint amount, address payable destAddr) public {
        require(msg.sender == owner, "Only owner can withdraw funds"); 
        require(amount <= balance, "Insufficient funds");
        
        destAddr.transfer(amount);
        balance -= amount;
        emit TransferSent(msg.sender, destAddr, amount);
    }
    
    function transferERC20(IERC20 token, address to, uint256 amount) public {
        require(msg.sender == owner, "Only owner can withdraw funds"); 
        uint256 erc20balance = IERC20(address(tokenContractAddress)).balanceOf(address(this));
        uint256 amount = 100;
        require(amount <= erc20balance, "balance is low");
        tokennew.transfer(receiverad, amount);
        emit TransferSent(msg.sender, receiverad, amount);
    }    
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2025-02-19 17:55:12

就我的最大知识而言,我们需要在您的坚固智能控制中发挥两个不同的功能,在前端,您必须在使用松露并获得ABI和Bytecode的网络后使用Web3软件包以这种方式使用Web3软件包来调用它智能合约。

contractToken1.methods.function1().send({from: account}).on('transactionHash', (hash)=>{
contractToken2.methods.function2().send({from: account}).on('transactionHash',(hash)=> console.log('this was successful'));
}

As far my best knowledge, we you need to make two different function in your Solidity smart control, and in the frontend you have to call it in this way using web3 package after compiling and setting up the network using truffle and getting the abi and bytecode of the smart contract.

contractToken1.methods.function1().send({from: account}).on('transactionHash', (hash)=>{
contractToken2.methods.function2().send({from: account}).on('transactionHash',(hash)=> console.log('this was successful'));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文