@@ -3,7 +3,6 @@ import web3 from './web3';
33import ipfs from './ipfs' ;
44import storehash from './storehash' ;
55import { Button } from 'reactstrap' ;
6- import { render } from 'react-dom' ;
76
87class App extends Component {
98
@@ -35,91 +34,102 @@ class App extends Component {
3534 this . setState ( { buffer} ) ;
3635 } ;
3736
38- async ( ) => { try { this . setState ( { blockNumber :"waiting.." } ) ; this . setState ( { gasUsed :"waiting..." } ) ;
37+ // On file submit to IPFS
38+ onSubmit = async ( event ) => {
39+ event . preventDefault ( ) ;
40+ //bring in user's metamask account address
41+ const accounts = await web3 . eth . getAccounts ( ) ;
3942
40- await web3 . eth . getTransactionReceipt ( this . state . transactionHash , ( err , txReceipt ) => {
41- console . log ( err , txReceipt ) ;
42- this . setState ( { txReceipt} ) ;
43- } ) ;
44- }
43+ console . log ( 'Sending from Metamask account: ' + accounts [ 0 ] ) ;
4544
46- catch ( error ) { console . log ( error ) ; } }
45+ //obtain contract address from storehash.js
46+ const ethAddress = await storehash . options . address ;
47+ this . setState ( { ethAddress} ) ;
4748
49+ //save document to IPFS,return its hash#, and set hash# to state
50+ await ipfs . add ( this . state . buffer , ( err , ipfsHash ) => {
51+ console . log ( err , ipfsHash ) ;
52+ //setState by setting ipfsHash to ipfsHash[0].hash
53+ this . setState ( { ipfsHash :ipfsHash [ 0 ] . hash } ) ;
4854
49- onSubmit = async ( event ) => { event . preventDefault ( ) ;
55+ // call Ethereum contract method "sendHash" and .send IPFS hash to etheruem contract
56+ //return the transaction hash from the ethereum contract
5057
51- //bring in user's metamask account address
52- const accounts = await web3 . eth . getAccounts ( ) ;
58+ storehash . methods . sendHash ( this . state . ipfsHash ) . send ( {
59+ from : accounts [ 0 ]
60+ } , ( error , transactionHash ) => {
61+ console . log ( transactionHash ) ;
62+ this . setState ( { transactionHash} ) ;
63+ } ) ; //storehash
64+ } ) //await ipfs.add
65+ } ; //onSubmit
5366
54- // render() { return ( <div className="App"> <header className="App-header"> <h1 className="App-title">Welcome to React</h1> </header> <p className="App-intro"> To get started, edit <code>src/App.js</code> and save to reload. </p> <hr /> <h2> Choose file to send to IPFS </h2> <form onSubmit={this.onSubmit}> <input type = "file" onChange = {this.captureFile} /> <input type="submit" /> </form> <hr /> <Button color="primary" onClick={this.onClick}>Get Transaction Receipt</Button> <Button color="primary" onClick={this.onClick}>Get Transaction Receipt Again</Button> <table> <thead> <tr> <th>Tx Receipt Category</th> <th>Values</th> <th> </th> </tr> </thead> <tbody> <tr> <td>IPFS Hash # stored on Eth Contract</td> <td>{this.state.ipfsHash}</td> <td></td> </tr> <tr> <td>Ethereum Contract Address</td> <td>{this.state.ethAddress}</td> <td></td> </tr> <tr> <td>Tx Hash # </td> <td>{this.state.transactionHash}</td> <td></td> </tr> <tr> <td>Block Number # </td> <td>{this.state.blockNumber}</td> <td></td> </tr> <tr> <td>Gas Used</td> <td>{this.state.gasUsed}</td> <td></td> </tr> </tbody> </table> </div> ); } }
67+ // Get transaction receipt from metamask
68+ getTransactionReceipt = async ( ) => {
69+ try {
70+ const txReceipt = await web3 . eth . getTransactionReceipt ( this . state . transactionHash ) ;
71+ console . log ( txReceipt ) ;
72+ this . setState ( { txReceipt} ) ;
73+ } catch ( error ) {
74+ console . log ( error ) ;
75+ }
76+ }
5577
56- // Get transaction receipt from metamask
57- getTransactionReceipt = async ( ) => {
58- try {
59- const txReceipt = await web3 . eth . getTransactionReceipt ( this . state . transactionHash ) ;
60- console . log ( txReceipt ) ;
61- this . setState ( { txReceipt} ) ;
62- } catch ( error ) {
63- console . log ( error ) ;
78+ // render the react app
79+ render ( ) {
80+ return (
81+ < div className = "App" >
82+ < header className = "App-header" >
83+ < h1 className = "App-title" > Welcome to React</ h1 >
84+ </ header >
85+ < hr />
86+ < h2 > Choose file to send to IPFS </ h2 >
87+ < form onSubmit = { this . onSubmit } >
88+ < input type = "file" onChange = { this . captureFile } />
89+ < input type = "submit" />
90+ </ form >
91+ < hr />
92+ < Button color = "primary" onClick = { this . getTransactionReceipt } > Get Transaction Receipt</ Button >
93+ < table >
94+ < thead >
95+ < tr >
96+ < th > Tx Receipt Category</ th >
97+ < th > Values</ th >
98+ < th > </ th >
99+ </ tr >
100+ </ thead >
101+ < tbody >
102+ < tr >
103+ < td > IPFS Hash # stored on Eth Contract</ td >
104+ < td > { this . state . ipfsHash } </ td >
105+ < td > </ td >
106+ </ tr >
107+ < tr >
108+ < td > Ethereum Contract Address</ td >
109+ < td > { this . state . ethAddress } </ td >
110+ < td > </ td >
111+ </ tr >
112+ < tr >
113+ < td > Tx Hash # </ td >
114+ < td > { this . state . transactionHash } </ td >
115+ < td > </ td >
116+ </ tr >
117+ < tr >
118+ < td > Block Number # </ td >
119+ < td > { this . state . blockNumber } </ td >
120+ < td > </ td >
121+ </ tr >
122+ < tr >
123+ < td > Gas Used</ td >
124+ < td > { this . state . gasUsed } </ td >
125+ < td > </ td >
126+ </ tr >
127+ </ tbody >
128+ </ table >
129+ </ div >
130+ ) ;
64131 }
65132}
66133
67- render ( ) {
68- return (
69- < div className = "App" >
70- < header className = "App-header" >
71- < h1 className = "App-title" > Welcome to React</ h1 >
72- </ header >
73- < p className = "App-intro" >
74- To get started, edit < code > src/App.js</ code > and save to reload.
75- </ p >
76- < hr />
77- < h2 > Choose file to send to IPFS </ h2 >
78- < form onSubmit = { this . onSubmit } >
79- < input type = "file" onChange = { this . captureFile } />
80- < input type = "submit" />
81- </ form >
82- < hr />
83- < Button color = "primary" onClick = { this . onClick } > Get Transaction Receipt</ Button >
84- < Button color = "primary" onClick = { this . onClick } > Get Transaction Receipt Again</ Button >
85- < table >
86- < thead >
87- < tr >
88- < th > Tx Receipt Category</ th >
89- < th > Values</ th >
90- < th > </ th >
91- </ tr >
92- </ thead >
93- < tbody >
94- < tr >
95- < td > IPFS Hash # stored on Eth Contract</ td >
96- < td > { this . state . ipfsHash } </ td >
97- < td > </ td >
98- </ tr >
99- < tr >
100- < td > Ethereum Contract Address</ td >
101- < td > { this . state . ethAddress } </ td >
102- < td > </ td >
103- </ tr >
104- < tr >
105- < td > Tx Hash # </ td >
106- < td > { this . state . transactionHash } </ td >
107- < td > </ td >
108- </ tr >
109- < tr >
110- < td > Block Number # </ td >
111- < td > { this . state . blockNumber } </ td >
112- < td > </ td >
113- </ tr >
114- < tr >
115- < td > Gas Used</ td >
116- < td > { this . state . gasUsed } </ td >
117- < td > </ td >
118- </ tr >
119- </ tbody >
120- </ table >
121- </ div >
122- ) ;
123- }
124- }
134+ export default App ;
125135
0 commit comments