Skip to content

Instantly share code, notes, and snippets.

@fabianekc
Created April 17, 2023 21:40
Show Gist options
  • Save fabianekc/07ac542dfc662172ce049c1c39051eae to your computer and use it in GitHub Desktop.
Save fabianekc/07ac542dfc662172ce049c1c39051eae to your computer and use it in GitHub Desktop.
// Import the required libraries
const Web3 = require('web3');
// Create a new instance of the Web3 provider
const web3 = new Web3('https://bellecour.iex.ec');
// Define the address of the smart contract and the ABI
const contractAddress = '0xF31A125fb44E0c2dca45c2665F272e9fc09f92AE';
const contractAbi = [
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "string",
"name": "_data",
"type": "string"
}
],
"name": "DataStored",
"type": "event"
},
{
"inputs": [
{
"internalType": "string",
"name": "_data",
"type": "string"
}
],
"name": "StoreInLogs",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
// Create a new instance of the smart contract object
const contract = new web3.eth.Contract(contractAbi, contractAddress);
// Define the function to be called and its arguments
const functionName = 'StoreInLogs';
const functionArgs = ["count-5"];
// Create a new account object using your private key
const privateKey = '0x0123456789012345678901234567890123456789012345678901234567890123'; // replace with your private key
const account = web3.eth.accounts.wallet.add(privateKey);
// Send the transaction to the smart contract
contract.methods[functionName](...functionArgs).send({
from: account.address, // use the address of the newly created account
gas: 100000, // the maximum amount of gas to be used for the transaction
})
.then((receipt) => {
console.log('Transaction receipt:', receipt);
})
.catch((error) => {
console.error('Error:', error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment