solidity 合约中的数学运算
Math operation in a solidity contract
我想创建一个 solidity 函数,从 A 处接收金额,使用合约账户余额将收到的金额翻倍,然后将翻倍的金额转账给 B。到目前为止,我创建的函数是只从 A 那里收到外部应付金额,我注意到放置的金额应该 > 2 以太币,但是对于加倍和转移金额,我对这个方法有点困惑。
contract MyContract {
address payable public personA;
address payable public personB;
mapping(address => uint) balances;
modifier onlyonlypersonA() {
require(msg.sender == onlypersonA, "Only onlypersonA can call this method");
_;}
function Send() onlypersonA external payable {
if(msg.value < 2 ether) {revert();}
balances[msg.sender] += msg.value;}
}
试试这个:
contract MyContract {
address payable public personA;
address payable public personB;
mapping(address => uint) balances;
modifier onlyonlypersonA() {
require(msg.sender == onlypersonA, "Only onlypersonA can call this method");
_;}
function Send() onlypersonA external payable {
if(msg.value < 2 ether) {revert();}
balances[msg.sender] += msg.value;
// Try doubling
require(msg.value * 2 <= address(this).balance, "Insufficient funds to doubling");
personB.transfer(msg.value * 2);
}
}
我想创建一个 solidity 函数,从 A 处接收金额,使用合约账户余额将收到的金额翻倍,然后将翻倍的金额转账给 B。到目前为止,我创建的函数是只从 A 那里收到外部应付金额,我注意到放置的金额应该 > 2 以太币,但是对于加倍和转移金额,我对这个方法有点困惑。
contract MyContract {
address payable public personA;
address payable public personB;
mapping(address => uint) balances;
modifier onlyonlypersonA() {
require(msg.sender == onlypersonA, "Only onlypersonA can call this method");
_;}
function Send() onlypersonA external payable {
if(msg.value < 2 ether) {revert();}
balances[msg.sender] += msg.value;}
}
试试这个:
contract MyContract {
address payable public personA;
address payable public personB;
mapping(address => uint) balances;
modifier onlyonlypersonA() {
require(msg.sender == onlypersonA, "Only onlypersonA can call this method");
_;}
function Send() onlypersonA external payable {
if(msg.value < 2 ether) {revert();}
balances[msg.sender] += msg.value;
// Try doubling
require(msg.value * 2 <= address(this).balance, "Insufficient funds to doubling");
personB.transfer(msg.value * 2);
}
}