|
| 1 | +const createScaffoldMiddleware = require('json-rpc-engine/src/createScaffoldMiddleware') |
| 2 | +const EthTx = require('ethereumjs-tx') |
| 3 | +const ethUtil = require('ethereumjs-util') |
1 | 4 | const incrementHexNumber = require('../../lib/hexUtils').incrementHexNumber |
2 | 5 | const formatHex = require('../../lib/hexUtils').formatHex |
3 | 6 |
|
| 7 | + |
4 | 8 | module.exports = class TestBlockMiddleware { |
5 | 9 |
|
6 | 10 | constructor() { |
@@ -48,16 +52,50 @@ module.exports = class TestBlockMiddleware { |
48 | 52 | } |
49 | 53 |
|
50 | 54 | 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 | + }) |
61 | 99 | } |
62 | 100 |
|
63 | 101 | } |
|
0 commit comments