【问题标题】:Send reward to a token holders in smart contracts在智能合约中向代币持有者发送奖励
【发布时间】:2026-01-29 11:15:02
【问题描述】:

我如何在可靠的智能合约中向代币持有者发送代币? 这意味着如何向代币持有者发送奖励?

【问题讨论】:

  • 嗨@M.Alaghemand - 如果您觉得我的回答有用,您可以投票赞成,我们将不胜感激。谢谢。

标签: ethereum solidity smartcontracts tron


【解决方案1】:

有一个地址列表并在调用本机erc传输方法时循环它们。在不知道访问密钥的情况下,您无法真正迭代映射(如果您正在考虑从诸如 balances 之类的东西中提取地址)。

【讨论】:

    【解决方案2】:

    我假设您想将 Ether 发送到另一个智能合约或 EOA(例如 Metamask)。您可以编写如下所示的智能合约,并使用 Remix Ethereum(一个 IDE)将 Ether 发送给外部方。使用公共功能 - transferEther。

    //SPDX-License-Identifier: GPL-3.0
     
    pragma solidity >= 0.6.0 < 0.9.0;
     
    contract Sample{
        
        address public owner;
        
        constructor() {
            owner = msg.sender;
        }
        
        receive() external payable { 
        }
        
        fallback() external payable {
        }
        
        function getBalance() public view returns (uint){
            return address(this).balance;
        }
    
        // this function is for sending the wei/ether OUT from this smart contract (address) to another contract/external account.
        function transferEther(address payable recipient, uint amount) public returns(bool){
            require(owner == msg.sender, "transfer failed because you are not the owner."); // 
            if (amount <= getBalance()) {
                recipient.transfer(amount);
                return true;
            } else {
                return false;
            }
        }
    }
    

    【讨论】: