@metamask/transaction-controller 41.1.0 → 42.1.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/CHANGELOG.md +40 -1
- package/dist/TransactionController.cjs +57 -51
- package/dist/TransactionController.cjs.map +1 -1
- package/dist/TransactionController.d.cts +72 -13
- package/dist/TransactionController.d.cts.map +1 -1
- package/dist/TransactionController.d.mts +72 -13
- package/dist/TransactionController.d.mts.map +1 -1
- package/dist/TransactionController.mjs +57 -51
- package/dist/TransactionController.mjs.map +1 -1
- package/dist/api/accounts-api.cjs +48 -3
- package/dist/api/accounts-api.cjs.map +1 -1
- package/dist/api/accounts-api.d.cts +49 -0
- package/dist/api/accounts-api.d.cts.map +1 -1
- package/dist/api/accounts-api.d.mts +49 -0
- package/dist/api/accounts-api.d.mts.map +1 -1
- package/dist/api/accounts-api.mjs +46 -2
- package/dist/api/accounts-api.mjs.map +1 -1
- package/dist/constants.cjs +1 -85
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +0 -84
- package/dist/constants.d.cts.map +1 -1
- package/dist/constants.d.mts +0 -84
- package/dist/constants.d.mts.map +1 -1
- package/dist/constants.mjs +0 -84
- package/dist/constants.mjs.map +1 -1
- package/dist/helpers/AccountsApiRemoteTransactionSource.cjs +183 -0
- package/dist/helpers/AccountsApiRemoteTransactionSource.cjs.map +1 -0
- package/dist/helpers/AccountsApiRemoteTransactionSource.d.cts +12 -0
- package/dist/helpers/AccountsApiRemoteTransactionSource.d.cts.map +1 -0
- package/dist/helpers/AccountsApiRemoteTransactionSource.d.mts +12 -0
- package/dist/helpers/AccountsApiRemoteTransactionSource.d.mts.map +1 -0
- package/dist/helpers/AccountsApiRemoteTransactionSource.mjs +183 -0
- package/dist/helpers/AccountsApiRemoteTransactionSource.mjs.map +1 -0
- package/dist/helpers/IncomingTransactionHelper.cjs +90 -142
- package/dist/helpers/IncomingTransactionHelper.cjs.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.d.cts +10 -16
- package/dist/helpers/IncomingTransactionHelper.d.cts.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.d.mts +10 -16
- package/dist/helpers/IncomingTransactionHelper.d.mts.map +1 -1
- package/dist/helpers/IncomingTransactionHelper.mjs +91 -143
- package/dist/helpers/IncomingTransactionHelper.mjs.map +1 -1
- package/dist/helpers/MultichainTrackingHelper.cjs +23 -70
- package/dist/helpers/MultichainTrackingHelper.cjs.map +1 -1
- package/dist/helpers/MultichainTrackingHelper.d.cts +1 -14
- package/dist/helpers/MultichainTrackingHelper.d.cts.map +1 -1
- package/dist/helpers/MultichainTrackingHelper.d.mts +1 -14
- package/dist/helpers/MultichainTrackingHelper.d.mts.map +1 -1
- package/dist/helpers/MultichainTrackingHelper.mjs +23 -70
- package/dist/helpers/MultichainTrackingHelper.mjs.map +1 -1
- package/dist/index.cjs +3 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +21 -14
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +21 -14
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/validation.cjs +6 -0
- package/dist/utils/validation.cjs.map +1 -1
- package/dist/utils/validation.d.cts.map +1 -1
- package/dist/utils/validation.d.mts.map +1 -1
- package/dist/utils/validation.mjs +6 -0
- package/dist/utils/validation.mjs.map +1 -1
- package/package.json +8 -8
- package/dist/helpers/EtherscanRemoteTransactionSource.cjs +0 -158
- package/dist/helpers/EtherscanRemoteTransactionSource.cjs.map +0 -1
- package/dist/helpers/EtherscanRemoteTransactionSource.d.cts +0 -16
- package/dist/helpers/EtherscanRemoteTransactionSource.d.cts.map +0 -1
- package/dist/helpers/EtherscanRemoteTransactionSource.d.mts +0 -16
- package/dist/helpers/EtherscanRemoteTransactionSource.d.mts.map +0 -1
- package/dist/helpers/EtherscanRemoteTransactionSource.mjs +0 -158
- package/dist/helpers/EtherscanRemoteTransactionSource.mjs.map +0 -1
- package/dist/utils/etherscan.cjs +0 -116
- package/dist/utils/etherscan.cjs.map +0 -1
- package/dist/utils/etherscan.d.cts +0 -74
- package/dist/utils/etherscan.d.cts.map +0 -1
- package/dist/utils/etherscan.d.mts +0 -74
- package/dist/utils/etherscan.d.mts.map +0 -1
- package/dist/utils/etherscan.mjs +0 -110
- package/dist/utils/etherscan.mjs.map +0 -1
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import type { Hex } from "@metamask/utils";
|
|
2
|
-
export interface EtherscanTransactionMetaBase {
|
|
3
|
-
blockNumber: string;
|
|
4
|
-
blockHash: string;
|
|
5
|
-
confirmations: string;
|
|
6
|
-
contractAddress: string;
|
|
7
|
-
cumulativeGasUsed: string;
|
|
8
|
-
from: string;
|
|
9
|
-
gas: string;
|
|
10
|
-
gasPrice: string;
|
|
11
|
-
gasUsed: string;
|
|
12
|
-
hash: string;
|
|
13
|
-
nonce: string;
|
|
14
|
-
timeStamp: string;
|
|
15
|
-
to: string;
|
|
16
|
-
transactionIndex: string;
|
|
17
|
-
value: string;
|
|
18
|
-
}
|
|
19
|
-
export interface EtherscanTransactionMeta extends EtherscanTransactionMetaBase {
|
|
20
|
-
functionName: string;
|
|
21
|
-
input: string;
|
|
22
|
-
isError: string;
|
|
23
|
-
methodId: string;
|
|
24
|
-
txreceipt_status: string;
|
|
25
|
-
}
|
|
26
|
-
export interface EtherscanTokenTransactionMeta extends EtherscanTransactionMetaBase {
|
|
27
|
-
tokenDecimal: string;
|
|
28
|
-
tokenName: string;
|
|
29
|
-
tokenSymbol: string;
|
|
30
|
-
}
|
|
31
|
-
export interface EtherscanTransactionResponse<T extends EtherscanTransactionMetaBase> {
|
|
32
|
-
status: '0' | '1';
|
|
33
|
-
message?: string;
|
|
34
|
-
result: string | T[];
|
|
35
|
-
}
|
|
36
|
-
export interface EtherscanTransactionRequest {
|
|
37
|
-
address: string;
|
|
38
|
-
apiKey?: string;
|
|
39
|
-
chainId: Hex;
|
|
40
|
-
fromBlock?: number;
|
|
41
|
-
limit?: number;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Retrieves transaction data from Etherscan.
|
|
45
|
-
*
|
|
46
|
-
* @param request - Configuration required to fetch transactions.
|
|
47
|
-
* @param request.address - Address to retrieve transactions for.
|
|
48
|
-
* @param request.apiKey - Etherscan API key to prevent rate limiting.
|
|
49
|
-
* @param request.chainId - Current chain ID used to determine subdomain and domain.
|
|
50
|
-
* @param request.fromBlock - Block number to start fetching transactions from.
|
|
51
|
-
* @param request.limit - Number of transactions to retrieve.
|
|
52
|
-
* @returns An Etherscan response object containing the request status and an array of token transaction data.
|
|
53
|
-
*/
|
|
54
|
-
export declare function fetchEtherscanTransactions({ address, apiKey, chainId, fromBlock, limit, }: EtherscanTransactionRequest): Promise<EtherscanTransactionResponse<EtherscanTransactionMeta>>;
|
|
55
|
-
/**
|
|
56
|
-
* Retrieves token transaction data from Etherscan.
|
|
57
|
-
*
|
|
58
|
-
* @param request - Configuration required to fetch token transactions.
|
|
59
|
-
* @param request.address - Address to retrieve token transactions for.
|
|
60
|
-
* @param request.apiKey - Etherscan API key to prevent rate limiting.
|
|
61
|
-
* @param request.chainId - Current chain ID used to determine subdomain and domain.
|
|
62
|
-
* @param request.fromBlock - Block number to start fetching token transactions from.
|
|
63
|
-
* @param request.limit - Number of token transactions to retrieve.
|
|
64
|
-
* @returns An Etherscan response object containing the request status and an array of token transaction data.
|
|
65
|
-
*/
|
|
66
|
-
export declare function fetchEtherscanTokenTransactions({ address, apiKey, chainId, fromBlock, limit, }: EtherscanTransactionRequest): Promise<EtherscanTransactionResponse<EtherscanTokenTransactionMeta>>;
|
|
67
|
-
/**
|
|
68
|
-
* Return the host url used to fetch data from Etherscan.
|
|
69
|
-
*
|
|
70
|
-
* @param chainId - Current chain ID used to determine subdomain and domain.
|
|
71
|
-
* @returns host URL to access Etherscan data.
|
|
72
|
-
*/
|
|
73
|
-
export declare function getEtherscanApiHost(chainId: Hex): string;
|
|
74
|
-
//# sourceMappingURL=etherscan.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"etherscan.d.mts","sourceRoot":"","sources":["../../src/utils/etherscan.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAU3C,MAAM,WAAW,4BAA4B;IAC3C,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,MAAM,CAAC;IACxB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,gBAAgB,EAAE,MAAM,CAAC;IACzB,KAAK,EAAE,MAAM,CAAC;CACf;AAKD,MAAM,WAAW,wBAAyB,SAAQ,4BAA4B;IAC5E,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IAGjB,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAKD,MAAM,WAAW,6BACf,SAAQ,4BAA4B;IACpC,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACrB;AAKD,MAAM,WAAW,4BAA4B,CAG3C,CAAC,SAAS,4BAA4B;IAEtC,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,GAAG,CAAC,EAAE,CAAC;CACtB;AAKD,MAAM,WAAW,2BAA2B;IAC1C,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,GAAG,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,0BAA0B,CAAC,EAC/C,OAAO,EACP,MAAM,EACN,OAAO,EACP,SAAS,EACT,KAAK,GACN,EAAE,2BAA2B,GAAG,OAAO,CACtC,4BAA4B,CAAC,wBAAwB,CAAC,CACvD,CAQA;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,+BAA+B,CAAC,EACpD,OAAO,EACP,MAAM,EACN,OAAO,EACP,SAAS,EACT,KAAK,GACN,EAAE,2BAA2B,GAAG,OAAO,CACtC,4BAA4B,CAAC,6BAA6B,CAAC,CAC5D,CAQA;AAoFD;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,GAAG,UAW/C"}
|
package/dist/utils/etherscan.mjs
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { handleFetch } from "@metamask/controller-utils";
|
|
2
|
-
import { ETHERSCAN_SUPPORTED_NETWORKS } from "../constants.mjs";
|
|
3
|
-
import { createModuleLogger, projectLogger } from "../logger.mjs";
|
|
4
|
-
const log = createModuleLogger(projectLogger, 'etherscan');
|
|
5
|
-
/**
|
|
6
|
-
* Retrieves transaction data from Etherscan.
|
|
7
|
-
*
|
|
8
|
-
* @param request - Configuration required to fetch transactions.
|
|
9
|
-
* @param request.address - Address to retrieve transactions for.
|
|
10
|
-
* @param request.apiKey - Etherscan API key to prevent rate limiting.
|
|
11
|
-
* @param request.chainId - Current chain ID used to determine subdomain and domain.
|
|
12
|
-
* @param request.fromBlock - Block number to start fetching transactions from.
|
|
13
|
-
* @param request.limit - Number of transactions to retrieve.
|
|
14
|
-
* @returns An Etherscan response object containing the request status and an array of token transaction data.
|
|
15
|
-
*/
|
|
16
|
-
export async function fetchEtherscanTransactions({ address, apiKey, chainId, fromBlock, limit, }) {
|
|
17
|
-
return await fetchTransactions('txlist', {
|
|
18
|
-
address,
|
|
19
|
-
apiKey,
|
|
20
|
-
chainId,
|
|
21
|
-
fromBlock,
|
|
22
|
-
limit,
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Retrieves token transaction data from Etherscan.
|
|
27
|
-
*
|
|
28
|
-
* @param request - Configuration required to fetch token transactions.
|
|
29
|
-
* @param request.address - Address to retrieve token transactions for.
|
|
30
|
-
* @param request.apiKey - Etherscan API key to prevent rate limiting.
|
|
31
|
-
* @param request.chainId - Current chain ID used to determine subdomain and domain.
|
|
32
|
-
* @param request.fromBlock - Block number to start fetching token transactions from.
|
|
33
|
-
* @param request.limit - Number of token transactions to retrieve.
|
|
34
|
-
* @returns An Etherscan response object containing the request status and an array of token transaction data.
|
|
35
|
-
*/
|
|
36
|
-
export async function fetchEtherscanTokenTransactions({ address, apiKey, chainId, fromBlock, limit, }) {
|
|
37
|
-
return await fetchTransactions('tokentx', {
|
|
38
|
-
address,
|
|
39
|
-
apiKey,
|
|
40
|
-
chainId,
|
|
41
|
-
fromBlock,
|
|
42
|
-
limit,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Retrieves transaction data from Etherscan from a specific endpoint.
|
|
47
|
-
*
|
|
48
|
-
* @param action - The Etherscan endpoint to use.
|
|
49
|
-
* @param options - Options bag.
|
|
50
|
-
* @param options.address - Address to retrieve transactions for.
|
|
51
|
-
* @param options.apiKey - Etherscan API key to prevent rate limiting.
|
|
52
|
-
* @param options.chainId - Current chain ID used to determine subdomain and domain.
|
|
53
|
-
* @param options.fromBlock - Block number to start fetching transactions from.
|
|
54
|
-
* @param options.limit - Number of transactions to retrieve.
|
|
55
|
-
* @returns An object containing the request status and an array of transaction data.
|
|
56
|
-
*/
|
|
57
|
-
async function fetchTransactions(action, { address, apiKey, chainId, fromBlock, limit, }) {
|
|
58
|
-
const urlParams = {
|
|
59
|
-
module: 'account',
|
|
60
|
-
address,
|
|
61
|
-
startBlock: fromBlock?.toString(),
|
|
62
|
-
offset: limit?.toString(),
|
|
63
|
-
sort: 'desc',
|
|
64
|
-
};
|
|
65
|
-
const etherscanTxUrl = getEtherscanApiUrl(chainId, {
|
|
66
|
-
...urlParams,
|
|
67
|
-
action,
|
|
68
|
-
apikey: apiKey,
|
|
69
|
-
});
|
|
70
|
-
log('Sending request', etherscanTxUrl);
|
|
71
|
-
const response = (await handleFetch(etherscanTxUrl));
|
|
72
|
-
return response;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Return a URL that can be used to fetch data from Etherscan.
|
|
76
|
-
*
|
|
77
|
-
* @param chainId - Current chain ID used to determine subdomain and domain.
|
|
78
|
-
* @param urlParams - The parameters used to construct the URL.
|
|
79
|
-
* @returns URL to access Etherscan data.
|
|
80
|
-
*/
|
|
81
|
-
function getEtherscanApiUrl(chainId, urlParams) {
|
|
82
|
-
const apiUrl = getEtherscanApiHost(chainId);
|
|
83
|
-
let url = `${apiUrl}/api?`;
|
|
84
|
-
for (const paramKey of Object.keys(urlParams)) {
|
|
85
|
-
const value = urlParams[paramKey];
|
|
86
|
-
if (!value) {
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
url += `${paramKey}=${value}&`;
|
|
90
|
-
}
|
|
91
|
-
url += 'tag=latest&page=1';
|
|
92
|
-
return url;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Return the host url used to fetch data from Etherscan.
|
|
96
|
-
*
|
|
97
|
-
* @param chainId - Current chain ID used to determine subdomain and domain.
|
|
98
|
-
* @returns host URL to access Etherscan data.
|
|
99
|
-
*/
|
|
100
|
-
export function getEtherscanApiHost(chainId) {
|
|
101
|
-
// @ts-expect-error We account for `chainId` not being a property below
|
|
102
|
-
const networkInfo = ETHERSCAN_SUPPORTED_NETWORKS[chainId];
|
|
103
|
-
if (!networkInfo) {
|
|
104
|
-
throw new Error(`Etherscan does not support chain with ID: ${chainId}`);
|
|
105
|
-
}
|
|
106
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
107
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
108
|
-
return `https://${networkInfo.subdomain}.${networkInfo.domain}`;
|
|
109
|
-
}
|
|
110
|
-
//# sourceMappingURL=etherscan.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"etherscan.mjs","sourceRoot":"","sources":["../../src/utils/etherscan.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,mCAAmC;AAGzD,OAAO,EAAE,4BAA4B,EAAE,yBAAqB;AAC5D,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,sBAAkB;AAE9D,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;AAsE3D;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAAC,EAC/C,OAAO,EACP,MAAM,EACN,OAAO,EACP,SAAS,EACT,KAAK,GACuB;IAG5B,OAAO,MAAM,iBAAiB,CAAC,QAAQ,EAAE;QACvC,OAAO;QACP,MAAM;QACN,OAAO;QACP,SAAS;QACT,KAAK;KACN,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,+BAA+B,CAAC,EACpD,OAAO,EACP,MAAM,EACN,OAAO,EACP,SAAS,EACT,KAAK,GACuB;IAG5B,OAAO,MAAM,iBAAiB,CAAC,SAAS,EAAE;QACxC,OAAO;QACP,MAAM;QACN,OAAO;QACP,SAAS;QACT,KAAK;KACN,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;;GAWG;AACH,KAAK,UAAU,iBAAiB,CAG9B,MAAc,EACd,EACE,OAAO,EACP,MAAM,EACN,OAAO,EACP,SAAS,EACT,KAAK,GAON;IAED,MAAM,SAAS,GAAG;QAChB,MAAM,EAAE,SAAS;QACjB,OAAO;QACP,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE;QACjC,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE;QACzB,IAAI,EAAE,MAAM;KACb,CAAC;IAEF,MAAM,cAAc,GAAG,kBAAkB,CAAC,OAAO,EAAE;QACjD,GAAG,SAAS;QACZ,MAAM;QACN,MAAM,EAAE,MAAM;KACf,CAAC,CAAC;IAEH,GAAG,CAAC,iBAAiB,EAAE,cAAc,CAAC,CAAC;IAEvC,MAAM,QAAQ,GAAG,CAAC,MAAM,WAAW,CACjC,cAAc,CACf,CAA+C,CAAC;IAEjD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CACzB,OAAY,EACZ,SAA6C;IAE7C,MAAM,MAAM,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;IAC5C,IAAI,GAAG,GAAG,GAAG,MAAM,OAAO,CAAC;IAE3B,KAAK,MAAM,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE;QAC7C,MAAM,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;QAElC,IAAI,CAAC,KAAK,EAAE;YACV,SAAS;SACV;QAED,GAAG,IAAI,GAAG,QAAQ,IAAI,KAAK,GAAG,CAAC;KAChC;IAED,GAAG,IAAI,mBAAmB,CAAC;IAE3B,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAY;IAC9C,uEAAuE;IACvE,MAAM,WAAW,GAAG,4BAA4B,CAAC,OAAO,CAAC,CAAC;IAE1D,IAAI,CAAC,WAAW,EAAE;QAChB,MAAM,IAAI,KAAK,CAAC,6CAA6C,OAAO,EAAE,CAAC,CAAC;KACzE;IAED,gFAAgF;IAChF,4EAA4E;IAC5E,OAAO,WAAW,WAAW,CAAC,SAAS,IAAI,WAAW,CAAC,MAAM,EAAE,CAAC;AAClE,CAAC","sourcesContent":["import { handleFetch } from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\nimport { ETHERSCAN_SUPPORTED_NETWORKS } from '../constants';\nimport { createModuleLogger, projectLogger } from '../logger';\n\nconst log = createModuleLogger(projectLogger, 'etherscan');\n\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface EtherscanTransactionMetaBase {\n blockNumber: string;\n blockHash: string;\n confirmations: string;\n contractAddress: string;\n cumulativeGasUsed: string;\n from: string;\n gas: string;\n gasPrice: string;\n gasUsed: string;\n hash: string;\n nonce: string;\n timeStamp: string;\n to: string;\n transactionIndex: string;\n value: string;\n}\n\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface EtherscanTransactionMeta extends EtherscanTransactionMetaBase {\n functionName: string;\n input: string;\n isError: string;\n methodId: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n txreceipt_status: string;\n}\n\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface EtherscanTokenTransactionMeta\n extends EtherscanTransactionMetaBase {\n tokenDecimal: string;\n tokenName: string;\n tokenSymbol: string;\n}\n\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface EtherscanTransactionResponse<\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n T extends EtherscanTransactionMetaBase,\n> {\n status: '0' | '1';\n message?: string;\n result: string | T[];\n}\n\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface EtherscanTransactionRequest {\n address: string;\n apiKey?: string;\n chainId: Hex;\n fromBlock?: number;\n limit?: number;\n}\n\n/**\n * Retrieves transaction data from Etherscan.\n *\n * @param request - Configuration required to fetch transactions.\n * @param request.address - Address to retrieve transactions for.\n * @param request.apiKey - Etherscan API key to prevent rate limiting.\n * @param request.chainId - Current chain ID used to determine subdomain and domain.\n * @param request.fromBlock - Block number to start fetching transactions from.\n * @param request.limit - Number of transactions to retrieve.\n * @returns An Etherscan response object containing the request status and an array of token transaction data.\n */\nexport async function fetchEtherscanTransactions({\n address,\n apiKey,\n chainId,\n fromBlock,\n limit,\n}: EtherscanTransactionRequest): Promise<\n EtherscanTransactionResponse<EtherscanTransactionMeta>\n> {\n return await fetchTransactions('txlist', {\n address,\n apiKey,\n chainId,\n fromBlock,\n limit,\n });\n}\n\n/**\n * Retrieves token transaction data from Etherscan.\n *\n * @param request - Configuration required to fetch token transactions.\n * @param request.address - Address to retrieve token transactions for.\n * @param request.apiKey - Etherscan API key to prevent rate limiting.\n * @param request.chainId - Current chain ID used to determine subdomain and domain.\n * @param request.fromBlock - Block number to start fetching token transactions from.\n * @param request.limit - Number of token transactions to retrieve.\n * @returns An Etherscan response object containing the request status and an array of token transaction data.\n */\nexport async function fetchEtherscanTokenTransactions({\n address,\n apiKey,\n chainId,\n fromBlock,\n limit,\n}: EtherscanTransactionRequest): Promise<\n EtherscanTransactionResponse<EtherscanTokenTransactionMeta>\n> {\n return await fetchTransactions('tokentx', {\n address,\n apiKey,\n chainId,\n fromBlock,\n limit,\n });\n}\n\n/**\n * Retrieves transaction data from Etherscan from a specific endpoint.\n *\n * @param action - The Etherscan endpoint to use.\n * @param options - Options bag.\n * @param options.address - Address to retrieve transactions for.\n * @param options.apiKey - Etherscan API key to prevent rate limiting.\n * @param options.chainId - Current chain ID used to determine subdomain and domain.\n * @param options.fromBlock - Block number to start fetching transactions from.\n * @param options.limit - Number of transactions to retrieve.\n * @returns An object containing the request status and an array of transaction data.\n */\nasync function fetchTransactions<\n ResponseData extends EtherscanTransactionMetaBase,\n>(\n action: string,\n {\n address,\n apiKey,\n chainId,\n fromBlock,\n limit,\n }: {\n address: string;\n apiKey?: string;\n chainId: Hex;\n fromBlock?: number;\n limit?: number;\n },\n): Promise<EtherscanTransactionResponse<ResponseData>> {\n const urlParams = {\n module: 'account',\n address,\n startBlock: fromBlock?.toString(),\n offset: limit?.toString(),\n sort: 'desc',\n };\n\n const etherscanTxUrl = getEtherscanApiUrl(chainId, {\n ...urlParams,\n action,\n apikey: apiKey,\n });\n\n log('Sending request', etherscanTxUrl);\n\n const response = (await handleFetch(\n etherscanTxUrl,\n )) as EtherscanTransactionResponse<ResponseData>;\n\n return response;\n}\n\n/**\n * Return a URL that can be used to fetch data from Etherscan.\n *\n * @param chainId - Current chain ID used to determine subdomain and domain.\n * @param urlParams - The parameters used to construct the URL.\n * @returns URL to access Etherscan data.\n */\nfunction getEtherscanApiUrl(\n chainId: Hex,\n urlParams: Record<string, string | undefined>,\n): string {\n const apiUrl = getEtherscanApiHost(chainId);\n let url = `${apiUrl}/api?`;\n\n for (const paramKey of Object.keys(urlParams)) {\n const value = urlParams[paramKey];\n\n if (!value) {\n continue;\n }\n\n url += `${paramKey}=${value}&`;\n }\n\n url += 'tag=latest&page=1';\n\n return url;\n}\n\n/**\n * Return the host url used to fetch data from Etherscan.\n *\n * @param chainId - Current chain ID used to determine subdomain and domain.\n * @returns host URL to access Etherscan data.\n */\nexport function getEtherscanApiHost(chainId: Hex) {\n // @ts-expect-error We account for `chainId` not being a property below\n const networkInfo = ETHERSCAN_SUPPORTED_NETWORKS[chainId];\n\n if (!networkInfo) {\n throw new Error(`Etherscan does not support chain with ID: ${chainId}`);\n }\n\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n return `https://${networkInfo.subdomain}.${networkInfo.domain}`;\n}\n"]}
|