Skip to main content
Added the contract code
Source Link

I am trying to run truffle code with Node.JS in the backend to deploy contracts. I want to be able to do this interactively and I am intending to use the Express Library to interact with the Node backend.

However, in the first instance I am just trying to run a .js in node to deploy a contract. I can do so easily in the deploy script but want to be able to do it on the fly. Here is the code I am talking about:

const Web3 = require("web3");
const HDWalletProvider = require("truffle-hdwallet-provider");
const mnemonic = "..."

var httpProvider = new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/<my API>');
var provider = new HDWalletProvider(mnemonic, httpProvider);

var contract        = require("truffle-contract");
var myContractJSON  = require('./build/contracts/Insurer.json');

// Read JSON and attach RPC connection (Provider)
var myContract = contract(myContractJSON);
myContract.setProvider(provider);

myContract.new("0x20202020202020202020204931303031", "0x20202020202020202020436f66616365")
    .then(function(instance) {
    return instance.getInsurerInfo.call()
    }).then(function(result) {
        console.log(result[0], result[1]);
        res.send('Insurer contract made successfully: '+result[0]+' '+result[1]);
        }, function(error) {
    console.log(error);
    });

and here is the relevant contract code:

    contract Insurer {
    bytes16 public insurerUIC;  
    bytes16 public insurerName;

    constructor(bytes16 _insurerUIC, bytes16 _insurerName) public {
        insurerUIC      = _insurerUIC;
        insurerName     = _insurerName;
        } // constructor(bytes16 _insurerUIC, bytes16 _insurerName) public {

    function () payable external {
        // fallback function to receive funds
    } // function () payable external {

    function getInsurerInfo() view public returns (bytes16, bytes16) {
        return (insurerUIC, insurerName);
        } // function getInsurerInfo() view public returns (bytes16, bytes16)

    } // contract Insurer {

The contract takes 2 parameters. When I run the code above it seems to run but nothing actually happens.

Any idea what is wrong? Also how can I debug Node code without being able to use the console?

Sorry for beginner questions but I am struggling!

Thanks,

Phil

I am trying to run truffle code with Node.JS in the backend to deploy contracts. I want to be able to do this interactively and I am intending to use the Express Library to interact with the Node backend.

However, in the first instance I am just trying to run a .js in node to deploy a contract. I can do so easily in the deploy script but want to be able to do it on the fly. Here is the code I am talking about:

const Web3 = require("web3");
const HDWalletProvider = require("truffle-hdwallet-provider");
const mnemonic = "..."

var httpProvider = new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/<my API>');
var provider = new HDWalletProvider(mnemonic, httpProvider);

var contract        = require("truffle-contract");
var myContractJSON  = require('./build/contracts/Insurer.json');

// Read JSON and attach RPC connection (Provider)
var myContract = contract(myContractJSON);
myContract.setProvider(provider);

myContract.new("0x20202020202020202020204931303031", "0x20202020202020202020436f66616365")
    .then(function(instance) {
    return instance.getInsurerInfo.call()
    }).then(function(result) {
        console.log(result[0], result[1]);
        res.send('Insurer contract made successfully: '+result[0]+' '+result[1]);
        }, function(error) {
    console.log(error);
    });

The contract takes 2 parameters. When I run the code above it seems to run but nothing actually happens.

Any idea what is wrong? Also how can I debug Node code without being able to use the console?

Sorry for beginner questions but I am struggling!

Thanks,

Phil

I am trying to run truffle code with Node.JS in the backend to deploy contracts. I want to be able to do this interactively and I am intending to use the Express Library to interact with the Node backend.

However, in the first instance I am just trying to run a .js in node to deploy a contract. I can do so easily in the deploy script but want to be able to do it on the fly. Here is the code I am talking about:

const Web3 = require("web3");
const HDWalletProvider = require("truffle-hdwallet-provider");
const mnemonic = "..."

var httpProvider = new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/<my API>');
var provider = new HDWalletProvider(mnemonic, httpProvider);

var contract        = require("truffle-contract");
var myContractJSON  = require('./build/contracts/Insurer.json');

// Read JSON and attach RPC connection (Provider)
var myContract = contract(myContractJSON);
myContract.setProvider(provider);

myContract.new("0x20202020202020202020204931303031", "0x20202020202020202020436f66616365")
    .then(function(instance) {
    return instance.getInsurerInfo.call()
    }).then(function(result) {
        console.log(result[0], result[1]);
        res.send('Insurer contract made successfully: '+result[0]+' '+result[1]);
        }, function(error) {
    console.log(error);
    });

and here is the relevant contract code:

    contract Insurer {
    bytes16 public insurerUIC;  
    bytes16 public insurerName;

    constructor(bytes16 _insurerUIC, bytes16 _insurerName) public {
        insurerUIC      = _insurerUIC;
        insurerName     = _insurerName;
        } // constructor(bytes16 _insurerUIC, bytes16 _insurerName) public {

    function () payable external {
        // fallback function to receive funds
    } // function () payable external {

    function getInsurerInfo() view public returns (bytes16, bytes16) {
        return (insurerUIC, insurerName);
        } // function getInsurerInfo() view public returns (bytes16, bytes16)

    } // contract Insurer {

The contract takes 2 parameters. When I run the code above it seems to run but nothing actually happens.

Any idea what is wrong? Also how can I debug Node code without being able to use the console?

Sorry for beginner questions but I am struggling!

Thanks,

Phil

Added some tags
Link
Source Link

Problem with running truffle code with node.js

I am trying to run truffle code with Node.JS in the backend to deploy contracts. I want to be able to do this interactively and I am intending to use the Express Library to interact with the Node backend.

However, in the first instance I am just trying to run a .js in node to deploy a contract. I can do so easily in the deploy script but want to be able to do it on the fly. Here is the code I am talking about:

const Web3 = require("web3");
const HDWalletProvider = require("truffle-hdwallet-provider");
const mnemonic = "..."

var httpProvider = new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/<my API>');
var provider = new HDWalletProvider(mnemonic, httpProvider);

var contract        = require("truffle-contract");
var myContractJSON  = require('./build/contracts/Insurer.json');

// Read JSON and attach RPC connection (Provider)
var myContract = contract(myContractJSON);
myContract.setProvider(provider);

myContract.new("0x20202020202020202020204931303031", "0x20202020202020202020436f66616365")
    .then(function(instance) {
    return instance.getInsurerInfo.call()
    }).then(function(result) {
        console.log(result[0], result[1]);
        res.send('Insurer contract made successfully: '+result[0]+' '+result[1]);
        }, function(error) {
    console.log(error);
    });

The contract takes 2 parameters. When I run the code above it seems to run but nothing actually happens.

Any idea what is wrong? Also how can I debug Node code without being able to use the console?

Sorry for beginner questions but I am struggling!

Thanks,

Phil