0

This is the deploy.js file:

        const assert = require('assert'); 
        const HDWalletProvider = require('truffle-hdwallet-provider'); 
        const Web3 = require('web3'); 
        const cF = require('./Build/CampaignFact.json');

        const provider = new HDWalletProvider( 
            /*account seed phrase*/,    
            'rinkeby.infura.io/account_Address' 
         ); 
        const web3 = new Web3(provider);

            const deploy = async () => {    
                const accounts = await web3.eth.getAccounts();

                console.log('Attemping to deploy from account', accounts[0]);

                const result = await new web3.eth.Contract(JSON.parse(cF.interface))        
                               .deploy({ data: '0x'+ cF.bytecode })         
                               .send({ from: accounts[0], gas: '1000000' });

                console.log('Contract deployed to: ', result.options.address);
            }; 
        deploy();

CampaignFact.json and Campaign.json are the compiled files of my contract. The Campiagn.sol file is described below:

pragma solidity ^0.4.25;

contract CampaignFact {
    address[] public deployedCampaigns;

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

function getDeployedCampaigns() public view returns (address[] memory) {
    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 restrict() {
    require(msg.sender == manager);
    _;
}

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

function contribute() public payable {
    require(msg.value > minimumContribution, "Error: You do not have enough money. You are poor.");
    approvers[msg.sender] = true;
    approversCount++;
}

function createRequest(string memory desc, uint val, address rec) public restrict {
    //make sure that this person has donated earlier
    require(approvers[msg.sender]);

    Request memory newReq = Request({
        description: desc,
        value: val,
        recipient: rec,
        complete: false,
        approvalCount: 0
    });

    requests.push(newReq);
}

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

    //make sure person has donated before he starts to vote
    require(approvers[msg.sender]);

    //make sure person has not voted before
    require(!r.approvals[msg.sender]);

    r.approvals[msg.sender] = true;
    r.approvalCount++;
}

function finalizeRequest(uint index) public restrict {

    Request storage r = requests[index];

    require(r.approvalCount > (approversCount / 2) );
    require(!r.complete);       

    r.recipient.transfer(r.value);
    r.complete = true;
  }
}

I don't know but the script gets working till a point comes where I need to exit the command prompt. Please help me out here.

I'll also link the compile.js file that compiles 2 contracts(Campaign and CampaignFact) together. I guess that's where the error would be.

const path = require('path');
const fs = require('fs-extra');
const solc = require('solc');

const buildPath = path.resolve(__dirname, 'Build');
fs.removeSync(buildPath); //deletes the build folder

const contractPath = path.resolve(__dirname, 'contracts', 'Campaign.sol');
const source = fs.readFileSync(contractPath, 'utf-8');
const output = solc.compile(source, 1).contracts;

fs.ensureDirSync(buildPath); //checks if exists; if doesn't, create one

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

The file structure is:

Ethereum

  • Build
    • Campaign.json
    • CampaignFact.json
  • Contracts
    • Campaign.sol
  • compile.js
  • deploy.js

Note: The contract gets deployed, but that address does not show up when I use it in my webpage. I wrote this because the deploy script is infinite.

Bear with me, please! Thanks a lot!

0

I assume you are doing the Udemy course of Stephen Grider cause I did the same couple months ago and it seems similar.

According to my own project, there are a couple of missing things on your compile.js. Probably cause you are still in an early state of the course. Although, I will leave my build here and you try it out and make some comparison. I made some comments in hope to make you understand it better. Good luck.

const path = require("path"); //nodejs ’path’ module
const solc = require("solc"); //solidity compiler module
const fs = require("fs-extra"); //file system module

// Feth path of build
const buildPath = path.resolve(__dirname, "build");
const contractspath = path.resolve(__dirname, "contracts");

// Removes folder build and every file in it
fs.removeSync(buildPath);

// Fetch all Contract files in Contracts folder
const fileNames = fs.readdirSync(contractspath);

// Gets ABI of all contracts into variable input
const input = fileNames.reduce(
  (input, fileName) => {
    const filePath = path.resolve(__dirname, "contracts", fileName);
    const source = fs.readFileSync(filePath, "utf8");
    return { sources: { ...input.sources, [fileName]: source } };
  },
  { sources: {} }
);

// Compile all contracts
const output = solc.compile(input, 1).contracts;

// Re-Create build folder for output files from each contract
fs.ensureDirSync(buildPath);

// Output contains all objects from all contracts
// Write the contents of each to different files
for (let contract in output) {
  fs.outputJsonSync(
    path.resolve(buildPath, contract.split(":")[1] + ".json"),
    output[contract]
  );
}
  • Thanks for the help. Yes, I am on that video series. It's just that the code goes on and on. Otherwise, it works really fine. – Ansh Mehta Mar 28 at 13:02

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.