Created
August 18, 2021 23:36
-
-
Save swkim109/b4090774d3bbadbd51ff57081d737979 to your computer and use it in GitHub Desktop.
@ethereumjs/common
This file contains 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 ethTx = require('@ethereumjs/tx').Transaction; | |
const Common = require('@ethereumjs/common').default; | |
const {bufferToHex} = require('ethereumjs-util'); | |
const Web3 = require('web3'); | |
const { GANACHE_WS, GANACHE_NETWORK_ID } = require('./eth.config'); | |
let NETWORK_ID; | |
let PROVIDER; | |
if (process.env.NODE_ENV === "development") { | |
PROVIDER = GANACHE_WS; | |
//NETWORK_ID = GANACHE_NETWORK_ID; | |
PROVIDER = 'http://localhost:8545'; | |
NETWORK_ID = "444"; | |
} | |
//const web3 = new Web3(new Web3.providers.WebsocketProvider(PROVIDER)); | |
const web3 = new Web3(PROVIDER); | |
const abi = require('../../../client/src/contracts/SimpleStorage.json').abi; | |
const address = require('../../../client/src/contracts/SimpleStorage.json').networks[NETWORK_ID].address; | |
set = async (ctx) => { | |
const {from, val} = ctx.request.body; | |
const contract = new web3.eth.Contract(abi, address); | |
try { | |
//The encoded ABI byte code to send via a transaction or call. | |
const data = contract.methods.set(val).encodeABI(); | |
let txObject = {}; | |
// const txCount = await web3.eth.getTransactionCount(from); | |
// txObject["nonce"] = txCount; | |
txObject["from"] = from; | |
txObject["to"] = address; | |
txObject["data"] = data; | |
txObject["gasLimit"] = web3.utils.toHex(3000000); | |
txObject["gasPrice"] = web3.utils.toHex(web3.utils.toWei('20','gwei')); | |
ctx.body = {success: true, rawTx: txObject}; | |
} catch (err) { | |
console.log(err); | |
ctx.throw(500); | |
} | |
} | |
setTx = async (ctx) => { | |
const {from, val} = ctx.request.body; | |
const contract = new web3.eth.Contract(abi, address); | |
const privateKey = Buffer.from("4099...8899", "hex"); | |
try { | |
const data = contract.methods.set(val).encodeABI(); | |
const txCount = await web3.eth.getTransactionCount(from); | |
console.log(txCount); | |
const txObject = { | |
nonce: web3.utils.toHex(txCount), | |
from: from, | |
to: address, | |
data: data, | |
gasLimit:web3.utils.toHex(3000000), | |
gasPrice:web3.utils.toHex(web3.utils.toWei('20','gwei')), | |
chainId: 444 | |
} | |
const local = Common.custom( | |
{ | |
name:'mainnet', | |
chainId: 444, | |
networkId: 444, | |
// defaultHardfork: 'berlin', | |
// genesis: {hash: '0x13489d16344c87c3bae32fb85df591ce095c55346f7c19811ce9b1aa3fc8a5e4'}, | |
// url: 'http://localhost:8545', | |
// hardforks: ['berlin'], | |
// bootstrapNodes: [], | |
// | |
} | |
); | |
const tx = ethTx.fromTxData(txObject, {common: local}); | |
//console.log(tx.common._chainParams); | |
const signedTx = tx.sign(privateKey); // sign a transaction with a given private key(32 bytes) | |
console.log(signedTx); | |
//console.log(bufferToHex(signedTx.getSenderAddress())); | |
const serializedTx = signedTx.serialize(); | |
// web3.eth.sendSignedTransaction(signedTransactionData [, callback]) | |
// Signed transaction data in HEX format | |
const txHash = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')); | |
//const txHash = '0x'; | |
ctx.body = {success: true, txHash}; | |
} catch (err) { | |
console.log(err); | |
ctx.throw(500); | |
} | |
} | |
module.exports = { | |
set, | |
setTx | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment