@toruslabs/ethereum-controllers 5.5.2 → 5.5.4
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/dist/ethereumControllers.cjs.js +22 -21
- package/dist/ethereumControllers.esm.js +22 -20
- package/dist/ethereumControllers.umd.min.js +1 -2
- package/dist/ethereumControllers.umd.min.js.LICENSE.txt +0 -2
- package/dist/types/Account/AccountTrackerController.d.ts +1 -1
- package/dist/types/utils/interfaces.d.ts +3 -0
- package/package.json +14 -15
- package/dist/ethereumControllers.cjs.js.map +0 -1
- package/dist/ethereumControllers.esm.js.map +0 -1
- package/dist/ethereumControllers.umd.min.js.map +0 -1
- package/src/Account/AccountTrackerController.ts +0 -172
- package/src/Block/PollingBlockTracker.ts +0 -89
- package/src/Currency/CurrencyController.ts +0 -117
- package/src/Gas/GasFeeController.ts +0 -261
- package/src/Gas/IGasFeeController.ts +0 -56
- package/src/Gas/gasUtil.ts +0 -163
- package/src/Keyring/KeyringController.ts +0 -117
- package/src/Message/AbstractMessageController.ts +0 -136
- package/src/Message/AddChainController.ts +0 -73
- package/src/Message/DecryptMessageController.ts +0 -76
- package/src/Message/EncryptionPublicKeyController.ts +0 -75
- package/src/Message/MessageController.ts +0 -74
- package/src/Message/PersonalMessageController.ts +0 -74
- package/src/Message/SwitchChainController.ts +0 -74
- package/src/Message/TypedMessageController.ts +0 -109
- package/src/Message/utils.ts +0 -155
- package/src/Network/NetworkController.ts +0 -184
- package/src/Network/createEthereumMiddleware.ts +0 -475
- package/src/Network/createJsonRpcClient.ts +0 -63
- package/src/Nfts/INftsController.ts +0 -13
- package/src/Nfts/NftHandler.ts +0 -191
- package/src/Nfts/NftsController.ts +0 -216
- package/src/Preferences/PreferencesController.ts +0 -473
- package/src/Tokens/ITokensController.ts +0 -13
- package/src/Tokens/TokenHandler.ts +0 -60
- package/src/Tokens/TokenRatesController.ts +0 -134
- package/src/Tokens/TokensController.ts +0 -273
- package/src/Transaction/NonceTracker.ts +0 -152
- package/src/Transaction/PendingTransactionTracker.ts +0 -235
- package/src/Transaction/TransactionController.ts +0 -558
- package/src/Transaction/TransactionGasUtil.ts +0 -74
- package/src/Transaction/TransactionStateHistoryHelper.ts +0 -41
- package/src/Transaction/TransactionStateManager.ts +0 -315
- package/src/Transaction/TransactionUtils.ts +0 -333
- package/src/index.ts +0 -49
- package/src/utils/abis.ts +0 -677
- package/src/utils/constants.ts +0 -438
- package/src/utils/contractAddresses.ts +0 -19
- package/src/utils/conversionUtils.ts +0 -269
- package/src/utils/helpers.ts +0 -234
- package/src/utils/interfaces.ts +0 -516
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
|
-
// @ts-nocheck
|
|
3
|
-
/* Currency Conversion Utility
|
|
4
|
-
* This utility function can be used for converting currency related values within metamask.
|
|
5
|
-
* The caller should be able to pass it a value, along with information about the value's
|
|
6
|
-
* numeric base, denomination and currency, and the desired numeric base, denomination and
|
|
7
|
-
* currency. It should return a single value.
|
|
8
|
-
*
|
|
9
|
-
* @param {(number | string | BN)} value - The value to convert.
|
|
10
|
-
* @param {Object} [options] - Options to specify details of the conversion
|
|
11
|
-
* @param {string} [options.fromCurrency = 'ETH' | 'USD'] - The currency of the passed value
|
|
12
|
-
* @param {string} [options.toCurrency = 'ETH' | 'USD'] - The desired currency of the result
|
|
13
|
-
* @param {string} [options.fromNumericBase = 'hex' | 'dec' | 'BN'] - The numeric basic of the passed value.
|
|
14
|
-
* @param {string} [options.toNumericBase = 'hex' | 'dec' | 'BN'] - The desired numeric basic of the result.
|
|
15
|
-
* @param {string} [options.fromDenomination = 'WEI'] - The denomination of the passed value
|
|
16
|
-
* @param {string} [options.numberOfDecimals] - The desired number of decimals in the result
|
|
17
|
-
* @param {string} [options.roundDown] - The desired number of decimals to round down to
|
|
18
|
-
* @param {number} [options.conversionRate] - The rate to use to make the fromCurrency -> toCurrency conversion
|
|
19
|
-
* @returns {(number | string | BN)}
|
|
20
|
-
*
|
|
21
|
-
* The utility passes value along with the options as a single object to the `converter` function.
|
|
22
|
-
* `converter` conditional modifies the supplied `value` property, depending
|
|
23
|
-
* on the accompanying options.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
import { stripHexPrefix } from "@ethereumjs/util";
|
|
27
|
-
import BigNumber from "bignumber.js";
|
|
28
|
-
import BN from "bn.js";
|
|
29
|
-
|
|
30
|
-
// Big Number Constants
|
|
31
|
-
const BIG_NUMBER_WEI_MULTIPLIER = new BigNumber("1000000000000000000");
|
|
32
|
-
const BIG_NUMBER_GWEI_MULTIPLIER = new BigNumber("1000000000");
|
|
33
|
-
const BIG_NUMBER_ETH_MULTIPLIER = new BigNumber("1");
|
|
34
|
-
|
|
35
|
-
type NumericBase = "hex" | "dec" | "BN";
|
|
36
|
-
|
|
37
|
-
type EthDenomination = "WEI" | "GWEI" | "ETH";
|
|
38
|
-
|
|
39
|
-
type ConverterOptions = {
|
|
40
|
-
value: BigNumber | string;
|
|
41
|
-
fromNumericBase: NumericBase;
|
|
42
|
-
fromDenomination: EthDenomination;
|
|
43
|
-
fromCurrency: string;
|
|
44
|
-
toNumericBase: NumericBase;
|
|
45
|
-
toDenomination: EthDenomination;
|
|
46
|
-
toCurrency: string;
|
|
47
|
-
numberOfDecimals: number;
|
|
48
|
-
conversionRate: number;
|
|
49
|
-
invertConversionRate: boolean;
|
|
50
|
-
roundDown?: number;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// Setter Maps
|
|
54
|
-
const toBigNumber = {
|
|
55
|
-
hex: (n) => new BigNumber(stripHexPrefix(n), 16),
|
|
56
|
-
dec: (n) => new BigNumber(String(n), 10),
|
|
57
|
-
BN: (n) => new BigNumber(n.toString(16), 16),
|
|
58
|
-
};
|
|
59
|
-
const toNormalizedDenomination = {
|
|
60
|
-
WEI: (bigNumber) => bigNumber.div(BIG_NUMBER_WEI_MULTIPLIER),
|
|
61
|
-
GWEI: (bigNumber) => bigNumber.div(BIG_NUMBER_GWEI_MULTIPLIER),
|
|
62
|
-
ETH: (bigNumber) => bigNumber.div(BIG_NUMBER_ETH_MULTIPLIER),
|
|
63
|
-
};
|
|
64
|
-
const toSpecifiedDenomination = {
|
|
65
|
-
WEI: (bigNumber) => bigNumber.times(BIG_NUMBER_WEI_MULTIPLIER).dp(0, BigNumber.ROUND_HALF_UP),
|
|
66
|
-
GWEI: (bigNumber) => bigNumber.times(BIG_NUMBER_GWEI_MULTIPLIER).dp(9, BigNumber.ROUND_HALF_UP),
|
|
67
|
-
ETH: (bigNumber) => bigNumber.times(BIG_NUMBER_ETH_MULTIPLIER).dp(9, BigNumber.ROUND_HALF_UP),
|
|
68
|
-
};
|
|
69
|
-
const baseChange = {
|
|
70
|
-
hex: (n) => n.toString(16),
|
|
71
|
-
dec: (n) => new BigNumber(n).toString(10),
|
|
72
|
-
BN: (n) => new BN(n.toString(16)),
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
// Utility function for checking base types
|
|
76
|
-
const isValidBase = (base) => Number.isInteger(base) && base > 1;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Utility method to convert a value between denominations, formats and currencies.
|
|
80
|
-
*/
|
|
81
|
-
const converter = ({
|
|
82
|
-
value,
|
|
83
|
-
fromNumericBase,
|
|
84
|
-
fromDenomination,
|
|
85
|
-
fromCurrency,
|
|
86
|
-
toNumericBase,
|
|
87
|
-
toDenomination,
|
|
88
|
-
toCurrency,
|
|
89
|
-
numberOfDecimals,
|
|
90
|
-
conversionRate,
|
|
91
|
-
invertConversionRate,
|
|
92
|
-
roundDown,
|
|
93
|
-
}: ConverterOptions) => {
|
|
94
|
-
let convertedValue = fromNumericBase ? toBigNumber[fromNumericBase](value) : value;
|
|
95
|
-
|
|
96
|
-
if (fromDenomination) {
|
|
97
|
-
convertedValue = toNormalizedDenomination[fromDenomination](convertedValue);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (fromCurrency !== toCurrency) {
|
|
101
|
-
if (conversionRate === null || conversionRate === undefined) {
|
|
102
|
-
throw new Error(`Converting from ${fromCurrency} to ${toCurrency} requires a conversionRate, but one was not provided`);
|
|
103
|
-
}
|
|
104
|
-
let rate = toBigNumber.dec(conversionRate);
|
|
105
|
-
if (invertConversionRate) {
|
|
106
|
-
rate = new BigNumber(1).div(conversionRate);
|
|
107
|
-
}
|
|
108
|
-
convertedValue = convertedValue.times(rate);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (toDenomination) {
|
|
112
|
-
convertedValue = toSpecifiedDenomination[toDenomination](convertedValue);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (numberOfDecimals) {
|
|
116
|
-
convertedValue = convertedValue.dp(numberOfDecimals, BigNumber.ROUND_HALF_DOWN);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (roundDown) {
|
|
120
|
-
convertedValue = convertedValue.dp(roundDown, BigNumber.ROUND_DOWN);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (toNumericBase) {
|
|
124
|
-
convertedValue = baseChange[toNumericBase](convertedValue);
|
|
125
|
-
}
|
|
126
|
-
return convertedValue;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
export const conversionUtil = (
|
|
130
|
-
value: BigNumber | string,
|
|
131
|
-
{
|
|
132
|
-
fromCurrency = null,
|
|
133
|
-
toCurrency = fromCurrency,
|
|
134
|
-
fromNumericBase,
|
|
135
|
-
toNumericBase,
|
|
136
|
-
fromDenomination,
|
|
137
|
-
toDenomination,
|
|
138
|
-
numberOfDecimals,
|
|
139
|
-
conversionRate,
|
|
140
|
-
invertConversionRate,
|
|
141
|
-
}: Omit<ConverterOptions, "value">
|
|
142
|
-
) => {
|
|
143
|
-
if (fromCurrency !== toCurrency && !conversionRate) {
|
|
144
|
-
return 0;
|
|
145
|
-
}
|
|
146
|
-
return converter({
|
|
147
|
-
fromCurrency,
|
|
148
|
-
toCurrency,
|
|
149
|
-
fromNumericBase,
|
|
150
|
-
toNumericBase,
|
|
151
|
-
fromDenomination,
|
|
152
|
-
toDenomination,
|
|
153
|
-
numberOfDecimals,
|
|
154
|
-
conversionRate,
|
|
155
|
-
invertConversionRate,
|
|
156
|
-
value,
|
|
157
|
-
});
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
export const getBigNumber = (value, base) => {
|
|
161
|
-
if (!isValidBase(base)) {
|
|
162
|
-
throw new Error("Must specificy valid base");
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// We don't include 'number' here, because BigNumber will throw if passed
|
|
166
|
-
// a number primitive it considers unsafe.
|
|
167
|
-
if (typeof value === "string" || value instanceof BigNumber) {
|
|
168
|
-
return new BigNumber(value, base);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return new BigNumber(String(value), base);
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
export const addCurrencies = (a, b, options: Record<string, unknown> = {}) => {
|
|
175
|
-
const { aBase, bBase, ...conversionOptions } = options;
|
|
176
|
-
|
|
177
|
-
if (!isValidBase(aBase) || !isValidBase(bBase)) {
|
|
178
|
-
throw new Error("Must specify valid aBase and bBase");
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const value = getBigNumber(a, aBase).plus(getBigNumber(b, bBase));
|
|
182
|
-
|
|
183
|
-
return converter({
|
|
184
|
-
value,
|
|
185
|
-
...conversionOptions,
|
|
186
|
-
} as ConverterOptions);
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
export const subtractCurrencies = (a, b, options: Record<string, unknown> = {}) => {
|
|
190
|
-
const { aBase, bBase, ...conversionOptions } = options;
|
|
191
|
-
|
|
192
|
-
if (!isValidBase(aBase) || !isValidBase(bBase)) {
|
|
193
|
-
throw new Error("Must specify valid aBase and bBase");
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const value = getBigNumber(a, aBase).minus(getBigNumber(b, bBase));
|
|
197
|
-
|
|
198
|
-
return converter({
|
|
199
|
-
value,
|
|
200
|
-
...conversionOptions,
|
|
201
|
-
} as ConverterOptions);
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
export const multiplyCurrencies = (a, b, options: Record<string, unknown> = {}) => {
|
|
205
|
-
const { multiplicandBase, multiplierBase, ...conversionOptions } = options;
|
|
206
|
-
|
|
207
|
-
if (!isValidBase(multiplicandBase) || !isValidBase(multiplierBase)) {
|
|
208
|
-
throw new Error("Must specify valid multiplicandBase and multiplierBase");
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const value = getBigNumber(a, multiplicandBase).times(getBigNumber(b, multiplierBase));
|
|
212
|
-
|
|
213
|
-
return converter({
|
|
214
|
-
value,
|
|
215
|
-
...conversionOptions,
|
|
216
|
-
} as ConverterOptions);
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
export const conversionGreaterThan = ({ ...firstProps }: ConverterOptions, { ...secondProps }: ConverterOptions) => {
|
|
220
|
-
const firstValue = converter({ ...firstProps });
|
|
221
|
-
const secondValue = converter({ ...secondProps });
|
|
222
|
-
|
|
223
|
-
return firstValue.gt(secondValue);
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
export const conversionLessThan = ({ ...firstProps }: ConverterOptions, { ...secondProps }: ConverterOptions) => {
|
|
227
|
-
const firstValue = converter({ ...firstProps });
|
|
228
|
-
const secondValue = converter({ ...secondProps });
|
|
229
|
-
|
|
230
|
-
return firstValue.lt(secondValue);
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
export const conversionMax = ({ ...firstProps }, { ...secondProps }) => {
|
|
234
|
-
const firstIsGreater = conversionGreaterThan({ ...firstProps }, { ...secondProps });
|
|
235
|
-
|
|
236
|
-
return firstIsGreater ? firstProps.value : secondProps.value;
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
export const conversionGTE = ({ ...firstProps }, { ...secondProps }) => {
|
|
240
|
-
const firstValue = converter({ ...firstProps } as ConverterOptions);
|
|
241
|
-
const secondValue = converter({ ...secondProps } as ConverterOptions);
|
|
242
|
-
return firstValue.isGreaterThanOrEqualTo(secondValue);
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
export const conversionLTE = ({ ...firstProps }, { ...secondProps }) => {
|
|
246
|
-
const firstValue = converter({ ...firstProps } as ConverterOptions);
|
|
247
|
-
const secondValue = converter({ ...secondProps } as ConverterOptions);
|
|
248
|
-
return firstValue.isLessThanOrEqualTo(secondValue);
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
export const toNegative = (n, options = {}) => multiplyCurrencies(n, -1, options);
|
|
252
|
-
|
|
253
|
-
export const decGWEIToHexWEI = (decGWEI: BigNumber): BigNumber => {
|
|
254
|
-
return conversionUtil(decGWEI, {
|
|
255
|
-
fromNumericBase: "dec",
|
|
256
|
-
toNumericBase: "hex",
|
|
257
|
-
fromDenomination: "GWEI",
|
|
258
|
-
toDenomination: "WEI",
|
|
259
|
-
} as ConverterOptions);
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
export const hexWEIToDecGWEI = (decGWEI: BigNumber | string): BigNumber => {
|
|
263
|
-
return conversionUtil(decGWEI, {
|
|
264
|
-
fromNumericBase: "hex",
|
|
265
|
-
toNumericBase: "dec",
|
|
266
|
-
fromDenomination: "WEI",
|
|
267
|
-
toDenomination: "GWEI",
|
|
268
|
-
} as ConverterOptions);
|
|
269
|
-
};
|
package/src/utils/helpers.ts
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { addHexPrefix, isValidAddress, toChecksumAddress } from "@ethereumjs/util";
|
|
2
|
-
import {
|
|
3
|
-
ACTIVITY_ACTION_RECEIVE,
|
|
4
|
-
ACTIVITY_ACTION_SEND,
|
|
5
|
-
addressSlicer,
|
|
6
|
-
formatSmallNumbers,
|
|
7
|
-
significantDigits,
|
|
8
|
-
TransactionStatus,
|
|
9
|
-
} from "@toruslabs/base-controllers";
|
|
10
|
-
import { SafeEventEmitterProvider } from "@toruslabs/openlogin-jrpc";
|
|
11
|
-
import BigNumber from "bignumber.js";
|
|
12
|
-
import { formatEther } from "ethers";
|
|
13
|
-
import log from "loglevel";
|
|
14
|
-
|
|
15
|
-
import { determineTransactionType } from "../Transaction/TransactionUtils";
|
|
16
|
-
import {
|
|
17
|
-
CONTRACT_TYPE_ERC20,
|
|
18
|
-
CONTRACT_TYPE_ERC721,
|
|
19
|
-
CONTRACT_TYPE_ERC1155,
|
|
20
|
-
CONTRACT_TYPE_ETH,
|
|
21
|
-
MAINNET_CHAIN_ID,
|
|
22
|
-
METHOD_TYPES,
|
|
23
|
-
SUPPORTED_NETWORKS,
|
|
24
|
-
TEST_CHAINS,
|
|
25
|
-
} from "./constants";
|
|
26
|
-
import { EtherscanTransaction, FormattedTransactionActivity, TransactionParams, TransactionPayload, TransactionReceipt } from "./interfaces";
|
|
27
|
-
|
|
28
|
-
export function getEtherScanHashLink(txHash: string, chainId: string) {
|
|
29
|
-
if (!SUPPORTED_NETWORKS[chainId]) return "";
|
|
30
|
-
return `${SUPPORTED_NETWORKS[chainId].blockExplorerUrl}/tx/${txHash}`;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const formatPastTx = (x: TransactionPayload, lowerCaseSelectedAddress?: string): FormattedTransactionActivity => {
|
|
34
|
-
let totalAmountString = "";
|
|
35
|
-
if (x.type === CONTRACT_TYPE_ERC721 || x.type === CONTRACT_TYPE_ERC1155) totalAmountString = x.symbol;
|
|
36
|
-
else if (x.type === CONTRACT_TYPE_ERC20) totalAmountString = formatSmallNumbers(Number.parseFloat(x.total_amount), x.symbol, true);
|
|
37
|
-
else totalAmountString = formatSmallNumbers(Number.parseFloat(x.total_amount), x.type_name, true);
|
|
38
|
-
const currencyAmountString =
|
|
39
|
-
x.type === CONTRACT_TYPE_ERC721 || x.type === CONTRACT_TYPE_ERC1155 || x.isEtherscan
|
|
40
|
-
? ""
|
|
41
|
-
: formatSmallNumbers(Number.parseFloat(x.currency_amount), x.selected_currency, true);
|
|
42
|
-
const finalObject: FormattedTransactionActivity = {
|
|
43
|
-
id: x.created_at.toString(),
|
|
44
|
-
date: new Date(x.created_at).toString(),
|
|
45
|
-
from: x.from,
|
|
46
|
-
from_aa_address: x.from_aa_address,
|
|
47
|
-
slicedFrom: typeof x.from === "string" ? addressSlicer(x.from) : "",
|
|
48
|
-
to: x.to,
|
|
49
|
-
slicedTo: typeof x.to === "string" ? addressSlicer(x.to) : "",
|
|
50
|
-
action: lowerCaseSelectedAddress === x.to?.toLowerCase() || "" ? ACTIVITY_ACTION_RECEIVE : ACTIVITY_ACTION_SEND,
|
|
51
|
-
totalAmount: x.total_amount,
|
|
52
|
-
totalAmountString,
|
|
53
|
-
currencyAmount: x.currency_amount,
|
|
54
|
-
currencyAmountString,
|
|
55
|
-
amount: `${totalAmountString} / ${currencyAmountString}`,
|
|
56
|
-
status: x.status,
|
|
57
|
-
etherscanLink: getEtherScanHashLink(x.transaction_hash, x.chain_id || MAINNET_CHAIN_ID),
|
|
58
|
-
chainId: x.chain_id,
|
|
59
|
-
ethRate:
|
|
60
|
-
Number.parseFloat(x?.total_amount) && Number.parseFloat(x?.currency_amount)
|
|
61
|
-
? `1 ${x.symbol} = ${significantDigits(Number.parseFloat(x.currency_amount) / Number.parseFloat(x.total_amount))}`
|
|
62
|
-
: "",
|
|
63
|
-
currencyUsed: x.selected_currency,
|
|
64
|
-
type: x.type,
|
|
65
|
-
type_name: x.type_name,
|
|
66
|
-
type_image_link: x.type_image_link,
|
|
67
|
-
transaction_hash: x.transaction_hash,
|
|
68
|
-
transaction_category: x.transaction_category,
|
|
69
|
-
isEtherscan: x.isEtherscan,
|
|
70
|
-
input: x.input || "",
|
|
71
|
-
token_id: x.token_id || "",
|
|
72
|
-
contract_address: x.contract_address || "",
|
|
73
|
-
nonce: x.nonce || "",
|
|
74
|
-
is_cancel: !!x.is_cancel || false,
|
|
75
|
-
gas: x.gas || "",
|
|
76
|
-
gasPrice: x.gasPrice || "",
|
|
77
|
-
};
|
|
78
|
-
return finalObject;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Ref - https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_gettransactionreceipt
|
|
83
|
-
*/
|
|
84
|
-
export const getEthTxStatus = async (hash: string, provider: SafeEventEmitterProvider): Promise<TransactionStatus | undefined> => {
|
|
85
|
-
try {
|
|
86
|
-
const result = await provider.request<[string], TransactionReceipt>({ method: METHOD_TYPES.ETH_GET_TRANSACTION_RECEIPT, params: [hash] });
|
|
87
|
-
if (result === null) return TransactionStatus.submitted;
|
|
88
|
-
if (result && result.status === "0x1") return TransactionStatus.confirmed;
|
|
89
|
-
if (result && result.status === "0x0") return TransactionStatus.rejected;
|
|
90
|
-
return undefined;
|
|
91
|
-
} catch (err) {
|
|
92
|
-
log.warn("unable to fetch transaction status", err);
|
|
93
|
-
return undefined;
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
export function formatDate(inputDate: string) {
|
|
98
|
-
const monthList = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
99
|
-
const date = new Date(inputDate);
|
|
100
|
-
const day = date.getDate();
|
|
101
|
-
const month = monthList[date.getMonth()];
|
|
102
|
-
const year = date.getFullYear();
|
|
103
|
-
return `${day} ${month} ${year}`;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function formatTime(time: number) {
|
|
107
|
-
return new Date(time).toTimeString().slice(0, 8);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export const idleTimeTracker = ((activityThresholdTime: number): { checkIfIdle: () => boolean } => {
|
|
111
|
-
let isIdle = false;
|
|
112
|
-
let idleTimeout: number = null;
|
|
113
|
-
|
|
114
|
-
const resetTimer = () => {
|
|
115
|
-
if (idleTimeout) {
|
|
116
|
-
window.clearTimeout(idleTimeout);
|
|
117
|
-
}
|
|
118
|
-
isIdle = false;
|
|
119
|
-
idleTimeout = window.setTimeout(() => {
|
|
120
|
-
isIdle = true;
|
|
121
|
-
}, activityThresholdTime * 1000);
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
125
|
-
window.addEventListener("load", resetTimer);
|
|
126
|
-
document.addEventListener("mousemove", resetTimer);
|
|
127
|
-
document.addEventListener("keydown", resetTimer);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function checkIfIdle() {
|
|
131
|
-
return isIdle;
|
|
132
|
-
}
|
|
133
|
-
return {
|
|
134
|
-
checkIfIdle,
|
|
135
|
-
};
|
|
136
|
-
})(60 * 3);
|
|
137
|
-
|
|
138
|
-
export function isAddressByChainId(address: string, _chainId: string) {
|
|
139
|
-
// TOOD: add rsk network checks.
|
|
140
|
-
return isValidAddress(address);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export function toChecksumAddressByChainId(address: string, chainId: string) {
|
|
144
|
-
// TOOD: add rsk network checks.
|
|
145
|
-
if (!isAddressByChainId(address, chainId)) return address;
|
|
146
|
-
return toChecksumAddress(address);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export const GAS_LIMITS = {
|
|
150
|
-
// maximum gasLimit of a simple send
|
|
151
|
-
SIMPLE: addHexPrefix((21_000).toString(16)),
|
|
152
|
-
// a base estimate for token transfers.
|
|
153
|
-
BASE_TOKEN_ESTIMATE: addHexPrefix((100_000).toString(16)),
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export function bnLessThan(a: string | number, b: string | number) {
|
|
157
|
-
if (a === null || a === undefined || b === null || b === undefined) {
|
|
158
|
-
return null;
|
|
159
|
-
}
|
|
160
|
-
return new BigNumber(a, 10).lt(b, 10);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export const getIpfsEndpoint = (path: string) => `https://infura-ipfs.io/${path}`;
|
|
164
|
-
|
|
165
|
-
export function sanitizeNftMetdataUrl(url: string): string {
|
|
166
|
-
let finalUri = url;
|
|
167
|
-
if (url?.startsWith("ipfs")) {
|
|
168
|
-
const ipfsPath = url.split("ipfs://")[1];
|
|
169
|
-
finalUri = getIpfsEndpoint(ipfsPath);
|
|
170
|
-
}
|
|
171
|
-
return finalUri;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export function getChainType(chainId: string) {
|
|
175
|
-
if (chainId === MAINNET_CHAIN_ID) {
|
|
176
|
-
return "mainnet";
|
|
177
|
-
} else if ((TEST_CHAINS as string[]).includes(chainId)) {
|
|
178
|
-
return "testnet";
|
|
179
|
-
}
|
|
180
|
-
return "custom";
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
export const addEtherscanTransactions = async (
|
|
184
|
-
txn: EtherscanTransaction[],
|
|
185
|
-
lowerCaseSelectedAddress: string,
|
|
186
|
-
provider: SafeEventEmitterProvider,
|
|
187
|
-
chainId: string
|
|
188
|
-
) => {
|
|
189
|
-
const transactionPromises = await Promise.all(
|
|
190
|
-
txn.map(async (tx: EtherscanTransaction) => {
|
|
191
|
-
const { category, type } = await determineTransactionType({ ...tx, data: tx.input } as TransactionParams, provider);
|
|
192
|
-
tx.transaction_category = tx.transaction_category || category;
|
|
193
|
-
tx.type_image_link = SUPPORTED_NETWORKS[chainId]?.logo || "";
|
|
194
|
-
tx.type_name = SUPPORTED_NETWORKS[chainId]?.ticker;
|
|
195
|
-
tx.type = type;
|
|
196
|
-
return tx;
|
|
197
|
-
})
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
const finalTxs = transactionPromises.reduce((accumulator, x) => {
|
|
201
|
-
const totalAmountString = x.value ? formatEther(x.value) : "";
|
|
202
|
-
|
|
203
|
-
const etherscanTransaction: TransactionPayload = {
|
|
204
|
-
etherscanLink: getEtherScanHashLink(x.hash, chainId),
|
|
205
|
-
type: x.type || SUPPORTED_NETWORKS[chainId]?.ticker || CONTRACT_TYPE_ETH,
|
|
206
|
-
type_image_link: x.type_image_link || "n/a",
|
|
207
|
-
type_name: x.type_name || "n/a",
|
|
208
|
-
symbol: SUPPORTED_NETWORKS[chainId]?.ticker,
|
|
209
|
-
token_id: x.tokenID || "",
|
|
210
|
-
total_amount: totalAmountString,
|
|
211
|
-
created_at: new Date(Number(x.timeStamp) * 1000),
|
|
212
|
-
from: x.from,
|
|
213
|
-
to: x.to,
|
|
214
|
-
transaction_hash: x.hash,
|
|
215
|
-
status: x.txreceipt_status && x.txreceipt_status === "0" ? TransactionStatus.failed : TransactionStatus.confirmed,
|
|
216
|
-
isEtherscan: true,
|
|
217
|
-
input: x.input,
|
|
218
|
-
contract_address: x.contractAddress,
|
|
219
|
-
transaction_category: x.transaction_category,
|
|
220
|
-
gas: x.gas,
|
|
221
|
-
gasPrice: x.gasPrice,
|
|
222
|
-
chain_id: chainId,
|
|
223
|
-
currency_amount: "",
|
|
224
|
-
nonce: x.nonce,
|
|
225
|
-
from_aa_address: "",
|
|
226
|
-
is_cancel: false,
|
|
227
|
-
selected_currency: "",
|
|
228
|
-
};
|
|
229
|
-
accumulator.push(formatPastTx(etherscanTransaction, lowerCaseSelectedAddress));
|
|
230
|
-
return accumulator;
|
|
231
|
-
}, []);
|
|
232
|
-
|
|
233
|
-
return finalTxs;
|
|
234
|
-
};
|