Solidity
契約在單個源文件中創建另一個契約
當部署為單個源文件時,儲存在
creator = TokenCreator(msg.sender)
函式內變數中OwnedToken
的地址是指合約TokenCreator
地址還是外部帳戶地址?contract OwnedToken { TokenCreator creator; address owner; bytes32 name; function OwnedToken(bytes32 _name) { owner = msg.sender; creator = TokenCreator(msg.sender); name = _name; } function changeName(bytes32 newName) { if (msg.sender == creator) name = newName; } function transfer(address newOwner) { if (msg.sender != owner) return; if (creator.isTokenTransferOK(owner, newOwner)) owner = newOwner; } contract TokenCreator { function createToken(bytes32 name) returns (OwnedToken tokenAddress) { return new OwnedToken(name); } function changeName(OwnedToken tokenAddress, bytes32 name) { tokenAddress.changeName(name); } function isTokenTransferOK( address currentOwner, address newOwner ) returns (bool ok) { address tokenAddress = msg.sender; return (sha3(newOwner) & 0xff) == (bytes20(tokenAddress) & 0xff); } }
在
creator = TokenCreator(msg.sender);
creator
地址為msg.sender
。
msg.sender
是創建 的帳戶的地址OwnedToken
。有2種情況:
- 創建的外部帳戶
OwnedToken
或者
- 一個契約,如
TokenCreator
,創建一個OwnedToken
(vianew OwnedToken(...)
)。當部署為單個源文件時,只有最後一個合約在區塊鏈上被實例化,這是 TokenCreator 的一個實例,它獲取一個地址,呼叫它
X
。當交易稍後被發送到
X
呼叫createToken
時,這是在區塊鏈上實例化 OwnedToken 並且creator
在該 OwnedToken 中將是X
。請注意,這
X
與用於部署單個源文件的外部帳戶不同。並且外部帳戶呼叫createToken
可以與用於部署單個源文件的帳戶相同或不同。