Skip to content

Commit

Permalink
Create data_integration_with_blockchain.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 13, 2024
1 parent aaf79ce commit 8ac5bde
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions data_integration_layer/data_integration_with_blockchain.js
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;

0 comments on commit 8ac5bde

Please sign in to comment.