Skip to content

Instantly share code, notes, and snippets.

@korrio
Created June 11, 2021 10:31
Show Gist options
  • Save korrio/f0ad410afe893cbecbd03d6ca3ea819c to your computer and use it in GitHub Desktop.
Save korrio/f0ad410afe893cbecbd03d6ca3ea819c to your computer and use it in GitHub Desktop.
sendTransaction.js
const drawing = async (privateKey) => {
console.log('Drawing...');
const address = getUserAddress(privateKey);
const nonce = await web3.eth.getTransactionCount(address);
const gasPriceWei = await web3.eth.getGasPrice();
const randomHex = Web3.utils.randomHex(32).substr(2);
const randomNumber = new BN(randomHex, 16).toString();
const data = LotteryContract.methods.drawing(randomNumber).encodeABI();
const signedTx = await web3.eth.accounts.signTransaction({
to: contractAddress,
gas: 2000000,
data: data,
gasPrice: gasPriceWei,
nonce: nonce,
chainId: CHAIN_ID,
}, privateKey);
const result = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log('drawing', result.transactionHash);
const logs = web3.eth.abi.decodeLog([
{
"indexed": true,
"internalType": "uint256",
"name": "issueIndex",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint8[4]",
"name": "winningNumbers",
"type": "uint8[4]"
}
],
result.logs[0].data,
result.logs[0].topics[1] // indexed topic
);
console.log('issueIndex', logs.issueIndex);
console.log('winningNumbers', logs.winningNumbers);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment