在学习Solidity时,我很难理解下面的isApprovedForAll
函数是如何实际工作的。
特别是,我想了解使用address
参数调用默认ProxyRegistry
构造函数是如何创建初始mapping
的,该初始映射是什么样子的,以及为什么我希望if语句返回true。
该代码取自一个官方的OpenSeaapi示例。
contract OwnableDelegateProxy {}
contract ProxyRegistry {
mapping(address => OwnableDelegateProxy) public proxies;
}
contract SomeContract {
address proxyRegistryAddress;
constructor(
address _proxyRegistryAddress
) {
proxyRegistryAddress = _proxyRegistryAddress;
}
function isApprovedForAll(address owner, address operator)
public
view
returns (bool)
{
ProxyRegistry proxyRegistry = ProxyRegistry(proxyRegistryAddress);
if (address(proxyRegistry.proxies(owner)) == operator) {
return true;
}
return false;
}
}
ProxyRegistry proxyRegistry = ProxyRegistry(proxyRegistryAddress);
此行不调用ProxyRegistry
构造函数。它创建了一个指向proxyRegistryAddress
地址的助手对象(名为proxyRegistry
(,并假设有一个实现ProxyRegistry
契约中定义的函数的契约。
在这种情况下,只定义interface ProxyRegistry {
而不是contract ProxyRegistry {
可能不那么令人困惑。
但是,如果您想将ProxyRegistry
契约部署到一个新地址并调用其构造函数,则需要使用new
关键字:
new ProxyRegistry(<constructor_params>);
if (address(proxyRegistry.proxies(owner)) == operator) {
return true;
}
此代码段调用远程合约上自动生成的getter函数proxies(address)
,通过其键检索映射值。如果远程映射值等于operator
值,则返回true
。