如何知道接口的 link 在以太坊区块链上的实现和声明之间在哪里

how to get to know where is interface's link between implementation and declaration on ethereum blockchain

我不明白如何在 solidity 中将接口声明连接到实现部分

这里是伪代码,只留下了相关的重要部分。

ApproveAndCall.sol

    contract ApproveAndCall {
        function receiveApproval(address _sender, uint256 _amount, address _addressOfToken, bytes _extraData) external {
            emit ReceiveApproval(_sender, _amount, _addressOfToken, _extraData);
        }
    }

TokenERC20.sol
    interface tokenRecipient { 
      function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; 
    }

    contract TokenERC20 is Pausable {
        function approveAndCall(address _spender, uint256 _value, bytes _extraData) public noReentrancy returns (bool success) {
                tokenRecipient spender = tokenRecipient(_spender);
                spender.receiveApproval(msg.sender, _value, this, _extraData);
        }
    }

如您所见,接口 "tokenRecipient" 是在 TokenERC20.sol 中声明的 名为 "spender" 的 tokenRecipient 将调用函数 "receiveApproval".

但是 TokenERC20 智能合约如何知道 "spender" 调用的真实 "receiveApproval"?

我认为这似乎与地址或其他东西没有任何联系。

两个智能合约都已经部署在 rinkeby 测试网上。它似乎仍然运行良好。

这个接口只是为了方便,所以你可以很容易地调用接口方法。你可以将任何智能合约地址投射到这个接口,即使它们不是这个接口的实例。

例如,如果我对您的示例进行以下更改:

ApproveAndCall.sol

contract ApproveAndCall {
    function receiveApproval(address _sender, uint256 _amount, address _addressOfToken, bytes _extraData) external {
        emit ReceiveApproval(_sender, _amount, _addressOfToken, _extraData);
    }
}

contract ApproveAndCall2 {
    function() public {
        emit Fallback(msg.data);
    }
}

TokenERC20.sol

interface tokenRecipient { 
  function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; 
}

contract TokenERC20 is Pausable {
    function approveAndCall(address _spender, uint256 _value, bytes _extraData) public noReentrancy returns (bool success) {
            tokenRecipient spender = tokenRecipient(_spender);
            spender.receiveApproval(msg.sender, _value, this, _extraData);
    }
}

如果使用ApproveAndCall合约的地址作为_spender参数,它会按预期工作,因为相应的函数实际上是在智能合约中定义的,所以receiveApproval 被调用。

但是,如果 ApproveAndCall2 合约的地址将用作 _spender 参数,则会调用 'fallback function',因为 receiveApproval 函数确实ApproveAndCall2 合同中不存在。 msg.data 变量包含此函数调用的编码调用数据(函数名称、参数值等)。