-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create data_integration_with_blockchain.js
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
data_integration_layer/data_integration_with_blockchain.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React, { useState, useEffect } from 'eact'; | ||
import axios from 'axios'; | ||
import Web3 from 'web3'; | ||
|
||
function DataIntegrationLayer() { | ||
const [data, setData] = useState({}); | ||
|
||
useEffect(() => { | ||
axios.get('https://api.example.com/data') | ||
.then(response => { | ||
setData(response.data); | ||
}) | ||
.catch(error => { | ||
console.error(error); | ||
}); | ||
}, []); | ||
|
||
const blockchain = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/YOUR_PROJECT_ID')); | ||
|
||
const contract = new blockchain.eth.Contract(YOUR_CONTRACT_ABI, YOUR_CONTRACT_ADDRESS); | ||
|
||
const sendDataToBlockchain = async () => { | ||
try { | ||
const txCount = await blockchain.eth.getTransactionCount(); | ||
const tx = { | ||
from: YOUR_WALLET_ADDRESS, | ||
to: YOUR_CONTRACT_ADDRESS, | ||
value: '0', | ||
gas: '20000', | ||
gasPrice: '20', | ||
data: contract.methods.sendData(data).encodeABI(), | ||
}; | ||
const signedTx = await blockchain.eth.accounts.signTransaction(tx, YOUR_WALLET_PRIVATE_KEY); | ||
const receipt = await blockchain.eth.sendSignedTransaction(signedTx.rawTransaction); | ||
console.log(`Transaction receipt: ${receipt.transactionHash}`); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h1>Data Integration Layer</h1> | ||
<p>Data: {JSON.stringify(data)}</p> | ||
<button onClick={sendDataToBlockchain}>Send data to blockchain</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default DataIntegrationLayer; |