首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Solidity文档中TokenCreator/OwnedToken示例的工作原理

Solidity文档中TokenCreator/OwnedToken示例的工作原理
EN

Ethereum用户
提问于 2021-04-16 04:12:02
回答 1查看 54关注 0票数 0

在这篇文章(从实体文档中理解TokenCreator/OwnedToken示例)中给出了详细的答复,但我仍然有一个关于“它是如何工作的”的问题。

代码语言:javascript
运行
复制
contract OwnedToken {
    // TokenCreator is a contract type that is defined below.
    // It is fine to reference it as long as it is not used
    // to create a new contract.
    TokenCreator public creator;
    address public owner;
    string public name;

    // This is the constructor which registers the
    // creator and the assigned name.
    function OwnedToken(string _name) {
        owner = msg.sender;
        // We do an explicit type conversion from `address`
        // to `TokenCreator` and assume that the type of
        // the calling contract is TokenCreator, there is
        // no real way to check that.
        creator = TokenCreator(msg.sender);
        name = _name;
    }

    function changeName(string newName) {
        // Only the creator can alter the name --
        // the comparison is possible since contracts
        // are implicitly convertible to addresses.
        if (msg.sender == address(creator))
            name = newName;
    }

    function transfer(address newOwner) {
        // Only the current owner can transfer the token.
        if (msg.sender != owner) 
            return;
        // We also want to ask the creator if the transfer
        // is fine. Note that this calls a function of the
        // contract defined below. If the call fails (e.g.
        // due to out-of-gas), the execution here stops
        // immediately.
        if (creator.isTokenTransferOK(owner, newOwner))
            owner = newOwner;
    }
}

contract TokenCreator {

    mapping(string => address) addresses;

    function getAddress(string name) constant returns (address) {
        return addresses[name];
    }

    function createToken(string name)
       returns (OwnedToken tokenAddress)
    {
        // Create a new Token contract and return its address.
        // From the JavaScript side, the return type is simply
        // "address", as this is the closest type available in
        // the ABI.
        tokenAddress = new OwnedToken(name);
        addresses[name] = tokenAddress;
    }

    function changeName(string oldName, string newName) {
        // Again, the external type of "tokenAddress" is
        // simply "address".
        address tokenAddress = addresses[oldName];
        delete addresses[oldName];
        addresses[newName] = tokenAddress;
        OwnedToken(tokenAddress).changeName(newName);
    }

    function isTokenTransferOK(
        address currentOwner,
        address newOwner
    ) returns (bool ok) {
        // Check some arbitrary condition.
        address tokenAddress = msg.sender;
        return (sha3(newOwner) & 0xff) == (bytes20(tokenAddress) & 0xff);
    }
}

当调用"function OwnedToken(string _name)“时,我没有得到?据我所知,当我们调用"new (Name)“时,”它不会执行构造函数“(c),就像官方文档中所说的那样。正如我假设的那样,它只创建了一个"OwnedToken“契约的实例,并且不知怎么(它是如何工作的,tho?)将其部署到某些地址(此地址位于?)地址;

EN

回答 1

Ethereum用户

回答已采纳

发布于 2021-04-16 08:11:52

根据最新的文档,通过new创建合同时传递的任何参数都将在构造函数中传递。

代码语言:javascript
运行
复制
contract D {
    uint public x;
    constructor(uint a) payable {
        x = a;
    }
}

然后,

代码语言:javascript
运行
复制
D d = new D(4); // will be executed as part of C's constructor
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/97319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档