7.Gas
你需要支付多少以太币来完成一笔交易?¶
你需要支付的以太币数量为:gas spent * gas price,其中
- gas是计算单位
- gas spent是一笔交易中使用的总gas量
- gas price是你愿意每个gas支付的以太币数量
- 具有更高gas价格的交易优先被包含在区块中。 未使用的gas将被退还。
Gas限制¶
有两个上限限制你可以使用的gas量
- gas限制(你愿意为交易使用的最大gas量,由你设置)
- 区块gas限制(网络设置的区块允许的最大gas量)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
contract Gas {
uint public i = 0;
// Using up all of the gas that you send causes your transaction to fail.
// State changes are undone.
// Gas spent are not refunded.
function forever() public {
// Here we run a loop until all of the gas are spent
// and the transaction fails
while (true) {
i += 1;
}
}
}