简体   繁体   English

在同一个合约中调用一个公共 function 和另一个 function

[英]call a public function with another function in the same contract in solidity

what is the simplest solution to set up a function in a contract that automatically call a publish function of the same contract?在自动调用同一合约的发布 function 的合约中设置 function 的最简单解决方案是什么?

The basic structure is:基本结构是:

function send(uint256 value) public {
    address sender = _msgSender();
    }

I want to set up a function within the same contract that automatically makes a call trigger of the above public function when a specific wallet caller has a certain amount value.我想在同一个合约中设置一个 function 当特定钱包调用者具有一定金额值时自动触发上述公共 function 的调用触发。

You can check the sender's balance by using the balance property of the address type.您可以使用address类型的balance属性检查发件人的余额。

function send(uint256 value) public {
    address sender = _msgSender();

    // this checks the sender's balance
    if (sender.balance >= 1 ether) {
        publish();
    }
}

function publish() public {
}

If you want to check how much they sent in the transaction executing your function, you can use the global variable msg.value .如果您想检查他们在执行 function 的事务中发送了多少,您可以使用全局变量msg.value

Also, in order to accept the non-zero value, you need to make the function payable .此外,为了接受非零值,您需要使 function payable

function send(uint256 value) public payable { // mind the `payable` keyword
    address sender = _msgSender();

    // this checks the value of this transaction
    if (msg.value >= 1 ether) {
        publish();
    }
}

function publish() public {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM