@subwallet/extension-base 1.1.53-beta.0 → 1.1.54-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/background/KoniTypes.d.ts +1 -9
- package/background/KoniTypes.js +0 -1
- package/background/errors/TransactionError.js +0 -9
- package/cjs/background/KoniTypes.js +0 -1
- package/cjs/background/errors/TransactionError.js +0 -9
- package/cjs/koni/api/staking/bonding/utils.js +6 -35
- package/cjs/koni/background/handlers/Extension.js +0 -85
- package/cjs/koni/background/handlers/State.js +5 -25
- package/cjs/packageInfo.js +1 -1
- package/cjs/services/balance-service/index.js +3 -6
- package/cjs/services/chain-service/constants.js +1 -1
- package/cjs/services/chain-service/index.js +18 -43
- package/cjs/services/chain-service/utils/index.js +2 -7
- package/cjs/services/chain-service/utils/patch.js +1 -1
- package/cjs/services/earning-service/constants/chains.js +2 -4
- package/cjs/services/earning-service/handlers/native-staking/astar.js +3 -4
- package/cjs/services/earning-service/handlers/native-staking/relay-chain.js +3 -22
- package/cjs/services/transaction-service/index.js +0 -20
- package/cjs/services/transaction-service/utils.js +0 -6
- package/cjs/types/fee/index.js +1 -27
- package/cjs/utils/index.js +0 -12
- package/koni/api/staking/bonding/utils.d.ts +1 -3
- package/koni/api/staking/bonding/utils.js +6 -32
- package/koni/background/handlers/Extension.d.ts +0 -5
- package/koni/background/handlers/Extension.js +0 -85
- package/koni/background/handlers/State.d.ts +0 -2
- package/koni/background/handlers/State.js +6 -26
- package/package.json +7 -64
- package/packageInfo.js +1 -1
- package/services/balance-service/index.js +3 -6
- package/services/base/types.d.ts +0 -4
- package/services/chain-service/constants.js +1 -1
- package/services/chain-service/index.d.ts +0 -4
- package/services/chain-service/index.js +0 -24
- package/services/chain-service/utils/index.d.ts +5 -6
- package/services/chain-service/utils/index.js +2 -5
- package/services/chain-service/utils/patch.js +1 -1
- package/services/earning-service/constants/chains.d.ts +0 -1
- package/services/earning-service/constants/chains.js +0 -1
- package/services/earning-service/handlers/native-staking/astar.js +3 -4
- package/services/earning-service/handlers/native-staking/relay-chain.js +5 -24
- package/services/event-service/types.d.ts +0 -1
- package/services/transaction-service/index.js +0 -20
- package/services/transaction-service/utils.d.ts +0 -2
- package/services/transaction-service/utils.js +2 -6
- package/types/fee/index.d.ts +49 -2
- package/types/fee/index.js +1 -5
- package/types/yield/info/chain/target.d.ts +0 -2
- package/types/yield/info/pallet.d.ts +0 -4
- package/utils/index.d.ts +0 -1
- package/utils/index.js +1 -2
- package/background/errors/SwapError.d.ts +0 -6
- package/background/errors/SwapError.js +0 -57
- package/cjs/background/errors/SwapError.js +0 -64
- package/cjs/koni/api/dotsama/balance.js +0 -464
- package/cjs/koni/api/nft/ordinal_nft/utils.js +0 -41
- package/cjs/services/balance-service/helpers/subscribe/balance.js +0 -140
- package/cjs/services/chain-service/utils.js +0 -506
- package/cjs/services/migration-service/scripts/MigrateEthProvider.js +0 -17
- package/cjs/services/migration-service/scripts/MigratePioneerProvider.js +0 -17
- package/cjs/services/migration-service/scripts/MigrateProvider.js +0 -29
- package/cjs/services/storage-service/index.js +0 -241
- package/cjs/services/swap-service/handler/base-handler.js +0 -171
- package/cjs/services/swap-service/handler/chainflip-handler.js +0 -407
- package/cjs/services/swap-service/handler/hydradx-handler.js +0 -444
- package/cjs/services/swap-service/index.js +0 -249
- package/cjs/services/swap-service/utils.js +0 -126
- package/cjs/types/balance.js +0 -1
- package/cjs/types/fee/evm.js +0 -1
- package/cjs/types/fee/fee.js +0 -70
- package/cjs/types/service-base.js +0 -1
- package/cjs/types/swap/index.js +0 -50
- package/cjs/types.js +0 -1
- package/cjs/utils/address.js +0 -34
- package/cjs/utils/keyring.js +0 -57
- package/cjs/utils/swap.js +0 -78
- package/services/swap-service/handler/base-handler.d.ts +0 -38
- package/services/swap-service/handler/base-handler.js +0 -162
- package/services/swap-service/handler/chainflip-handler.d.ts +0 -30
- package/services/swap-service/handler/chainflip-handler.js +0 -399
- package/services/swap-service/handler/hydradx-handler.d.ts +0 -36
- package/services/swap-service/handler/hydradx-handler.js +0 -435
- package/services/swap-service/index.d.ts +0 -32
- package/services/swap-service/index.js +0 -240
- package/services/swap-service/utils.d.ts +0 -18
- package/services/swap-service/utils.js +0 -105
- package/types/fee/evm.d.ts +0 -49
- package/types/fee/evm.js +0 -1
- package/types/fee/fee.d.ts +0 -32
- package/types/fee/fee.js +0 -63
- package/types/service-base.d.ts +0 -10
- package/types/service-base.js +0 -1
- package/types/swap/index.d.ts +0 -166
- package/types/swap/index.js +0 -41
- package/utils/swap.d.ts +0 -3
- package/utils/swap.js +0 -70
|
@@ -1,241 +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.DatabaseService = void 0;
|
|
8
|
-
var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
|
|
9
|
-
var _databases = _interopRequireDefault(require("@subwallet/extension-base/services/storage-service/databases"));
|
|
10
|
-
var _dbStores = require("@subwallet/extension-base/services/storage-service/db-stores");
|
|
11
|
-
var _ChainStakingMetadata = _interopRequireDefault(require("@subwallet/extension-base/services/storage-service/db-stores/ChainStakingMetadata"));
|
|
12
|
-
var _NominatorMetadata = _interopRequireDefault(require("@subwallet/extension-base/services/storage-service/db-stores/NominatorMetadata"));
|
|
13
|
-
var _utils = require("@subwallet/extension-base/utils");
|
|
14
|
-
var _util = require("@polkadot/util");
|
|
15
|
-
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
16
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
-
|
|
18
|
-
class DatabaseService {
|
|
19
|
-
// TODO: might remove this
|
|
20
|
-
|
|
21
|
-
constructor(eventService) {
|
|
22
|
-
this.eventService = eventService;
|
|
23
|
-
this.logger = (0, _util.logger)('DB-Service');
|
|
24
|
-
this._db = new _databases.default();
|
|
25
|
-
this._db.on('ready', () => {
|
|
26
|
-
this.eventService.emit('database.ready', true);
|
|
27
|
-
});
|
|
28
|
-
this.stores = {
|
|
29
|
-
price: new _dbStores.PriceStore(this._db.price),
|
|
30
|
-
balance: new _dbStores.BalanceStore(this._db.balances),
|
|
31
|
-
nft: new _dbStores.NftStore(this._db.nfts),
|
|
32
|
-
nftCollection: new _dbStores.NftCollectionStore(this._db.nftCollections),
|
|
33
|
-
crowdloan: new _dbStores.CrowdloanStore(this._db.crowdloans),
|
|
34
|
-
staking: new _dbStores.StakingStore(this._db.stakings),
|
|
35
|
-
transaction: new _dbStores.TransactionStore(this._db.transactions),
|
|
36
|
-
migration: new _dbStores.MigrationStore(this._db.migrations),
|
|
37
|
-
metadata: new _dbStores.MetadataStore(this._db.metadata),
|
|
38
|
-
chain: new _dbStores.ChainStore(this._db.chain),
|
|
39
|
-
asset: new _dbStores.AssetStore(this._db.asset),
|
|
40
|
-
// staking
|
|
41
|
-
chainStakingMetadata: new _ChainStakingMetadata.default(this._db.chainStakingMetadata),
|
|
42
|
-
nominatorMetadata: new _NominatorMetadata.default(this._db.nominatorMetadata)
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
async updatePriceStore(priceData) {
|
|
46
|
-
await this.stores.price.table.put(priceData);
|
|
47
|
-
}
|
|
48
|
-
async getPriceStore() {
|
|
49
|
-
try {
|
|
50
|
-
const rs = await this.stores.price.table.get('usd');
|
|
51
|
-
return rs;
|
|
52
|
-
} catch (e) {
|
|
53
|
-
this.logger.error(e);
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Balance
|
|
59
|
-
async getStoredBalance() {
|
|
60
|
-
return this.stores.balance.table.toArray();
|
|
61
|
-
}
|
|
62
|
-
async updateBalanceStore(address, item) {
|
|
63
|
-
if (item.state === _KoniTypes.APIItemState.READY) {
|
|
64
|
-
return this.stores.balance.upsert({
|
|
65
|
-
address,
|
|
66
|
-
...item
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
async removeFromBalanceStore(assets) {
|
|
71
|
-
return this.stores.balance.removeBySlugs(assets);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Crowdloan
|
|
75
|
-
async updateCrowdloanStore(chain, address, item) {
|
|
76
|
-
if (item.state === _KoniTypes.APIItemState.READY && item.contribute !== '0') {
|
|
77
|
-
return this.stores.crowdloan.upsert({
|
|
78
|
-
chain,
|
|
79
|
-
address,
|
|
80
|
-
...item
|
|
81
|
-
});
|
|
82
|
-
} else {
|
|
83
|
-
return this.stores.crowdloan.deleteByChainAndAddress(chain, address);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Staking
|
|
88
|
-
async updateStaking(chain, address, item) {
|
|
89
|
-
if (item.state === _KoniTypes.APIItemState.READY) {
|
|
90
|
-
return this.stores.staking.upsert(item);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
async getStakings(addresses, chains) {
|
|
94
|
-
return this.stores.staking.getStakings(addresses, chains);
|
|
95
|
-
}
|
|
96
|
-
async getStakingsByChains(chains) {
|
|
97
|
-
return this.stores.staking.getStakingsByChains(chains);
|
|
98
|
-
}
|
|
99
|
-
async getPooledStakings(addresses, chainHashes) {
|
|
100
|
-
return this.stores.staking.getPooledStakings(addresses, chainHashes);
|
|
101
|
-
}
|
|
102
|
-
subscribeStaking(addresses, chainList, callback) {
|
|
103
|
-
this.stakingSubscription && this.stakingSubscription.unsubscribe();
|
|
104
|
-
this.stakingSubscription = this.stores.staking.subscribeStaking(addresses, chainList).subscribe({
|
|
105
|
-
next: stakings => callback && callback(stakings)
|
|
106
|
-
});
|
|
107
|
-
return this.stakingSubscription;
|
|
108
|
-
}
|
|
109
|
-
subscribeChainStakingMetadata(chains, callback) {
|
|
110
|
-
this.stores.chainStakingMetadata.subscribeByChain(chains).subscribe({
|
|
111
|
-
next: data => callback && callback(data)
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
subscribeNominatorMetadata(addresses, callback) {
|
|
115
|
-
return this.stores.nominatorMetadata.subscribeByAddresses(addresses).subscribe({
|
|
116
|
-
next: data => callback && callback(data)
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// Transaction histories
|
|
121
|
-
async getHistories(query) {
|
|
122
|
-
return this.stores.transaction.queryHistory(query);
|
|
123
|
-
}
|
|
124
|
-
async upsertHistory(histories) {
|
|
125
|
-
const cleanedHistory = histories.filter(x => x && x.address && x.chain && x.extrinsicHash);
|
|
126
|
-
return this.stores.transaction.bulkUpsert(cleanedHistory);
|
|
127
|
-
}
|
|
128
|
-
async updateHistoryByExtrinsicHash(extrinsicHash, updateData) {
|
|
129
|
-
const canUpdate = updateData && extrinsicHash;
|
|
130
|
-
if (!canUpdate) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
return this.stores.transaction.updateWithQuery({
|
|
134
|
-
extrinsicHash
|
|
135
|
-
}, updateData);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// NFT Collection
|
|
139
|
-
async addNftCollection(collection) {
|
|
140
|
-
return this.stores.nftCollection.upsert(collection);
|
|
141
|
-
}
|
|
142
|
-
async deleteNftCollection(chain, collectionId) {
|
|
143
|
-
await this.stores.nftCollection.removeCollection(chain, collectionId);
|
|
144
|
-
await this.stores.nft.deleteNftsByCollection(chain, collectionId);
|
|
145
|
-
}
|
|
146
|
-
getAllNftCollection(chainHashes) {
|
|
147
|
-
return this.stores.nftCollection.getNftCollection(chainHashes);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// NFT
|
|
151
|
-
subscribeNft(addresses, chainHashes, callback) {
|
|
152
|
-
this.nftSubscription && this.nftSubscription.unsubscribe();
|
|
153
|
-
this.nftSubscription = this.stores.nft.subscribeNft(addresses, chainHashes).subscribe({
|
|
154
|
-
next: nfts => callback && callback(nfts)
|
|
155
|
-
});
|
|
156
|
-
return this.nftSubscription;
|
|
157
|
-
}
|
|
158
|
-
async cleanUpNft(chain, owner, collectionIds, nftIds, ownNothing) {
|
|
159
|
-
if (ownNothing) {
|
|
160
|
-
return this.stores.nft.deleteNftsByChainAndOwner(chain, (0, _utils.reformatAddress)(owner, 42), collectionIds);
|
|
161
|
-
}
|
|
162
|
-
return this.stores.nft.cleanUpNfts(chain, (0, _utils.reformatAddress)(owner, 42), collectionIds, nftIds);
|
|
163
|
-
}
|
|
164
|
-
async getNft(addresses, chainHashes) {
|
|
165
|
-
return this.stores.nft.getNft(addresses, chainHashes);
|
|
166
|
-
}
|
|
167
|
-
async addNft(address, nft) {
|
|
168
|
-
return this.stores.nft.upsert({
|
|
169
|
-
...nft,
|
|
170
|
-
address
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
handleNftTransfer(chain, addresses, nftItem) {
|
|
174
|
-
return this.stores.nft.deleteNftItem(chain, addresses, nftItem);
|
|
175
|
-
}
|
|
176
|
-
removeNfts(chain, address, collectionId, nftIds) {
|
|
177
|
-
return this.stores.nft.removeNfts(chain, address, collectionId, nftIds);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Chain
|
|
181
|
-
async updateChainStore(item) {
|
|
182
|
-
return this.stores.chain.upsert(item);
|
|
183
|
-
}
|
|
184
|
-
async bulkUpdateChainStore(data) {
|
|
185
|
-
return this.stores.chain.bulkUpsert(data);
|
|
186
|
-
}
|
|
187
|
-
async removeFromChainStore(chains) {
|
|
188
|
-
return this.stores.chain.removeChains(chains);
|
|
189
|
-
}
|
|
190
|
-
async getAllChainStore() {
|
|
191
|
-
return this.stores.chain.getAll();
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Asset
|
|
195
|
-
async updateAssetStore(item) {
|
|
196
|
-
return this.stores.asset.upsert(item);
|
|
197
|
-
}
|
|
198
|
-
async getAllAssetStore() {
|
|
199
|
-
return this.stores.asset.getAll();
|
|
200
|
-
}
|
|
201
|
-
async removeFromAssetStore(items) {
|
|
202
|
-
return this.stores.asset.removeAssets(items);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// Staking
|
|
206
|
-
async updateChainStakingMetadata(item, changes) {
|
|
207
|
-
const existingRecord = await this.stores.chainStakingMetadata.getByChainAndType(item.chain, item.type);
|
|
208
|
-
if (existingRecord && changes) {
|
|
209
|
-
return this.stores.chainStakingMetadata.updateByChainAndType(item.chain, item.type, changes);
|
|
210
|
-
}
|
|
211
|
-
return this.stores.chainStakingMetadata.upsert(item);
|
|
212
|
-
}
|
|
213
|
-
async getChainStakingMetadata() {
|
|
214
|
-
return this.stores.chainStakingMetadata.getAll();
|
|
215
|
-
}
|
|
216
|
-
async getStakingMetadataByChain(chain) {
|
|
217
|
-
let type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _KoniTypes.StakingType.NOMINATED;
|
|
218
|
-
return this.stores.chainStakingMetadata.getByChainAndType(chain, type);
|
|
219
|
-
}
|
|
220
|
-
async updateNominatorMetadata(item) {
|
|
221
|
-
return this.stores.nominatorMetadata.upsert(item);
|
|
222
|
-
}
|
|
223
|
-
async getNominatorMetadata() {
|
|
224
|
-
return this.stores.nominatorMetadata.getAll();
|
|
225
|
-
}
|
|
226
|
-
async resetWallet(resetAll) {
|
|
227
|
-
return new Promise((resolve, reject) => {
|
|
228
|
-
const stores = [this.stores.balance, this.stores.nft, this.stores.nftCollection, this.stores.crowdloan, this.stores.staking, this.stores.transaction, this.stores.nominatorMetadata];
|
|
229
|
-
if (resetAll) {
|
|
230
|
-
stores.push(this.stores.chain, this.stores.asset);
|
|
231
|
-
}
|
|
232
|
-
const promises = stores.map(store => store.clear());
|
|
233
|
-
Promise.all(promises).then(() => {
|
|
234
|
-
resolve();
|
|
235
|
-
}).catch(e => {
|
|
236
|
-
reject(e);
|
|
237
|
-
});
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
exports.DatabaseService = DatabaseService;
|
|
@@ -1,171 +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.SwapBaseHandler = void 0;
|
|
8
|
-
var _TransactionError = require("@subwallet/extension-base/background/errors/TransactionError");
|
|
9
|
-
var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
|
|
10
|
-
var _utils = require("@subwallet/extension-base/services/chain-service/utils");
|
|
11
|
-
var _utils2 = require("@subwallet/extension-base/services/swap-service/utils");
|
|
12
|
-
var _swap = require("@subwallet/extension-base/types/swap");
|
|
13
|
-
var _utils3 = require("@subwallet/extension-base/utils");
|
|
14
|
-
var _bignumber = _interopRequireDefault(require("bignumber.js"));
|
|
15
|
-
var _i18next = require("i18next");
|
|
16
|
-
var _utilCrypto = require("@polkadot/util-crypto");
|
|
17
|
-
// Copyright 2019-2022 @subwallet/extension-base
|
|
18
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
19
|
-
|
|
20
|
-
class SwapBaseHandler {
|
|
21
|
-
constructor(_ref) {
|
|
22
|
-
let {
|
|
23
|
-
balanceService,
|
|
24
|
-
chainService,
|
|
25
|
-
providerName,
|
|
26
|
-
providerSlug
|
|
27
|
-
} = _ref;
|
|
28
|
-
this.providerName = providerName;
|
|
29
|
-
this.providerSlug = providerSlug;
|
|
30
|
-
this.chainService = chainService;
|
|
31
|
-
this.balanceService = balanceService;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// public abstract getSwapQuote(request: SwapRequest): Promise<SwapQuote | SwapError>;
|
|
35
|
-
async generateOptimalProcess(params, genStepFuncList) {
|
|
36
|
-
const result = {
|
|
37
|
-
totalFee: [_utils2.MOCK_SWAP_FEE],
|
|
38
|
-
steps: [_utils2.DEFAULT_SWAP_FIRST_STEP]
|
|
39
|
-
};
|
|
40
|
-
try {
|
|
41
|
-
for (const genStepFunc of genStepFuncList) {
|
|
42
|
-
const step = await genStepFunc.bind(this, params)();
|
|
43
|
-
if (step) {
|
|
44
|
-
result.steps.push({
|
|
45
|
-
id: result.steps.length,
|
|
46
|
-
...step[0]
|
|
47
|
-
});
|
|
48
|
-
result.totalFee.push(step[1]);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return result;
|
|
52
|
-
} catch (e) {
|
|
53
|
-
return result;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
async validateXcmStep(params, stepIndex) {
|
|
57
|
-
const bnAmount = new _bignumber.default(params.selectedQuote.fromAmount);
|
|
58
|
-
const swapPair = params.selectedQuote.pair;
|
|
59
|
-
const alternativeAssetSlug = (0, _utils2.getSwapAlternativeAsset)(swapPair);
|
|
60
|
-
if (!alternativeAssetSlug) {
|
|
61
|
-
return [new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.INTERNAL_ERROR)];
|
|
62
|
-
}
|
|
63
|
-
const alternativeAsset = this.chainService.getAssetBySlug(alternativeAssetSlug);
|
|
64
|
-
const fromAsset = this.chainService.getAssetBySlug(swapPair.from);
|
|
65
|
-
const [alternativeAssetBalance, fromAssetBalance] = await Promise.all([this.balanceService.getTokenFreeBalance(params.address, alternativeAsset.originChain, alternativeAssetSlug), this.balanceService.getTokenFreeBalance(params.address, fromAsset.originChain, fromAsset.slug)]);
|
|
66
|
-
const bnAlternativeAssetBalance = new _bignumber.default(alternativeAssetBalance.value);
|
|
67
|
-
const bnFromAssetBalance = new _bignumber.default(fromAssetBalance.value);
|
|
68
|
-
const xcmFeeComponent = params.process.totalFee[stepIndex].feeComponent[0]; // todo: can do better than indexing
|
|
69
|
-
const xcmFee = new _bignumber.default(xcmFeeComponent.amount || '0');
|
|
70
|
-
let xcmAmount = bnAmount.minus(bnFromAssetBalance);
|
|
71
|
-
if ((0, _utils._isNativeToken)(alternativeAsset)) {
|
|
72
|
-
xcmAmount = xcmAmount.plus(xcmFee);
|
|
73
|
-
}
|
|
74
|
-
const alternativeTokenMinAmount = new _bignumber.default(alternativeAsset.minAmount || '0');
|
|
75
|
-
if (!bnAlternativeAssetBalance.minus(xcmAmount).gte(alternativeTokenMinAmount)) {
|
|
76
|
-
const maxBn = bnFromAssetBalance.plus(new _bignumber.default(alternativeAssetBalance.value)).minus(xcmFee).minus(alternativeTokenMinAmount);
|
|
77
|
-
const maxValue = (0, _utils3.formatNumber)(maxBn.toString(), fromAsset.decimals || 0);
|
|
78
|
-
const altInputTokenInfo = this.chainService.getAssetBySlug(alternativeAssetSlug);
|
|
79
|
-
const symbol = altInputTokenInfo.symbol;
|
|
80
|
-
const alternativeChain = this.chainService.getChainInfoByKey(altInputTokenInfo.originChain);
|
|
81
|
-
const chain = this.chainService.getChainInfoByKey(fromAsset.originChain);
|
|
82
|
-
const inputNetworkName = chain.name;
|
|
83
|
-
const altNetworkName = alternativeChain.name;
|
|
84
|
-
const currentValue = (0, _utils3.formatNumber)(bnFromAssetBalance.toString(), fromAsset.decimals || 0);
|
|
85
|
-
const bnMaxXCM = new _bignumber.default(alternativeAssetBalance.value).minus(xcmFee).minus(alternativeTokenMinAmount);
|
|
86
|
-
const maxXCMValue = (0, _utils3.formatNumber)(bnMaxXCM.toString(), fromAsset.decimals || 0);
|
|
87
|
-
if (maxBn.lte(0) || bnFromAssetBalance.lte(0) || bnMaxXCM.lte(0)) {
|
|
88
|
-
return [new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.NOT_ENOUGH_BALANCE, (0, _i18next.t)(`Insufficient balance. Deposit ${fromAsset.symbol} and try again.`))];
|
|
89
|
-
}
|
|
90
|
-
return [new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.NOT_ENOUGH_BALANCE, (0, _i18next.t)('You can only enter a maximum of {{maxValue}} {{symbol}}, which is {{currentValue}} {{symbol}} ({{inputNetworkName}}) and {{maxXCMValue}} {{symbol}} ({{altNetworkName}}). Lower your amount and try again.', {
|
|
91
|
-
replace: {
|
|
92
|
-
symbol,
|
|
93
|
-
maxValue,
|
|
94
|
-
inputNetworkName,
|
|
95
|
-
altNetworkName,
|
|
96
|
-
currentValue,
|
|
97
|
-
maxXCMValue
|
|
98
|
-
}
|
|
99
|
-
}))];
|
|
100
|
-
}
|
|
101
|
-
return [];
|
|
102
|
-
}
|
|
103
|
-
async validateTokenApproveStep(params, stepIndex) {
|
|
104
|
-
return Promise.resolve([]);
|
|
105
|
-
}
|
|
106
|
-
async validateSetFeeTokenStep(params, stepIndex) {
|
|
107
|
-
return Promise.resolve([]);
|
|
108
|
-
}
|
|
109
|
-
async validateSwapStep(params, isXcmOk, stepIndex) {
|
|
110
|
-
if (!params.selectedQuote) {
|
|
111
|
-
return Promise.resolve([new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.INTERNAL_ERROR)]);
|
|
112
|
-
}
|
|
113
|
-
const selectedQuote = params.selectedQuote;
|
|
114
|
-
const currentTimestamp = +Date.now();
|
|
115
|
-
if (selectedQuote.aliveUntil <= currentTimestamp) {
|
|
116
|
-
return Promise.resolve([new _TransactionError.TransactionError(_swap.SwapErrorType.QUOTE_TIMEOUT)]);
|
|
117
|
-
}
|
|
118
|
-
const bnAmount = new _bignumber.default(params.selectedQuote.fromAmount);
|
|
119
|
-
const fromAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.from);
|
|
120
|
-
const stepFee = params.process.totalFee[stepIndex].feeComponent;
|
|
121
|
-
const networkFee = stepFee.find(fee => fee.feeType === _swap.SwapFeeType.NETWORK_FEE);
|
|
122
|
-
if (!networkFee) {
|
|
123
|
-
return Promise.resolve([new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.INTERNAL_ERROR)]);
|
|
124
|
-
}
|
|
125
|
-
const feeTokenInfo = this.chainService.getAssetBySlug(networkFee.tokenSlug);
|
|
126
|
-
const feeTokenChain = this.chainService.getChainInfoByKey(feeTokenInfo.originChain);
|
|
127
|
-
const [feeTokenBalance, fromAssetBalance] = await Promise.all([this.balanceService.getTokenFreeBalance(params.address, feeTokenInfo.originChain, feeTokenInfo.slug), this.balanceService.getTokenFreeBalance(params.address, fromAsset.originChain, fromAsset.slug)]);
|
|
128
|
-
const bnFeeTokenBalance = new _bignumber.default(feeTokenBalance.value);
|
|
129
|
-
const bnFromAssetBalance = new _bignumber.default(fromAssetBalance.value);
|
|
130
|
-
if (bnFeeTokenBalance.lte(new _bignumber.default(networkFee.amount))) {
|
|
131
|
-
return Promise.resolve([new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.NOT_ENOUGH_BALANCE, `You don't have enough ${feeTokenInfo.symbol} (${feeTokenChain.name}) to pay transaction fee`)]);
|
|
132
|
-
}
|
|
133
|
-
if (params.selectedQuote.minSwap) {
|
|
134
|
-
const minProtocolSwap = new _bignumber.default(params.selectedQuote.minSwap);
|
|
135
|
-
if (!isXcmOk && bnFromAssetBalance.lte(minProtocolSwap)) {
|
|
136
|
-
const parsedMinSwapValue = (0, _utils3.formatNumber)(minProtocolSwap, (0, _utils._getAssetDecimals)(fromAsset));
|
|
137
|
-
return Promise.resolve([new _TransactionError.TransactionError(_swap.SwapErrorType.SWAP_NOT_ENOUGH_BALANCE, `Insufficient balance. You need more than ${parsedMinSwapValue} ${fromAsset.symbol} to start swapping. Deposit ${fromAsset.symbol} and try again.`)]); // todo: min swap or amount?
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const bnSrcAssetMinAmount = new _bignumber.default((0, _utils._getTokenMinAmount)(fromAsset));
|
|
142
|
-
const bnMaxBalanceSwap = bnFromAssetBalance.minus(bnSrcAssetMinAmount);
|
|
143
|
-
if (!isXcmOk && bnAmount.gte(bnMaxBalanceSwap)) {
|
|
144
|
-
const parsedMaxBalanceSwap = (0, _utils3.formatNumber)(bnMaxBalanceSwap, (0, _utils._getAssetDecimals)(fromAsset));
|
|
145
|
-
return Promise.resolve([new _TransactionError.TransactionError(_swap.SwapErrorType.SWAP_EXCEED_ALLOWANCE, `Amount too high. Lower your amount ${bnMaxBalanceSwap.gt(0) ? `below ${parsedMaxBalanceSwap} ${fromAsset.symbol}` : ''} and try again`)]);
|
|
146
|
-
}
|
|
147
|
-
if (params.recipient) {
|
|
148
|
-
const toAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.to);
|
|
149
|
-
const destChainInfo = this.chainService.getChainInfoByKey(toAsset.originChain);
|
|
150
|
-
const isEvmAddress = (0, _utilCrypto.isEthereumAddress)(params.recipient);
|
|
151
|
-
const isEvmDestChain = (0, _utils._isChainEvmCompatible)(destChainInfo);
|
|
152
|
-
if (isEvmAddress && !isEvmDestChain || !isEvmAddress && isEvmDestChain) {
|
|
153
|
-
return Promise.resolve([new _TransactionError.TransactionError(_swap.SwapErrorType.INVALID_RECIPIENT)]);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return Promise.resolve([]);
|
|
157
|
-
}
|
|
158
|
-
get name() {
|
|
159
|
-
return this.providerName;
|
|
160
|
-
}
|
|
161
|
-
get slug() {
|
|
162
|
-
return this.providerSlug;
|
|
163
|
-
}
|
|
164
|
-
get providerInfo() {
|
|
165
|
-
return {
|
|
166
|
-
id: this.providerSlug,
|
|
167
|
-
name: this.providerName
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
exports.SwapBaseHandler = SwapBaseHandler;
|