Skip to content

Commit 04a1279

Browse files
authored
Merge pull request #240 from openbitdev/koni/dev/issue-239
[Issue-239] Fix enforcing the minimum miner tip
2 parents ac905aa + 0e2f2c5 commit 04a1279

File tree

6 files changed

+27
-7
lines changed

6 files changed

+27
-7
lines changed

packages/extension-base/src/services/chain-service/handler/EvmApi.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class EvmApi implements _EvmApi {
1616
api: Web3;
1717
apiUrl: string;
1818
provider: HttpProvider | WebsocketProvider;
19+
isTestnet?: boolean;
1920
apiError?: string;
2021
apiRetry = 0;
2122
public readonly isApiConnectedSubject = new BehaviorSubject(false);
@@ -77,11 +78,12 @@ export class EvmApi implements _EvmApi {
7778
}
7879
}
7980

80-
constructor (chainSlug: string, apiUrl: string, { providerName }: _ApiOptions = {}) {
81+
constructor (chainSlug: string, apiUrl: string, { isTestnet, providerName }: _ApiOptions = {}) {
8182
this.chainSlug = chainSlug;
8283
this.apiUrl = apiUrl;
8384
this.providerName = providerName || 'unknown';
8485
this.provider = this.createProvider(apiUrl);
86+
this.isTestnet = isTestnet;
8587
this.api = new Web3(this.provider);
8688
this.isReadyHandler = createPromiseHandler<_EvmApi>();
8789

packages/extension-base/src/services/chain-service/handler/EvmChainHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class EvmChainHandler extends AbstractChainHandler {
4040
this.evmApiMap[chainSlug] = evmApi;
4141
}
4242

43-
public async initApi (chainSlug: string, apiUrl: string, { onUpdateStatus, providerName }: Omit<_ApiOptions, 'metadata'> = {}) {
43+
public async initApi (chainSlug: string, apiUrl: string, { isTestnet, onUpdateStatus, providerName }: Omit<_ApiOptions, 'metadata'> = {}) {
4444
const existed = this.getEvmApiByChain(chainSlug);
4545

4646
if (existed) {
@@ -53,7 +53,7 @@ export class EvmChainHandler extends AbstractChainHandler {
5353
return existed;
5454
}
5555

56-
const apiObject = new EvmApi(chainSlug, apiUrl, { providerName });
56+
const apiObject = new EvmApi(chainSlug, apiUrl, { isTestnet, providerName });
5757

5858
apiObject.connectionStatusSubject.subscribe(this.handleConnection.bind(this, chainSlug));
5959
apiObject.connectionStatusSubject.subscribe(onUpdateStatus);

packages/extension-base/src/services/chain-service/handler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface _SubstrateChainSpec {
2626

2727
export interface _ApiOptions {
2828
providerName?: string,
29+
isTestnet?: boolean,
2930
metadata?: MetadataItem,
3031
onUpdateStatus?: (status: _ChainConnectionStatus) => void,
3132
externalApiPromise?: ApiPromise

packages/extension-base/src/services/chain-service/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,8 @@ export class ChainService {
969969
}
970970

971971
if (chainInfo.evmInfo !== null && chainInfo.evmInfo !== undefined) {
972-
const chainApi = await this.evmChainHandler.initApi(chainInfo.slug, endpoint, { providerName, onUpdateStatus });
972+
const isTestnet = chainInfo.isTestnet;
973+
const chainApi = await this.evmChainHandler.initApi(chainInfo.slug, endpoint, { isTestnet, providerName, onUpdateStatus });
973974

974975
this.evmChainHandler.setEvmApi(chainInfo.slug, chainApi);
975976
}

packages/extension-base/src/services/chain-service/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export interface _ChainBaseApi {
5050
chainSlug: string;
5151
apiUrl: string;
5252
providerName?: string;
53+
isTestnet?: boolean;
5354

5455
apiError?: string;
5556
apiRetry?: number;

packages/extension-base/src/services/fee-service/utils/index.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import BigN from 'bignumber.js';
1010
const INFURA_API_KEY = process.env.INFURA_API_KEY || '';
1111
const INFURA_API_KEY_SECRET = process.env.INFURA_API_KEY_SECRET || '';
1212
const INFURA_AUTH = 'Basic ' + Buffer.from(INFURA_API_KEY + ':' + INFURA_API_KEY_SECRET).toString('base64');
13+
const EIP1559_MIN_PRIORITY_FEE = '1';
1314

1415
export const parseInfuraFee = (info: InfuraFeeInfo, threshold: InfuraThresholdInfo): EvmFeeInfo => {
1516
const base = new BigN(info.estimatedBaseFee).multipliedBy(BN_WEI);
@@ -167,15 +168,19 @@ export const calculateGasFeeParams = async (web3: _EvmApi, networkKey: string, u
167168
const averagePriorityFee = history.reward.reduce((previous, rewards) => previous.plus(rewards[1]), BN_ZERO).dividedBy(numBlock).decimalPlaces(0);
168169
const fastPriorityFee = history.reward.reduce((previous, rewards) => previous.plus(rewards[2]), BN_ZERO).dividedBy(numBlock).decimalPlaces(0);
169170

171+
const slowFee = enforceMinOneTip(getEIP1559GasFee(baseGasFee, slowPriorityFee, 30000), web3.isTestnet);
172+
const averageFee = enforceMinOneTip(getEIP1559GasFee(baseGasFee, averagePriorityFee, 45000), web3.isTestnet);
173+
const fastFee = enforceMinOneTip(getEIP1559GasFee(baseGasFee, fastPriorityFee, 60000), web3.isTestnet);
174+
170175
return {
171176
type: 'evm',
172177
gasPrice: undefined,
173178
baseGasFee: baseGasFee.toString(),
174179
busyNetwork,
175180
options: {
176-
slow: getEIP1559GasFee(baseGasFee, slowPriorityFee, 30000),
177-
average: getEIP1559GasFee(baseGasFee, averagePriorityFee, 45000),
178-
fast: getEIP1559GasFee(baseGasFee, fastPriorityFee, 60000),
181+
slow: slowFee,
182+
average: averageFee,
183+
fast: fastFee,
179184
default: busyNetwork ? 'average' : 'slow'
180185
}
181186
};
@@ -192,3 +197,13 @@ export const calculateGasFeeParams = async (web3: _EvmApi, networkKey: string, u
192197
};
193198
}
194199
};
200+
201+
const enforceMinOneTip = (feeOptionDetail: EvmEIP1995FeeOptionDetail, isTestnet?: boolean): EvmEIP1995FeeOptionDetail => {
202+
if (isTestnet && feeOptionDetail.maxPriorityFeePerGas === '0') {
203+
feeOptionDetail.maxPriorityFeePerGas = EIP1559_MIN_PRIORITY_FEE;
204+
205+
return feeOptionDetail;
206+
}
207+
208+
return feeOptionDetail;
209+
};

0 commit comments

Comments
 (0)