0

I have deployed a contract using truffle on the testrpc network. I need to write a Javascript code that can connect to the contract and run the functions of the contract but I can't figure out how to do that.

Edit:

I am now using truffle-contract and truffle-artifactor in order to connect to deployed contracts. Here is the artifactor code :

var Web3 = require('web3');
var fs = require("fs");
var Artifactor = require("truffle-artifactor");
var provider = new Web3.providers.HttpProvider("http://localhost:8545");
var contract = require("truffle-contract");
var temp = require("temp").track();
var path = require("path");
var requireNoCache = require("require-nocache")(module);


var abiVal = JSON.parse(fs.readFileSync('./build/contracts/DINRegistry.json'))

var contractData = {
    contract_name: "DINRegistry",
    abi: abiVal
}

var dirPath = temp.mkdirSync({
    dir: path.resolve("./"),
});

var my_path = '/home/lee/Music/DIN-master';
var expected_filepath = path.join(my_path, '/build/contracts/DINRegistry.json')

function artifactCreator() {
    const artifactor = new Artifactor(my_path);

    artifactor.save({
        contract_name: "DINRegistry",
        abi: abiVal
    }, "./DINRegistry.sol.js")
}

artifactCreator();

The code isn't able to create the file DINRegistry.sol.js. What is incorrect in this code?

  • Please read the updated question. – lee huang Aug 23 '17 at 4:01
2

We had this same requirement in one of the project where we interacted with truffle deployed contract from JavaScript. You can check this script for reference - https://github.com/Imaginea/lms/blob/master/server/routes.js

Basically, we are doing following things -

  • define truffle artifacts
  • define web3 http provider, set provider
  • create contract instance with the help of deployed address.

Hope this helps.

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.