-1

This is my hands on contract with this Udemy Course - Ethereum-and-solidity-the-complete-developers-guide

This is the contract file, which contains two contracts - Campaign and FactoryCampaign. I know there is a similar thread on this error that is answered in the link here

However my problem here is slightly different. In my case, there are two contracts and I want to export these as two different outputs.

As per the course, this is the main contract;

    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 {
        require(approvers[msg.sender]);
        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;

    }
}

The compile code is;

    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, 'utf8');
const output = solc.compile(source, 1).contracts;

fs.ensureDirSync(buildPath);

for (let contract in output) {
  fs.outputJsonSync(
    path.resolve(buildPath, contract + '.json'),
    output[contract]
  );
}

Any help or direction to update the compile.js code to export the two contracts output successfully would be much appreciated.

0

That course is outdated you can ask for a refund. But I really recommend you not to do that, instead update your own code. Your problem is maybe because you may installed the new solc in your directory package which is not compatible with the old version of solidity, hence it is not working with your old JavaScript code. I have the updated code for pragma solidity ^0.6.6 you can use (this is the final code. it has some extra functions because in the end of that course the solidity code will be changed):

Here is the solidity Code:

pragma solidity ^0.6.6;

contract campaignFactory{
    address[] public deployedCampaigns;

    function createCampaign(uint min) public {
        Campaign newCampaign = new Campaign(min, msg.sender);
        deployedCampaigns.push(address(newCampaign));
    }

    function getDeployedCampaigns() public view returns (address[] memory){
        return deployedCampaigns;
    }
}

contract Campaign{
    struct Request{
        string description;
        uint value;
        address payable recipient;
        bool compelete;
        uint approvalCount;
        mapping(address => bool) hasVoted;
    }

    Request[] public requests;
    address public manager;
    uint public minContribution;
    mapping(address => bool) public approvers;
    uint public approversCount;

    modifier onlyManager(){
        require(msg.sender == manager);
        _;
    }

    constructor (uint min,address creator) public{
        manager = creator;
        minContribution = min;
    }

    function contribute() public payable{
        require(msg.value > minContribution);

        approvers[msg.sender] = true;
        approversCount++;
    }

    function createRequest(string memory description, uint value, address payable recipient) public onlyManager {
        Request memory newRequest = Request({
            description: description,
            value: value,
            recipient: recipient,
            compelete: false,
            approvalCount: 0
        });

        requests.push(newRequest);
    }

    function approveRequest(uint index) public {
        Request storage thisRequest = requests[index];

        require(approvers[msg.sender]);
        require(!thisRequest.hasVoted[msg.sender]);

        thisRequest.hasVoted[msg.sender] = true;
        thisRequest.approvalCount++;
    }

    function finalizeRequest(uint index) public onlyManager{
        Request storage thisRequest = requests[index];

        require(thisRequest.approvalCount > (approversCount / 2));
        require(!thisRequest.compelete);

        thisRequest.recipient.transfer(thisRequest.value);
        thisRequest.compelete = true;
    }

    function getSummary() public view returns(uint, uint, uint, uint, address){
        return(
            minContribution,
            address(this).balance,
            requests.length,
            approversCount,
            manager
            );
    }

    function getRequestsCount() public view returns (uint){
        return requests.length;
    }
}

Here is the updated Compile.js code:

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, 'utf8');
var input = {
    language: 'Solidity',
    sources: {
        'Campaign.sol' : {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
};

var output = JSON.parse(solc.compile(JSON.stringify(input)));

output = output.contracts['Campaign.sol'];

//will search for the folder to ensure it exists
//and if not, will make one

fs.ensureDirSync(buildPath);

for (let contract in output){
  fs.outputJsonSync(
    path.resolve(buildPath, contract.replace('.sol','') + '.json'), output[contract]
  );
}

Check the differences and learn.

| improve this answer | |
  • Thank you very much Masoud, much appreciated. – Devendran M May 24 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.