Created
June 11, 2021 10:31
-
-
Save korrio/f0ad410afe893cbecbd03d6ca3ea819c to your computer and use it in GitHub Desktop.
sendTransaction.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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