fallback has a 2300 gas limit when called by transfer or send.
폴백은 transfer 또는 call로 호출될 때 2300개의 가스 제한이 있습니다.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Fallback {
event Log(string func, uint gas);
// Fallback function must be declared as external.
// 폴백 함수는 external로 선언되어야 합니다.
fallback() external payable {
// send / transfer (forwards 2300 gas to this fallback function)
// call (forwards all of the gas)
emit Log("fallback", gasleft());
}
// Receive is a variant of fallback that is triggered when msg.data is empty
// receive는 msg.data가 비어 있을 때 트리거되는 폴백의 변형입니다
receive() external payable {
emit Log("receive", gasleft());
}
// Helper function to check the balance of this contract
// 이 계약의 잔액을 확인하는 도우미 함수
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract SendToFallback {
function transferToFallback(address payable _to) public payable {
_to.transfer(msg.value);
}
function callFallback(address payable _to) public payable {
(bool sent, ) = _to.call{value: msg.value}("");
require(sent, "Failed to send Ether");
}
}
fallback can optionally take bytes for input and output
폴백은 선택적으로 입력 및 출력을 위한 바이트를 취할 수 있습니다
pragma solidity ^0.8.17;
// TestFallbackInputOutput -> FallbackInputOutput -> Counter
contract FallbackInputOutput {
address immutable target;
constructor(address _target) {
target = _target;
}
fallback(bytes calldata data) external payable returns (bytes memory) {
(bool ok, bytes memory res) = target.call{value: msg.value}(data);
require(ok, "call failed");
return res;
}
}
contract Counter {
uint public count;
function get() external view returns (uint) {
return count;
}
function inc() external returns (uint) {
count += 1;
return count;
}
}
contract TestFallbackInputOutput {
event Log(bytes res);
function test(address _fallback, bytes calldata data) external {
(bool ok, bytes memory res) = _fallback.call(data);
require(ok, "call failed");
emit Log(res);
}
function getTestData() external pure returns (bytes memory, bytes memory) {
return (abi.encodeCall(Counter.get, ()), abi.encodeCall(Counter.inc, ()));
}
}
Solidity - Delegatecall (0) | 2023.01.18 |
---|---|
Solidity - Call (0) | 2023.01.17 |
Solidity - Interface (0) | 2023.01.05 |
Solidity - Visibility (0) | 2023.01.04 |
Solidity - Calling Parent Contracts (0) | 2023.01.04 |