我很难理解 Solidity 中的接口。我错过了什么?
I am having a difficulty of understanding interfaces in Solidity. What am I missing?
我来自 Java OOP 背景并了解接口。
目前正在开发一个简单的预算应用程序 (https://github.com/compound-developers/compound-supply-examples),它接受 ETH 或稳定币并存入 Compound 并赚取利息。
我的困惑是如何使用 Solidity 接口。我来自 OOP (Java) 背景并且非常熟悉接口。
所以在这段代码(MyContracts.sol
)中你可以看到界面中有一个mint()
函数。然而,它没有实现,但你可以看到它在这里使用 uint mintResult = cToken.mint(_numTokensToSupply);
没有任何实现。
任何人都可以在没有实现的情况下如何使用接口函数吗?在这种情况下,当你调用 mint 时,实际执行的代码是什么?
我相信我找到了令我困惑的主要问题。
因此,如果您具有 OOP 背景,那么我们对接口的了解如下:
interface IERC20 {
function totalSupply() external view returns (uint256);
}
contract XYZ is IERC20 {
// then implement totalSupply here
function totalSupply() external view returns (uint256) {
// implementiation goes here.
address public add='0x123...4'
}
所以此时您可以调用 XYZ 的 totalSupply()
,您应该没问题。
但是,在 Solidity 中还有另一种使用接口的方法。我将以复合协议中的这段代码为例 (https://github.com/compound-developers/compound-supply-examples)
如果你看到MyContracts.sol
,它有如下界面:
interface CEth {
function mint() external payable;
function exchangeRateCurrent() external returns (uint256);
function supplyRatePerBlock() external returns (uint256);
function redeem(uint) external returns (uint);
function redeemUnderlying(uint) external returns (uint);
}
但是,我们的合同中没有地方使用关键字 IS 并实现任何方法。所以你可能会问我们的接口是如何使用的?
现在让我们回到 MyContracts.sol
文件中的 MyContract
合约,并在 supplyEthToCompound
函数下查看此代码:
CEth cToken = CEth(_cEtherContract);
这里我们为 CEth 接口提供 Compound 的合约地址(即 _cEtherContract
并且该地址的合约具有 mint() 函数。)
当您在下一行调用 cToken.exchangeRateCurrent();
时,我们基本上是在复合合约上调用函数 exchangeRateCurrent。
起初似乎 exchangeRateCurrent 在我们调用它的文件中没有实现,但实现位于 _cEtherContract 地址。
我希望这能消除你的困惑,尤其是如果你有传统的 OOP 背景的话。
请随时指出我的回答中任何误导的地方。
我来自 Java OOP 背景并了解接口。
目前正在开发一个简单的预算应用程序 (https://github.com/compound-developers/compound-supply-examples),它接受 ETH 或稳定币并存入 Compound 并赚取利息。
我的困惑是如何使用 Solidity 接口。我来自 OOP (Java) 背景并且非常熟悉接口。
所以在这段代码(MyContracts.sol
)中你可以看到界面中有一个mint()
函数。然而,它没有实现,但你可以看到它在这里使用 uint mintResult = cToken.mint(_numTokensToSupply);
没有任何实现。
任何人都可以在没有实现的情况下如何使用接口函数吗?在这种情况下,当你调用 mint 时,实际执行的代码是什么?
我相信我找到了令我困惑的主要问题。
因此,如果您具有 OOP 背景,那么我们对接口的了解如下:
interface IERC20 {
function totalSupply() external view returns (uint256);
}
contract XYZ is IERC20 {
// then implement totalSupply here
function totalSupply() external view returns (uint256) {
// implementiation goes here.
address public add='0x123...4'
}
所以此时您可以调用 XYZ 的 totalSupply()
,您应该没问题。
但是,在 Solidity 中还有另一种使用接口的方法。我将以复合协议中的这段代码为例 (https://github.com/compound-developers/compound-supply-examples)
如果你看到MyContracts.sol
,它有如下界面:
interface CEth {
function mint() external payable;
function exchangeRateCurrent() external returns (uint256);
function supplyRatePerBlock() external returns (uint256);
function redeem(uint) external returns (uint);
function redeemUnderlying(uint) external returns (uint);
}
但是,我们的合同中没有地方使用关键字 IS 并实现任何方法。所以你可能会问我们的接口是如何使用的?
现在让我们回到 MyContracts.sol
文件中的 MyContract
合约,并在 supplyEthToCompound
函数下查看此代码:
CEth cToken = CEth(_cEtherContract);
这里我们为 CEth 接口提供 Compound 的合约地址(即 _cEtherContract
并且该地址的合约具有 mint() 函数。)
当您在下一行调用 cToken.exchangeRateCurrent();
时,我们基本上是在复合合约上调用函数 exchangeRateCurrent。
起初似乎 exchangeRateCurrent 在我们调用它的文件中没有实现,但实现位于 _cEtherContract 地址。
我希望这能消除你的困惑,尤其是如果你有传统的 OOP 背景的话。
请随时指出我的回答中任何误导的地方。