Solidity

solc 為具有建構子和繼承的合約返回空 bin

  • January 31, 2017

有一個簡單的合約結構,如下所示:

pragma solidity ^0.4.8;

contract A {
 uint public a1;
}

contract B is A {
 uint8 public b1;
}

我可以編譯它:

> solc --combined-json "abi,bin" inheritance.sol > ./out/test_output.json

要獲得以下資訊:

{
 "contracts": {
   "A": {
     "abi": "[{\"constant\":true,\"inputs\":[],\"name\":\"a1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"}]",
     "bin": "6060604052346000575b608e806100176000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063119552a114603c575b6000565b346000576046605c565b6040518082815260200191505060405180910390f35b600054815600a165627a7a723058205a60899c35d4ed35d09808494352b2a1170b28969925c2aa7e86a45128a569c60029"
   },
   "B": {
     "abi": "[{\"constant\":true,\"inputs\":[],\"name\":\"a1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"b1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"type\":\"function\"}]",
     "bin": "606060405234610000575b60d1806100186000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063119552a1146046578063ee495002146066575b6000565b346000576050608c565b6040518082815260200191505060405180910390f35b3460005760706092565b604051808260ff1660ff16815260200191505060405180910390f35b60005481565b600160009054906101000a900460ff16815600a165627a7a72305820c2b3dacd4eb8217e675d9a1ab699d05e0e267d2f98d09a8a81a0b1251e8adfbe0029"
   }
 },
 "version": "0.4.8+commit.60cc1668.Darwin.appleclang"
}

在契約中添加一個建構子A,我收到了空的 bin 部分B。原始碼:

pragma solidity ^0.4.8;

contract A {
 uint public a1;

 function A (address _a) {
 }
}

contract B is A {

 uint8 public b1;
}

生成的json:

{
 "contracts": {
   "A": {
     "abi": "[{\"constant\":true,\"inputs\":[],\"name\":\"a1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"_a\",\"type\":\"address\"}],\"payable\":false,\"type\":\"constructor\"}]",
     "bin": "6060604052346000576040516020806100c4833981016040528080519060200190919050505b5b505b608e806100366000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063119552a114603c575b6000565b346000576046605c565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820b498791e10bedac88be35dd16a0733002f88d24b34bb81fd03d029cda7db4fa00029"
   },
   "B": {
     "abi": "[{\"constant\":true,\"inputs\":[],\"name\":\"a1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"b1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"type\":\"function\"}]",
     "bin": ""
   }
 },
 "version": "0.4.8+commit.60cc1668.Darwin.appleclang"
}

中的無參數建構子A不會干擾bin.B

A如果你不給它一個參數,你就不能構造它。這將為您提供 B 的二進製文件:

合約 A {
uint public a1;

功能A(地址_a){
}
}

合約 B 是 A {
uint8公共b1;

函式 B(地址 _a) A(_a) {
}
}

這個也是:

合約 A {
uint public a1;

功能A(地址_a){
}
}

合約 B 是 A {
uint8公共b1;

函式B()A(0x0011223344556677889900112233445566778899){
}
}

引用自:https://ethereum.stackexchange.com/questions/11802