Skip to content

Commit

Permalink
Create contractUtils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Oct 31, 2024
1 parent 90456fe commit 16359fd
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions blockchain_integration/pi_network/utils/contractUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// utils/contractUtils.js
const Web3 = require('web3');
const web3 = new Web3(Web3.givenProvider || "http://localhost:8545");

/**
* Create a contract instance
* @param {string} abi - The contract ABI
* @param {string} address - The contract address
* @returns {Object} - The contract instance
*/
const getContractInstance = (abi, address) => {
return new web3.eth.Contract(abi, address);
};

/**
* Send a transaction to a contract method
* @param {Object} contract - The contract instance
* @param {string} methodName - The method name to call
* @param {Array} args - The arguments for the method
* @param {string} from - The address sending the transaction
* @param {string} value - The value to send (in Wei)
* @returns {Promise<Object>} - The transaction receipt
*/
const sendTransaction = async (contract, methodName, args, from, value = '0') => {
const method = contract.methods[methodName](...args);
const gas = await method.estimateGas({ from, value });
return await method.send({ from, gas, value });
};

/**
* Call a contract method (read-only)
* @param {Object} contract - The contract instance
* @param {string} methodName - The method name to call
* @param {Array} args - The arguments for the method
* @returns {Promise<any>} - The result of the method call
*/
const callMethod = async (contract, methodName, args) => {
return await contract.methods[methodName](...args).call();
};

module.exports = {
getContractInstance,
sendTransaction,
callMethod,
};

0 comments on commit 16359fd

Please sign in to comment.