Skip to content

Commit

Permalink
Merge b513d29 into ae70e6b
Browse files Browse the repository at this point in the history
  • Loading branch information
lsaether committed Aug 7, 2018
2 parents ae70e6b + b513d29 commit 16ce1d7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/Actions/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default class Actions {
if (
await hasPending(this.config, txRequest, {
type: 'execute',
exactPrice: txRequest.gasPrice
minPrice: txRequest.gasPrice
})
) {
return ExecuteStatus.PENDING;
Expand Down
43 changes: 19 additions & 24 deletions src/Actions/Pending.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import Cache from '../Cache';
import { FnSignatures } from '../Enum';

interface PendingOpts {
type?: string;
checkGasPrice?: boolean;
minPrice?: number;
}

/**
* Uses the Parity specific RPC request `parity_pendingTransactions` to search
* for pending transactions in the transaction pool.
* @param {TransactionRequest} txRequest
* @param {string} type (optional) Type of pending request: claim,execute.
* @param {boolean} checkGasPrice (optional, default: true) Check if transaction's gasPrice is sufficient for Network.
* @param {number} exactPrice (optional) Expected gasPrice.
* @param {number} minPrice (optional) Expected gasPrice.
* @returns {Promise<boolean>} True if a pending transaction to this address exists.
*/
const hasPendingParity = (
conf: any,
txRequest: any,
opts: { type?: string; checkGasPrice?: boolean; exactPrice?: any }
): Promise<boolean> => {
const hasPendingParity = (conf: any, txRequest: any, opts: PendingOpts): Promise<boolean> => {
opts.checkGasPrice = opts.checkGasPrice === undefined ? true : opts.checkGasPrice;
const provider = conf.web3.currentProvider;

Expand All @@ -39,7 +42,7 @@ const hasPendingParity = (
const withValidGasPrice =
res.result[count] &&
(!opts.checkGasPrice ||
(await hasValidGasPrice(conf, res.result[count], opts.exactPrice)));
(await hasValidGasPrice(conf, res.result[count], opts.minPrice)));
if (
res.result[count] &&
isOfType(res.result[count], opts.type) &&
Expand All @@ -65,14 +68,10 @@ const hasPendingParity = (
* @param {TransactionRequest} txRequest
* @param {string} type (optional) Type of pending request: claim,execute.
* @param {boolean} checkGasPrice (optional, default: true) Check if transaction's gasPrice is sufficient for Network.
* @param {number} exactPrice (optional) Expected gasPrice.
* @param {number} minPrice (optional) Expected gasPrice.
* @returns {Promise<object>} Transaction, if a pending transaction to this address exists.
*/
const hasPendingGeth = (
conf: any,
txRequest: any,
opts: { type?: string; checkGasPrice?: boolean; exactPrice?: any }
): Promise<boolean> => {
const hasPendingGeth = (conf: any, txRequest: any, opts: PendingOpts): Promise<boolean> => {
opts.checkGasPrice = opts.checkGasPrice === undefined ? true : opts.checkGasPrice;
const provider = conf.web3.currentProvider;

Expand Down Expand Up @@ -102,7 +101,7 @@ const hasPendingGeth = (
(await hasValidGasPrice(
conf,
res.result.pending[account][nonce],
opts.exactPrice
opts.minPrice
)));
if (
res.result.pending[account][nonce] &&
Expand All @@ -129,12 +128,12 @@ const hasPendingGeth = (
* for pending transactions in the transaction pool.
* @param {Config} conf Config object.
* @param {TransactionReceipt} transaction Ethereum transaction receipt
* @param {number} exactPrice (optional) Expected gasPrice.
* @param {number} minPrice (optional) Expected gasPrice.
* @returns {Promise<boolean>} Transaction, if a pending transaction to this address exists.
*/
const hasValidGasPrice = async (conf: any, transaction: any, exactPrice?: any) => {
if (exactPrice) {
return exactPrice.valueOf() === transaction.gasPrice.valueOf();
const hasValidGasPrice = async (conf: any, transaction: any, minPrice?: any) => {
if (minPrice) {
return minPrice.valueOf() === transaction.gasPrice.valueOf();
}
const spread = 0.3;
let currentGasPrice: number;
Expand Down Expand Up @@ -172,13 +171,9 @@ const isOfType = (transaction: any, type?: string) => {
* @param {TransactionRequest} txRequest Transaction Request object to check.
* @param {string} type (optional) Type of pending request: claim,execute.
* @param {boolean} checkGasPrice (optional, default: true) Check if transaction's gasPrice is sufficient for Network.
* @param {number} exactPrice (optional) Expected gasPrice to compare.
* @param {number} minPrice (optional) Expected gasPrice to compare.
*/
const hasPending = async (
conf: any,
txRequest: any,
opts: { type?: string; checkGasPrice?: boolean; exactPrice?: any }
): Promise<boolean> => {
const hasPending = async (conf: any, txRequest: any, opts: PendingOpts): Promise<boolean> => {
let result = false;

if (conf.client === 'parity') {
Expand Down
38 changes: 22 additions & 16 deletions test/unit/UnitTestPending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { mockConfig } from '../helpers';
import { FnSignatures } from '../../src/Enum';
import hasPending from '../../src/Actions/Pending';

class provider {
class Provider {
public result: any;

constructor(opts?: any) {
const found: any = {};
console.log('opts', opts);
opts && opts.gas ? (found.gas = opts.gas) : undefined;
opts && opts.input ? (found.input = opts.input) : undefined;
opts && opts.value ? (found.value = opts.value) : undefined;
Expand All @@ -21,9 +22,9 @@ class provider {
}
public send = (request?: any, callback?: any) => {
return new Promise((resolve: any) => {
if (request.method == 'parity_pendingTransactions') {
if (request.method === 'parity_pendingTransactions') {
resolve(callback(null, pendingTx(Object.assign({}, this.result, { client: 'parity' }))));
} else if (request.method == 'txpool_content') {
} else if (request.method === 'txpool_content') {
resolve(callback(null, pendingTx(Object.assign({}, this.result, { client: 'geth' }))));
}
});
Expand Down Expand Up @@ -59,7 +60,7 @@ const pendingTx = (opts?: any) => {

const preConfig = (config: Config, opt?: any) => {
config.web3 = {
currentProvider: opt.provider ? opt.provider : new provider(),
currentProvider: opt.provider ? opt.provider : new Provider(opt),
eth: {
getGasPrice: async (callback?: any) => {
const gasPrice = opt.netGasPrice ? opt.netGasPrice : opt.gasPrice;
Expand Down Expand Up @@ -111,7 +112,9 @@ describe('hasPendingParity()', () => {
const gasPrice = 1 * 1e12;
const config = preConfig(mockConfig(), { client: 'parity', gasPrice });
const pending = await hasPending(config, mockTx({ address: startAddr, gasPrice }), {});
/* tslint:disable */
expect(pending).to.be.true;
/* tslint:enable */
});
});

Expand All @@ -120,7 +123,9 @@ describe('hasPendingGeth()', () => {
const gasPrice = 1 * 1e12;
const config = preConfig(mockConfig(), { client: 'geth', gasPrice });
const pending = await hasPending(config, mockTx({ address: startAddr, gasPrice }), {});
/* tslint:disable */
expect(pending).to.be.true;
/* tslint:enable */
});
});

Expand All @@ -129,7 +134,9 @@ describe('hasPending()', () => {
const gasPrice = 1 * 1e12;
const config = preConfig(mockConfig(), { client: '', gasPrice });
const pending = await hasPending(config, mockTx({ address: startAddr, gasPrice }), {});
/* tslint:disable */
expect(pending).to.be.false;
/* tslint:enable */
});
});

Expand All @@ -142,7 +149,7 @@ describe('Pending Unit Tests', () => {
preConfig(mockConfig(), {
client,
gasPrice,
provider: new provider({ input: FnSignatures.claim })
provider: new Provider({ input: FnSignatures.claim })
})
);
await Promise.all(
Expand All @@ -164,7 +171,7 @@ describe('Pending Unit Tests', () => {
preConfig(mockConfig(), {
client,
gasPrice,
provider: new provider({ input: FnSignatures.execute })
provider: new Provider({ input: FnSignatures.execute })
})
);
await Promise.all(
Expand All @@ -187,7 +194,7 @@ describe('Pending Unit Tests', () => {
client,
gasPrice,
netGasPrice: 15 * 1e13,
provider: new provider({ input: FnSignatures.claim })
provider: new Provider({ input: FnSignatures.claim })
})
);
await Promise.all(
Expand All @@ -209,7 +216,7 @@ describe('Pending Unit Tests', () => {
preConfig(mockConfig(), {
client,
gasPrice,
provider: new provider({ input: FnSignatures.execute })
provider: new Provider({ input: FnSignatures.execute })
})
);
await Promise.all(
Expand All @@ -231,7 +238,7 @@ describe('Pending Unit Tests', () => {
preConfig(mockConfig(), {
client,
gasPrice,
provider: new provider({ input: FnSignatures.claim })
provider: new Provider({ input: FnSignatures.claim })
})
);
await Promise.all(
Expand All @@ -249,21 +256,20 @@ describe('Pending Unit Tests', () => {
const expected = [false, false];
const results: any = [];
const gasPrice = 1 * 1e12;
const exactPrice = 0.9 * 1e12;
const minPrice = 0.9 * 1e12;
const testConfigs: Config[] = CLIENTS.map(client =>
preConfig(mockConfig(), {
client,
gasPrice,
provider: new provider({ input: FnSignatures.execute })
provider: new Provider({ input: FnSignatures.execute })
})
);
await Promise.all(
testConfigs.map(async conf => {
const pending = await hasPending(
conf,
mockTx({ address: startAddr, gasPrice: exactPrice }),
{ type: 'execute', exactPrice }
);
const pending = await hasPending(conf, mockTx({ address: startAddr, gasPrice: minPrice }), {
type: 'execute',
minPrice
});
results.push(pending);
})
);
Expand Down

0 comments on commit 16ce1d7

Please sign in to comment.