如何在 Remix IDE 中设置 msg.value

hcw*_*wil 5 solidity ether remix

这可能是我错过的一个简单错误,但我一生都无法弄清楚如何在这个合同中设置 msg.value 变量。我在网上读到这个值是与交易相关的 wei 数量,但是作为合约的调用者,我如何具体设置该值。这是我正在努力处理的合同。

杂注坚固性 0.8.7;

合同自动售货机 {

// Declare state variables of the contract
address public owner;
mapping (address => uint) public cupcakeBalances;

// When 'VendingMachine' contract is deployed:
// 1. set the deploying address as the owner of the contract
// 2. set the deployed smart contract's cupcake balance to 100
constructor() {
    owner = msg.sender;
    cupcakeBalances[address(this)] = 100;
}

// Allow the owner to increase the smart contract's cupcake balance
function refill(uint amount) public {
    require(msg.sender == owner, "Only the owner can refill.");
    cupcakeBalances[address(this)] += amount;
}

// Allow anyone to purchase cupcakes
function purchase(uint amount) public payable {
    require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per cupcake");
    require(cupcakeBalances[address(this)] >= amount, "Not enough cupcakes in stock to complete this purchase");
    cupcakeBalances[address(this)] -= amount;
    cupcakeBalances[msg.sender] += amount;
}
Run Code Online (Sandbox Code Playgroud)

}

每次我输入金额时,都会出现错误“每个纸杯蛋糕必须至少支付 1 ETH”

我没有地方可以具体输入我要为此支付的金额,任何帮助都会很棒

这是我在 Remix 上部署合约时可以输入的内容

Kia*_*ush 10

在部署按钮顶部,您可以看到值字段:

在此输入图像描述

当您想要调用时purchase,首先填写值字段并选择,Ether然后调用您的函数。

我用你的代码尝试这种方式,效果很好。