<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>fiveeth</title>
        <link>https://paragraph.com/@fiveeth</link>
        <description>a little boy like crypto</description>
        <lastBuildDate>Mon, 04 May 2026 22:43:57 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <image>
            <title>fiveeth</title>
            <url>https://storage.googleapis.com/papyrus_images/e26ac4c4ada7239fffe879308036a78eb02140260eae5cb689230a3972565732.png</url>
            <link>https://paragraph.com/@fiveeth</link>
        </image>
        <copyright>All rights reserved</copyright>
        <item>
            <title><![CDATA[合约调用合约的三种方式]]></title>
            <link>https://paragraph.com/@fiveeth/QkTEh0IteuCEq4Pzpdwh</link>
            <guid>QkTEh0IteuCEq4Pzpdwh</guid>
            <pubDate>Fri, 20 May 2022 08:01:29 GMT</pubDate>
            <description><![CDATA[第一种：接口形式调用合约为什么采用这种方式？ 原因是因为被调用合约过于复杂的时候，全引入进来的话没有必要！ 被调用合约：// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract Counter { uint public count; function inc() external { count += 1; } function dec() external { count -= 1; } } 调用合约：// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; // 这个接口名称叫什么不重要，规范建议首字母大写 interface ICounter { function count() external view returns(uint); function inc() external; } contract Interface { uint public count; // _counter为被调用合约的地址 function examples(a...]]></description>
            <content:encoded><![CDATA[<h3 id="h-" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">第一种：接口形式调用合约</h3><p>为什么采用这种方式？</p><p>原因是因为被调用合约过于复杂的时候，全引入进来的话没有必要！</p><p>被调用合约：</p><pre data-type="codeBlock" text="// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Counter {
    uint public count;

    function inc() external  {
       count += 1;
    }

    function dec() external {
        count -= 1;
    }
}
"><code><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">Counter</span> </span>{
    <span class="hljs-keyword">uint</span> <span class="hljs-keyword">public</span> count;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">inc</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span>  </span>{
       count <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">dec</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> </span>{
        count <span class="hljs-operator">-</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>;
    }
}
</code></pre><p>调用合约：</p><pre data-type="codeBlock" text="// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// 这个接口名称叫什么不重要，规范建议首字母大写
interface ICounter {
    function count() external view returns(uint);
    function inc() external;
}

contract Interface {
    uint public count;
    
    // _counter为被调用合约的地址
    function examples(address _counter) external {
        ICounter(_counter).inc();
        count = ICounter(_counter).count();
    }
}
"><code><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.0;</span>

<span class="hljs-comment">// 这个接口名称叫什么不重要，规范建议首字母大写</span>
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">ICounter</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">count</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span>(<span class="hljs-params"><span class="hljs-keyword">uint</span></span>)</span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">inc</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span></span>;
}

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">Interface</span> </span>{
    <span class="hljs-keyword">uint</span> <span class="hljs-keyword">public</span> count;
    
    <span class="hljs-comment">// _counter为被调用合约的地址</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">examples</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _counter</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> </span>{
        ICounter(_counter).inc();
        count <span class="hljs-operator">=</span> ICounter(_counter).count();
    }
}
</code></pre><h3 id="h-" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">第二种：直接引入形式调用合约</h3><pre data-type="codeBlock" text="// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract CallOtherContract {
    // 第一种方式
    // function setX(address _test, uint _x) external {
    //     TestContract(_test).setX(_x);
    // }

    // 第二种方式
    function setX(TestContract _test, uint _x) external {
        _test.setX(_x);
    }

    function getX(address _test) external view returns(uint) {
        return TestContract(_test).getX();
    }

    function setXandSendEth(address _test, uint _x) external payable{
        TestContract(_test).setXandSendEth{value: msg.value} (_x);
    }

    function getXandSendEth(address _test) external view returns(uint x, uint value) {
        (x, value) = TestContract(_test).getXandSendEth();
    }
}

contract TestContract {
    uint public x;
    uint public value;

    function setX(uint _x) public {
        x = _x;
    }

    function getX() public view returns(uint) {
        return x;
    }

    function setXandSendEth(uint _x) public payable {
        x = _x;
        value = msg.value;
    }

    function getXandSendEth() public view returns(uint,uint) {
        return (x, value);
    }
}
"><code><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">CallOtherContract</span> </span>{
    <span class="hljs-comment">// 第一种方式</span>
    <span class="hljs-comment">// function setX(address _test, uint _x) external {</span>
    <span class="hljs-comment">//     TestContract(_test).setX(_x);</span>
    <span class="hljs-comment">// }</span>

    <span class="hljs-comment">// 第二种方式</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setX</span>(<span class="hljs-params">TestContract _test, <span class="hljs-keyword">uint</span> _x</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> </span>{
        _test.setX(_x);
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getX</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _test</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span>(<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
        <span class="hljs-keyword">return</span> TestContract(_test).getX();
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setXandSendEth</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _test, <span class="hljs-keyword">uint</span> _x</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">payable</span></span></span>{
        TestContract(_test).setXandSendEth{<span class="hljs-built_in">value</span>: <span class="hljs-built_in">msg</span>.<span class="hljs-built_in">value</span>} (_x);
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getXandSendEth</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _test</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> x, <span class="hljs-keyword">uint</span> value</span>) </span>{
        (x, value) <span class="hljs-operator">=</span> TestContract(_test).getXandSendEth();
    }
}

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">TestContract</span> </span>{
    <span class="hljs-keyword">uint</span> <span class="hljs-keyword">public</span> x;
    <span class="hljs-keyword">uint</span> <span class="hljs-keyword">public</span> value;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setX</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _x</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
        x <span class="hljs-operator">=</span> _x;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getX</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span>(<span class="hljs-params"><span class="hljs-keyword">uint</span></span>) </span>{
        <span class="hljs-keyword">return</span> x;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setXandSendEth</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> _x</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">payable</span></span> </span>{
        x <span class="hljs-operator">=</span> _x;
        value <span class="hljs-operator">=</span> <span class="hljs-built_in">msg</span>.<span class="hljs-built_in">value</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getXandSendEth</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span>(<span class="hljs-params"><span class="hljs-keyword">uint</span>,<span class="hljs-keyword">uint</span></span>) </span>{
        <span class="hljs-keyword">return</span> (x, value);
    }
}
</code></pre><h3 id="h-call" class="text-2xl font-header !mt-6 !mb-4 first:!mt-0 first:!mb-0">第三种：call形式调用合约（注意问题）</h3><pre data-type="codeBlock" text="// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract TestCall {
    string public message;
    uint public x;

    event Log(string message);

    fallback() external payable {
        emit Log(&quot;fallback called&quot;);
    }

    function foo(string memory _message, uint _x) external payable returns(bool, uint) {
        message = _message;
        x = _x;
        return (true, 999);
    }
}


contract Call {
    bytes public data;

    function callFoo(address _test) external payable {
        (bool _status, bytes memory _data) = _test.call{value: 100}(abi.encodeWithSignature(
            &quot;foo(string,uint256)&quot;, &quot;call&quot;, 10));  // &quot;foo(string,uint256)&quot;这块参数类型之间一定不能有空格
        require(_status, &quot;call failed&quot;);
        data = _data;
    }

    function callOther(address _test) external {
        (bool _status, ) = _test.call(abi.encodeWithSignature(
            &quot;callOther()&quot;));
        require(_status, &quot;callOther failed&quot;);
    }
}
"><code><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.0;</span>

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">TestCall</span> </span>{
    <span class="hljs-keyword">string</span> <span class="hljs-keyword">public</span> message;
    <span class="hljs-keyword">uint</span> <span class="hljs-keyword">public</span> x;

    <span class="hljs-function"><span class="hljs-keyword">event</span> <span class="hljs-title">Log</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> message</span>)</span>;

    <span class="hljs-function"><span class="hljs-keyword">fallback</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">payable</span></span> </span>{
        <span class="hljs-keyword">emit</span> Log(<span class="hljs-string">"fallback called"</span>);
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span> _message, <span class="hljs-keyword">uint</span> _x</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">payable</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span>(<span class="hljs-params"><span class="hljs-keyword">bool</span>, <span class="hljs-keyword">uint</span></span>) </span>{
        message <span class="hljs-operator">=</span> _message;
        x <span class="hljs-operator">=</span> _x;
        <span class="hljs-keyword">return</span> (<span class="hljs-literal">true</span>, <span class="hljs-number">999</span>);
    }
}


<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">Call</span> </span>{
    <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">public</span> data;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callFoo</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _test</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">payable</span></span> </span>{
        (<span class="hljs-keyword">bool</span> _status, <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> _data) <span class="hljs-operator">=</span> _test.<span class="hljs-built_in">call</span>{<span class="hljs-built_in">value</span>: <span class="hljs-number">100</span>}(<span class="hljs-built_in">abi</span>.<span class="hljs-built_in">encodeWithSignature</span>(
            <span class="hljs-string">"foo(string,uint256)"</span>, <span class="hljs-string">"call"</span>, <span class="hljs-number">10</span>));  <span class="hljs-comment">// "foo(string,uint256)"这块参数类型之间一定不能有空格</span>
        <span class="hljs-built_in">require</span>(_status, <span class="hljs-string">"call failed"</span>);
        data <span class="hljs-operator">=</span> _data;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callOther</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _test</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> </span>{
        (<span class="hljs-keyword">bool</span> _status, ) <span class="hljs-operator">=</span> _test.<span class="hljs-built_in">call</span>(<span class="hljs-built_in">abi</span>.<span class="hljs-built_in">encodeWithSignature</span>(
            <span class="hljs-string">"callOther()"</span>));
        <span class="hljs-built_in">require</span>(_status, <span class="hljs-string">"callOther failed"</span>);
    }
}
</code></pre>]]></content:encoded>
            <author>fiveeth@newsletter.paragraph.com (fiveeth)</author>
        </item>
    </channel>
</rss>