Solidity
在建構子中轉移契約的所有權
我正在嘗試創建契約,並同時設置其所有者
contract ERC721CrowdSale is Ownable { ... }//contract is in the same file and has access to methods contract CS_Creator is Ownable{ ERC721CrowdSale _cs; function create_crowdsale(string _name, address _wallet, uint256 _token_goal) onlyOwner returns(address){ address _new_crowdsale = new ERC721CrowdSale(_name, _wallet, _token_goal); transfer_CS_ownership(_new_crowdsale, _wallet); return _new_crowdsale; } function transfer_CS_ownership (address _new_crowdsale, address _wallet) internal { _cs = ERC721CrowdSale(_new_crowdsale); _cs.transferOwnership(_wallet); } }
這會創建眾籌,但是在混音中,當我在眾籌上嘗試任何方法時都會出錯
transact to ERC721CrowdSale.(fallback) errored: Cannot read property 'length' of undefined
我只需要一種方便的方式將眾籌的所有權轉移到錢包地址。現在它們都歸於
CS_Creator
我已經將程式碼與所有者和 ERC721CrowdSale 的相應補充混合執行,並且您的程式碼執行良好。我只是稍微修改了您的契約,以便獲得函式 create_crowdsale(…) 返回的眾籌目前所有者的價值。
pragma solidity ^0.4.19; contract Ownable { address public owner; function Ownable() public { owner = msg.sender; } modifier onlyOwner { require(msg.sender == owner); _; } function transferOwnership(address _newOwner) public onlyOwner { owner = _newOwner; } } contract ERC721CrowdSale is Ownable { string public name; address public wallet; uint256 public token_goal; function ERC721CrowdSale(string _name, address _wallet, uint256 _token_goal) public { name = _name; wallet = _wallet; token_goal = _token_goal; } //... } contract CS_Creator is Ownable{ ERC721CrowdSale _cs; function create_crowdsale(string _name, address _wallet, uint256 _token_goal) onlyOwner returns(address, address){ address _new_crowdsale = new ERC721CrowdSale(_name, _wallet, _token_goal); transfer_CS_ownership(_new_crowdsale, _wallet); return (_new_crowdsale, get_CS_ownership(_new_crowdsale)); } function transfer_CS_ownership(address _new_crowdsale, address _wallet) internal { _cs = ERC721CrowdSale(_new_crowdsale); _cs.transferOwnership(_wallet); } function get_CS_ownership(address _crowdsale) public returns(address) { return ERC721CrowdSale(_crowdsale).owner(); } }
作為眾籌所有者的返回值,我得到錢包地址的值。
我可以想像你實際上在 remix 中創建了一個合約 ERC721CrowdSale,然後在部署 CS_Creator 之後檢查所有者地址是否已更改。如果是這樣:在這種情況下,您正在使用函式 create_crowdsale(…) 創建契約 ERC721CrowdSale 的新實例,因此,它完全獨立於您較早部署的其他 ERC721CrowdSale 契約。但是,所有者地址僅在由函式 create_crowdsale(…) 創建的實例中更改。
希望能幫助到你!