Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for speedUp and stop based on provided gasValues from consumer #535

Merged
merged 23 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 45 additions & 21 deletions src/transaction/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
query,
getIncreasedPriceFromExisting,
isEIP1559Transaction,
checkGasValues,
} from '../util';
import { MAINNET, RPC } from '../constants';

Expand Down Expand Up @@ -79,6 +80,12 @@ export interface Transaction {
estimatedBaseFee?: string;
}

export interface GasValues {
gasPrice?: string;
rickycodes marked this conversation as resolved.
Show resolved Hide resolved
maxFeePerGas?: string;
maxPriorityFeePerGas?: string;
}

/**
* The status of the transaction. Each status represents the state of the transaction internally
* in the wallet. Some of these correspond with the state of the transaction on the network, but
Expand Down Expand Up @@ -701,7 +708,10 @@ export class TransactionController extends BaseController<
*
* @param transactionID - ID of the transaction to cancel
*/
async stopTransaction(transactionID: string) {
async stopTransaction(transactionID: string, gasValues?: GasValues) {
if (gasValues) {
checkGasValues(gasValues);
}
const transactionMeta = this.state.transactions.find(
({ id }) => id === transactionID,
);
Expand All @@ -713,21 +723,28 @@ export class TransactionController extends BaseController<
throw new Error('No sign method defined.');
}

const gasPrice = getIncreasedPriceFromExisting(
transactionMeta.transaction.gasPrice,
CANCEL_RATE,
);
const gasPrice =
gasValues?.gasPrice ||
rickycodes marked this conversation as resolved.
Show resolved Hide resolved
getIncreasedPriceFromExisting(
transactionMeta.transaction.gasPrice,
CANCEL_RATE,
);

const existingMaxFeePerGas = transactionMeta.transaction?.maxFeePerGas;
const existingMaxPriorityFeePerGas =
transactionMeta.transaction?.maxPriorityFeePerGas;

const newMaxFeePerGas =
existingMaxFeePerGas &&
getIncreasedPriceFromExisting(existingMaxFeePerGas, CANCEL_RATE);
gasValues?.maxFeePerGas ||
(existingMaxFeePerGas &&
getIncreasedPriceFromExisting(existingMaxFeePerGas, CANCEL_RATE));
const newMaxPriorityFeePerGas =
existingMaxPriorityFeePerGas &&
getIncreasedPriceFromExisting(existingMaxPriorityFeePerGas, CANCEL_RATE);
gasValues?.maxPriorityFeePerGas ||
(existingMaxPriorityFeePerGas &&
getIncreasedPriceFromExisting(
existingMaxPriorityFeePerGas,
CANCEL_RATE,
));

const txParams =
newMaxFeePerGas && newMaxPriorityFeePerGas
Expand Down Expand Up @@ -767,7 +784,10 @@ export class TransactionController extends BaseController<
*
* @param transactionID - ID of the transaction to speed up
*/
async speedUpTransaction(transactionID: string) {
async speedUpTransaction(transactionID: string, gasValues?: GasValues) {
if (gasValues) {
checkGasValues(gasValues);
}
const transactionMeta = this.state.transactions.find(
({ id }) => id === transactionID,
);
Expand All @@ -782,24 +802,28 @@ export class TransactionController extends BaseController<
}

const { transactions } = this.state;
const gasPrice = getIncreasedPriceFromExisting(
transactionMeta.transaction.gasPrice,
SPEED_UP_RATE,
);
const gasPrice =
gasValues?.gasPrice ||
getIncreasedPriceFromExisting(
transactionMeta.transaction.gasPrice,
SPEED_UP_RATE,
);

const existingMaxFeePerGas = transactionMeta.transaction?.maxFeePerGas;
const existingMaxPriorityFeePerGas =
transactionMeta.transaction?.maxPriorityFeePerGas;

const newMaxFeePerGas =
existingMaxFeePerGas &&
getIncreasedPriceFromExisting(existingMaxFeePerGas, SPEED_UP_RATE);
gasValues?.maxFeePerGas ||
(existingMaxFeePerGas &&
getIncreasedPriceFromExisting(existingMaxFeePerGas, SPEED_UP_RATE));
const newMaxPriorityFeePerGas =
existingMaxPriorityFeePerGas &&
getIncreasedPriceFromExisting(
existingMaxPriorityFeePerGas,
SPEED_UP_RATE,
);
gasValues?.maxPriorityFeePerGas ||
(existingMaxPriorityFeePerGas &&
getIncreasedPriceFromExisting(
existingMaxPriorityFeePerGas,
SPEED_UP_RATE,
));

const txParams =
newMaxFeePerGas && newMaxPriorityFeePerGas
Expand Down
15 changes: 14 additions & 1 deletion src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import nock from 'nock';
import HttpProvider from 'ethjs-provider-http';
import EthQuery from 'eth-query';
import * as util from './util';
import { Transaction } from './transaction/TransactionController';
import { Transaction, GasValues } from './transaction/TransactionController';

const VALID = '4e1fF7229BDdAf0A73DF183a88d9c3a04cc975e0';
const SOME_API = 'https://someapi.com';
Expand Down Expand Up @@ -923,4 +923,17 @@ describe('util', () => {
expect(util.isEIP1559Transaction(tx)).toBe(false);
});
});

describe('checkGasValues', () => {
it('should be invalid', () => {
const gasValues: GasValues = { maxFeePerGas: 'lol' };
expect(() => util.checkGasValues(gasValues)).toThrow(
'expected hex string for maxFeePerGas but received: lol',
);
});
it('should be valid', () => {
const gasValues: GasValues = { maxFeePerGas: '0x1' };
expect(util.checkGasValues(gasValues)).toBe(true);
});
});
});
12 changes: 12 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { validate } from 'jsonschema';
import {
Transaction,
FetchAllOptions,
GasValues,
} from './transaction/TransactionController';
import { MessageParams } from './message-manager/MessageManager';
import { PersonalMessageParams } from './message-manager/PersonalMessageManager';
Expand Down Expand Up @@ -688,6 +689,17 @@ export const getIncreasedPriceFromExisting = (
return getIncreasedPriceHex(convertPriceToDecimal(value), rate);
};

export const checkGasValues = (gasValues: GasValues) => {
const keys = Object.keys(gasValues);
keys.forEach((key) => {
const val = (gasValues as any)[key];
if (typeof val !== 'string' || !isHexString(val)) {
Copy link
Contributor Author

@rickycodes rickycodes Jul 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically this typeof check is redundant since you can't set these values to anything other than string. That said, I don't think it hurts to have an additional check.

throw new Error(`expected hex string for ${key} but received: ${val}`);
}
});
return true;
rickycodes marked this conversation as resolved.
Show resolved Hide resolved
};

export default {
BNToHex,
fractionBN,
Expand Down