@subwallet/extension-base 1.1.40-0 → 1.1.41-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/background/KoniTypes.d.ts +3 -1
- package/cjs/constants/index.js +1 -1
- package/cjs/constants/staking.js +1 -1
- package/cjs/koni/api/dotsama/transfer.js +1 -1
- package/cjs/koni/api/nft/bit.country/index.js +2 -0
- package/cjs/koni/api/nft/config.js +3 -1
- package/cjs/koni/api/tokens/evm/transfer.js +4 -4
- package/cjs/koni/background/handlers/Extension.js +32 -17
- package/cjs/koni/background/handlers/State.js +41 -9
- package/cjs/packageInfo.js +1 -1
- package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +58 -0
- package/cjs/services/chain-service/constants.js +11 -7
- package/cjs/services/chain-service/handler/chain-spec/goldberg.js +75 -8
- package/cjs/services/chain-service/index.js +71 -59
- package/cjs/services/chain-service/utils.js +11 -6
- package/cjs/services/earning-service/constants/chains.js +2 -2
- package/cjs/services/earning-service/handlers/liquid-staking/acala.js +9 -3
- package/cjs/services/earning-service/handlers/liquid-staking/bifrost.js +8 -1
- package/cjs/services/earning-service/handlers/liquid-staking/parallel.js +6 -1
- package/cjs/services/earning-service/handlers/liquid-staking/stella-swap.js +25 -17
- package/cjs/services/earning-service/handlers/native-staking/amplitude.js +4 -0
- package/cjs/services/earning-service/handlers/native-staking/astar.js +6 -2
- package/cjs/services/earning-service/handlers/native-staking/para-chain.js +5 -0
- package/cjs/services/earning-service/handlers/native-staking/relay-chain.js +14 -16
- package/cjs/services/earning-service/handlers/nomination-pool/index.js +9 -11
- package/cjs/services/earning-service/handlers/special.js +2 -0
- package/cjs/services/earning-service/service.js +51 -18
- package/cjs/services/fee-service/service.js +70 -0
- package/cjs/services/fee-service/utils/index.js +209 -0
- package/cjs/services/migration-service/scripts/DeleteChainStaking.js +23 -0
- package/cjs/services/migration-service/scripts/index.js +3 -1
- package/cjs/services/transaction-service/index.js +37 -37
- package/cjs/types/fee/index.js +1 -0
- package/cjs/types/index.js +11 -0
- package/cjs/utils/eth.js +2 -105
- package/cjs/utils/index.js +12 -0
- package/cjs/utils/reportError.js +31 -0
- package/constants/index.d.ts +1 -1
- package/constants/index.js +1 -1
- package/constants/staking.js +1 -1
- package/koni/api/dotsama/transfer.js +1 -1
- package/koni/api/nft/bit.country/index.js +3 -1
- package/koni/api/nft/config.d.ts +1 -0
- package/koni/api/nft/config.js +1 -0
- package/koni/api/tokens/evm/transfer.js +1 -1
- package/koni/background/handlers/Extension.d.ts +1 -0
- package/koni/background/handlers/Extension.js +18 -3
- package/koni/background/handlers/State.d.ts +4 -1
- package/koni/background/handlers/State.js +32 -1
- package/package.json +32 -7
- package/packageInfo.js +1 -1
- package/services/balance-service/helpers/subscribe/substrate/index.js +59 -1
- package/services/chain-service/constants.d.ts +2 -0
- package/services/chain-service/constants.js +11 -7
- package/services/chain-service/handler/chain-spec/goldberg.d.ts +70 -1
- package/services/chain-service/handler/chain-spec/goldberg.js +75 -8
- package/services/chain-service/index.d.ts +7 -3
- package/services/chain-service/index.js +62 -51
- package/services/chain-service/types.d.ts +4 -0
- package/services/chain-service/utils.d.ts +1 -0
- package/services/chain-service/utils.js +10 -6
- package/services/earning-service/constants/chains.js +2 -2
- package/services/earning-service/handlers/liquid-staking/acala.js +9 -3
- package/services/earning-service/handlers/liquid-staking/bifrost.js +8 -1
- package/services/earning-service/handlers/liquid-staking/parallel.d.ts +1 -1
- package/services/earning-service/handlers/liquid-staking/parallel.js +6 -1
- package/services/earning-service/handlers/liquid-staking/stella-swap.js +25 -17
- package/services/earning-service/handlers/native-staking/amplitude.js +4 -0
- package/services/earning-service/handlers/native-staking/astar.js +6 -2
- package/services/earning-service/handlers/native-staking/para-chain.js +5 -0
- package/services/earning-service/handlers/native-staking/relay-chain.js +15 -17
- package/services/earning-service/handlers/nomination-pool/index.js +10 -12
- package/services/earning-service/handlers/special.d.ts +1 -0
- package/services/earning-service/handlers/special.js +2 -0
- package/services/earning-service/service.d.ts +4 -1
- package/services/earning-service/service.js +52 -19
- package/services/fee-service/service.d.ts +12 -0
- package/services/fee-service/service.js +63 -0
- package/services/fee-service/utils/index.d.ts +8 -0
- package/services/fee-service/utils/index.js +193 -0
- package/services/migration-service/scripts/DeleteChainStaking.d.ts +4 -0
- package/services/migration-service/scripts/DeleteChainStaking.js +15 -0
- package/services/migration-service/scripts/index.js +3 -1
- package/services/transaction-service/index.js +2 -2
- package/types/fee/index.d.ts +49 -0
- package/types/fee/index.js +1 -0
- package/types/index.d.ts +1 -0
- package/types/index.js +1 -0
- package/types/yield/info/account/unstake.d.ts +2 -0
- package/types/yield/info/pallet.d.ts +4 -0
- package/utils/eth.d.ts +0 -15
- package/utils/eth.js +0 -101
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/reportError.d.ts +1 -0
- package/utils/reportError.js +23 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Copyright 2019-2022 @subwallet/extension-base
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { _isChainEvmCompatible } from '@subwallet/extension-base/services/chain-service/utils';
|
|
5
|
+
import { calculateGasFeeParams } from '@subwallet/extension-base/services/fee-service/utils';
|
|
6
|
+
import { BehaviorSubject } from 'rxjs';
|
|
7
|
+
export default class FeeService {
|
|
8
|
+
evmFeeSubject = new BehaviorSubject({});
|
|
9
|
+
constructor(state) {
|
|
10
|
+
this.state = state;
|
|
11
|
+
this.useInfura = true;
|
|
12
|
+
}
|
|
13
|
+
changeMode(useInfura) {
|
|
14
|
+
this.useInfura = useInfura;
|
|
15
|
+
}
|
|
16
|
+
async updateFees() {
|
|
17
|
+
await this.state.eventService.waitChainReady;
|
|
18
|
+
const activeNetworks = this.state.activeNetworks;
|
|
19
|
+
const chains = Object.values(activeNetworks).filter(chainInfo => _isChainEvmCompatible(chainInfo)).map(chainInfo => chainInfo.slug);
|
|
20
|
+
const promises = [];
|
|
21
|
+
for (const chain of chains) {
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises,no-async-promise-executor
|
|
23
|
+
const promise = new Promise(async resolve => {
|
|
24
|
+
const api = this.state.getEvmApi(chain);
|
|
25
|
+
const result = await calculateGasFeeParams(api, chain, this.useInfura);
|
|
26
|
+
this.updateChainFee(chain, result);
|
|
27
|
+
resolve();
|
|
28
|
+
});
|
|
29
|
+
promises.push(promise);
|
|
30
|
+
}
|
|
31
|
+
await Promise.all(promises);
|
|
32
|
+
}
|
|
33
|
+
updateChainFee(chain, info) {
|
|
34
|
+
const rs = Object.assign({}, this.evmFeeSubject.getValue());
|
|
35
|
+
rs[chain] = info;
|
|
36
|
+
this.evmFeeSubject.next(rs);
|
|
37
|
+
}
|
|
38
|
+
subscribeFees(callback) {
|
|
39
|
+
let cancel = false;
|
|
40
|
+
|
|
41
|
+
// eslint-disable-next-line prefer-const
|
|
42
|
+
|
|
43
|
+
const fetchData = () => {
|
|
44
|
+
this.updateFees().finally(() => {
|
|
45
|
+
if (!cancel) {
|
|
46
|
+
callback(this.evmFeeSubject.getValue());
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
fetchData();
|
|
51
|
+
const interval = setInterval(() => {
|
|
52
|
+
if (cancel) {
|
|
53
|
+
clearInterval(interval);
|
|
54
|
+
} else {
|
|
55
|
+
fetchData();
|
|
56
|
+
}
|
|
57
|
+
}, 30 * 1000);
|
|
58
|
+
return () => {
|
|
59
|
+
cancel = true;
|
|
60
|
+
clearInterval(interval);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { _EvmApi } from '@subwallet/extension-base/services/chain-service/types';
|
|
2
|
+
import { EvmFeeInfo, InfuraFeeInfo } from '@subwallet/extension-base/types';
|
|
3
|
+
export declare const parseInfuraFee: (info: InfuraFeeInfo) => EvmFeeInfo;
|
|
4
|
+
export declare const fetchInfuraFeeData: (chainId: number, infuraAuth?: string) => Promise<EvmFeeInfo | null>;
|
|
5
|
+
export declare const fetchSubWalletFeeData: (chainId: number, networkKey: string) => Promise<EvmFeeInfo | null>;
|
|
6
|
+
export declare const fetchOnlineFeeData: (chainId: number, networkKey: string, useInfura?: boolean) => Promise<EvmFeeInfo | null>;
|
|
7
|
+
export declare const recalculateGasPrice: (_price: string, chain: string) => string;
|
|
8
|
+
export declare const calculateGasFeeParams: (web3: _EvmApi, networkKey: string, useOnline?: boolean, useInfura?: boolean) => Promise<EvmFeeInfo>;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// Copyright 2019-2022 @subwallet/extension-base
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { GAS_PRICE_RATIO, NETWORK_MULTI_GAS_FEE } from '@subwallet/extension-base/constants';
|
|
5
|
+
import { BN_WEI, BN_ZERO } from '@subwallet/extension-base/utils';
|
|
6
|
+
import BigN from 'bignumber.js';
|
|
7
|
+
const INFURA_API_KEY = process.env.INFURA_API_KEY || '';
|
|
8
|
+
const INFURA_API_KEY_SECRET = process.env.INFURA_API_KEY_SECRET || '';
|
|
9
|
+
const INFURA_AUTH = 'Basic ' + Buffer.from(INFURA_API_KEY + ':' + INFURA_API_KEY_SECRET).toString('base64');
|
|
10
|
+
export const parseInfuraFee = info => {
|
|
11
|
+
const base = new BigN(info.estimatedBaseFee).multipliedBy(BN_WEI);
|
|
12
|
+
const low = new BigN(info.low.suggestedMaxPriorityFeePerGas).multipliedBy(BN_WEI);
|
|
13
|
+
const busyNetwork = base.gt(BN_ZERO) ? low.dividedBy(base).gte(0.3) : false;
|
|
14
|
+
const data = !busyNetwork ? info.low : info.medium;
|
|
15
|
+
return {
|
|
16
|
+
busyNetwork,
|
|
17
|
+
gasPrice: undefined,
|
|
18
|
+
baseGasFee: base,
|
|
19
|
+
maxFeePerGas: new BigN(data.suggestedMaxFeePerGas).multipliedBy(BN_WEI).integerValue(BigN.ROUND_UP),
|
|
20
|
+
maxPriorityFeePerGas: new BigN(data.suggestedMaxPriorityFeePerGas).multipliedBy(BN_WEI).integerValue(BigN.ROUND_UP)
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
export const fetchInfuraFeeData = async (chainId, infuraAuth) => {
|
|
24
|
+
return await new Promise(resolve => {
|
|
25
|
+
const baseUrl = 'https://gas.api.infura.io/networks/{{chainId}}/suggestedGasFees';
|
|
26
|
+
const url = baseUrl.replaceAll('{{chainId}}', chainId.toString());
|
|
27
|
+
fetch(url, {
|
|
28
|
+
method: 'GET',
|
|
29
|
+
headers: {
|
|
30
|
+
Authorization: infuraAuth || INFURA_AUTH
|
|
31
|
+
}
|
|
32
|
+
}).then(rs => {
|
|
33
|
+
return rs.json();
|
|
34
|
+
}).then(info => {
|
|
35
|
+
resolve(parseInfuraFee(info));
|
|
36
|
+
}).catch(e => {
|
|
37
|
+
console.warn(e);
|
|
38
|
+
resolve(null);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
export const fetchSubWalletFeeData = async (chainId, networkKey) => {
|
|
43
|
+
return await new Promise(resolve => {
|
|
44
|
+
const baseUrl = 'https://api-cache.subwallet.app/sw-evm-gas/{{chain}}';
|
|
45
|
+
const url = baseUrl.replaceAll('{{chain}}', networkKey);
|
|
46
|
+
fetch(url, {
|
|
47
|
+
method: 'GET'
|
|
48
|
+
}).then(rs => {
|
|
49
|
+
return rs.json();
|
|
50
|
+
}).then(info => {
|
|
51
|
+
if (info.gasPrice !== undefined) {
|
|
52
|
+
resolve(info);
|
|
53
|
+
} else {
|
|
54
|
+
resolve({
|
|
55
|
+
busyNetwork: info.busyNetwork,
|
|
56
|
+
gasPrice: info.gasPrice,
|
|
57
|
+
baseGasFee: new BigN(info.baseGasFee),
|
|
58
|
+
maxFeePerGas: new BigN(info.maxFeePerGas),
|
|
59
|
+
maxPriorityFeePerGas: new BigN(info.maxPriorityFeePerGas)
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}).catch(e => {
|
|
63
|
+
console.warn(e);
|
|
64
|
+
resolve(null);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
export const fetchOnlineFeeData = async (chainId, networkKey, useInfura = false) => {
|
|
69
|
+
return await new Promise(resolve => {
|
|
70
|
+
const fetchFunction = useInfura ? fetchInfuraFeeData : fetchSubWalletFeeData;
|
|
71
|
+
fetchFunction(chainId, networkKey).then(info => {
|
|
72
|
+
resolve(info);
|
|
73
|
+
}).catch(e => {
|
|
74
|
+
console.warn(e);
|
|
75
|
+
resolve(null);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
export const recalculateGasPrice = (_price, chain) => {
|
|
80
|
+
const needMulti = NETWORK_MULTI_GAS_FEE.includes(chain) || NETWORK_MULTI_GAS_FEE.includes('*');
|
|
81
|
+
return needMulti ? new BigN(_price).multipliedBy(GAS_PRICE_RATIO).toFixed(0) : _price;
|
|
82
|
+
};
|
|
83
|
+
export const calculateGasFeeParams = async (web3, networkKey, useOnline = true, useInfura = false) => {
|
|
84
|
+
if (useOnline) {
|
|
85
|
+
try {
|
|
86
|
+
const chainId = await web3.api.eth.getChainId();
|
|
87
|
+
const onlineData = await fetchOnlineFeeData(chainId, networkKey, useInfura);
|
|
88
|
+
if (onlineData) {
|
|
89
|
+
return onlineData;
|
|
90
|
+
}
|
|
91
|
+
} catch (e) {}
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const numBlock = 20;
|
|
95
|
+
const rewardPercent = [];
|
|
96
|
+
for (let i = 0; i <= 100; i = i + 5) {
|
|
97
|
+
rewardPercent.push(i);
|
|
98
|
+
}
|
|
99
|
+
const history = await web3.api.eth.getFeeHistory(numBlock, 'latest', rewardPercent);
|
|
100
|
+
const baseGasFee = new BigN(history.baseFeePerGas[history.baseFeePerGas.length - 1]); // Last element is latest
|
|
101
|
+
|
|
102
|
+
const blocksBusy = history.reward.reduce((previous, rewards, currentIndex) => {
|
|
103
|
+
const [firstPriority] = rewards;
|
|
104
|
+
const base = history.baseFeePerGas[currentIndex];
|
|
105
|
+
const priorityBN = new BigN(firstPriority);
|
|
106
|
+
const baseBN = new BigN(base);
|
|
107
|
+
|
|
108
|
+
/*
|
|
109
|
+
* True if priority >= 0.3 * base
|
|
110
|
+
* */
|
|
111
|
+
const blockIsBusy = baseBN.gt(BN_ZERO) ? priorityBN.dividedBy(baseBN).gte(0.3) ? 1 : 0 : 0; // Special for bsc, base fee = 0
|
|
112
|
+
|
|
113
|
+
return previous + blockIsBusy;
|
|
114
|
+
}, 0);
|
|
115
|
+
const busyNetwork = blocksBusy >= numBlock / 2; // True, if half of block is busy
|
|
116
|
+
|
|
117
|
+
const maxPriorityFeePerGas = history.reward.reduce((previous, rewards) => {
|
|
118
|
+
let firstBN = BN_ZERO;
|
|
119
|
+
let firstIndex = 0;
|
|
120
|
+
|
|
121
|
+
/* Get first priority which greater than 0 */
|
|
122
|
+
for (let i = 0; i < rewards.length; i++) {
|
|
123
|
+
firstIndex = i;
|
|
124
|
+
const current = rewards[i];
|
|
125
|
+
const currentBN = new BigN(current);
|
|
126
|
+
if (currentBN.gt(BN_ZERO)) {
|
|
127
|
+
firstBN = currentBN;
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
let secondBN = firstBN;
|
|
132
|
+
|
|
133
|
+
/* Get second priority which greater than first priority */
|
|
134
|
+
for (let i = firstIndex; i < rewards.length; i++) {
|
|
135
|
+
const current = rewards[i];
|
|
136
|
+
const currentBN = new BigN(current);
|
|
137
|
+
if (currentBN.gt(firstBN)) {
|
|
138
|
+
secondBN = currentBN;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
let current;
|
|
143
|
+
if (busyNetwork) {
|
|
144
|
+
current = secondBN.dividedBy(2).gte(firstBN) ? firstBN : secondBN; // second too larger than first (> 2 times), use first else use second
|
|
145
|
+
} else {
|
|
146
|
+
current = firstBN;
|
|
147
|
+
}
|
|
148
|
+
if (busyNetwork) {
|
|
149
|
+
/* Get max value */
|
|
150
|
+
return current.gte(previous) ? current : previous; // get max priority
|
|
151
|
+
} else {
|
|
152
|
+
/* Get min value which greater than 0 */
|
|
153
|
+
if (previous.eq(BN_ZERO)) {
|
|
154
|
+
return current; // get min priority
|
|
155
|
+
} else if (current.eq(BN_ZERO)) {
|
|
156
|
+
return previous;
|
|
157
|
+
}
|
|
158
|
+
return current.lte(previous) ? current : previous; // get min priority
|
|
159
|
+
}
|
|
160
|
+
}, BN_ZERO);
|
|
161
|
+
if (maxPriorityFeePerGas.eq(BN_ZERO)) {
|
|
162
|
+
const _price = await web3.api.eth.getGasPrice();
|
|
163
|
+
const gasPrice = recalculateGasPrice(_price, networkKey);
|
|
164
|
+
return {
|
|
165
|
+
gasPrice,
|
|
166
|
+
maxFeePerGas: undefined,
|
|
167
|
+
maxPriorityFeePerGas: undefined,
|
|
168
|
+
baseGasFee: undefined,
|
|
169
|
+
busyNetwork: false
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/* Max gas = (base + priority) * 1.5 (if not busy or 2 when busy); */
|
|
174
|
+
const maxFeePerGas = baseGasFee.plus(maxPriorityFeePerGas).multipliedBy(busyNetwork ? 2 : 1.5).decimalPlaces(0);
|
|
175
|
+
return {
|
|
176
|
+
gasPrice: undefined,
|
|
177
|
+
maxFeePerGas,
|
|
178
|
+
maxPriorityFeePerGas,
|
|
179
|
+
baseGasFee,
|
|
180
|
+
busyNetwork
|
|
181
|
+
};
|
|
182
|
+
} catch (e) {
|
|
183
|
+
const _price = await web3.api.eth.getGasPrice();
|
|
184
|
+
const gasPrice = recalculateGasPrice(_price, networkKey);
|
|
185
|
+
return {
|
|
186
|
+
gasPrice,
|
|
187
|
+
maxFeePerGas: undefined,
|
|
188
|
+
maxPriorityFeePerGas: undefined,
|
|
189
|
+
baseGasFee: undefined,
|
|
190
|
+
busyNetwork: false
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Copyright 2019-2022 @subwallet/extension-koni authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import BaseMigrationJob from '@subwallet/extension-base/services/migration-service/Base';
|
|
5
|
+
export default class DeleteChainStaking extends BaseMigrationJob {
|
|
6
|
+
async run() {
|
|
7
|
+
try {
|
|
8
|
+
await this.state.dbService.stores.yieldPoolInfo.table.where({
|
|
9
|
+
slug: 'CAPS___native_staking___ternoa_alphanet'
|
|
10
|
+
}).delete();
|
|
11
|
+
} catch (e) {
|
|
12
|
+
console.error(e);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -9,6 +9,7 @@ import MigrateProvidersV1M1P24 from "./providers/MigrateProvidersV1M1P24.js";
|
|
|
9
9
|
import MigratePolygonUSDCProvider from "./tokens/MigratePolygonUSDCProvider.js";
|
|
10
10
|
import AutoEnableChainsTokens from "./AutoEnableChainsTokens.js";
|
|
11
11
|
import DeleteChain from "./DeleteChain.js";
|
|
12
|
+
import DeleteChainStaking from "./DeleteChainStaking.js";
|
|
12
13
|
import EnableVaraChain from "./EnableVaraChain.js";
|
|
13
14
|
import MigrateAuthUrls from "./MigrateAuthUrls.js";
|
|
14
15
|
import MigrateAutoLock from "./MigrateAutoLock.js";
|
|
@@ -41,6 +42,7 @@ export default {
|
|
|
41
42
|
'1.1.17-03': EnableVaraChain,
|
|
42
43
|
'1.1.24-01': MigrateProvidersV1M1P24,
|
|
43
44
|
'1.1.26-01': MigratePolygonUSDCProvider,
|
|
44
|
-
'1.1.28-01': MigrateEarningVersion
|
|
45
|
+
'1.1.28-01': MigrateEarningVersion,
|
|
46
|
+
'1.1.41-01': DeleteChainStaking
|
|
45
47
|
// [`${EVERYTIME}-1`]: AutoEnableChainsTokens
|
|
46
48
|
};
|
|
@@ -7,6 +7,7 @@ import { BasicTxErrorType, BasicTxWarningCode, ChainType, EvmProviderErrorType,
|
|
|
7
7
|
import { TransactionWarning } from '@subwallet/extension-base/background/warnings/TransactionWarning';
|
|
8
8
|
import { ALL_ACCOUNT_KEY } from '@subwallet/extension-base/constants';
|
|
9
9
|
import { _getAssetDecimals, _getAssetSymbol, _getChainNativeTokenBasicInfo, _getEvmChainId, _isChainEvmCompatible } from '@subwallet/extension-base/services/chain-service/utils';
|
|
10
|
+
import { calculateGasFeeParams } from '@subwallet/extension-base/services/fee-service/utils';
|
|
10
11
|
import { EXTENSION_REQUEST_URL } from '@subwallet/extension-base/services/request-service/constants';
|
|
11
12
|
import { TRANSACTION_TIMEOUT } from '@subwallet/extension-base/services/transaction-service/constants';
|
|
12
13
|
import { parseLiquidStakingEvents, parseLiquidStakingFastUnstakeEvents, parseTransferEventLogs, parseXcmEventLogs } from '@subwallet/extension-base/services/transaction-service/event-parser';
|
|
@@ -14,8 +15,7 @@ import { getBaseTransactionInfo, getTransactionId, isSubstrateTransaction } from
|
|
|
14
15
|
import { getExplorerLink, parseTransactionData } from '@subwallet/extension-base/services/transaction-service/utils';
|
|
15
16
|
import { isWalletConnectRequest } from '@subwallet/extension-base/services/wallet-connect-service/helpers';
|
|
16
17
|
import { YieldPoolType } from '@subwallet/extension-base/types';
|
|
17
|
-
import { reformatAddress } from '@subwallet/extension-base/utils';
|
|
18
|
-
import { anyNumberToBN, calculateGasFeeParams } from '@subwallet/extension-base/utils/eth';
|
|
18
|
+
import { anyNumberToBN, reformatAddress } from '@subwallet/extension-base/utils';
|
|
19
19
|
import { mergeTransactionAndSignature } from '@subwallet/extension-base/utils/eth/mergeTransactionAndSignature';
|
|
20
20
|
import { isContractAddress, parseContractInput } from '@subwallet/extension-base/utils/eth/parseTransaction';
|
|
21
21
|
import { BN_ZERO } from '@subwallet/extension-base/utils/number';
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import BigN from 'bignumber.js';
|
|
2
|
+
interface BaseFeeInfo {
|
|
3
|
+
busyNetwork: boolean;
|
|
4
|
+
}
|
|
5
|
+
export interface EvmLegacyFeeInfo extends BaseFeeInfo {
|
|
6
|
+
gasPrice: string;
|
|
7
|
+
maxFeePerGas: undefined;
|
|
8
|
+
maxPriorityFeePerGas: undefined;
|
|
9
|
+
baseGasFee: undefined;
|
|
10
|
+
}
|
|
11
|
+
export interface EvmEIP1995FeeInfo extends BaseFeeInfo {
|
|
12
|
+
gasPrice: undefined;
|
|
13
|
+
maxFeePerGas: BigN;
|
|
14
|
+
maxPriorityFeePerGas: BigN;
|
|
15
|
+
baseGasFee: BigN;
|
|
16
|
+
}
|
|
17
|
+
export declare type EvmFeeInfo = EvmLegacyFeeInfo | EvmEIP1995FeeInfo;
|
|
18
|
+
export interface EvmLegacyFeeInfoCache extends BaseFeeInfo {
|
|
19
|
+
gasPrice: string;
|
|
20
|
+
maxFeePerGas: undefined;
|
|
21
|
+
maxPriorityFeePerGas: undefined;
|
|
22
|
+
baseGasFee: undefined;
|
|
23
|
+
}
|
|
24
|
+
export interface EvmEIP1995FeeInfoCache extends BaseFeeInfo {
|
|
25
|
+
gasPrice: undefined;
|
|
26
|
+
maxFeePerGas: string;
|
|
27
|
+
maxPriorityFeePerGas: string;
|
|
28
|
+
baseGasFee: string;
|
|
29
|
+
}
|
|
30
|
+
export declare type EvmFeeInfoCache = EvmLegacyFeeInfoCache | EvmEIP1995FeeInfoCache;
|
|
31
|
+
export interface InfuraFeeDetail {
|
|
32
|
+
suggestedMaxPriorityFeePerGas: string;
|
|
33
|
+
suggestedMaxFeePerGas: string;
|
|
34
|
+
minWaitTimeEstimate: number;
|
|
35
|
+
maxWaitTimeEstimate: number;
|
|
36
|
+
}
|
|
37
|
+
export interface InfuraFeeInfo {
|
|
38
|
+
low: InfuraFeeDetail;
|
|
39
|
+
medium: InfuraFeeDetail;
|
|
40
|
+
high: InfuraFeeDetail;
|
|
41
|
+
networkCongestion: number;
|
|
42
|
+
estimatedBaseFee: string;
|
|
43
|
+
latestPriorityFeeRange: [string, string];
|
|
44
|
+
historicalPriorityFeeRange: [string, string];
|
|
45
|
+
historicalBaseFeeRange: [string, string];
|
|
46
|
+
priorityFeeTrend: 'down' | 'up';
|
|
47
|
+
baseFeeTrend: 'down' | 'up';
|
|
48
|
+
}
|
|
49
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/types/index.d.ts
CHANGED
package/types/index.js
CHANGED
|
@@ -136,6 +136,10 @@ export interface PalletStakingStakingLedger {
|
|
|
136
136
|
unlocking: UnlockingChunk[];
|
|
137
137
|
claimedRewards: number[];
|
|
138
138
|
}
|
|
139
|
+
export interface PalletStakingActiveEraInfo {
|
|
140
|
+
index: string;
|
|
141
|
+
start: string;
|
|
142
|
+
}
|
|
139
143
|
export interface RuntimeDispatchInfo {
|
|
140
144
|
weight: {
|
|
141
145
|
refTime: number;
|
package/utils/eth.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { _EvmApi } from '@subwallet/extension-base/services/chain-service/types';
|
|
2
1
|
import BigN from 'bignumber.js';
|
|
3
2
|
import BNEther from 'bn.js';
|
|
4
3
|
import { SignedTransaction } from 'web3-core';
|
|
@@ -16,17 +15,3 @@ export declare class Transaction {
|
|
|
16
15
|
export declare const anyNumberToBN: (value?: string | number | BNEther) => BigN;
|
|
17
16
|
export declare const createTransactionFromRLP: (rlp: string) => Transaction | null;
|
|
18
17
|
export declare const signatureToHex: (sig: SignedTransaction) => string;
|
|
19
|
-
export declare const recalculateGasPrice: (_price: string, chain: string) => string;
|
|
20
|
-
export declare const calculateGasFeeParams: (web3: _EvmApi, networkKey: string) => Promise<{
|
|
21
|
-
gasPrice: string;
|
|
22
|
-
busyNetwork: boolean;
|
|
23
|
-
maxFeePerGas?: undefined;
|
|
24
|
-
maxPriorityFeePerGas?: undefined;
|
|
25
|
-
baseGasFee?: undefined;
|
|
26
|
-
} | {
|
|
27
|
-
maxFeePerGas: BigN;
|
|
28
|
-
maxPriorityFeePerGas: BigN;
|
|
29
|
-
baseGasFee: BigN;
|
|
30
|
-
busyNetwork: boolean;
|
|
31
|
-
gasPrice?: undefined;
|
|
32
|
-
}>;
|
package/utils/eth.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
// Copyright 2019-2022 @subwallet/extension-base
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import { GAS_PRICE_RATIO, NETWORK_MULTI_GAS_FEE } from '@subwallet/extension-base/constants';
|
|
5
4
|
import BigN from 'bignumber.js';
|
|
6
5
|
import { ethers } from 'ethers';
|
|
7
6
|
import { hexStripPrefix, numberToHex } from '@polkadot/util';
|
|
8
|
-
import { BN_ZERO } from "./number.js";
|
|
9
7
|
const hexToNumberString = s => {
|
|
10
8
|
const temp = parseInt(s, 16);
|
|
11
9
|
if (isNaN(temp)) {
|
|
@@ -60,103 +58,4 @@ export const signatureToHex = sig => {
|
|
|
60
58
|
const hexS = s.length % 2 === 1 ? `0${s}` : s;
|
|
61
59
|
const hexV = hexStripPrefix(numberToHex(v));
|
|
62
60
|
return hexR + hexS + hexV;
|
|
63
|
-
};
|
|
64
|
-
export const recalculateGasPrice = (_price, chain) => {
|
|
65
|
-
const needMulti = NETWORK_MULTI_GAS_FEE.includes(chain) || NETWORK_MULTI_GAS_FEE.includes('*');
|
|
66
|
-
return needMulti ? new BigN(_price).multipliedBy(GAS_PRICE_RATIO).toFixed(0) : _price;
|
|
67
|
-
};
|
|
68
|
-
export const calculateGasFeeParams = async (web3, networkKey) => {
|
|
69
|
-
try {
|
|
70
|
-
const numBlock = 20;
|
|
71
|
-
const rewardPercent = [];
|
|
72
|
-
for (let i = 0; i <= 100; i = i + 5) {
|
|
73
|
-
rewardPercent.push(i);
|
|
74
|
-
}
|
|
75
|
-
const history = await web3.api.eth.getFeeHistory(numBlock, 'latest', rewardPercent);
|
|
76
|
-
const baseGasFee = new BigN(history.baseFeePerGas[history.baseFeePerGas.length - 1]); // Last element is latest
|
|
77
|
-
|
|
78
|
-
const blocksBusy = history.reward.reduce((previous, rewards, currentIndex) => {
|
|
79
|
-
const [firstPriority] = rewards;
|
|
80
|
-
const base = history.baseFeePerGas[currentIndex];
|
|
81
|
-
const priorityBN = new BigN(firstPriority);
|
|
82
|
-
const baseBN = new BigN(base);
|
|
83
|
-
|
|
84
|
-
/*
|
|
85
|
-
* True if priority >= 0.3 * base
|
|
86
|
-
* */
|
|
87
|
-
const blockIsBusy = baseBN.gt(BN_ZERO) ? priorityBN.dividedBy(baseBN).gte(0.3) ? 1 : 0 : 0; // Special for bsc, base fee = 0
|
|
88
|
-
|
|
89
|
-
return previous + blockIsBusy;
|
|
90
|
-
}, 0);
|
|
91
|
-
const busyNetwork = blocksBusy >= numBlock / 2; // True, if half of block is busy
|
|
92
|
-
|
|
93
|
-
const maxPriorityFeePerGas = history.reward.reduce((previous, rewards) => {
|
|
94
|
-
let firstBN = BN_ZERO;
|
|
95
|
-
let firstIndex = 0;
|
|
96
|
-
|
|
97
|
-
/* Get first priority which greater than 0 */
|
|
98
|
-
for (let i = 0; i < rewards.length; i++) {
|
|
99
|
-
firstIndex = i;
|
|
100
|
-
const current = rewards[i];
|
|
101
|
-
const currentBN = new BigN(current);
|
|
102
|
-
if (currentBN.gt(BN_ZERO)) {
|
|
103
|
-
firstBN = currentBN;
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
let secondBN = firstBN;
|
|
108
|
-
|
|
109
|
-
/* Get second priority which greater than first priority */
|
|
110
|
-
for (let i = firstIndex; i < rewards.length; i++) {
|
|
111
|
-
const current = rewards[i];
|
|
112
|
-
const currentBN = new BigN(current);
|
|
113
|
-
if (currentBN.gt(firstBN)) {
|
|
114
|
-
secondBN = currentBN;
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
let current;
|
|
119
|
-
if (busyNetwork) {
|
|
120
|
-
current = secondBN.dividedBy(2).gte(firstBN) ? firstBN : secondBN; // second too larger than first (> 2 times), use first else use second
|
|
121
|
-
} else {
|
|
122
|
-
current = firstBN;
|
|
123
|
-
}
|
|
124
|
-
if (busyNetwork) {
|
|
125
|
-
/* Get max value */
|
|
126
|
-
return current.gte(previous) ? current : previous; // get max priority
|
|
127
|
-
} else {
|
|
128
|
-
/* Get min value which greater than 0 */
|
|
129
|
-
if (previous.eq(BN_ZERO)) {
|
|
130
|
-
return current; // get min priority
|
|
131
|
-
} else if (current.eq(BN_ZERO)) {
|
|
132
|
-
return previous;
|
|
133
|
-
}
|
|
134
|
-
return current.lte(previous) ? current : previous; // get min priority
|
|
135
|
-
}
|
|
136
|
-
}, BN_ZERO);
|
|
137
|
-
if (maxPriorityFeePerGas.eq(BN_ZERO)) {
|
|
138
|
-
const _price = await web3.api.eth.getGasPrice();
|
|
139
|
-
const gasPrice = recalculateGasPrice(_price, networkKey);
|
|
140
|
-
return {
|
|
141
|
-
gasPrice,
|
|
142
|
-
busyNetwork: false
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/* Max gas = (base + priority) * 1.5 (if not busy or 2 when busy); */
|
|
147
|
-
const maxFeePerGas = baseGasFee.plus(maxPriorityFeePerGas).multipliedBy(busyNetwork ? 2 : 1.5).decimalPlaces(0);
|
|
148
|
-
return {
|
|
149
|
-
maxFeePerGas,
|
|
150
|
-
maxPriorityFeePerGas,
|
|
151
|
-
baseGasFee,
|
|
152
|
-
busyNetwork
|
|
153
|
-
};
|
|
154
|
-
} catch (e) {
|
|
155
|
-
const _price = await web3.api.eth.getGasPrice();
|
|
156
|
-
const gasPrice = recalculateGasPrice(_price, networkKey);
|
|
157
|
-
return {
|
|
158
|
-
gasPrice,
|
|
159
|
-
busyNetwork: false
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
61
|
};
|
package/utils/index.d.ts
CHANGED
|
@@ -43,6 +43,7 @@ export declare function wait(milliseconds: number): Promise<void>;
|
|
|
43
43
|
export * from './account';
|
|
44
44
|
export * from './array';
|
|
45
45
|
export * from './environment';
|
|
46
|
+
export * from './eth';
|
|
46
47
|
export * from './number';
|
|
47
48
|
export * from './lazy';
|
|
48
49
|
export * from './promise';
|
package/utils/index.js
CHANGED
|
@@ -345,6 +345,7 @@ export function wait(milliseconds) {
|
|
|
345
345
|
export * from "./account.js";
|
|
346
346
|
export * from "./array.js";
|
|
347
347
|
export * from "./environment.js";
|
|
348
|
+
export * from "./eth.js";
|
|
348
349
|
export * from "./number.js";
|
|
349
350
|
export * from "./lazy.js";
|
|
350
351
|
export * from "./promise.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const reportError: (e: Error, pathName: string) => Promise<void>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { TARGET_ENV } from '@subwallet/extension-base/utils/environment';
|
|
5
|
+
import fetch from 'cross-fetch';
|
|
6
|
+
const EXTENSION_REPORT_ERROR_URL = 'https://extension-feedback.subwallet.app/gelf';
|
|
7
|
+
const WEBAPP_REPORT_ERROR_URL = 'https://web-feedback.subwallet.app/gelf';
|
|
8
|
+
export const reportError = async (e, pathName) => {
|
|
9
|
+
const errorContent = JSON.stringify(e.stack);
|
|
10
|
+
const errorShortMessage = e.message;
|
|
11
|
+
const body = {
|
|
12
|
+
content: errorContent,
|
|
13
|
+
short_message: errorShortMessage,
|
|
14
|
+
platform: TARGET_ENV,
|
|
15
|
+
device_version: '',
|
|
16
|
+
app_version: JSON.stringify(process.env.PKG_VERSION),
|
|
17
|
+
current_routes: pathName
|
|
18
|
+
};
|
|
19
|
+
await fetch(TARGET_ENV === 'extension' ? EXTENSION_REPORT_ERROR_URL : WEBAPP_REPORT_ERROR_URL, {
|
|
20
|
+
method: 'POST',
|
|
21
|
+
body: JSON.stringify(body)
|
|
22
|
+
});
|
|
23
|
+
};
|