Skip to content

Commit

Permalink
Update Call.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
AmazingAng authored Sep 28, 2024
1 parent 5febe81 commit 0ea7a3f
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions 22_Call/Call.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,61 @@
pragma solidity ^0.8.21;

contract OtherContract {
uint256 private _x = 0; // 状態変数_x
// ethを受け取るイベント、amountとgasを記録
uint256 private _x = 0; // 状态变量x
// 收到eth事件,记录amount和gas
event Log(uint amount, uint gas);

event Log(uint256 amount, uint256 gas);
fallback() external payable{}

fallback() external payable {}

// コントラクトのETH残高を返す関数
function getBalance() public view returns (uint256) {
// 返回合约ETH余额
function getBalance() view public returns(uint) {
return address(this).balance;
}

// _xの値を設定できる関数。同時にコントラクトへETHを送信することもできる(payable)
function setX(uint256 x) external payable {
// 可以调整状态变量_x的函数,并且可以往合约转ETH (payable)
function setX(uint256 x) external payable{
_x = x;
// もしETHの送信がある場合のみLogイベントを放出
if (msg.value > 0) {
// 如果转入ETH,则释放Log事件
if(msg.value > 0){
emit Log(msg.value, gasleft());
}
}

// xの値を取得する関数
function getX() external view returns (uint256 x) {
// 读取x
function getX() external view returns(uint x){
x = _x;
}
}

contract Call {
// Response イベントは`call`の結果`success`と`data`を出力します
contract Call{
// 定义Response事件,输出call返回的结果success和data
event Response(bool success, bytes data);

function callSetX(address payable _addr, uint256 x) public payable {
// setX()をcallし、ETHを送信
(bool success, bytes memory data) = _addr.call{value: msg.value}(abi.encodeWithSignature("setX(uint256)", x));
// call setX(),同时可以发送ETH
(bool success, bytes memory data) = _addr.call{value: msg.value}(
abi.encodeWithSignature("setX(uint256)", x)
);

emit Response(success, data); // イベントを放出
emit Response(success, data); //释放事件
}

function callGetX(address _addr) external returns (uint256) {
function callGetX(address _addr) external returns(uint256){
// call getX()
(bool success, bytes memory data) = _addr.call(abi.encodeWithSignature("getX()"));
(bool success, bytes memory data) = _addr.call(
abi.encodeWithSignature("getX()")
);

emit Response(success, data); // イベントを放出
emit Response(success, data); //释放事件
return abi.decode(data, (uint256));
}

function callNonExist(address _addr) external {
// 存在しない関数を呼び出す
(bool success, bytes memory data) = _addr.call(abi.encodeWithSignature("foo(uint256)"));
function callNonExist(address _addr) external{
// call 不存在的函数
(bool success, bytes memory data) = _addr.call(
abi.encodeWithSignature("foo(uint256)")
);

emit Response(success, data); // イベントを放出
emit Response(success, data); //释放事件
}
}

0 comments on commit 0ea7a3f

Please sign in to comment.