我正在尝试使用由两个合同组成的solc来编译一个solidity文件,以便将编译后的输出保存为json格式的文件夹中。
我的合同代码(Campaign.sol):-
pragma solidity ^0.4.17;
contract CampaignFactory{
address[] public deployedCampaigns;
function createCampaign(uint minimum) public {
address newCampaign = new Campaign(minimum, msg.sender);
deployedCampaigns.push(newCampaign);
}
function getDeployedCampaigns() public view returns (address[]){
return deployedCampaigns;
}
}
contract Campaign {
struct Request {
string description;
uint value;
address recipient;
bool complete;
uint approvalCount;
mapping(address => bool) approvals;
}
Request[] public requests;
address public manager;
uint public minimumContribution;
mapping(address => bool) public approvers;
uint public approversCount;
modifier restricted(){
require(msg.sender == manager);
_;
}
function Campaign(uint minimum, address creator) public{
manager = creator;
minimumContribution = minimum;
}
function contribute() public payable {
require(msg.value > minimumContribution);
approvers[msg.sender] = true;
approversCount++;
}
function createRequest(string description, uint value, address recipient) public restricted {
Request memory newRequest = Request({
description: description,
value:value,
recipient:recipient,
complete: false,
approvalCount:0
});
requests.push(newRequest);
}
function approveRequest(uint index) public {
Request storage request = requests[index];
require(approvers[msg.sender]);
require(!request.approvals[msg.sender]);
request.approvals[msg.sender]=true;
request.approvalCount++;
}
function finalizeRequest(uint index) public restricted
{
Request storage request = requests[index];
require(request.approvalCount > (approversCount / 2));
require(!request.complete);
request.recipient.transfer(request.value);
request.complete = true;
}
function getsummary() public view returns (uint,uint,uint,address) {
return (
minimumContribution,
this.balance,
requests.length,
approversCount,
manager
)
}
function getRequestsCount() public view returns (uint) {
return requests.length;
}
}
编译文件(compile.js):-
const path = require('path');
const solc = require('solc');
const fs = require('fs-extra');
const buildPath = path.resolve(__dirname,'build');
fs.removeSync(buildPath);
const campaignPath = path.resolve(__dirname,'contracts','Campaign.sol');
const source = fs.readFileSync(campaignPath,'utf-8');
const output = solc.compile(source,1).contracts;
fs.ensureDirSync(buildPath);
for(let contract in output){
fs.outputJsonSync(
path.resolve(buildPath,contract.replace(':','')+'.json'),
output[contract]
);
}
在编译时,我希望将这两个合同的已编译json文件加载到build文件夹中。 但是在命令节点compile.js上,我没有收到任何错误,但是正在创建空的构建文件夹,而没有json文件的编译输出。