合约可以有钱!
合约与其他合约或者EOA之间可以转账
https://github.com/wolflo/evm-opcodes/blob/main/gas.md
实际的gas是完全由执行逻辑决定的,一个固定的逻辑的合约函数执行,gas没有变化,有变化的是gas的价格,是由transaction来设定交易发起者设定最多消耗多少:gaslimit;剩下没用完的gas会“退款”, 交易失败时,已经用了
的gas不退,合约之间函数调用可以设置gaslimit:调用者来控制gas消耗
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract ReceiveEther {
uint public x;
event Fallback(bytes cdata, uint value, uint gas);
event Foo(bytes cdata, uint value, uint gas);
// Fallback function is called when msg.data is not empty
fallback() external {
emit Fallback(msg.data, 0, gasleft());
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
function foo() public payable {
emit Foo(msg.data, msg.value, gasleft());
}
}
contract SendEther {
function sendViaCall(address payable _to) public payable {
(bool sent, ) = _to.call{value: msg.value}("");
require(sent, "Failed to send Ether");
}
function sendViaFoo(address payable _to) public payable {
ReceiveEther re = ReceiveEther(_to);
re.foo{gas:2300, value: msg.value}();
//msg.value是sendViaFoo的调用者发送给SendEther
}
}