Skip to content

Commit

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

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

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

// 返回合约ETH余额
function getBalance() view public returns(uint) {
fallback() external payable {}

// コントラクトのETH残高を返す関数
function getBalance() public view returns (uint256) {
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(uint x){
// xの値を取得する関数
function getX() external view returns (uint256 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 {
// call setX(),同时可以发送ETH
(bool success, bytes memory data) = _addr.call{value: msg.value}(
abi.encodeWithSignature("setX(uint256)", x)
);
// setX()をcallし、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{
// call 不存在的函数
(bool success, bytes memory data) = _addr.call(
abi.encodeWithSignature("foo(uint256)")
);
function callNonExist(address _addr) external {
// 存在しない関数を呼び出す
(bool success, bytes memory data) = _addr.call(abi.encodeWithSignature("foo(uint256)"));

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

0 comments on commit 5febe81

Please sign in to comment.