简体   繁体   English

带有地址参数的 Solidity 默认构造函数

[英]Solidity default constructor with address parameter

As I'm learning Solidity I'm struggling to understand how the isApprovedForAll function below actually works.当我学习 Solidity 时,我很难理解下面的isApprovedForAll function 是如何工作的。

In particular I'd like to understand how calling the default ProxyRegistry constructor with an address parameter creates an initial mapping , what that initial mapping looks like, and why I should expect the if statement to ever return true.特别是,我想了解使用address参数调用默认ProxyRegistry构造函数如何创建初始mapping ,该初始映射是什么样的,以及为什么我应该期望 if 语句返回 true。

The code is taken from an official OpenSea api example.代码取自官方 OpenSea api 示例。

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);

This line does not call the ProxyRegistry constructor.此行不调用ProxyRegistry构造函数。 It creates a helper object (named proxyRegistry ) pointing to the proxyRegistryAddress address, and assumes there's a contract implementing functions defined in the ProxyRegistry contract.它创建了一个指向proxyRegistryAddress地址的助手 object (名为proxyRegistry ,并假设在ProxyRegistry合约中定义了一个实现函数的合约。

In this case, it might be less confusing to just define interface ProxyRegistry { instead of the contract ProxyRegistry { .在这种情况下,只定义interface ProxyRegistry {而不是contract ProxyRegistry {可能不会那么混乱。

But if you wanted to deploy the ProxyRegistry contract to a new address and call its constructor, you'd need use the new keyword:但是,如果您想将ProxyRegistry合约部署到新地址并调用其构造函数,则需要使用new关键字:

new ProxyRegistry(<constructor_params>);

if (address(proxyRegistry.proxies(owner)) == operator) {
    return true;
}

This snippet calls the autogenerated getter function proxies(address) on the remote contract, retrieving the mapping value by its key.此代码段在远程合约上调用自动生成的 getter function proxies(address) ,并通过其键检索映射值。 And if the remote mapping value equals the operator value, it returns true .如果远程映射值等于operator值,则返回true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM