Skip to content

Commit 31b7ce4

Browse files
committed
testBlockMiddleware - handle eth_sendRawTransaction
1 parent 52129cc commit 31b7ce4

1 file changed

Lines changed: 48 additions & 10 deletions

File tree

test/util/testBlockMiddleware.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
const createScaffoldMiddleware = require('json-rpc-engine/src/createScaffoldMiddleware')
2+
const EthTx = require('ethereumjs-tx')
3+
const ethUtil = require('ethereumjs-util')
14
const incrementHexNumber = require('../../lib/hexUtils').incrementHexNumber
25
const formatHex = require('../../lib/hexUtils').formatHex
36

7+
48
module.exports = class TestBlockMiddleware {
59

610
constructor() {
@@ -48,16 +52,50 @@ module.exports = class TestBlockMiddleware {
4852
}
4953

5054
createMiddleware() {
51-
return (req, res, next, end) => {
52-
if (req.method !== 'eth_getBlockByNumber') return next()
53-
const blockRef = req.params[0]
54-
if (blockRef === 'latest') {
55-
res.result = this.currentBlock
56-
} else {
57-
res.result = this._blockchain[blockRef]
58-
}
59-
end()
60-
}
55+
return createScaffoldMiddleware({
56+
57+
eth_getBlockByNumber: (req, res, next, end) => {
58+
const blockRef = req.params[0]
59+
if (blockRef === 'latest') {
60+
res.result = this.currentBlock
61+
} else {
62+
res.result = this._blockchain[blockRef]
63+
}
64+
end()
65+
},
66+
67+
eth_sendRawTransaction: (req, res, next, end) => {
68+
const rawTx = req.params[0]
69+
const rawTxBuffer = ethUtil.toBuffer(rawTx)
70+
const tx = new EthTx(rawTxBuffer)
71+
const txHash = tx.hash()
72+
const txJson = {
73+
// raw data
74+
nonce: ethUtil.bufferToHex(tx.nonce),
75+
gasLimit: ethUtil.bufferToHex(tx.gasLimit),
76+
gasPrice: ethUtil.bufferToHex(tx.gasPrice),
77+
to: ethUtil.bufferToHex(tx.to),
78+
value: ethUtil.bufferToHex(tx.value),
79+
data: ethUtil.bufferToHex(tx.data),
80+
v: ethUtil.bufferToHex(tx.v),
81+
r: ethUtil.bufferToHex(tx.r),
82+
s: ethUtil.bufferToHex(tx.s),
83+
chainId: tx.chainId,
84+
// meta
85+
hash: txHash,
86+
address: ethUtil.bufferToHex(tx.from),
87+
topics: [],
88+
logIndex: '0xdeadbeef',
89+
transactionIndex: '0x' + this._pendingTxs.length.toString(16),
90+
blockNumber: this.currentBlock.number,
91+
blockHash: this.currentBlock.hash,
92+
}
93+
this.addTx(txJson)
94+
res.result = txHash
95+
end()
96+
},
97+
98+
})
6199
}
62100

63101
}

0 commit comments

Comments
 (0)