我在 Ropsten 以太坊测试网络上部署了以下智能合约,之后我尝试使用 @alch/alchemy-web3 npm 包进行交易(是的,我正在使用 Alchemy API),但正如你所看到的,我在交易中收取了费用。这是为什么?公共视图函数调用不应该花费 0 Gas 吗?
\n部署的智能合约
\n// SPDX-Lincense-Identifier: MIT\npragma solidity ^0.8.11;\n\ncontract VendingMachine {\n address public owner;\n mapping(address => uint256) public donutBalances;\n\n constructor() {\n owner = msg.sender;\n donutBalances[address(this)] = 100;\n }\n\n function getVendingMachineBalance() public view returns (uint256) {\n return donutBalances[address(this)];\n }\n\n function restock(uint amount) public {\n require(msg.sender == owner, "Only the owner can restock this machine.");\n donutBalances[address(this)] += amount;\n }\n\n function purchase(uint amount) public payable {\n require(msg.sender == owner, "Only the owner can …
Run Code Online (Sandbox Code Playgroud)