4

This is the contract code.

      pragma solidity ^0.4.2;

     contract SimpleStorage {
           uint storedData;
           function set(uint x) {
                          storedData = x;
           }
           function get() constant returns (uint retVal) {
                          return storedData;
           }
    }

This is the javascript code used (index.js)

        var addToLog = function(txt) {
              $(".logs").append("<br>" + txt);
        };

        $(document).ready(function() {

              $("button.set").click(function() {
              var value = parseInt($("input.text").val(), 10);
              SimpleStorage.set(value);
              addToLog("SimpleStorage.set(" + value + ")");
       });

       $("button.get").click(function() {
              SimpleStorage.get().then(function(value) {
                 $(".value").html(value.toNumber());
             });
             addToLog("SimpleStorage.get()");
     });

   });

This is the code for html file(index.html)

              <html>
              <head>
                <title>Embark - SimpleStorage Demo</title>
                <link rel="stylesheet" href="css/app.css">
                <script src="js/app.js"></script>
             </head>
             <body class="container">
                 <h3>Embark - SimpleStorage Demo</h3>
                <h3> 1. Set the value in the blockchain</h3>
                <div class="form-group form-inline">
                         <input type="text" class="text form-control" >
                         <input type="text" class="text form-control">
                         <button class="set btn btn-primary">Set Value</button>
                </div>

               <h3> 2. Get the current value</h3>
               <div class="form-group">
                    <div>
                        current value is <span class="value"></span>
                    </div>
               <button class="get btn btn-primary">Get Value</button>
               </div>

             <h3> 3. Contract Calls </h3>
             <div class="logs">
                <p>Javascript call being made: </p>
             </div>

            </body>
            </html>

I am trying to integrate ipfs for storing 'x' in ipfs.I am using embark framework for developing the dapp.

I have installed ipfs-api using npm as follows.

         npm install --save ipfs-api                                     

Then i have updated the above index.js with following lines

           var ipfsAPI = require('ipfs-api')

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

what are the further modifications required in index.js and index.html files for storing and retrieving the value using ipfs?

  • @samuraj jack Please help me to develop a dapp using IPFS – Crissi Mariam Robert Jan 11 '17 at 4:29
  • can you please be more specific on what you are trying to do? why you need dapp? why you need ipfs? – max taldykin Jan 11 '17 at 5:18
  • @ max taldykin I am trying to learn IPFS integration with dapp. Please do consider the case with default 'Simple Storage ' contract in 'Embark'.How can i integrate IPFS with it? – Crissi Mariam Robert Jan 11 '17 at 6:06
  • This is already better than your previous attempt, however, it's still not clear what exactly you need. I’d love to help you to improve your chances of getting an answer, here are some tips: How do I ask a good question? – Waqar Lim Jan 11 '17 at 9:41
  • @5chdn I have modified my question.please do help me – Crissi Mariam Robert Jan 11 '17 at 11:30
7

As per the documentation

  1. Set the provider:

    EmbarkJS.Messages.setProvider('orbit', {server: 'localhost', port: 5001})

  2. Saving Text

    EmbarkJS.Storage.saveText("hello world").then(function(hash) {});

  3. Retrieving Data/Text

    EmbarkJS.Storage.get(hash).then(function(content) {});

Deploying to IPFS

To deploy a dapp to IPFS, all you need to do is run a local IPFS node and then run embark ipfs. If you want to deploy to the livenet then after configuring you account on config/blockchain.json on the production environment then you can deploy to that chain by specifying the environment embark ipfs production.

EDIT from @iurimatias: A small note: as of 2.2.0 to upload to ipfs the new command is embark upload ipfs also setting the provider is needed: EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'})

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.