0

So I created a truffle folder for solidity, wrote down the basic test.js and ipfsContract.sol needed (Creating a form in IPFS for users to enter data). HOWEVER, on doing truffle test I get 0 passing in green, although I do ask test.js to print out the hash code!

[I also do one edit in test.js - const IPFS = require('ipfs') // importing IPFS module and instantiating it. const node = new IPFS()]

Reading various articles on the net, I am thinking if I need to create an extra .js file outside the truffle directory to make it work. But I could be wrong! ANy help?

1

I have used Js implementation for IPFS

Refer this :- https://github.com/ipfs/js-ipfs-api

Connect IPFS as follows :-

   var ipfsAPI = require('ipfs-api')

   // connect to ipfs daemon API server
   var ipfs = ipfsAPI('localhost', '5001', {protocol: 'http'})

I have given a code for adding a textinput into ipfs for your reference. The given code will convert the input to hash and passed it into the contract function.

 storeTest:function(amount){

     ipfs.add(new Buffer(amount), function (err, res){
           if(err || !res) return console.error("ipfs add error", err, res);
           else{

              res.forEach(function(text) {
                    console.log('successfully stored', text.hash);
                    //Storing hash in blockchain                   
                               SimpleStorage.deployed().then(function(instance) {
                                  console.log("Initializing set function");
                                  instance.set(text.hash,{from: account, gas: 3000000})
                                          .then(function(v){ 
                                           console.log("Function  set Executed");

                                   });

                            }).catch(function(e) {
                                          console.log(e);
                            });  

             });
           }
 });
 }
  • Right thanks! But did yu write all this in test.js provided by truffle? – Rahul Kothari Mar 9 '18 at 10:11
  • @RahulKothari I didn't get. – Crissi Mariam Robert Mar 9 '18 at 10:37
  • I mean, can it be written in test.js (that comes in the test folder when we do truffle init) ALSO, why do we have amount in the first line? – Rahul Kothari Mar 9 '18 at 13:52
  • Test.js deals with testing of solidity.you have to add the above code in app.js to convert your input into hash.amount is the input from frontend,which is used for creating hash. – Crissi Mariam Robert Mar 9 '18 at 14:22
  • Perfect! Thank you so much! Lastly, why do you mention amount in the first line? – Rahul Kothari Mar 10 '18 at 11:50

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.