Skip to content

Instantly share code, notes, and snippets.

@drgorillamd
Created January 19, 2022 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drgorillamd/cdf9a480ce8123eca5e46dceecc9c412 to your computer and use it in GitHub Desktop.
Save drgorillamd/cdf9a480ce8123eca5e46dceecc9c412 to your computer and use it in GitHub Desktop.
Craft tx
const ethers = require('ethers');
const { Interface } = require('ethers/lib/utils');
const contractAddress = '';
const key = '';
const RPC_SERVER = '';
async function main () {
try {
const provider = new ethers.providers.JsonRpcProvider(RPC_SERVER);
const signer = new ethers.Wallet(key, provider);
const EXTRA_ADDRESS = ethers.Wallet.createRandom();
// function myFn(uint256 _amount) external
let iface = new Interface(['function myFn(uint256)']);
let inputData = ethers.utils.defaultAbiCoder.encode(["uint256","address"],[1, EXTRA_ADDRESS.address]);
inputData = iface.getSighash('myFn')+inputData.slice(2);
const txParams = {
to: contractAddress,
data: inputData,
};
let tx = await signer.sendTransaction(txParams);
await tx.wait()
} catch (e) {
console.log(e);
}
}
main();
process.on('exit', function(code) {
return console.log(`Exit with code ${code}`);
});
process.on('SIGINT', _ => {
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment