2021-05-07 21:38:24 +02:00
|
|
|
import { strict as assert } from 'assert';
|
2021-06-08 17:25:48 +02:00
|
|
|
import { GAS_LIMITS } from '../../../../shared/constants/gas';
|
2021-10-28 01:55:02 +02:00
|
|
|
import { TRANSACTION_ENVELOPE_TYPES } from '../../../../shared/constants/transaction';
|
2021-03-16 22:00:08 +01:00
|
|
|
import { txMetaStub } from '../../../../test/stub/tx-meta-stub';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
createPendingNonceMiddleware,
|
|
|
|
createPendingTxMiddleware,
|
2021-03-16 22:00:08 +01:00
|
|
|
} from './middleware/pending';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('PendingNonceMiddleware', function () {
|
|
|
|
describe('#createPendingNonceMiddleware', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const getPendingNonce = async () => '0x2';
|
|
|
|
const address = '0xF231D46dD78806E1DD93442cf33C7671f8538748';
|
2020-11-03 00:41:28 +01:00
|
|
|
const pendingNonceMiddleware = createPendingNonceMiddleware({
|
|
|
|
getPendingNonce,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-10-30 23:15:54 +01:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should call next if not a eth_getTransactionCount request', function (done) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const req = { method: 'eth_getBlockByNumber' };
|
|
|
|
const res = {};
|
|
|
|
pendingNonceMiddleware(req, res, () => done());
|
|
|
|
});
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should call next if not a "pending" block request', function (done) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const req = { method: 'eth_getTransactionCount', params: [address] };
|
|
|
|
const res = {};
|
|
|
|
pendingNonceMiddleware(req, res, () => done());
|
|
|
|
});
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should fill the result with a the "pending" nonce', function (done) {
|
2020-11-03 00:41:28 +01:00
|
|
|
const req = {
|
|
|
|
method: 'eth_getTransactionCount',
|
|
|
|
params: [address, 'pending'],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const res = {};
|
2020-11-03 00:41:28 +01:00
|
|
|
pendingNonceMiddleware(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
done(new Error('should not have called next'));
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert(res.result === '0x2');
|
|
|
|
done();
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2019-10-30 23:15:54 +01:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('#createPendingTxMiddleware', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
let returnUndefined = true;
|
2020-11-03 00:41:28 +01:00
|
|
|
const getPendingTransactionByHash = () =>
|
2021-02-04 19:15:23 +01:00
|
|
|
returnUndefined ? undefined : txMetaStub;
|
|
|
|
const address = '0xF231D46dD78806E1DD93442cf33C7671f8538748';
|
2020-11-03 00:41:28 +01:00
|
|
|
const pendingTxMiddleware = createPendingTxMiddleware({
|
|
|
|
getPendingTransactionByHash,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-02-11 17:51:13 +01:00
|
|
|
const spec = {
|
2021-07-16 20:53:01 +02:00
|
|
|
accessList: null,
|
2020-11-03 00:41:28 +01:00
|
|
|
blockHash: null,
|
|
|
|
blockNumber: null,
|
|
|
|
from: '0xf231d46dd78806e1dd93442cf33c7671f8538748',
|
2021-06-08 17:25:48 +02:00
|
|
|
gas: GAS_LIMITS.SIMPLE,
|
2020-11-03 00:41:28 +01:00
|
|
|
gasPrice: '0x1e8480',
|
|
|
|
hash:
|
|
|
|
'0x2cc5a25744486f7383edebbf32003e5a66e18135799593d6b5cdd2bb43674f09',
|
|
|
|
input: '0x',
|
|
|
|
nonce: '0x4',
|
2021-10-28 01:55:02 +02:00
|
|
|
type: TRANSACTION_ENVELOPE_TYPES.LEGACY,
|
2020-11-03 00:41:28 +01:00
|
|
|
to: '0xf231d46dd78806e1dd93442cf33c7671f8538748',
|
|
|
|
transactionIndex: null,
|
|
|
|
value: '0x0',
|
|
|
|
v: '0x2c',
|
|
|
|
r: '0x5f973e540f2d3c2f06d3725a626b75247593cb36477187ae07ecfe0a4db3cf57',
|
|
|
|
s: '0x0259b52ee8c58baaa385fb05c3f96116e58de89bcc165cb3bfdfc708672fed8a',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should call next if not a eth_getTransactionByHash request', function (done) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const req = { method: 'eth_getBlockByNumber' };
|
|
|
|
const res = {};
|
|
|
|
pendingTxMiddleware(req, res, () => done());
|
|
|
|
});
|
2019-10-30 23:15:54 +01:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should call next if no pending txMeta is in history', function (done) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const req = { method: 'eth_getTransactionByHash', params: [address] };
|
|
|
|
const res = {};
|
|
|
|
pendingTxMiddleware(req, res, () => done());
|
|
|
|
});
|
2019-10-30 23:15:54 +01:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should fill the result with a the "pending" tx the result should match the rpc spec', function (done) {
|
2021-02-04 19:15:23 +01:00
|
|
|
returnUndefined = false;
|
2020-11-03 00:41:28 +01:00
|
|
|
const req = {
|
|
|
|
method: 'eth_getTransactionByHash',
|
|
|
|
params: [address, 'pending'],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const res = {};
|
2020-11-03 00:41:28 +01:00
|
|
|
pendingTxMiddleware(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
done(new Error('should not have called next'));
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
res.result,
|
|
|
|
spec,
|
|
|
|
new Error('result does not match the spec object'),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
done();
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|