小编Lup*_*lol的帖子

为什么交易要消耗gas来实现公共视图功能?

我在 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)

javascript ethereum solidity web3js

0
推荐指数
1
解决办法
900
查看次数

标签 统计

ethereum ×1

javascript ×1

solidity ×1

web3js ×1