我正在练习用Solidity编写智能合约,并在下面的结果中运行以下代码 没有此类文件或目录错误。
错误:ENOENT:没有此类文件或目录,请打开“ C:\ kickstart \ ethereum \ build:Campaign.json”
这是一个Kickstarter类型的合约,有多个参与者和一个经理(合约的所有者)。用户能够捐款。经理是唯一能够提取资金的人,但是这种操作需要其他用户的许可。
**Campaign.sol**
pragma solidity ^0.4.20;
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;
//address[] public approvers; mapping has a constant time lookup
mapping(address => bool) public approvers;
uint public approversCount;
modifier restricted() {
require(msg.sender == manager);
_;
}
constructor (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.complete);
request.complete = true;
}
}
**compile.js**
const path = require("path");
const solc = require("solc");
//file system on local compuiter
const fs = require("fs-extra");
const buildPath = path.resolve(__dirname, "build");
// file removal in fs-extra version
fs.removeSync(buildPath);
//path to the file
const campaignPath = path.resolve(__dirname, "contracts", "Campaign.sol");
//get a content of file
const source = fs.readFileSync(campaignPath, "utf8");
// output from the compiler
const output = solc.compile(source, 1).contracts;
for (let contract in output) {
fs.outputJsonSync(
path.resolve(buildPath, contract + ".json"),
output[contract]
);
}
答案 0 :(得分:0)
回答您的问题:Campain.json位于build文件夹中,其中包含Campaign合同的所有实例的输出。
我能够通过执行以下步骤来纠正错误:
请在下面找到该项目的链接: https://github.com/StephenGrider/EthereumCasts
答案 1 :(得分:0)
如果要确保正确配置了compile.js文件,请在for循环之前添加“ console.log(solc.compile(source,1)); ”并检查输出
关于此问题,您可以替换: path.resolve(buildPath,contract +'.json'), 与 path.resolve(buildPath,contract.replace(':','')+'.json'),
希望这会有所帮助!
答案 2 :(得分:0)
我遇到了同样的问题。问题在于Windows对文件名中包含特殊字符(在这种情况下为冒号“:”字符)感到不满意。
这样,当您按如下所示输出结果时,请删除冒号:
for (let contract in output) {
fs.outputJSONSync(
path.resolve(buildPath, contract.replace(':', '') + ".json"),
output[contract]
);
}