I am attempting to call view function “0x…” with argument of value “3…” to a specific address “0x…” and also send X wei to that address
I am looking to acquire the hash of the successful tx.
Tools/Platforms: Web.js, Alchemy, EVM, Polygon Mumbai
I have issues completing the transaction. The console log is showing the transaction as successful but there is no update to the blockchain on mumbai.polygonscan.
I have connected metamask wallet (with test MATIC and Eth) to the testnet and coded into my program.
Code:
const { ethers } = require('ethers');
const provider = new ethers.providers.JsonRpcProvider('https://polygon-mumbai.g.alchemy.com/v2/H....');
const contractAddress = '0x...';
const firstFunctionSelector = '0x...';
const secondFunctionSelector = '0x...';
// Define the sender's private key
const senderPrivateKey = '3...'; // Replace 'YOUR_PRIVATE_KEY_HERE' with the actual private key
async function callFunction() {
try {
const wallet = new ethers.Wallet(senderPrivateKey, provider);
// 1st interaction: Call 0x.... function
const firstCallData = firstFunctionSelector;
const firstTx = {
from: wallet.address,
to: contractAddress,
data: firstCallData,
};
const firstTxResult = await provider.call(firstTx);
// Extract the argument and value from the response of the first call
const argument = firstTxResult.slice(0, 66); // Extract the first 32 bytes (64 characters) as the argument
const hexValue = firstTxResult.slice(66); // Extract the value
console.log(firstTxResult)
console.log(argument)
console.log(hexValue)
// 2nd interaction: Use output from first call to send transaction
// const secondTx = {
// to: contractAddress,
// data: secondFunctionSelector + argument.slice(2), // Remove '0x' prefix from the argument
// value: ethers.BigNumber.from(hexValue), // Convert hexadecimal string to BigNumber
// gasLimit: 500000,
// chainId: 80001, // Assuming Mumbai testnet
// };
// // const txResponse = await wallet.sendTransaction(secondTx);
// console.log('Transaction Hash:', txResponse.hash);
} catch (error) {
console.error('Error calling the function:', error);
}
}
callFunction();
Output:
node call-view-function.js 0x...
0x0000000000000000000000000000000000000000000000000000000000000020
00...
I need to decode this data, and use the result to send the transaction in the next step
Please assist with the code to achieve this.