@subwallet/extension-base 0.8.1-wr2x → 0.8.1-wr3
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 +31 -1
- package/background/KoniTypes.js +15 -1
- package/background/errors/BalanceError.d.ts +6 -0
- package/background/errors/BalanceError.js +33 -0
- package/background/handlers/State.js +2 -1
- package/cjs/background/KoniTypes.js +18 -2
- package/cjs/background/errors/BalanceError.js +39 -0
- package/cjs/background/handlers/State.js +2 -1
- package/cjs/koni/api/dotsama/balance.js +1 -0
- package/cjs/koni/background/handlers/Extension.js +140 -94
- package/cjs/koni/background/handlers/State.js +15 -11
- package/cjs/services/balance-service/index.js +95 -2
- package/cjs/services/chain-service/utils.js +13 -3
- package/cjs/services/notification-service/NotificationService.js +23 -1
- package/cjs/services/request-service/handler/PopupHandler.js +10 -16
- package/cjs/services/request-service/index.js +2 -6
- package/cjs/services/setting-service/SettingService.js +2 -1
- package/cjs/services/storage-service/DatabaseService.js +3 -2
- package/cjs/services/storage-service/db-stores/Balance.js +0 -25
- package/cjs/services/transaction-service/index.js +22 -6
- package/koni/api/dotsama/balance.d.ts +1 -0
- package/koni/api/dotsama/balance.js +1 -1
- package/koni/background/handlers/Extension.d.ts +4 -0
- package/koni/background/handlers/Extension.js +58 -14
- package/koni/background/handlers/State.d.ts +3 -1
- package/koni/background/handlers/State.js +15 -11
- package/package.json +8 -3
- package/services/balance-service/index.d.ts +7 -5
- package/services/balance-service/index.js +95 -2
- package/services/chain-service/utils.d.ts +1 -1
- package/services/chain-service/utils.js +13 -3
- package/services/notification-service/NotificationService.d.ts +6 -1
- package/services/notification-service/NotificationService.js +23 -1
- package/services/request-service/handler/PopupHandler.d.ts +0 -1
- package/services/request-service/handler/PopupHandler.js +10 -15
- package/services/request-service/index.d.ts +3 -2
- package/services/request-service/index.js +2 -6
- package/services/setting-service/SettingService.js +2 -1
- package/services/storage-service/DatabaseService.d.ts +2 -1
- package/services/storage-service/DatabaseService.js +3 -2
- package/services/storage-service/db-stores/Balance.js +0 -25
- package/services/transaction-service/index.d.ts +3 -1
- package/services/transaction-service/index.js +23 -7
- package/cjs/background/errors/EvmRpcError.js +0 -21
- package/cjs/background/errors/SubWalletProviderError.js +0 -17
- package/cjs/constants/ethereum.js +0 -19
- package/cjs/utils/eth/parseTransactionData.js +0 -284
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { _ChainAsset } from '@subwallet/chain-list/types';
|
|
2
2
|
import { BalanceItem, ChainStakingMetadata, CrowdloanItem, NftCollection, NftItem, NominatorMetadata, PriceJson, StakingItem, StakingType, TransactionHistoryItem } from '@subwallet/extension-base/background/KoniTypes';
|
|
3
|
-
import { IChain, INft } from '@subwallet/extension-base/services/storage-service/databases';
|
|
3
|
+
import { IBalance, IChain, INft } from '@subwallet/extension-base/services/storage-service/databases';
|
|
4
4
|
import { AssetStore, BalanceStore, ChainStore, CrowdloanStore, MigrationStore, NftCollectionStore, NftStore, PriceStore, StakingStore, TransactionStore } from '@subwallet/extension-base/services/storage-service/db-stores';
|
|
5
5
|
import ChainStakingMetadataStore from '@subwallet/extension-base/services/storage-service/db-stores/ChainStakingMetadata';
|
|
6
6
|
import NominatorMetadataStore from '@subwallet/extension-base/services/storage-service/db-stores/NominatorMetadata';
|
|
@@ -28,6 +28,7 @@ export default class DatabaseService {
|
|
|
28
28
|
constructor();
|
|
29
29
|
updatePriceStore(priceData: PriceJson): Promise<void>;
|
|
30
30
|
getPriceStore(): Promise<PriceJson | undefined>;
|
|
31
|
+
getStoredBalance(): Promise<IBalance[]>;
|
|
31
32
|
updateBalanceStore(address: string, item: BalanceItem): Promise<unknown>;
|
|
32
33
|
removeFromBalanceStore(assets: string[]): Promise<number>;
|
|
33
34
|
updateCrowdloanStore(chain: string, address: string, item: CrowdloanItem): Promise<unknown>;
|
|
@@ -34,9 +34,7 @@ export default class DatabaseService {
|
|
|
34
34
|
}
|
|
35
35
|
async getPriceStore() {
|
|
36
36
|
try {
|
|
37
|
-
console.log('abcd');
|
|
38
37
|
const rs = await this.stores.price.table.get('usd');
|
|
39
|
-
console.log('abcd', rs);
|
|
40
38
|
return rs;
|
|
41
39
|
} catch (e) {
|
|
42
40
|
return undefined;
|
|
@@ -44,6 +42,9 @@ export default class DatabaseService {
|
|
|
44
42
|
}
|
|
45
43
|
|
|
46
44
|
// Balance
|
|
45
|
+
async getStoredBalance() {
|
|
46
|
+
return this.stores.balance.table.toArray();
|
|
47
|
+
}
|
|
47
48
|
async updateBalanceStore(address, item) {
|
|
48
49
|
if (item.state === APIItemState.READY) {
|
|
49
50
|
this.logger.log(`Updating balance for [${item.tokenSlug}]`);
|
|
@@ -21,29 +21,4 @@ export default class BalanceStore extends BaseStoreWithAddress {
|
|
|
21
21
|
async removeBySlugs(tokenSlugs) {
|
|
22
22
|
return this.table.where('tokenSlug').anyOfIgnoreCase(tokenSlugs).delete();
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
// private balanceSub!: Subscription;
|
|
26
|
-
// liveQueryBalance (address: string, cb: (result: BalanceJson) => void) {
|
|
27
|
-
// if (this.balanceSub) {
|
|
28
|
-
// this.balanceSub.unsubscribe();
|
|
29
|
-
// }
|
|
30
|
-
//
|
|
31
|
-
// const subscription = liveQuery(
|
|
32
|
-
// () => this.table.where('address').equals(address).toArray()
|
|
33
|
-
// );
|
|
34
|
-
//
|
|
35
|
-
// this.balanceSub = subscription.subscribe({
|
|
36
|
-
// next: (rs) => {
|
|
37
|
-
// const data = this.convertToJsonObject(rs);
|
|
38
|
-
//
|
|
39
|
-
// if (Object.keys(data).length) {
|
|
40
|
-
// const res: BalanceJson = { details: data };
|
|
41
|
-
//
|
|
42
|
-
// cb(res);
|
|
43
|
-
// }
|
|
44
|
-
// }
|
|
45
|
-
// });
|
|
46
|
-
//
|
|
47
|
-
// return this.balanceSub;
|
|
48
|
-
// }
|
|
49
24
|
}
|
|
@@ -2,6 +2,7 @@ import { TransactionError } from '@subwallet/extension-base/background/errors/Tr
|
|
|
2
2
|
import { BalanceService } from '@subwallet/extension-base/services/balance-service';
|
|
3
3
|
import { ChainService } from '@subwallet/extension-base/services/chain-service';
|
|
4
4
|
import { HistoryService } from '@subwallet/extension-base/services/history-service';
|
|
5
|
+
import NotificationService from '@subwallet/extension-base/services/notification-service/NotificationService';
|
|
5
6
|
import RequestService from '@subwallet/extension-base/services/request-service';
|
|
6
7
|
import { SWTransaction, SWTransactionInput, SWTransactionResponse, TransactionEmitter } from '@subwallet/extension-base/services/transaction-service/types';
|
|
7
8
|
import { BehaviorSubject } from 'rxjs';
|
|
@@ -12,9 +13,10 @@ export default class TransactionService {
|
|
|
12
13
|
private readonly requestService;
|
|
13
14
|
private readonly balanceService;
|
|
14
15
|
private readonly historyService;
|
|
16
|
+
private readonly notificationService;
|
|
15
17
|
private readonly transactionSubject;
|
|
16
18
|
private get transactions();
|
|
17
|
-
constructor(chainService: ChainService, requestService: RequestService, balanceService: BalanceService, historyService: HistoryService);
|
|
19
|
+
constructor(chainService: ChainService, requestService: RequestService, balanceService: BalanceService, historyService: HistoryService, notificationService: NotificationService);
|
|
18
20
|
private get allTransactions();
|
|
19
21
|
private get processingTransactions();
|
|
20
22
|
getTransaction(id: string): SWTransaction;
|
|
@@ -3,10 +3,9 @@
|
|
|
3
3
|
|
|
4
4
|
import { EvmProviderError } from '@subwallet/extension-base/background/errors/EvmProviderError';
|
|
5
5
|
import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
|
|
6
|
-
import { BasicTxErrorType, BasicTxWarningCode, ChainType, EvmProviderErrorType, ExtrinsicStatus, ExtrinsicType, TransactionDirection } from '@subwallet/extension-base/background/KoniTypes';
|
|
6
|
+
import { BasicTxErrorType, BasicTxWarningCode, ChainType, EvmProviderErrorType, ExtrinsicStatus, ExtrinsicType, NotificationType, TransactionDirection } from '@subwallet/extension-base/background/KoniTypes';
|
|
7
7
|
import { TransactionWarning } from '@subwallet/extension-base/background/warnings/TransactionWarning';
|
|
8
8
|
import { _getChainNativeTokenBasicInfo, _getEvmChainId } from '@subwallet/extension-base/services/chain-service/utils';
|
|
9
|
-
import NotificationService from '@subwallet/extension-base/services/notification-service/NotificationService';
|
|
10
9
|
import { EXTENSION_REQUEST_URL } from '@subwallet/extension-base/services/request-service/constants';
|
|
11
10
|
import { getTransactionId, isSubstrateTransaction } from '@subwallet/extension-base/services/transaction-service/helpers';
|
|
12
11
|
import { getTransactionLink, parseTransactionData } from '@subwallet/extension-base/services/transaction-service/utils';
|
|
@@ -23,11 +22,12 @@ export default class TransactionService {
|
|
|
23
22
|
get transactions() {
|
|
24
23
|
return this.transactionSubject.getValue();
|
|
25
24
|
}
|
|
26
|
-
constructor(chainService, requestService, balanceService, historyService) {
|
|
25
|
+
constructor(chainService, requestService, balanceService, historyService, notificationService) {
|
|
27
26
|
this.chainService = chainService;
|
|
28
27
|
this.requestService = requestService;
|
|
29
28
|
this.balanceService = balanceService;
|
|
30
29
|
this.historyService = historyService;
|
|
30
|
+
this.notificationService = notificationService;
|
|
31
31
|
}
|
|
32
32
|
get allTransactions() {
|
|
33
33
|
return Object.values(this.transactions);
|
|
@@ -122,10 +122,10 @@ export default class TransactionService {
|
|
|
122
122
|
// Balance
|
|
123
123
|
const transferNative = validationResponse.transferNativeAmount || '0';
|
|
124
124
|
const nativeTokenInfo = this.chainService.getNativeTokenInfo(chain);
|
|
125
|
-
const balance = await this.balanceService.
|
|
125
|
+
const balance = await this.balanceService.getTokenFreeBalance(address, chain, nativeTokenInfo.slug);
|
|
126
126
|
const existentialDeposit = nativeTokenInfo.minAmount || '0';
|
|
127
127
|
const feeNum = parseInt(estimateFee.value);
|
|
128
|
-
const balanceNum = parseInt(balance);
|
|
128
|
+
const balanceNum = parseInt(balance.value);
|
|
129
129
|
const edNum = parseInt(existentialDeposit);
|
|
130
130
|
const transferNativeNum = parseInt(transferNative);
|
|
131
131
|
if (transferNativeNum + feeNum > balanceNum) {
|
|
@@ -394,7 +394,15 @@ export default class TransactionService {
|
|
|
394
394
|
blockNumber: blockNumber || 0,
|
|
395
395
|
blockHash: blockHash || ''
|
|
396
396
|
}).catch(console.error);
|
|
397
|
-
|
|
397
|
+
this.notificationService.notify({
|
|
398
|
+
type: NotificationType.SUCCESS,
|
|
399
|
+
title: 'Transaction completed',
|
|
400
|
+
message: `Transaction ${transaction === null || transaction === void 0 ? void 0 : transaction.extrinsicHash} completed`,
|
|
401
|
+
action: {
|
|
402
|
+
url: this.getTransactionLink(id)
|
|
403
|
+
},
|
|
404
|
+
notifyViaBrowser: true
|
|
405
|
+
});
|
|
398
406
|
}
|
|
399
407
|
onFailed({
|
|
400
408
|
blockHash,
|
|
@@ -416,7 +424,15 @@ export default class TransactionService {
|
|
|
416
424
|
blockNumber: blockNumber || 0,
|
|
417
425
|
blockHash: blockHash || ''
|
|
418
426
|
}).catch(console.error);
|
|
419
|
-
|
|
427
|
+
this.notificationService.notify({
|
|
428
|
+
type: NotificationType.ERROR,
|
|
429
|
+
title: 'Transaction failed',
|
|
430
|
+
message: `Transaction ${transaction === null || transaction === void 0 ? void 0 : transaction.extrinsicHash} failed`,
|
|
431
|
+
action: {
|
|
432
|
+
url: this.getTransactionLink(id)
|
|
433
|
+
},
|
|
434
|
+
notifyViaBrowser: true
|
|
435
|
+
});
|
|
420
436
|
}
|
|
421
437
|
|
|
422
438
|
// Log transaction errors
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.EvmRpcError = void 0;
|
|
7
|
-
var _ethereum = require("@subwallet/extension-base/constants/ethereum");
|
|
8
|
-
// Copyright 2019-2022 @subwallet/extension-koni authors & contributors
|
|
9
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
10
|
-
|
|
11
|
-
class EvmRpcError extends Error {
|
|
12
|
-
constructor(type, message, data) {
|
|
13
|
-
const [code, prefix] = _ethereum.EVM_PROVIDER_RPC_ERRORS_MAP[type];
|
|
14
|
-
const finalMessage = message ? `${prefix}: ${message}` : prefix;
|
|
15
|
-
super(finalMessage);
|
|
16
|
-
this.code = code;
|
|
17
|
-
this.message = finalMessage;
|
|
18
|
-
this.data = data;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
exports.EvmRpcError = EvmRpcError;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.SubWalletProviderError = void 0;
|
|
7
|
-
// Copyright 2019-2022 @subwallet/extension-koni authors & contributors
|
|
8
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
9
|
-
|
|
10
|
-
class SubWalletProviderError extends Error {
|
|
11
|
-
constructor(message, code, data) {
|
|
12
|
-
super(message);
|
|
13
|
-
this.code = code;
|
|
14
|
-
this.data = data;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
exports.SubWalletProviderError = SubWalletProviderError;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.EVM_PROVIDER_RPC_ERRORS_MAP = void 0;
|
|
7
|
-
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
8
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
9
|
-
|
|
10
|
-
const EVM_PROVIDER_RPC_ERRORS_MAP = {
|
|
11
|
-
USER_REJECTED_REQUEST: [4001, 'User Rejected Request'],
|
|
12
|
-
UNAUTHORIZED: [4100, 'Unauthorized'],
|
|
13
|
-
UNSUPPORTED_METHOD: [4200, 'Unsupported Method'],
|
|
14
|
-
DISCONNECTED: [4900, 'Disconnected'],
|
|
15
|
-
CHAIN_DISCONNECTED: [4901, 'Chain Disconnected'],
|
|
16
|
-
INVALID_PARAMS: [-32602, 'Invalid Params'],
|
|
17
|
-
INTERNAL_ERROR: [-32603, 'Internal Error']
|
|
18
|
-
};
|
|
19
|
-
exports.EVM_PROVIDER_RPC_ERRORS_MAP = EVM_PROVIDER_RPC_ERRORS_MAP;
|
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.InputDataDecoder = void 0;
|
|
8
|
-
var _buffer = require("buffer");
|
|
9
|
-
var _ethers = require("ethers");
|
|
10
|
-
var _isBuffer = _interopRequireDefault(require("is-buffer"));
|
|
11
|
-
var _web3Utils = require("web3-utils");
|
|
12
|
-
// Copyright 2019-2022 @subwallet/extension-koni-base authors & contributors
|
|
13
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
14
|
-
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
|
|
17
|
-
const ABI_TYPES = ['function', 'constructor', 'event', 'fallback'];
|
|
18
|
-
const instanceOfAbiItem = object => {
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access
|
|
20
|
-
return 'type' in object && ABI_TYPES.includes(object.type);
|
|
21
|
-
};
|
|
22
|
-
const checkArrayAbiItems = data => {
|
|
23
|
-
if (Array.isArray(data)) {
|
|
24
|
-
return data.length > 0 && data.every(value => instanceOfAbiItem(value));
|
|
25
|
-
} else {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
const genType = type => {
|
|
30
|
-
if (typeof type === 'string') {
|
|
31
|
-
return type;
|
|
32
|
-
} else {
|
|
33
|
-
if (type.components) {
|
|
34
|
-
var _type$components;
|
|
35
|
-
const arr = (_type$components = type.components) === null || _type$components === void 0 ? void 0 : _type$components.map(genType);
|
|
36
|
-
const tupleStr = `(${arr.join(',')})`;
|
|
37
|
-
if (type.type === 'tuple[]') {
|
|
38
|
-
return tupleStr + '[]';
|
|
39
|
-
} else {
|
|
40
|
-
return tupleStr;
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
return type.type;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
const getMethodId = abi => {
|
|
48
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-call
|
|
49
|
-
return (0, _web3Utils.keccak256)((0, _web3Utils._jsonInterfaceMethodToString)(abi)).slice(2, 10);
|
|
50
|
-
};
|
|
51
|
-
const getMethodName = abi => {
|
|
52
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-call
|
|
53
|
-
return (0, _web3Utils._jsonInterfaceMethodToString)(abi);
|
|
54
|
-
};
|
|
55
|
-
const deepRemoveUnwantedArrayProperties = arr => {
|
|
56
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-return
|
|
57
|
-
return [...arr.map(item => {
|
|
58
|
-
if (Array.isArray(item)) {
|
|
59
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
60
|
-
return deepRemoveUnwantedArrayProperties(item);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
64
|
-
return item;
|
|
65
|
-
})];
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
// remove 0x from addresses
|
|
69
|
-
function deepStripTupleAddresses(input, tupleTypes) {
|
|
70
|
-
return input.map((item, i) => {
|
|
71
|
-
// We find tupleTypes to not be an array where internalType is present in the ABI indicating item is a structure
|
|
72
|
-
const type = tupleTypes[i] ? tupleTypes[i].type : null;
|
|
73
|
-
if (type === 'address' && typeof item === 'string') {
|
|
74
|
-
return item;
|
|
75
|
-
}
|
|
76
|
-
if (type === 'address[]' && Array.isArray(item)) {
|
|
77
|
-
return item.map(a => a);
|
|
78
|
-
}
|
|
79
|
-
if (Array.isArray(item)) {
|
|
80
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
81
|
-
return deepStripTupleAddresses(item, tupleTypes);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
85
|
-
return item;
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
const toHexString = byteArray => {
|
|
89
|
-
return Array.from(byteArray, function (byte) {
|
|
90
|
-
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
|
|
91
|
-
}).join('');
|
|
92
|
-
};
|
|
93
|
-
class InputDataDecoder {
|
|
94
|
-
constructor(prop) {
|
|
95
|
-
this.abi = [];
|
|
96
|
-
if (typeof prop === 'string') {
|
|
97
|
-
try {
|
|
98
|
-
this.abi = JSON.parse(prop);
|
|
99
|
-
} catch (err) {
|
|
100
|
-
throw new Error(`Invalid ABI: ${err.message}`);
|
|
101
|
-
}
|
|
102
|
-
} else if (checkArrayAbiItems(prop)) {
|
|
103
|
-
this.abi = prop;
|
|
104
|
-
} else {
|
|
105
|
-
throw new TypeError('Must pass ABI array object or file path to constructor');
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
decodeConstructor(data) {
|
|
109
|
-
if ((0, _isBuffer.default)(data)) {
|
|
110
|
-
data = data.toString('utf8');
|
|
111
|
-
}
|
|
112
|
-
if (typeof data !== 'string') {
|
|
113
|
-
data = '';
|
|
114
|
-
}
|
|
115
|
-
data = data.trim();
|
|
116
|
-
for (let i = 0; i < this.abi.length; i++) {
|
|
117
|
-
const obj = this.abi[i];
|
|
118
|
-
if (obj.type !== 'constructor') {
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
const method = obj.name || null;
|
|
122
|
-
const methodName = getMethodName(obj);
|
|
123
|
-
const types = obj.inputs ? obj.inputs.map(x => x.type) : [];
|
|
124
|
-
const names = obj.inputs ? obj.inputs.map(x => x.name) : [];
|
|
125
|
-
|
|
126
|
-
// take last 32 bytes
|
|
127
|
-
data = data.slice(-256);
|
|
128
|
-
if (data.length !== 256) {
|
|
129
|
-
throw new Error('fail');
|
|
130
|
-
}
|
|
131
|
-
if (data.indexOf('0x') !== 0) {
|
|
132
|
-
data = `0x${data}`;
|
|
133
|
-
}
|
|
134
|
-
const _inputs = _ethers.ethers.utils.defaultAbiCoder.decode(types, data);
|
|
135
|
-
const inputs = deepRemoveUnwantedArrayProperties(_inputs);
|
|
136
|
-
return {
|
|
137
|
-
methodName,
|
|
138
|
-
method,
|
|
139
|
-
types,
|
|
140
|
-
inputs,
|
|
141
|
-
names
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
throw new Error('not found');
|
|
145
|
-
}
|
|
146
|
-
decodeData(data) {
|
|
147
|
-
if ((0, _isBuffer.default)(data)) {
|
|
148
|
-
data = data.toString('utf8');
|
|
149
|
-
}
|
|
150
|
-
if (typeof data !== 'string') {
|
|
151
|
-
data = '';
|
|
152
|
-
}
|
|
153
|
-
data = data.trim();
|
|
154
|
-
const dataBuf = _buffer.Buffer.from(data.replace(/^0x/, ''), 'hex');
|
|
155
|
-
const methodId = toHexString(dataBuf.subarray(0, 4));
|
|
156
|
-
const inputsBuf = dataBuf.subarray(4);
|
|
157
|
-
let result = {
|
|
158
|
-
method: null,
|
|
159
|
-
methodName: null,
|
|
160
|
-
types: [],
|
|
161
|
-
inputs: [],
|
|
162
|
-
names: []
|
|
163
|
-
};
|
|
164
|
-
for (const abi of this.abi) {
|
|
165
|
-
try {
|
|
166
|
-
if (abi.type === 'constructor') {
|
|
167
|
-
continue;
|
|
168
|
-
}
|
|
169
|
-
if (abi.type === 'event') {
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
const method = abi.name || null;
|
|
173
|
-
const methodName = getMethodName(abi);
|
|
174
|
-
const types = abi.inputs ? abi.inputs.map(x => {
|
|
175
|
-
if (x.type.includes('tuple')) {
|
|
176
|
-
return x;
|
|
177
|
-
} else {
|
|
178
|
-
return x.type;
|
|
179
|
-
}
|
|
180
|
-
}) : [];
|
|
181
|
-
const names = abi.inputs ? abi.inputs.map(x => {
|
|
182
|
-
if (x.type.includes('tuple') && x.components) {
|
|
183
|
-
return [x.name, x.components.map(a => a.name)];
|
|
184
|
-
} else {
|
|
185
|
-
return x.name;
|
|
186
|
-
}
|
|
187
|
-
}) : [];
|
|
188
|
-
const hash = getMethodId(abi);
|
|
189
|
-
if (hash === methodId) {
|
|
190
|
-
let inputs = [];
|
|
191
|
-
try {
|
|
192
|
-
// @ts-ignore
|
|
193
|
-
inputs = _ethers.ethers.utils.defaultAbiCoder.decode(types, inputsBuf);
|
|
194
|
-
} catch (err) {
|
|
195
|
-
try {
|
|
196
|
-
const ifc = new _ethers.ethers.utils.Interface([]);
|
|
197
|
-
inputs = ifc.decodeFunctionData(_ethers.ethers.utils.FunctionFragment.fromObject(abi), data);
|
|
198
|
-
} catch (err) {}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// TODO: do this normalization into normalizeAddresses
|
|
202
|
-
let _inputs = [];
|
|
203
|
-
_inputs = inputs.map((input, i) => {
|
|
204
|
-
if (types[i].components) {
|
|
205
|
-
const tupleTypes = types[i].components;
|
|
206
|
-
|
|
207
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-argument
|
|
208
|
-
return deepStripTupleAddresses(input, tupleTypes);
|
|
209
|
-
}
|
|
210
|
-
if (types[i] === 'address' && typeof input === 'string') {
|
|
211
|
-
return input;
|
|
212
|
-
}
|
|
213
|
-
if (types[i] === 'address[]' && Array.isArray(input)) {
|
|
214
|
-
return input.map(address => address);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
218
|
-
return input;
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
// Map any tuple types into arrays
|
|
222
|
-
const typesToReturn = types.map(genType);
|
|
223
|
-
|
|
224
|
-
// defaultAbiCoder attaches some unwanted properties to the list object
|
|
225
|
-
_inputs = deepRemoveUnwantedArrayProperties(_inputs);
|
|
226
|
-
result = {
|
|
227
|
-
methodName,
|
|
228
|
-
method,
|
|
229
|
-
types: typesToReturn,
|
|
230
|
-
inputs: _inputs,
|
|
231
|
-
names
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
} catch (err) {
|
|
235
|
-
console.log(err);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
if (!result.method) {
|
|
239
|
-
for (const obj of this.abi) {
|
|
240
|
-
if (obj.type === 'constructor') {
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
if (obj.type === 'event') {
|
|
244
|
-
continue;
|
|
245
|
-
}
|
|
246
|
-
const method = obj.name || null;
|
|
247
|
-
try {
|
|
248
|
-
const ifc = new _ethers.ethers.utils.Interface([]);
|
|
249
|
-
const _result = ifc.decodeFunctionData(_ethers.ethers.utils.FunctionFragment.fromObject(obj), data);
|
|
250
|
-
const inputs = deepRemoveUnwantedArrayProperties(_result);
|
|
251
|
-
result.method = method;
|
|
252
|
-
result.methodName = getMethodName(obj);
|
|
253
|
-
result.inputs = inputs;
|
|
254
|
-
result.names = obj.inputs ? obj.inputs.map(x => {
|
|
255
|
-
if (x.type.includes('tuple')) {
|
|
256
|
-
var _x$components;
|
|
257
|
-
return [x.name, ((_x$components = x.components) === null || _x$components === void 0 ? void 0 : _x$components.map(a => a.name)) || ''];
|
|
258
|
-
} else {
|
|
259
|
-
return x.name;
|
|
260
|
-
}
|
|
261
|
-
}) : [];
|
|
262
|
-
const types = obj.inputs ? obj.inputs.map(x => {
|
|
263
|
-
if (x.type.includes('tuple')) {
|
|
264
|
-
return x;
|
|
265
|
-
} else {
|
|
266
|
-
return x.type;
|
|
267
|
-
}
|
|
268
|
-
}) : [];
|
|
269
|
-
result.types = types.map(genType);
|
|
270
|
-
} catch (err) {}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
if (!result.method) {
|
|
274
|
-
try {
|
|
275
|
-
const decoded = this.decodeConstructor(data);
|
|
276
|
-
if (decoded) {
|
|
277
|
-
return decoded;
|
|
278
|
-
}
|
|
279
|
-
} catch (err) {}
|
|
280
|
-
}
|
|
281
|
-
return result;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
exports.InputDataDecoder = InputDataDecoder;
|