0

I have the below contract to create document details:

pragma solidity ^0.5.0;

contract Documents {

   struct Document
     {
       uint256 creationDate;
       uint32 documentCreationTimestamp;
       string revisionNumber;
       string documentStatus;
       bytes32 ipfsHash;
      // string[100] documentModifierslist;
     }

   mapping(bytes32 => Document) documents;
   bytes32[] public documentsList;

   function setDocument(uint256  _creationDate, uint32  _documentCreationTimestamp,string memory _revisionNumber, string memory  _documentStatus,bytes32 _ipfsHash) public
    {

      Document storage document = documents[_ipfsHash];
      document.creationDate = _creationDate;
      document.documentCreationTimestamp = _documentCreationTimestamp;
      document.revisionNumber = _revisionNumber;
      document.documentStatus = _documentStatus;
      document.ipfsHash = _ipfsHash;
    //  document.documentModifierslist = _documentModifierslist;

      documentsList.push(_ipfsHash) -1;

    }

    function getDocumentList() view public returns(bytes32[] memory) {
       return documentsList;

    }

    function getDocument(bytes32 doc) view public returns(bytes32, uint256, uint32, string memory , string memory)
     {
        return(documents[doc].ipfsHash, documents[doc].creationDate, documents[doc].documentCreationTimestamp, documents[doc].revisionNumber,
        documents[doc].documentStatus);
     }
}   

I have the App.js which i used from a tutorial initially to store documents in blockchain. But, later I decided to store the document details in smart contract storage. What I am trying to do is take the ipfsHash generated and send it to the smart contract function setDocument to create the document details and store in smart contract storage. Below is my App.js

import React, { Component } from 'react';
import './App.css';
import web3 from './web3';
import ipfs from './ipfs';
import storeMyValue from './storeMyValue';

class App extends Component {

state = {
ipfsHash:null,
buffer:'',
gasUsed:'',
};

captureFile =(event) => {
event.stopPropagation()
event.preventDefault()
const file = event.target.files[0]
let reader = new window.FileReader()
reader.readAsArrayBuffer(file)
reader.onloadend = () => this.convertToBuffer(reader)
};

convertToBuffer = async(reader) => {
//file is converted to a buffer for upload to IPFS
const buffer = await Buffer.from(reader.result);
//set this buffer -using es6 syntax
this.setState({buffer});
console.log(buffer);
};

onClick = async () => {

try{
        this.setState({blockNumber:"waiting.."});
        this.setState({gasUsed:"waiting..."});

//get Transaction Receipt in console on click
//See: https://web3js.readthedocs.io/en/1.0/web3-eth.html#gettransactionreceipt

/*await web3.eth.getTransactionReceipt(this.state.transactionHash, (err, txReceipt)=>{
          console.log(err,txReceipt);
          this.setState({txReceipt});
        }); */ //await for getTransactionReceipt

/*await this.setState({blockNumber: this.state.txReceipt.blockNumber});
        await this.setState({gasUsed: this.state.txReceipt.gasUsed});*/
      } //try
    catch(error){
        console.log(error);
      } //catch
  } //onClick


onSubmit = async (event) => {

event.preventDefault();
console.log("web3 value is ",web3.eth.personal.getAccounts());
const accounts = await web3.eth.personal.getAccounts();
console.log('Sending from Metamask account: ' , accounts[0]);
const ethAddress= await storeMyValue.options.address;
this.setState({ethAddress});


await ipfs.add(this.state.buffer, (err, ipfsHash) => {
console.log(err,ipfsHash);
this.setState({ ipfsHash:ipfsHash[0].hash });


/*storeMyValue.methods.set(this.state.ipfsHash).send({
from: accounts[0]
}, (error, transactionHash) => {
console.log("transaction hash is ",transactionHash);
this.setState({transactionHash});
});*/
})


};


render() {

return (
<div className="App">
<header className="App-header">
<h1> IPFS Dapp</h1>
</header>
<hr />
<h3> Choose file to send to IPFS </h3>

<form onSubmit={this.onSubmit}>
<input type="file" onChange={this.captureFile}/>
<button type="submit"> Send it </button>
</form>

<hr/>
<table >
<thead>
<tr>
<th>Sl No</th>
<th>Values</th>
</tr>
</thead>
<tbody>
<tr>
<td>IPFS Hash # stored on Eth Contract</td>
<td>{this.state.ipfsHash}</td>
</tr>
<tr>

<td>Ethereum Contract Address</td>
<td>{this.state.ethAddress}</td>
</tr>
</tbody>

</table>
</div>);
}
}

export default App;

I dont know how i can access smart contract function from App.js and send the ipfsHash to the function from App.js.i am new to this. So, someone let me know how i can do it? I tried modifying the petshop code below and use it to access smart contract but it didnt work:

initContract: function() {
    $.getJSON('Adoption.json', function(data) {
      // Get the necessary contract artifact file and instantiate it with truffle-contract
      var AdoptionArtifact = data;
      // Truffle contract is redundant to web3, but allows you to absorb truffle build files
      // with deployed addresses and ABIs that you  would have to set otherwise in Web3 - NJ
      App.contracts.Adoption = TruffleContract(AdoptionArtifact);

      // Set the provider for our contract
      App.contracts.Adoption.setProvider(App.web3Provider);

      // Use our contract to retrieve and mark the adopted pets
      return App.markAdopted();
    });

    return App.bindEvents();
  },

  bindEvents: function() {
    $(document).on('click', '.btn-adopt', App.handleAdopt);
},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.