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 7 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,
validateGasValues,
} 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) {
validateGasValues(gasValues);
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
}
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) {
validateGasValues(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
32 changes: 31 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,34 @@ describe('util', () => {
expect(util.isEIP1559Transaction(tx)).toBe(false);
});
});

describe('validateGasValues', () => {
const MAX_FEE_PER_GAS = 'maxFeePerGas';
const MAX_PRIORITY_FEE_PER_GAS = 'maxPriorityFeePerGas';
const GAS_PRICE = 'gasPrice';
const FAIL = 'lol';
const PASS = '0x1';
it('should throw when provided invalid gas values', () => {
const gasValues: GasValues = { [MAX_FEE_PER_GAS]: FAIL };
expect(() => util.validateGasValues(gasValues)).toThrow(TypeError);
expect(() => util.validateGasValues(gasValues)).toThrow(
`expected hex string for ${MAX_FEE_PER_GAS} but received: ${FAIL}`,
);
});
it('should throw when any provided gas values are invalid', () => {
const gasValues: GasValues = {
[MAX_PRIORITY_FEE_PER_GAS]: PASS,
[MAX_FEE_PER_GAS]: FAIL,
[GAS_PRICE]: PASS,
};
expect(() => util.validateGasValues(gasValues)).toThrow(TypeError);
expect(() => util.validateGasValues(gasValues)).toThrow(
`expected hex string for ${MAX_FEE_PER_GAS} but received: ${FAIL}`,
);
});
it('should return true when provided valid gas values', () => {
const gasValues: GasValues = { [MAX_FEE_PER_GAS]: PASS };
expect(util.validateGasValues(gasValues)).toBe(true);
});
});
});
13 changes: 13 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,18 @@ export const getIncreasedPriceFromExisting = (
return getIncreasedPriceHex(convertPriceToDecimal(value), rate);
};

export const validateGasValues = (gasValues: GasValues) => {
Object.keys(gasValues).forEach((key) => {
const value = (gasValues as any)[key];
if (typeof value !== 'string' || !isHexString(value)) {
throw new TypeError(
`expected hex string for ${key} but received: ${value}`,
);
}
});
return true;
rickycodes marked this conversation as resolved.
Show resolved Hide resolved
};

export default {
BNToHex,
fractionBN,
Expand Down