@subwallet/extension-base 1.2.23-0 → 1.2.24-1
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 +14 -0
- package/cjs/constants/index.js +4 -1
- package/cjs/core/substrate/assets-pallet.js +35 -0
- package/cjs/core/substrate/foreign-asset-pallet.js +13 -3
- package/cjs/core/substrate/nominationpools-pallet.js +4 -6
- package/cjs/core/substrate/ormlTokens-pallet.js +24 -0
- package/cjs/core/substrate/system-pallet.js +16 -18
- package/cjs/core/substrate/tokens-pallet.js +24 -0
- package/cjs/core/substrate/types.js +19 -0
- package/cjs/core/substrate/xcm-parser.js +16 -2
- package/cjs/core/utils.js +31 -0
- package/cjs/koni/background/cron.js +10 -0
- package/cjs/koni/background/handlers/Extension.js +61 -5
- package/cjs/koni/background/handlers/State.js +3 -0
- package/cjs/packageInfo.js +1 -1
- package/cjs/services/balance-service/helpers/subscribe/substrate/equilibrium.js +2 -2
- package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +120 -133
- package/cjs/services/balance-service/transfer/xcm/index.js +2 -1
- package/cjs/services/balance-service/transfer/xcm/polkadotXcm.js +1 -1
- package/cjs/services/balance-service/transfer/xcm/utils.js +3 -2
- package/cjs/services/chain-service/constants.js +1 -1
- package/cjs/services/chain-service/handler/SubstrateApi.js +128 -34
- package/cjs/services/chain-service/handler/SubstrateChainHandler.js +26 -9
- package/cjs/services/chain-service/utils/index.js +5 -0
- package/cjs/services/migration-service/scripts/MigrateLedgerAccountV2.js +3 -3
- package/cjs/services/mkt-campaign-service/index.js +284 -0
- package/cjs/services/mkt-campaign-service/types.js +17 -0
- package/cjs/services/storage-service/DatabaseService.js +115 -0
- package/cjs/services/storage-service/db-stores/Balance.js +6 -0
- package/cjs/services/storage-service/db-stores/Crowdloan.js +8 -0
- package/cjs/services/storage-service/db-stores/Nft.js +12 -0
- package/cjs/services/storage-service/db-stores/NftCollection.js +11 -0
- package/cjs/services/storage-service/db-stores/YieldPositionStore.js +6 -0
- package/constants/index.d.ts +1 -0
- package/constants/index.js +1 -0
- package/core/substrate/assets-pallet.d.ts +4 -0
- package/core/substrate/assets-pallet.js +28 -0
- package/core/substrate/foreign-asset-pallet.d.ts +4 -8
- package/core/substrate/foreign-asset-pallet.js +13 -3
- package/core/substrate/nominationpools-pallet.d.ts +4 -10
- package/core/substrate/nominationpools-pallet.js +4 -5
- package/core/substrate/ormlTokens-pallet.d.ts +4 -0
- package/core/substrate/ormlTokens-pallet.js +17 -0
- package/core/substrate/system-pallet.d.ts +4 -24
- package/core/substrate/system-pallet.js +16 -21
- package/core/substrate/tokens-pallet.d.ts +5 -0
- package/core/substrate/tokens-pallet.js +16 -0
- package/core/substrate/types.d.ts +43 -0
- package/core/substrate/types.js +12 -0
- package/core/substrate/xcm-parser.d.ts +1 -0
- package/core/substrate/xcm-parser.js +15 -2
- package/core/utils.d.ts +4 -0
- package/core/utils.js +23 -0
- package/koni/api/staking/bonding/relayChain.d.ts +1 -1
- package/koni/background/cron.d.ts +1 -0
- package/koni/background/cron.js +11 -1
- package/koni/background/handlers/Extension.d.ts +3 -0
- package/koni/background/handlers/Extension.js +61 -5
- package/koni/background/handlers/State.d.ts +4 -2
- package/koni/background/handlers/State.js +3 -0
- package/package.json +41 -6
- package/packageInfo.js +1 -1
- package/services/balance-service/helpers/subscribe/substrate/equilibrium.js +2 -2
- package/services/balance-service/helpers/subscribe/substrate/index.js +109 -121
- package/services/balance-service/transfer/xcm/index.d.ts +1 -1
- package/services/balance-service/transfer/xcm/index.js +2 -1
- package/services/balance-service/transfer/xcm/polkadotXcm.js +1 -1
- package/services/balance-service/transfer/xcm/utils.d.ts +1 -1
- package/services/balance-service/transfer/xcm/utils.js +3 -2
- package/services/chain-service/constants.js +1 -1
- package/services/chain-service/handler/SubstrateApi.d.ts +5 -7
- package/services/chain-service/handler/SubstrateApi.js +128 -35
- package/services/chain-service/handler/SubstrateChainHandler.d.ts +5 -6
- package/services/chain-service/handler/SubstrateChainHandler.js +26 -9
- package/services/chain-service/index.d.ts +3 -3
- package/services/chain-service/types.d.ts +22 -5
- package/services/chain-service/utils/index.d.ts +1 -0
- package/services/chain-service/utils/index.js +3 -0
- package/services/earning-service/handlers/nomination-pool/index.d.ts +1 -1
- package/services/migration-service/scripts/MigrateLedgerAccountV2.js +3 -3
- package/services/mkt-campaign-service/index.d.ts +30 -0
- package/services/mkt-campaign-service/index.js +275 -0
- package/services/mkt-campaign-service/types.d.ts +107 -0
- package/services/mkt-campaign-service/types.js +11 -0
- package/services/storage-service/DatabaseService.d.ts +5 -0
- package/services/storage-service/DatabaseService.js +116 -1
- package/services/storage-service/db-stores/Balance.d.ts +2 -0
- package/services/storage-service/db-stores/Balance.js +6 -0
- package/services/storage-service/db-stores/Crowdloan.d.ts +2 -0
- package/services/storage-service/db-stores/Crowdloan.js +8 -0
- package/services/storage-service/db-stores/Nft.d.ts +2 -0
- package/services/storage-service/db-stores/Nft.js +12 -0
- package/services/storage-service/db-stores/NftCollection.d.ts +1 -0
- package/services/storage-service/db-stores/NftCollection.js +11 -0
- package/services/storage-service/db-stores/YieldPositionStore.d.ts +2 -0
- package/services/storage-service/db-stores/YieldPositionStore.js +6 -0
- package/types/balance/index.d.ts +2 -3
|
@@ -10,7 +10,6 @@ var _api2 = require("@gear-js/api");
|
|
|
10
10
|
var _types = require("@oak-foundation/types");
|
|
11
11
|
var _constants = require("@subwallet/extension-base/services/chain-service/constants");
|
|
12
12
|
var _lightClient = require("@subwallet/extension-base/services/chain-service/handler/light-client");
|
|
13
|
-
var _SubstrateChainHandler = require("@subwallet/extension-base/services/chain-service/handler/SubstrateChainHandler");
|
|
14
13
|
var _types2 = require("@subwallet/extension-base/services/chain-service/types");
|
|
15
14
|
var _promise = require("@subwallet/extension-base/utils/promise");
|
|
16
15
|
var _availJsSdk = require("avail-js-sdk");
|
|
@@ -18,8 +17,6 @@ var _rxjs = require("rxjs");
|
|
|
18
17
|
var _api3 = require("@polkadot/api");
|
|
19
18
|
var _api4 = require("@polkadot/apps-config/api");
|
|
20
19
|
var _create = require("@polkadot/types/create");
|
|
21
|
-
var _util = require("@polkadot/util");
|
|
22
|
-
var _defaults = require("@polkadot/util-crypto/address/defaults");
|
|
23
20
|
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
24
21
|
// SPDX-License-Identifier: Apache-2.0
|
|
25
22
|
|
|
@@ -62,6 +59,11 @@ class SubstrateApi {
|
|
|
62
59
|
this.connectionStatusSubject.next(status);
|
|
63
60
|
}
|
|
64
61
|
}
|
|
62
|
+
|
|
63
|
+
// apiDefaultTx?: SubmittableExtrinsicFunction;
|
|
64
|
+
// apiDefaultTxSudo?: SubmittableExtrinsicFunction;
|
|
65
|
+
// defaultFormatBalance?: _SubstrateDefaultFormatBalance;
|
|
66
|
+
|
|
65
67
|
specName = '';
|
|
66
68
|
specVersion = '';
|
|
67
69
|
systemChain = '';
|
|
@@ -231,44 +233,136 @@ class SubstrateApi {
|
|
|
231
233
|
console.warn(`${this.chainSlug} connection got error`, e);
|
|
232
234
|
}
|
|
233
235
|
async fillApiInfo() {
|
|
234
|
-
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
} = this;
|
|
239
|
-
const DEFAULT_DECIMALS = registry.createType('u32', 12);
|
|
240
|
-
const DEFAULT_SS58 = registry.createType('u32', _defaults.defaults.prefix);
|
|
236
|
+
// const { registry } = this;
|
|
237
|
+
// const DEFAULT_DECIMALS = registry.createType('u32', 12);
|
|
238
|
+
// const DEFAULT_SS58 = registry.createType('u32', addressDefaults.prefix);
|
|
239
|
+
|
|
241
240
|
this.specName = this.api.runtimeVersion.specName.toString();
|
|
242
241
|
this.specVersion = this.api.runtimeVersion.specVersion.toString();
|
|
243
|
-
const [systemChain, systemName, systemVersion] = await Promise.all([
|
|
242
|
+
const [systemChain, systemName, systemVersion] = await Promise.all([this.makeRpcQuery({
|
|
243
|
+
section: 'rpc',
|
|
244
|
+
module: 'system',
|
|
245
|
+
method: 'chain'
|
|
246
|
+
}), this.makeRpcQuery({
|
|
247
|
+
section: 'rpc',
|
|
248
|
+
module: 'system',
|
|
249
|
+
method: 'name'
|
|
250
|
+
}), this.makeRpcQuery({
|
|
251
|
+
section: 'rpc',
|
|
252
|
+
module: 'system',
|
|
253
|
+
method: 'version'
|
|
254
|
+
})]);
|
|
244
255
|
this.systemChain = systemChain.toString();
|
|
245
256
|
this.systemName = systemName.toString();
|
|
246
257
|
this.systemVersion = systemVersion.toString();
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
})
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
258
|
+
|
|
259
|
+
// const [ss58Format, tokenDecimals, tokenSymbol] = await Promise.all([
|
|
260
|
+
// this.makeRpcQuery<number | undefined>({ section: 'registry', module: 'chainSS58' }),
|
|
261
|
+
// this.makeRpcQuery<number[]>({ section: 'registry', module: 'chainDecimals' }),
|
|
262
|
+
// this.makeRpcQuery<string[]>({ section: 'registry', module: 'chainTokens' })
|
|
263
|
+
// ]);
|
|
264
|
+
//
|
|
265
|
+
// const properties = registry.createType('ChainProperties', {
|
|
266
|
+
// ss58Format,
|
|
267
|
+
// tokenDecimals,
|
|
268
|
+
// tokenSymbol
|
|
269
|
+
// });
|
|
270
|
+
// const ss58Format = properties.ss58Format.unwrapOr(DEFAULT_SS58).toNumber();
|
|
271
|
+
// const tokenSymbol = properties.tokenSymbol.unwrapOr([formatBalance.getDefaults().unit, ...DEFAULT_AUX]);
|
|
272
|
+
// const tokenDecimals = properties.tokenDecimals.unwrapOr([DEFAULT_DECIMALS]);
|
|
273
|
+
//
|
|
274
|
+
// registry.setChainProperties(registry.createType('ChainProperties', { ss58Format, tokenDecimals, tokenSymbol }));
|
|
260
275
|
|
|
261
276
|
// first set up the UI helpers
|
|
262
|
-
this.defaultFormatBalance = {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
const
|
|
270
|
-
|
|
271
|
-
|
|
277
|
+
// this.defaultFormatBalance = {
|
|
278
|
+
// decimals: tokenDecimals.map((b: BN) => {
|
|
279
|
+
// return b.toNumber();
|
|
280
|
+
// }),
|
|
281
|
+
// unit: tokenSymbol[0].toString()
|
|
282
|
+
// };
|
|
283
|
+
//
|
|
284
|
+
// const defaultSection = Object.keys(api.tx)[0];
|
|
285
|
+
// const defaultMethod = Object.keys(api.tx[defaultSection])[0];
|
|
286
|
+
|
|
287
|
+
// this.apiDefaultTx = api.tx[defaultSection][defaultMethod];
|
|
288
|
+
// this.apiDefaultTxSudo = (api.tx.system && api.tx.system.setCode) || this.apiDefaultTx;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
async makeRpcQuery(_ref) {
|
|
292
|
+
let {
|
|
293
|
+
args,
|
|
294
|
+
method,
|
|
295
|
+
module,
|
|
296
|
+
section
|
|
297
|
+
} = _ref;
|
|
298
|
+
const isGetterCall = section === 'genesisHash' || section === 'extrinsicVersion' || section === 'runtimeVersion' || section === 'runtimeMetadata' || section === 'registry';
|
|
299
|
+
const isRuntimeConstQuery = section === 'consts' && !!method && !!module && !args;
|
|
300
|
+
const isRpcQuery = section === 'rpc' && !!method && !!module && !args;
|
|
301
|
+
const isStateQuery = section === 'query' && method && module;
|
|
302
|
+
if (isGetterCall) {
|
|
303
|
+
if (section === 'genesisHash') {
|
|
304
|
+
return this.api[section].toHex();
|
|
305
|
+
} else if (section === 'extrinsicVersion') {
|
|
306
|
+
return this.api[section];
|
|
307
|
+
} else if (section === 'runtimeVersion') {
|
|
308
|
+
return this.api[section].toPrimitive();
|
|
309
|
+
} else if (section === 'runtimeMetadata') {
|
|
310
|
+
return this.api[section].toHex();
|
|
311
|
+
} else if (section === 'registry') {
|
|
312
|
+
return this.api[section];
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (isRuntimeConstQuery) {
|
|
316
|
+
if (!this.api[section][module]) {
|
|
317
|
+
return undefined;
|
|
318
|
+
}
|
|
319
|
+
return this.api[section][module][method].toPrimitive();
|
|
320
|
+
}
|
|
321
|
+
if (isRpcQuery) {
|
|
322
|
+
// @ts-ignore
|
|
323
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
|
324
|
+
if (!this.api[section][module]) {
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// @ts-ignore
|
|
329
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
|
330
|
+
return (await this.api[section][module][method]()).toPrimitive(); // todo: improve this
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (isStateQuery) {
|
|
334
|
+
if (!this.api[section][module]) {
|
|
335
|
+
return undefined;
|
|
336
|
+
}
|
|
337
|
+
if (args) {
|
|
338
|
+
return (await this.api[section][module][method](...args)).toPrimitive();
|
|
339
|
+
} else {
|
|
340
|
+
return (await this.api[section][module][method]()).toPrimitive();
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return Promise.reject(new Error('Cannot handle query'));
|
|
344
|
+
}
|
|
345
|
+
subscribeDataWithMulti(params, callback) {
|
|
346
|
+
const apiRx = this.api.rx;
|
|
347
|
+
const observables = {};
|
|
348
|
+
params.forEach(queryParams => {
|
|
349
|
+
const {
|
|
350
|
+
args,
|
|
351
|
+
method,
|
|
352
|
+
module,
|
|
353
|
+
section
|
|
354
|
+
} = queryParams;
|
|
355
|
+
const key = `${section}_${module}_${method}`;
|
|
356
|
+
if (!this.api[section][module] || !this.api[section][module][method]) {
|
|
357
|
+
// if method not found, returns an empty observable
|
|
358
|
+
observables[key] = new _rxjs.Observable(subscriber => {
|
|
359
|
+
subscriber.next([]);
|
|
360
|
+
});
|
|
361
|
+
} else {
|
|
362
|
+
observables[key] = apiRx[section][module][method].multi(args).pipe((0, _rxjs.map)(codecs => codecs.map(codec => codec.toPrimitive())));
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
return (0, _rxjs.combineLatest)(observables).subscribe(callback);
|
|
272
366
|
}
|
|
273
367
|
}
|
|
274
368
|
exports.SubstrateApi = SubstrateApi;
|
|
@@ -69,12 +69,13 @@ class SubstrateChainHandler extends _AbstractChainHandler.AbstractChainHandler {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
async getChainSpec(substrateApi) {
|
|
72
|
-
var _substrateApi$api$gen, _substrateApi$api, _substrateApi$api$con, _substrateApi$api$con2, _substrateApi$api$con3;
|
|
73
72
|
const result = {
|
|
74
73
|
addressPrefix: -1,
|
|
75
74
|
decimals: 0,
|
|
76
75
|
existentialDeposit: '',
|
|
77
|
-
genesisHash:
|
|
76
|
+
genesisHash: await substrateApi.makeRpcQuery({
|
|
77
|
+
section: 'genesisHash'
|
|
78
|
+
}),
|
|
78
79
|
name: '',
|
|
79
80
|
symbol: '',
|
|
80
81
|
paraId: null
|
|
@@ -82,17 +83,33 @@ class SubstrateChainHandler extends _AbstractChainHandler.AbstractChainHandler {
|
|
|
82
83
|
const {
|
|
83
84
|
chainDecimals,
|
|
84
85
|
chainTokens
|
|
85
|
-
} = substrateApi.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
} = await substrateApi.makeRpcQuery({
|
|
87
|
+
section: 'registry'
|
|
88
|
+
});
|
|
89
|
+
result.paraId = await substrateApi.makeRpcQuery({
|
|
90
|
+
section: 'query',
|
|
91
|
+
module: 'parachainInfo',
|
|
92
|
+
method: 'parachainId'
|
|
93
|
+
});
|
|
89
94
|
|
|
90
95
|
// get first token by default, might change
|
|
91
|
-
result.name =
|
|
96
|
+
result.name = await substrateApi.makeRpcQuery({
|
|
97
|
+
section: 'rpc',
|
|
98
|
+
module: 'system',
|
|
99
|
+
method: 'chain'
|
|
100
|
+
});
|
|
92
101
|
result.symbol = chainTokens[0];
|
|
93
102
|
result.decimals = chainDecimals[0];
|
|
94
|
-
result.addressPrefix =
|
|
95
|
-
|
|
103
|
+
result.addressPrefix = await substrateApi.makeRpcQuery({
|
|
104
|
+
section: 'consts',
|
|
105
|
+
module: 'system',
|
|
106
|
+
method: 'ss58Prefix'
|
|
107
|
+
});
|
|
108
|
+
result.existentialDeposit = await substrateApi.makeRpcQuery({
|
|
109
|
+
section: 'consts',
|
|
110
|
+
module: 'balances',
|
|
111
|
+
method: 'existentialDeposit'
|
|
112
|
+
});
|
|
96
113
|
return result;
|
|
97
114
|
}
|
|
98
115
|
async getPsp22TokenInfo(apiPromise, contractAddress, contractCaller) {
|
|
@@ -81,6 +81,7 @@ var _exportNames = {
|
|
|
81
81
|
findChainInfoByChainId: true,
|
|
82
82
|
_isMantaZkAsset: true,
|
|
83
83
|
_getChainExistentialDeposit: true,
|
|
84
|
+
_getAssetExistentialDeposit: true,
|
|
84
85
|
randomizeProvider: true,
|
|
85
86
|
_isAssetCanPayTxFee: true,
|
|
86
87
|
updateLatestChainInfo: true
|
|
@@ -88,6 +89,7 @@ var _exportNames = {
|
|
|
88
89
|
exports._checkSmartContractSupportByChain = _checkSmartContractSupportByChain;
|
|
89
90
|
exports._generateCustomProviderKey = _generateCustomProviderKey;
|
|
90
91
|
exports._getAssetDecimals = _getAssetDecimals;
|
|
92
|
+
exports._getAssetExistentialDeposit = _getAssetExistentialDeposit;
|
|
91
93
|
exports._getAssetName = _getAssetName;
|
|
92
94
|
exports._getAssetOriginChain = _getAssetOriginChain;
|
|
93
95
|
exports._getAssetPriceId = _getAssetPriceId;
|
|
@@ -599,6 +601,9 @@ function _getChainExistentialDeposit(chainInfo) {
|
|
|
599
601
|
var _chainInfo$substrateI20;
|
|
600
602
|
return (chainInfo === null || chainInfo === void 0 ? void 0 : (_chainInfo$substrateI20 = chainInfo.substrateInfo) === null || _chainInfo$substrateI20 === void 0 ? void 0 : _chainInfo$substrateI20.existentialDeposit) || '0';
|
|
601
603
|
}
|
|
604
|
+
function _getAssetExistentialDeposit(chainAsset) {
|
|
605
|
+
return (chainAsset === null || chainAsset === void 0 ? void 0 : chainAsset.minAmount) || '0';
|
|
606
|
+
}
|
|
602
607
|
function randomizeProvider(providers, excludedKeys) {
|
|
603
608
|
if (Object.keys(providers).length === 0) {
|
|
604
609
|
return {
|
|
@@ -19,7 +19,7 @@ class MigrateLedgerAccountV2 extends _Base.default {
|
|
|
19
19
|
const store = new _stores.AccountsStore();
|
|
20
20
|
const update = (key, value) => {
|
|
21
21
|
var _value$meta;
|
|
22
|
-
if (key.startsWith('account:') && value.meta && (0, _util.isString)((_value$meta = value.meta) === null || _value$meta === void 0 ? void 0 : _value$meta.
|
|
22
|
+
if (key.startsWith('account:') && value.meta && (0, _util.isString)((_value$meta = value.meta) === null || _value$meta === void 0 ? void 0 : _value$meta.genesisHash)) {
|
|
23
23
|
const newValue = {
|
|
24
24
|
...value
|
|
25
25
|
};
|
|
@@ -28,10 +28,10 @@ class MigrateLedgerAccountV2 extends _Base.default {
|
|
|
28
28
|
if (isEther) {
|
|
29
29
|
newValue.meta.isGeneric = true;
|
|
30
30
|
} else {
|
|
31
|
-
newValue.meta.isGeneric = !newValue.meta.
|
|
31
|
+
newValue.meta.isGeneric = !newValue.meta.genesisHash;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
newValue.meta.availableGenesisHashes = [value.meta.
|
|
34
|
+
newValue.meta.availableGenesisHashes = [value.meta.genesisHash];
|
|
35
35
|
store.set(key, newValue);
|
|
36
36
|
}
|
|
37
37
|
};
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = void 0;
|
|
8
|
+
var _utils = require("@subwallet/extension-base/services/chain-service/utils");
|
|
9
|
+
var _types = require("@subwallet/extension-base/services/mkt-campaign-service/types");
|
|
10
|
+
var _utils2 = require("@subwallet/extension-base/utils");
|
|
11
|
+
var _uiKeyring = require("@subwallet/ui-keyring");
|
|
12
|
+
var _bignumber = _interopRequireDefault(require("bignumber.js"));
|
|
13
|
+
var _rxjs = require("rxjs");
|
|
14
|
+
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
15
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
|
|
17
|
+
class MktCampaignService {
|
|
18
|
+
#state;
|
|
19
|
+
appPopupSubject = new _rxjs.BehaviorSubject([]);
|
|
20
|
+
appBannerSubject = new _rxjs.BehaviorSubject([]);
|
|
21
|
+
appConfirmationSubject = new _rxjs.BehaviorSubject([]);
|
|
22
|
+
constructor(state) {
|
|
23
|
+
this.#state = state;
|
|
24
|
+
this.appPopupSubject.next([]);
|
|
25
|
+
this.appBannerSubject.next([]);
|
|
26
|
+
this.appConfirmationSubject.next([]);
|
|
27
|
+
this.fetchPopupData().catch(e => {
|
|
28
|
+
console.error('Error on fetch popup', e);
|
|
29
|
+
});
|
|
30
|
+
this.fetchBannerData().catch(e => {
|
|
31
|
+
console.error('Error on fetch banner', e);
|
|
32
|
+
});
|
|
33
|
+
this.fetchConfirmationData().catch(e => {
|
|
34
|
+
console.error('Error on fetch confirmation', e);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
fetchMktCampaignData() {
|
|
38
|
+
let timeout = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10000;
|
|
39
|
+
(0, _utils2.wait)(timeout).finally(() => {
|
|
40
|
+
this.fetchPopupData().catch(console.error);
|
|
41
|
+
this.fetchBannerData().catch(console.error);
|
|
42
|
+
this.fetchConfirmationData().catch(console.error);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
init() {
|
|
46
|
+
this.fetchMktCampaignData();
|
|
47
|
+
console.log('Mkt campaign service ready');
|
|
48
|
+
}
|
|
49
|
+
async fetchPopupData() {
|
|
50
|
+
const respAppPopupData = await (0, _utils2.fetchStaticData)('app-popups');
|
|
51
|
+
const result = await this.handleMktCampaignData(respAppPopupData);
|
|
52
|
+
this.appPopupSubject.next(result);
|
|
53
|
+
}
|
|
54
|
+
async fetchBannerData() {
|
|
55
|
+
const respAppBannerData = await (0, _utils2.fetchStaticData)('app-banners');
|
|
56
|
+
const result = await this.handleMktCampaignData(respAppBannerData);
|
|
57
|
+
this.appBannerSubject.next(result);
|
|
58
|
+
}
|
|
59
|
+
async fetchConfirmationData() {
|
|
60
|
+
const respAppConfirmationData = await (0, _utils2.fetchStaticData)('app-confirmations');
|
|
61
|
+
const result = await this.handleMktCampaignData(respAppConfirmationData);
|
|
62
|
+
this.appConfirmationSubject.next(result);
|
|
63
|
+
}
|
|
64
|
+
subscribePopupsData(callback) {
|
|
65
|
+
return this.appPopupSubject.subscribe({
|
|
66
|
+
next: callback
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
subscribeBannersData(callback) {
|
|
70
|
+
return this.appBannerSubject.subscribe({
|
|
71
|
+
next: callback
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
subscribeConfirmationsData(callback) {
|
|
75
|
+
return this.appConfirmationSubject.subscribe({
|
|
76
|
+
next: callback
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
getAppPopupsData() {
|
|
80
|
+
return this.appPopupSubject.getValue();
|
|
81
|
+
}
|
|
82
|
+
getAppBannersData() {
|
|
83
|
+
return this.appBannerSubject.getValue();
|
|
84
|
+
}
|
|
85
|
+
getAppConfirmationsData() {
|
|
86
|
+
return this.appConfirmationSubject.getValue();
|
|
87
|
+
}
|
|
88
|
+
async handleMktCampaignData(data) {
|
|
89
|
+
const addresses = _uiKeyring.keyring.getPairs().map(pair => pair.address);
|
|
90
|
+
const allConditions = this.getAllConditions(data);
|
|
91
|
+
const conditionBalanceMap = await this.checkBalanceCondition(allConditions, addresses);
|
|
92
|
+
const conditionEarningMap = await this.checkEarningCondition(allConditions, addresses);
|
|
93
|
+
const conditionNftMap = await this.checkNftCondition(allConditions, addresses);
|
|
94
|
+
const conditionCrowdloanMap = await this.checkCrowdloanCondition(allConditions, addresses);
|
|
95
|
+
const conditionHasMoneyMap = await this.checkHasMoneyCondition(allConditions, addresses);
|
|
96
|
+
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
98
|
+
const conditionMap = Object.assign({}, conditionBalanceMap, conditionEarningMap, conditionNftMap, conditionCrowdloanMap, conditionHasMoneyMap);
|
|
99
|
+
const filteredData = this.checkActiveAndPlatformCondition(data);
|
|
100
|
+
return filteredData.filter(item => this.filterCampaignFunc(item, conditionMap));
|
|
101
|
+
}
|
|
102
|
+
checkActiveAndPlatformCondition(data) {
|
|
103
|
+
return data.filter(item => {
|
|
104
|
+
if (item.info) {
|
|
105
|
+
return this.checkCampaignExistTime(item.info) && item.info.platforms.includes(_utils2.TARGET_ENV);
|
|
106
|
+
} else {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
checkCampaignExistTime(info) {
|
|
112
|
+
if (info.start_time && info.stop_time) {
|
|
113
|
+
const now = new Date();
|
|
114
|
+
const startTime = new Date(info.start_time);
|
|
115
|
+
const endTime = new Date(info.stop_time);
|
|
116
|
+
return now >= startTime && now <= endTime;
|
|
117
|
+
} else {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async checkBalanceCondition(allConditions, addresses) {
|
|
122
|
+
var _allConditions$MktCam;
|
|
123
|
+
const allBalanceConditions = (_allConditions$MktCam = allConditions[_types.MktCampaignConditionTypeValue.BALANCE]) === null || _allConditions$MktCam === void 0 ? void 0 : _allConditions$MktCam.map(condition => {
|
|
124
|
+
return JSON.parse(condition);
|
|
125
|
+
});
|
|
126
|
+
const allBalanceConditionPromises = [];
|
|
127
|
+
allBalanceConditions.forEach(balanceCondition => {
|
|
128
|
+
const decimals = (0, _utils._getAssetDecimals)(this.#state.getAssetBySlug(balanceCondition.chain_asset));
|
|
129
|
+
const convertedValue = new _bignumber.default(balanceCondition.value).shiftedBy(decimals);
|
|
130
|
+
allBalanceConditionPromises.push(this.#state.dbService.checkBalanceByTokens([balanceCondition.chain_asset], balanceCondition.comparison, addresses, convertedValue.toString()));
|
|
131
|
+
});
|
|
132
|
+
const promiseBalanceResult = await Promise.all(allBalanceConditionPromises);
|
|
133
|
+
const conditionBalanceMap = {};
|
|
134
|
+
promiseBalanceResult.forEach((rs, i) => {
|
|
135
|
+
conditionBalanceMap[allConditions[_types.MktCampaignConditionTypeValue.BALANCE][i]] = rs > 0;
|
|
136
|
+
});
|
|
137
|
+
return conditionBalanceMap;
|
|
138
|
+
}
|
|
139
|
+
async checkEarningCondition(allConditions, addresses) {
|
|
140
|
+
var _allConditions$MktCam2;
|
|
141
|
+
const allEarningConditions = (_allConditions$MktCam2 = allConditions[_types.MktCampaignConditionTypeValue.EARNING]) === null || _allConditions$MktCam2 === void 0 ? void 0 : _allConditions$MktCam2.map(condition => {
|
|
142
|
+
return JSON.parse(condition);
|
|
143
|
+
});
|
|
144
|
+
const allEarningConditionPromises = [];
|
|
145
|
+
allEarningConditions.forEach(earningCondition => {
|
|
146
|
+
const poolSlugSplittedArr = earningCondition.pool_slug.split('___');
|
|
147
|
+
if (poolSlugSplittedArr[1] === 'liquid_staking') {
|
|
148
|
+
allEarningConditionPromises.push(new Promise(() => false));
|
|
149
|
+
}
|
|
150
|
+
const chain = earningCondition.pool_slug.split('___')[2];
|
|
151
|
+
const chainInfo = this.#state.getChainInfo(chain);
|
|
152
|
+
const {
|
|
153
|
+
decimals
|
|
154
|
+
} = (0, _utils._getChainNativeTokenBasicInfo)(chainInfo);
|
|
155
|
+
const convertedValue = new _bignumber.default(earningCondition.value).shiftedBy(decimals);
|
|
156
|
+
allEarningConditionPromises.push(this.#state.dbService.checkEarningByTokens(earningCondition.pool_slug, earningCondition.comparison, addresses, convertedValue.toString()));
|
|
157
|
+
});
|
|
158
|
+
const promiseEarningResult = await Promise.all(allEarningConditionPromises);
|
|
159
|
+
const conditionEarningMap = {};
|
|
160
|
+
promiseEarningResult.forEach((rs, i) => {
|
|
161
|
+
conditionEarningMap[allConditions[_types.MktCampaignConditionTypeValue.EARNING][i]] = rs > 0;
|
|
162
|
+
});
|
|
163
|
+
return conditionEarningMap;
|
|
164
|
+
}
|
|
165
|
+
async checkNftCondition(allConditions, addresses) {
|
|
166
|
+
var _allConditions$MktCam3;
|
|
167
|
+
const allNftConditions = (_allConditions$MktCam3 = allConditions[_types.MktCampaignConditionTypeValue.NFT]) === null || _allConditions$MktCam3 === void 0 ? void 0 : _allConditions$MktCam3.map(condition => {
|
|
168
|
+
return JSON.parse(condition);
|
|
169
|
+
});
|
|
170
|
+
const allNftConditionPromises = [];
|
|
171
|
+
allNftConditions.forEach(nftCondition => {
|
|
172
|
+
allNftConditionPromises.push(this.#state.dbService.checkNftByTokens(nftCondition.chain, addresses, nftCondition.collection_id));
|
|
173
|
+
});
|
|
174
|
+
const promiseNftResult = await Promise.all(allNftConditionPromises);
|
|
175
|
+
const conditionNftMap = {};
|
|
176
|
+
promiseNftResult.forEach((rs, i) => {
|
|
177
|
+
conditionNftMap[allConditions[_types.MktCampaignConditionTypeValue.NFT][i]] = rs > 0;
|
|
178
|
+
});
|
|
179
|
+
return conditionNftMap;
|
|
180
|
+
}
|
|
181
|
+
async checkCrowdloanCondition(allConditions, addresses) {
|
|
182
|
+
var _allConditions$MktCam4;
|
|
183
|
+
const allCrowdloanConditions = (_allConditions$MktCam4 = allConditions[_types.MktCampaignConditionTypeValue.CROWDLOAN]) === null || _allConditions$MktCam4 === void 0 ? void 0 : _allConditions$MktCam4.map(condition => {
|
|
184
|
+
return JSON.parse(condition);
|
|
185
|
+
});
|
|
186
|
+
const allCrowdloanConditionPromises = [];
|
|
187
|
+
allCrowdloanConditions.forEach(crowdloanCondition => {
|
|
188
|
+
allCrowdloanConditionPromises.push(this.#state.dbService.checkCrowdloanByChain(crowdloanCondition.chain, addresses));
|
|
189
|
+
});
|
|
190
|
+
const promiseCrowdloanResult = await Promise.all(allCrowdloanConditionPromises);
|
|
191
|
+
const conditionCrowdloanMap = {};
|
|
192
|
+
promiseCrowdloanResult.forEach((rs, i) => {
|
|
193
|
+
conditionCrowdloanMap[allConditions[_types.MktCampaignConditionTypeValue.CROWDLOAN][i]] = rs > 0;
|
|
194
|
+
});
|
|
195
|
+
return conditionCrowdloanMap;
|
|
196
|
+
}
|
|
197
|
+
async checkHasMoneyCondition(allConditions, addresses) {
|
|
198
|
+
const hasMoneyCondition = allConditions[_types.MktCampaignConditionTypeValue.HAS_MONEY];
|
|
199
|
+
const allHasMoneyConditions = hasMoneyCondition === null || hasMoneyCondition === void 0 ? void 0 : hasMoneyCondition.map(condition => {
|
|
200
|
+
return JSON.parse(condition);
|
|
201
|
+
});
|
|
202
|
+
const allHasMoneyConditionPromises = [];
|
|
203
|
+
allHasMoneyConditions.forEach(hasMoneyCondition => {
|
|
204
|
+
allHasMoneyConditionPromises.push(this.#state.dbService.checkHasMoneyByTokens(hasMoneyCondition.has_money, addresses));
|
|
205
|
+
});
|
|
206
|
+
const promiseHasMoneyResult = await Promise.all(allHasMoneyConditionPromises);
|
|
207
|
+
const conditionHasMoneyMap = {};
|
|
208
|
+
promiseHasMoneyResult.forEach((rs, i) => {
|
|
209
|
+
conditionHasMoneyMap[hasMoneyCondition[i]] = rs > 0;
|
|
210
|
+
});
|
|
211
|
+
return conditionHasMoneyMap;
|
|
212
|
+
}
|
|
213
|
+
getAllConditions(respAppPopupData) {
|
|
214
|
+
const result = {
|
|
215
|
+
[_types.MktCampaignConditionTypeValue.BALANCE]: [],
|
|
216
|
+
[_types.MktCampaignConditionTypeValue.EARNING]: [],
|
|
217
|
+
[_types.MktCampaignConditionTypeValue.CROWDLOAN]: [],
|
|
218
|
+
[_types.MktCampaignConditionTypeValue.NFT]: [],
|
|
219
|
+
[_types.MktCampaignConditionTypeValue.HAS_MONEY]: []
|
|
220
|
+
};
|
|
221
|
+
const getConditionByType = (campaignCondition, type) => {
|
|
222
|
+
const campaignConditionByType = campaignCondition[type];
|
|
223
|
+
campaignConditionByType === null || campaignConditionByType === void 0 ? void 0 : campaignConditionByType.forEach(condition => {
|
|
224
|
+
const conditionStringify = JSON.stringify(condition);
|
|
225
|
+
if (!result[type].includes(conditionStringify)) {
|
|
226
|
+
result[type].push(conditionStringify);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
};
|
|
230
|
+
respAppPopupData.forEach(popup => {
|
|
231
|
+
const conditionKeys = Object.keys(popup.conditions);
|
|
232
|
+
if (conditionKeys && conditionKeys.length) {
|
|
233
|
+
conditionKeys.forEach(con => getConditionByType(popup.conditions, con));
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
filterCampaignFunc(item, conditionMap) {
|
|
239
|
+
const isPassValidation = [];
|
|
240
|
+
if (Object.values(item.conditions) && Object.values(item.conditions).length) {
|
|
241
|
+
const conditionBalance = Object.values(item.conditions[_types.MktCampaignConditionTypeValue.BALANCE] || {});
|
|
242
|
+
const conditionEarning = Object.values(item.conditions[_types.MktCampaignConditionTypeValue.EARNING] || {});
|
|
243
|
+
const conditionNft = Object.values(item.conditions[_types.MktCampaignConditionTypeValue.NFT] || {});
|
|
244
|
+
const conditionCrowdloan = Object.values(item.conditions[_types.MktCampaignConditionTypeValue.CROWDLOAN] || {});
|
|
245
|
+
const conditionHasMoney = Object.values(item.conditions[_types.MktCampaignConditionTypeValue.HAS_MONEY] || {});
|
|
246
|
+
if (conditionBalance && conditionBalance.length) {
|
|
247
|
+
const isValidArr = conditionBalance.map(condition => {
|
|
248
|
+
return conditionMap[JSON.stringify(condition)];
|
|
249
|
+
});
|
|
250
|
+
isPassValidation.push(isValidArr.some(i => i));
|
|
251
|
+
}
|
|
252
|
+
if (conditionEarning && conditionEarning.length) {
|
|
253
|
+
const isValidArr = conditionEarning.map(condition => {
|
|
254
|
+
return conditionMap[JSON.stringify(condition)];
|
|
255
|
+
});
|
|
256
|
+
isPassValidation.push(isValidArr.some(i => i));
|
|
257
|
+
}
|
|
258
|
+
if (conditionNft && conditionNft.length) {
|
|
259
|
+
const isValidArr = conditionNft.map(condition => {
|
|
260
|
+
return conditionMap[JSON.stringify(condition)];
|
|
261
|
+
});
|
|
262
|
+
isPassValidation.push(isValidArr.some(i => i));
|
|
263
|
+
}
|
|
264
|
+
if (conditionCrowdloan && conditionCrowdloan.length) {
|
|
265
|
+
const isValidArr = conditionCrowdloan.map(condition => {
|
|
266
|
+
return conditionMap[JSON.stringify(condition)];
|
|
267
|
+
});
|
|
268
|
+
isPassValidation.push(isValidArr.some(i => i));
|
|
269
|
+
}
|
|
270
|
+
if (conditionHasMoney && conditionHasMoney.length) {
|
|
271
|
+
const isValidArr = conditionHasMoney.map(condition => {
|
|
272
|
+
return conditionMap[JSON.stringify(condition)];
|
|
273
|
+
});
|
|
274
|
+
isPassValidation.push(isValidArr.some(i => i));
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (isPassValidation && isPassValidation.length) {
|
|
278
|
+
return isPassValidation.some(_i => _i);
|
|
279
|
+
} else {
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
exports.default = MktCampaignService;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.MktCampaignConditionTypeValue = void 0;
|
|
7
|
+
// Copyright 2019-2022 @subwallet/extension-koni authors & contributors
|
|
8
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9
|
+
let MktCampaignConditionTypeValue;
|
|
10
|
+
exports.MktCampaignConditionTypeValue = MktCampaignConditionTypeValue;
|
|
11
|
+
(function (MktCampaignConditionTypeValue) {
|
|
12
|
+
MktCampaignConditionTypeValue["BALANCE"] = "condition-balance";
|
|
13
|
+
MktCampaignConditionTypeValue["EARNING"] = "condition-earning";
|
|
14
|
+
MktCampaignConditionTypeValue["NFT"] = "condition-nft";
|
|
15
|
+
MktCampaignConditionTypeValue["CROWDLOAN"] = "condition-crowdloan";
|
|
16
|
+
MktCampaignConditionTypeValue["HAS_MONEY"] = "condition-has-money";
|
|
17
|
+
})(MktCampaignConditionTypeValue || (exports.MktCampaignConditionTypeValue = MktCampaignConditionTypeValue = {}));
|