@imtbl/sdk 0.0.1-alpha.2 → 0.1.0-alpha
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/README.md +28 -0
- package/dist/checkout_sdk.d.ts +293 -2
- package/dist/checkout_sdk.js +283 -142
- package/dist/checkout_widgets.d.ts +22 -6
- package/dist/checkout_widgets.js +32 -12
- package/dist/config.d.ts +18 -0
- package/dist/config.js +15 -0
- package/dist/{starkex.d.ts → immutablex_client.d.ts} +2571 -720
- package/dist/immutablex_client.js +510 -0
- package/dist/index.cjs +2547 -1872
- package/dist/index.d.ts +2490 -620
- package/dist/index.js +2531 -1862
- package/dist/passport.d.ts +45 -16
- package/dist/passport.js +128 -99
- package/dist/provider.d.ts +36 -20
- package/dist/provider.js +102 -91
- package/package.json +23 -15
- package/dist/base.d.ts +0 -123
- package/dist/base.js +0 -27
- package/dist/browser/checkout.js +0 -2
- package/dist/starkex.js +0 -10
package/dist/provider.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { Config, TransfersApi, OrdersApi, UsersApi, Contracts, TradesApi, ExchangesApi, WithdrawalsApi, MintsApi, DepositsApi, EncodingApi, TokensApi } from '@imtbl/core-sdk';
|
|
1
2
|
import BN from 'bn.js';
|
|
2
3
|
import * as encUtils from 'enc-utils';
|
|
3
|
-
import { Config, TransfersApi, OrdersApi, UsersApi, Contracts, TradesApi, ExchangesApi, WithdrawalsApi, MintsApi, DepositsApi, EncodingApi, TokensApi } from '@imtbl/core-sdk';
|
|
4
4
|
import { parseUnits } from '@ethersproject/units';
|
|
5
5
|
import { BigNumber } from '@ethersproject/bignumber';
|
|
6
6
|
import { ethers } from 'ethers';
|
|
@@ -78,18 +78,9 @@ function convertToSignableToken(token) {
|
|
|
78
78
|
|
|
79
79
|
var Environment;
|
|
80
80
|
(function (Environment) {
|
|
81
|
-
Environment["DEVELOPMENT"] = "development";
|
|
82
|
-
Environment["SANDBOX"] = "sandbox";
|
|
83
81
|
Environment["PRODUCTION"] = "production";
|
|
82
|
+
Environment["SANDBOX"] = "sandbox";
|
|
84
83
|
})(Environment || (Environment = {}));
|
|
85
|
-
({
|
|
86
|
-
...Config.PRODUCTION,
|
|
87
|
-
env: Environment.PRODUCTION,
|
|
88
|
-
});
|
|
89
|
-
({
|
|
90
|
-
...Config.SANDBOX,
|
|
91
|
-
env: Environment.SANDBOX,
|
|
92
|
-
});
|
|
93
84
|
|
|
94
85
|
function isChainValid(chainID, config) {
|
|
95
86
|
return chainID === config.ethConfiguration.chainID;
|
|
@@ -100,10 +91,10 @@ async function validateChain(signer, config) {
|
|
|
100
91
|
throw new Error('The wallet used for this operation is not connected to the correct network.');
|
|
101
92
|
}
|
|
102
93
|
|
|
103
|
-
async function transfer({ signers: { ethSigner,
|
|
104
|
-
await validateChain(ethSigner, config.
|
|
94
|
+
async function transfer({ signers: { ethSigner, starkSigner }, request, config, }) {
|
|
95
|
+
await validateChain(ethSigner, config.immutableXConfig);
|
|
105
96
|
const ethAddress = await ethSigner.getAddress();
|
|
106
|
-
const transfersApi = new TransfersApi(config.
|
|
97
|
+
const transfersApi = new TransfersApi(config.immutableXConfig.apiConfiguration);
|
|
107
98
|
const transferAmount = request.type === 'ERC721' ? '1' : request.amount;
|
|
108
99
|
const signableResult = await transfersApi.getSignableTransferV1({
|
|
109
100
|
getSignableTransferRequest: {
|
|
@@ -115,7 +106,7 @@ async function transfer({ signers: { ethSigner, starkExSigner }, request, config
|
|
|
115
106
|
});
|
|
116
107
|
const { signable_message: signableMessage, payload_hash: payloadHash } = signableResult.data;
|
|
117
108
|
const ethSignature = await signRaw(signableMessage, ethSigner);
|
|
118
|
-
const starkSignature = await
|
|
109
|
+
const starkSignature = await starkSigner.signMessage(payloadHash);
|
|
119
110
|
const transferSigningParams = {
|
|
120
111
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
121
112
|
sender_stark_key: signableResult.data.sender_stark_key,
|
|
@@ -140,10 +131,10 @@ async function transfer({ signers: { ethSigner, starkExSigner }, request, config
|
|
|
140
131
|
transfer_id: response?.data.transfer_id,
|
|
141
132
|
};
|
|
142
133
|
}
|
|
143
|
-
async function batchTransfer({ signers: { ethSigner,
|
|
144
|
-
await validateChain(ethSigner, config.
|
|
134
|
+
async function batchTransfer({ signers: { ethSigner, starkSigner }, request, config, }) {
|
|
135
|
+
await validateChain(ethSigner, config.immutableXConfig);
|
|
145
136
|
const ethAddress = await ethSigner.getAddress();
|
|
146
|
-
const transfersApi = new TransfersApi(config.
|
|
137
|
+
const transfersApi = new TransfersApi(config.immutableXConfig.apiConfiguration);
|
|
147
138
|
const signableRequests = request.map((nftTransfer) => {
|
|
148
139
|
return {
|
|
149
140
|
amount: '1',
|
|
@@ -168,7 +159,7 @@ async function batchTransfer({ signers: { ethSigner, starkExSigner }, request, c
|
|
|
168
159
|
const ethSignature = await signRaw(signableMessage, ethSigner);
|
|
169
160
|
const requests = [];
|
|
170
161
|
for (const resp of signableResult.data.signable_responses) {
|
|
171
|
-
const starkSignature = await
|
|
162
|
+
const starkSignature = await starkSigner.signMessage(resp.payload_hash);
|
|
172
163
|
const req = {
|
|
173
164
|
sender_vault_id: resp.sender_vault_id,
|
|
174
165
|
receiver_stark_key: resp.receiver_stark_key,
|
|
@@ -197,9 +188,9 @@ async function batchTransfer({ signers: { ethSigner, starkExSigner }, request, c
|
|
|
197
188
|
}
|
|
198
189
|
|
|
199
190
|
async function createOrder({ signers, request, config, }) {
|
|
200
|
-
await validateChain(signers.ethSigner, config.
|
|
191
|
+
await validateChain(signers.ethSigner, config.immutableXConfig);
|
|
201
192
|
const ethAddress = await signers.ethSigner.getAddress();
|
|
202
|
-
const ordersApi = new OrdersApi(config.
|
|
193
|
+
const ordersApi = new OrdersApi(config.immutableXConfig.apiConfiguration);
|
|
203
194
|
const amountSell = request.sell.type === 'ERC721' ? '1' : request.sell.amount;
|
|
204
195
|
const amountBuy = request.buy.type === 'ERC721' ? '1' : request.buy.amount;
|
|
205
196
|
const getSignableOrderRequest = {
|
|
@@ -216,7 +207,7 @@ async function createOrder({ signers, request, config, }) {
|
|
|
216
207
|
});
|
|
217
208
|
const { signable_message: signableMessage, payload_hash: payloadHash } = getSignableOrderResponse.data;
|
|
218
209
|
const ethSignature = await signRaw(signableMessage, signers.ethSigner);
|
|
219
|
-
const starkSignature = await signers.
|
|
210
|
+
const starkSignature = await signers.starkSigner.signMessage(payloadHash);
|
|
220
211
|
const resp = getSignableOrderResponse.data;
|
|
221
212
|
const orderParams = {
|
|
222
213
|
createOrderRequest: {
|
|
@@ -242,7 +233,7 @@ async function createOrder({ signers, request, config, }) {
|
|
|
242
233
|
};
|
|
243
234
|
}
|
|
244
235
|
async function cancelOrder({ signers, request, config, }) {
|
|
245
|
-
const ordersApi = new OrdersApi(config.
|
|
236
|
+
const ordersApi = new OrdersApi(config.immutableXConfig.apiConfiguration);
|
|
246
237
|
const getSignableCancelOrderResponse = await ordersApi.getSignableCancelOrder({
|
|
247
238
|
getSignableCancelOrderRequest: {
|
|
248
239
|
order_id: request.order_id,
|
|
@@ -250,7 +241,7 @@ async function cancelOrder({ signers, request, config, }) {
|
|
|
250
241
|
});
|
|
251
242
|
const { signable_message: signableMessage, payload_hash: payloadHash } = getSignableCancelOrderResponse.data;
|
|
252
243
|
const ethSignature = await signRaw(signableMessage, signers.ethSigner);
|
|
253
|
-
const starkSignature = await signers.
|
|
244
|
+
const starkSignature = await signers.starkSigner.signMessage(payloadHash);
|
|
254
245
|
const ethAddress = await signers.ethSigner.getAddress();
|
|
255
246
|
const cancelOrderResponse = await ordersApi.cancelOrder({
|
|
256
247
|
id: request.order_id.toString(),
|
|
@@ -268,10 +259,10 @@ async function cancelOrder({ signers, request, config, }) {
|
|
|
268
259
|
}
|
|
269
260
|
|
|
270
261
|
async function registerOffchain(signers, config) {
|
|
271
|
-
await validateChain(signers.ethSigner, config.
|
|
272
|
-
const usersApi = new UsersApi(config.
|
|
262
|
+
await validateChain(signers.ethSigner, config.immutableXConfig);
|
|
263
|
+
const usersApi = new UsersApi(config.immutableXConfig.apiConfiguration);
|
|
273
264
|
const userAddress = await signers.ethSigner.getAddress();
|
|
274
|
-
const starkPublicKey = await signers.
|
|
265
|
+
const starkPublicKey = await signers.starkSigner.getAddress();
|
|
275
266
|
const signableResult = await usersApi.getSignableRegistrationOffchain({
|
|
276
267
|
getSignableRegistrationRequest: {
|
|
277
268
|
ether_key: userAddress,
|
|
@@ -280,7 +271,7 @@ async function registerOffchain(signers, config) {
|
|
|
280
271
|
});
|
|
281
272
|
const { signable_message: signableMessage, payload_hash: payloadHash } = signableResult.data;
|
|
282
273
|
const ethSignature = await signRaw(signableMessage, signers.ethSigner);
|
|
283
|
-
const starkSignature = await signers.
|
|
274
|
+
const starkSignature = await signers.starkSigner.signMessage(payloadHash);
|
|
284
275
|
const registeredUser = await usersApi.registerUser({
|
|
285
276
|
registerUserRequest: {
|
|
286
277
|
eth_signature: ethSignature,
|
|
@@ -292,8 +283,8 @@ async function registerOffchain(signers, config) {
|
|
|
292
283
|
return registeredUser.data;
|
|
293
284
|
}
|
|
294
285
|
async function isRegisteredOnChain(starkPublicKey, ethSigner, config) {
|
|
295
|
-
await validateChain(ethSigner, config.
|
|
296
|
-
const registrationContract = Contracts.Registration.connect(config.
|
|
286
|
+
await validateChain(ethSigner, config.immutableXConfig);
|
|
287
|
+
const registrationContract = Contracts.Registration.connect(config.immutableXConfig.ethConfiguration.registrationContractAddress, ethSigner);
|
|
297
288
|
try {
|
|
298
289
|
return await registrationContract.isRegistered(starkPublicKey);
|
|
299
290
|
}
|
|
@@ -323,7 +314,7 @@ const assertIsDefined = (value) => {
|
|
|
323
314
|
throw new Error('undefined field exception');
|
|
324
315
|
};
|
|
325
316
|
async function prepareWithdrawalAction(params) {
|
|
326
|
-
const { signers: { ethSigner,
|
|
317
|
+
const { signers: { ethSigner, starkSigner }, type, config, } = params;
|
|
327
318
|
await validateChain(ethSigner, params.config);
|
|
328
319
|
const withdrawalsApi = new WithdrawalsApi(config.apiConfiguration);
|
|
329
320
|
const withdrawalAmount = type === 'ERC721' ? '1' : params.amount;
|
|
@@ -335,7 +326,7 @@ async function prepareWithdrawalAction(params) {
|
|
|
335
326
|
},
|
|
336
327
|
});
|
|
337
328
|
const { signable_message: signableMessage, payload_hash: payloadHash } = signableWithdrawalResult.data;
|
|
338
|
-
const starkSignature = await
|
|
329
|
+
const starkSignature = await starkSigner.signMessage(payloadHash);
|
|
339
330
|
const { ethAddress, ethSignature } = await signMessage(signableMessage, ethSigner);
|
|
340
331
|
const prepareWithdrawalResponse = await withdrawalsApi.createWithdrawal({
|
|
341
332
|
createWithdrawalRequest: {
|
|
@@ -368,10 +359,10 @@ async function getEncodeAssetInfo(assetType, tokenType, config, tokenData) {
|
|
|
368
359
|
|
|
369
360
|
async function executeRegisterAndWithdrawERC20({ ethSigner, assetType, starkPublicKey, config, }) {
|
|
370
361
|
const etherKey = await ethSigner.getAddress();
|
|
371
|
-
const
|
|
372
|
-
const usersApi = new UsersApi(
|
|
362
|
+
const imxConfig = config.immutableXConfig;
|
|
363
|
+
const usersApi = new UsersApi(imxConfig.apiConfiguration);
|
|
373
364
|
const signableResult = await getSignableRegistrationOnchain(etherKey, starkPublicKey, usersApi);
|
|
374
|
-
const contract = Contracts.Registration.connect(config.
|
|
365
|
+
const contract = Contracts.Registration.connect(config.immutableXConfig.ethConfiguration.registrationContractAddress, ethSigner);
|
|
375
366
|
const populatedTransaction = await contract.populateTransaction.registerAndWithdraw(etherKey, starkPublicKey, signableResult.operator_signature, assetType);
|
|
376
367
|
return ethSigner.sendTransaction(populatedTransaction);
|
|
377
368
|
}
|
|
@@ -381,9 +372,9 @@ async function executeWithdrawERC20(ethSigner, assetType, starkPublicKey, config
|
|
|
381
372
|
return ethSigner.sendTransaction(populatedTransaction);
|
|
382
373
|
}
|
|
383
374
|
async function completeERC20WithdrawalAction({ ethSigner, starkPublicKey, token, config, }) {
|
|
384
|
-
await validateChain(ethSigner, config.
|
|
385
|
-
const
|
|
386
|
-
const assetType = await getEncodeAssetInfo('asset', 'ERC20',
|
|
375
|
+
await validateChain(ethSigner, config.immutableXConfig);
|
|
376
|
+
const imxConfig = config.immutableXConfig;
|
|
377
|
+
const assetType = await getEncodeAssetInfo('asset', 'ERC20', imxConfig, {
|
|
387
378
|
token_address: token.tokenAddress,
|
|
388
379
|
});
|
|
389
380
|
const isRegistered = await isRegisteredOnChain(starkPublicKey, ethSigner, config);
|
|
@@ -396,7 +387,7 @@ async function completeERC20WithdrawalAction({ ethSigner, starkPublicKey, token,
|
|
|
396
387
|
});
|
|
397
388
|
}
|
|
398
389
|
else {
|
|
399
|
-
return executeWithdrawERC20(ethSigner, assetType.asset_type, starkPublicKey,
|
|
390
|
+
return executeWithdrawERC20(ethSigner, assetType.asset_type, starkPublicKey, imxConfig);
|
|
400
391
|
}
|
|
401
392
|
}
|
|
402
393
|
|
|
@@ -419,8 +410,8 @@ function getMintingBlob(token) {
|
|
|
419
410
|
return encUtils.sanitizeHex(encUtils.utf8ToHex(`{${id}}:{${blueprint}}`));
|
|
420
411
|
}
|
|
421
412
|
async function completeMintableERC721Withdrawal(ethSigner, starkPublicKey, token, config) {
|
|
422
|
-
const
|
|
423
|
-
const assetType = await getEncodeAssetInfo('mintable-asset', 'ERC721',
|
|
413
|
+
const imxConfig = config.immutableXConfig;
|
|
414
|
+
const assetType = await getEncodeAssetInfo('mintable-asset', 'ERC721', imxConfig, {
|
|
424
415
|
id: token.data.id,
|
|
425
416
|
token_address: token.data.tokenAddress,
|
|
426
417
|
...(token.data.blueprint && { blueprint: token.data.blueprint }),
|
|
@@ -428,10 +419,10 @@ async function completeMintableERC721Withdrawal(ethSigner, starkPublicKey, token
|
|
|
428
419
|
const mintingBlob = getMintingBlob(token);
|
|
429
420
|
const isRegistered = await isRegisteredOnChain(starkPublicKey, ethSigner, config);
|
|
430
421
|
if (!isRegistered) {
|
|
431
|
-
return executeRegisterAndWithdrawMintableERC721(ethSigner, assetType.asset_type, starkPublicKey, mintingBlob,
|
|
422
|
+
return executeRegisterAndWithdrawMintableERC721(ethSigner, assetType.asset_type, starkPublicKey, mintingBlob, imxConfig);
|
|
432
423
|
}
|
|
433
424
|
else {
|
|
434
|
-
return executeWithdrawMintableERC721(ethSigner, assetType.asset_type, starkPublicKey, mintingBlob,
|
|
425
|
+
return executeWithdrawMintableERC721(ethSigner, assetType.asset_type, starkPublicKey, mintingBlob, imxConfig);
|
|
435
426
|
}
|
|
436
427
|
}
|
|
437
428
|
async function executeRegisterAndWithdrawERC721(ethSigner, assetType, starkPublicKey, tokenId, config) {
|
|
@@ -448,25 +439,25 @@ async function executeWithdrawERC721(ethSigner, assetType, starkPublicKey, token
|
|
|
448
439
|
return ethSigner.sendTransaction(populatedTransaction);
|
|
449
440
|
}
|
|
450
441
|
async function completeERC721Withdrawal(ethSigner, starkPublicKey, token, config) {
|
|
451
|
-
const
|
|
452
|
-
const assetType = await getEncodeAssetInfo('asset', 'ERC721',
|
|
442
|
+
const imxConfig = config.immutableXConfig;
|
|
443
|
+
const assetType = await getEncodeAssetInfo('asset', 'ERC721', imxConfig, {
|
|
453
444
|
token_id: token.tokenId,
|
|
454
445
|
token_address: token.tokenAddress,
|
|
455
446
|
});
|
|
456
447
|
const isRegistered = await isRegisteredOnChain(starkPublicKey, ethSigner, config);
|
|
457
448
|
if (!isRegistered) {
|
|
458
|
-
return executeRegisterAndWithdrawERC721(ethSigner, assetType.asset_type, starkPublicKey, token.tokenId,
|
|
449
|
+
return executeRegisterAndWithdrawERC721(ethSigner, assetType.asset_type, starkPublicKey, token.tokenId, imxConfig);
|
|
459
450
|
}
|
|
460
451
|
else {
|
|
461
|
-
return executeWithdrawERC721(ethSigner, assetType.asset_type, starkPublicKey, token.tokenId,
|
|
452
|
+
return executeWithdrawERC721(ethSigner, assetType.asset_type, starkPublicKey, token.tokenId, imxConfig);
|
|
462
453
|
}
|
|
463
454
|
}
|
|
464
455
|
async function completeERC721WithdrawalAction({ ethSigner, starkPublicKey, token, config, }) {
|
|
465
|
-
await validateChain(ethSigner, config.
|
|
456
|
+
await validateChain(ethSigner, config.immutableXConfig);
|
|
466
457
|
const tokenAddress = token.tokenAddress;
|
|
467
458
|
const tokenId = token.tokenId;
|
|
468
|
-
const
|
|
469
|
-
const mintsApi = new MintsApi(
|
|
459
|
+
const imxConfig = config.immutableXConfig;
|
|
460
|
+
const mintsApi = new MintsApi(imxConfig.apiConfiguration);
|
|
470
461
|
return await mintsApi
|
|
471
462
|
.getMintableTokenDetailsByClientTokenId({
|
|
472
463
|
tokenAddress,
|
|
@@ -503,23 +494,22 @@ async function executeWithdrawEth(ethSigner, assetType, starkPublicKey, config)
|
|
|
503
494
|
return ethSigner.sendTransaction(populatedTransaction);
|
|
504
495
|
}
|
|
505
496
|
async function completeEthWithdrawalAction({ ethSigner, starkPublicKey, config, }) {
|
|
506
|
-
await validateChain(ethSigner, config.
|
|
507
|
-
const
|
|
508
|
-
const assetType = await getEncodeAssetInfo('asset', 'ETH',
|
|
497
|
+
await validateChain(ethSigner, config.immutableXConfig);
|
|
498
|
+
const imxConfig = config.immutableXConfig;
|
|
499
|
+
const assetType = await getEncodeAssetInfo('asset', 'ETH', imxConfig);
|
|
509
500
|
const isRegistered = await isRegisteredOnChain(starkPublicKey, ethSigner, config);
|
|
510
501
|
if (!isRegistered) {
|
|
511
|
-
return executeRegisterAndWithdrawEth(ethSigner, assetType.asset_type, starkPublicKey,
|
|
502
|
+
return executeRegisterAndWithdrawEth(ethSigner, assetType.asset_type, starkPublicKey, imxConfig);
|
|
512
503
|
}
|
|
513
504
|
else {
|
|
514
|
-
return executeWithdrawEth(ethSigner, assetType.asset_type, starkPublicKey,
|
|
505
|
+
return executeWithdrawEth(ethSigner, assetType.asset_type, starkPublicKey, imxConfig);
|
|
515
506
|
}
|
|
516
507
|
}
|
|
517
508
|
|
|
518
509
|
async function prepareWithdrawal({ signers, withdrawal, config, }) {
|
|
519
|
-
const starkExConfig = config.getStarkExConfig();
|
|
520
510
|
return prepareWithdrawalAction({
|
|
521
511
|
signers,
|
|
522
|
-
config:
|
|
512
|
+
config: config.immutableXConfig,
|
|
523
513
|
...withdrawal,
|
|
524
514
|
});
|
|
525
515
|
}
|
|
@@ -544,10 +534,10 @@ async function completeWithdrawal({ signers: { ethSigner }, starkPublicKey, toke
|
|
|
544
534
|
}
|
|
545
535
|
}
|
|
546
536
|
|
|
547
|
-
async function createTrade({ signers: { ethSigner,
|
|
548
|
-
await validateChain(ethSigner, config.
|
|
537
|
+
async function createTrade({ signers: { ethSigner, starkSigner }, request, config, }) {
|
|
538
|
+
await validateChain(ethSigner, config.immutableXConfig);
|
|
549
539
|
const ethAddress = await ethSigner.getAddress();
|
|
550
|
-
const tradesApi = new TradesApi(config.
|
|
540
|
+
const tradesApi = new TradesApi(config.immutableXConfig.apiConfiguration);
|
|
551
541
|
const signableResult = await tradesApi.getSignableTrade({
|
|
552
542
|
getSignableTradeRequest: {
|
|
553
543
|
user: ethAddress,
|
|
@@ -557,7 +547,7 @@ async function createTrade({ signers: { ethSigner, starkExSigner }, request, con
|
|
|
557
547
|
});
|
|
558
548
|
const { signable_message: signableMessage, payload_hash: payloadHash } = signableResult.data;
|
|
559
549
|
const ethSignature = await signRaw(signableMessage, ethSigner);
|
|
560
|
-
const starkSignature = await
|
|
550
|
+
const starkSignature = await starkSigner.signMessage(payloadHash);
|
|
561
551
|
const createTradeResponse = await tradesApi.createTrade({
|
|
562
552
|
createTradeRequest: {
|
|
563
553
|
amount_buy: signableResult.data.amount_buy,
|
|
@@ -582,16 +572,16 @@ async function createTrade({ signers: { ethSigner, starkExSigner }, request, con
|
|
|
582
572
|
}
|
|
583
573
|
|
|
584
574
|
async function depositEth({ signers: { ethSigner }, deposit, config, }) {
|
|
585
|
-
await validateChain(ethSigner, config.
|
|
575
|
+
await validateChain(ethSigner, config.immutableXConfig);
|
|
586
576
|
const user = await ethSigner.getAddress();
|
|
587
577
|
const data = {
|
|
588
578
|
decimals: 18,
|
|
589
579
|
};
|
|
590
580
|
const amount = parseUnits(deposit.amount, 'wei');
|
|
591
|
-
const
|
|
592
|
-
const depositsApi = new DepositsApi(
|
|
593
|
-
const encodingApi = new EncodingApi(
|
|
594
|
-
const usersApi = new UsersApi(
|
|
581
|
+
const imxConfig = config.immutableXConfig;
|
|
582
|
+
const depositsApi = new DepositsApi(imxConfig.apiConfiguration);
|
|
583
|
+
const encodingApi = new EncodingApi(imxConfig.apiConfiguration);
|
|
584
|
+
const usersApi = new UsersApi(imxConfig.apiConfiguration);
|
|
595
585
|
const getSignableDepositRequest = {
|
|
596
586
|
user,
|
|
597
587
|
token: {
|
|
@@ -616,10 +606,10 @@ async function depositEth({ signers: { ethSigner }, deposit, config, }) {
|
|
|
616
606
|
const vaultId = signableDepositResult.data.vault_id;
|
|
617
607
|
const isRegistered = await isRegisteredOnChain(starkPublicKey, ethSigner, config);
|
|
618
608
|
if (!isRegistered) {
|
|
619
|
-
return executeRegisterAndDepositEth(ethSigner, amount, assetType, starkPublicKey, vaultId,
|
|
609
|
+
return executeRegisterAndDepositEth(ethSigner, amount, assetType, starkPublicKey, vaultId, imxConfig, usersApi);
|
|
620
610
|
}
|
|
621
611
|
else {
|
|
622
|
-
return executeDepositEth(ethSigner, amount, assetType, starkPublicKey, vaultId,
|
|
612
|
+
return executeDepositEth(ethSigner, amount, assetType, starkPublicKey, vaultId, imxConfig);
|
|
623
613
|
}
|
|
624
614
|
}
|
|
625
615
|
async function executeRegisterAndDepositEth(ethSigner, amount, assetType, starkPublicKey, vaultId, config, usersApi) {
|
|
@@ -636,8 +626,8 @@ async function executeDepositEth(ethSigner, amount, assetType, starkPublicKey, v
|
|
|
636
626
|
}
|
|
637
627
|
|
|
638
628
|
async function depositERC20({ signers: { ethSigner }, deposit, config, }) {
|
|
639
|
-
await validateChain(ethSigner, config.
|
|
640
|
-
const { apiConfiguration, ethConfiguration } = config.
|
|
629
|
+
await validateChain(ethSigner, config.immutableXConfig);
|
|
630
|
+
const { apiConfiguration, ethConfiguration } = config.immutableXConfig;
|
|
641
631
|
const user = await ethSigner.getAddress();
|
|
642
632
|
const tokensApi = new TokensApi(apiConfiguration);
|
|
643
633
|
const depositsApi = new DepositsApi(apiConfiguration);
|
|
@@ -704,12 +694,12 @@ async function executeRegisterAndDepositERC20(ethSigner, quantizedAmount, assetT
|
|
|
704
694
|
}
|
|
705
695
|
|
|
706
696
|
async function depositERC721({ signers: { ethSigner }, deposit, config, }) {
|
|
707
|
-
await validateChain(ethSigner, config.
|
|
697
|
+
await validateChain(ethSigner, config.immutableXConfig);
|
|
708
698
|
const user = await ethSigner.getAddress();
|
|
709
|
-
const
|
|
710
|
-
const depositsApi = new DepositsApi(
|
|
711
|
-
const encodingApi = new EncodingApi(
|
|
712
|
-
const usersApi = new UsersApi(
|
|
699
|
+
const { immutableXConfig } = config;
|
|
700
|
+
const depositsApi = new DepositsApi(immutableXConfig.apiConfiguration);
|
|
701
|
+
const encodingApi = new EncodingApi(immutableXConfig.apiConfiguration);
|
|
702
|
+
const usersApi = new UsersApi(immutableXConfig.apiConfiguration);
|
|
713
703
|
const data = {
|
|
714
704
|
token_address: deposit.tokenAddress,
|
|
715
705
|
token_id: deposit.tokenId,
|
|
@@ -745,18 +735,18 @@ async function depositERC721({ signers: { ethSigner }, deposit, config, }) {
|
|
|
745
735
|
const isRegistered = await isRegisteredOnChain(starkPublicKey, ethSigner, config);
|
|
746
736
|
// Approve whether an amount of token from an account can be spent by a third-party account
|
|
747
737
|
const tokenContract = Contracts.IERC721.connect(deposit.tokenAddress, ethSigner);
|
|
748
|
-
const operator =
|
|
738
|
+
const operator = immutableXConfig.ethConfiguration.coreContractAddress;
|
|
749
739
|
const isApprovedForAll = await tokenContract.isApprovedForAll(user, operator);
|
|
750
740
|
if (!isApprovedForAll) {
|
|
751
741
|
await tokenContract.setApprovalForAll(operator, true);
|
|
752
742
|
}
|
|
753
743
|
if (!isRegistered) {
|
|
754
744
|
const signableResult = await getSignableRegistrationOnchain(user, starkPublicKey, usersApi);
|
|
755
|
-
const coreContract = Contracts.Core.connect(
|
|
745
|
+
const coreContract = Contracts.Core.connect(immutableXConfig.ethConfiguration.coreContractAddress, ethSigner);
|
|
756
746
|
// Note: proxy registration contract registerAndDepositNft method is not used as it currently fails erc721 transfer ownership check
|
|
757
747
|
await coreContract.registerUser(user, starkPublicKey, signableResult.operator_signature);
|
|
758
748
|
}
|
|
759
|
-
return executeDepositERC721(ethSigner, deposit.tokenId, assetType, starkPublicKey, vaultId,
|
|
749
|
+
return executeDepositERC721(ethSigner, deposit.tokenId, assetType, starkPublicKey, vaultId, immutableXConfig);
|
|
760
750
|
}
|
|
761
751
|
async function executeDepositERC721(ethSigner, tokenId, assetType, starkPublicKey, vaultId, config) {
|
|
762
752
|
const coreContract = Contracts.Core.connect(config.ethConfiguration.coreContractAddress, ethSigner);
|
|
@@ -776,8 +766,8 @@ async function deposit({ signers, deposit, config }) {
|
|
|
776
766
|
}
|
|
777
767
|
|
|
778
768
|
async function exchangeTransfer({ signers, request, config, }) {
|
|
779
|
-
await validateChain(signers.ethSigner, config.
|
|
780
|
-
const exchangeApi = new ExchangesApi(config.
|
|
769
|
+
await validateChain(signers.ethSigner, config.immutableXConfig);
|
|
770
|
+
const exchangeApi = new ExchangesApi(config.immutableXConfig.apiConfiguration);
|
|
781
771
|
const ethAddress = await signers.ethSigner.getAddress();
|
|
782
772
|
const transferAmount = request.amount;
|
|
783
773
|
const signableResult = await exchangeApi.getExchangeSignableTransfer({
|
|
@@ -791,7 +781,7 @@ async function exchangeTransfer({ signers, request, config, }) {
|
|
|
791
781
|
});
|
|
792
782
|
const { signable_message: signableMessage, payload_hash: payloadHash } = signableResult.data;
|
|
793
783
|
const ethSignature = await signRaw(signableMessage, signers.ethSigner);
|
|
794
|
-
const starkSignature = await signers.
|
|
784
|
+
const starkSignature = await signers.starkSigner.signMessage(payloadHash);
|
|
795
785
|
const transferSigningParams = {
|
|
796
786
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
797
787
|
sender_stark_key: signableResult.data.sender_stark_key,
|
|
@@ -821,9 +811,9 @@ async function exchangeTransfer({ signers, request, config, }) {
|
|
|
821
811
|
class GenericIMXProvider {
|
|
822
812
|
config;
|
|
823
813
|
signers;
|
|
824
|
-
constructor(config, ethSigner,
|
|
814
|
+
constructor(config, ethSigner, starkSigner) {
|
|
825
815
|
this.config = config;
|
|
826
|
-
this.signers = { ethSigner,
|
|
816
|
+
this.signers = { ethSigner, starkSigner };
|
|
827
817
|
}
|
|
828
818
|
async getAddress() {
|
|
829
819
|
return await this.signers.ethSigner.getAddress();
|
|
@@ -882,7 +872,7 @@ class GenericIMXProvider {
|
|
|
882
872
|
});
|
|
883
873
|
}
|
|
884
874
|
async isRegisteredOnchain() {
|
|
885
|
-
const starkPublicKey = await this.signers.
|
|
875
|
+
const starkPublicKey = await this.signers.starkSigner.getAddress();
|
|
886
876
|
return isRegisteredOnChain(starkPublicKey, this.signers.ethSigner, this.config);
|
|
887
877
|
}
|
|
888
878
|
prepareWithdrawal(request) {
|
|
@@ -1000,7 +990,6 @@ class ImxSigner {
|
|
|
1000
990
|
|
|
1001
991
|
const IMX_WALLET_IFRAME_ID = 'imx-wallet-app';
|
|
1002
992
|
const IMX_WALLET_IFRAME_HOSTS = {
|
|
1003
|
-
[Environment.DEVELOPMENT]: 'http://localhost:8080',
|
|
1004
993
|
[Environment.SANDBOX]: 'https://wallets.sandbox.immutable.com',
|
|
1005
994
|
[Environment.PRODUCTION]: 'https://wallets.immutable.com',
|
|
1006
995
|
};
|
|
@@ -1094,16 +1083,15 @@ const withProviderError = async (fn, customError) => {
|
|
|
1094
1083
|
|
|
1095
1084
|
class MetaMaskIMXProvider extends GenericIMXProvider {
|
|
1096
1085
|
static imxSigner;
|
|
1097
|
-
constructor(config, ethSigner,
|
|
1098
|
-
super(config, ethSigner,
|
|
1086
|
+
constructor(config, ethSigner, starkSigner) {
|
|
1087
|
+
super(config, ethSigner, starkSigner);
|
|
1099
1088
|
}
|
|
1100
1089
|
static async connect(config) {
|
|
1101
|
-
const starkExConfig = config.getStarkExConfig();
|
|
1102
1090
|
return await withProviderError(async () => {
|
|
1103
1091
|
const metaMaskProvider = await connect$1({
|
|
1104
|
-
chainID:
|
|
1092
|
+
chainID: config.immutableXConfig.ethConfiguration.chainID,
|
|
1105
1093
|
});
|
|
1106
|
-
this.imxSigner = await connect(metaMaskProvider,
|
|
1094
|
+
this.imxSigner = await connect(metaMaskProvider, config.baseConfig.environment);
|
|
1107
1095
|
return new MetaMaskIMXProvider(config, metaMaskProvider.getSigner(), this.imxSigner);
|
|
1108
1096
|
}, { type: ProviderErrorType.WALLET_CONNECTION_ERROR });
|
|
1109
1097
|
}
|
|
@@ -1125,4 +1113,27 @@ class MetaMaskIMXProvider extends GenericIMXProvider {
|
|
|
1125
1113
|
}
|
|
1126
1114
|
}
|
|
1127
1115
|
|
|
1128
|
-
|
|
1116
|
+
class ProviderConfiguration {
|
|
1117
|
+
immutableXConfig;
|
|
1118
|
+
baseConfig;
|
|
1119
|
+
constructor({ baseConfig, overrides }) {
|
|
1120
|
+
this.baseConfig = baseConfig;
|
|
1121
|
+
if (overrides) {
|
|
1122
|
+
this.immutableXConfig = overrides.immutableXConfig;
|
|
1123
|
+
}
|
|
1124
|
+
else {
|
|
1125
|
+
switch (baseConfig.environment) {
|
|
1126
|
+
case Environment.SANDBOX: {
|
|
1127
|
+
this.immutableXConfig = Config.SANDBOX;
|
|
1128
|
+
break;
|
|
1129
|
+
}
|
|
1130
|
+
case Environment.PRODUCTION: {
|
|
1131
|
+
this.immutableXConfig = Config.PRODUCTION;
|
|
1132
|
+
break;
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
export { GenericIMXProvider, MetaMaskIMXProvider, ProviderConfiguration };
|
package/package.json
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imtbl/sdk",
|
|
3
3
|
"description": "Immutable Unified SDK",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0-alpha",
|
|
5
5
|
"author": "Immutable",
|
|
6
6
|
"bugs": "https://github.com/immutable/ts-immutable-sdk/issues",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@biom3/design-tokens": "0.1.4-beta",
|
|
9
|
-
"@biom3/react": "^0.1.3-beta",
|
|
10
8
|
"@ethersproject/providers": "^5.7.2",
|
|
11
|
-
"@imtbl/core-sdk": "^1.
|
|
9
|
+
"@imtbl/core-sdk": "^1.1.1-alpha.1",
|
|
12
10
|
"@magic-ext/oidc": "^1.0.1",
|
|
13
11
|
"@metamask/detect-provider": "^2.0.0",
|
|
14
12
|
"axios": "^1.3.5",
|
|
@@ -17,19 +15,19 @@
|
|
|
17
15
|
"magic-sdk": "^13.3.1",
|
|
18
16
|
"oidc-client-ts": "^2.2.1",
|
|
19
17
|
"react": "^18.2.0",
|
|
20
|
-
"react-dom": "^18.2.0"
|
|
21
|
-
"react-router-dom": "^6.10.0"
|
|
18
|
+
"react-dom": "^18.2.0"
|
|
22
19
|
},
|
|
23
20
|
"devDependencies": {
|
|
24
21
|
"@imtbl/checkout-sdk-web": "0.0.0",
|
|
25
22
|
"@imtbl/checkout-ui-types": "0.0.0",
|
|
26
23
|
"@imtbl/config": "0.0.0",
|
|
24
|
+
"@imtbl/immutablex-client": "0.0.0",
|
|
27
25
|
"@imtbl/passport": "0.0.0",
|
|
28
26
|
"@imtbl/provider": "0.0.0",
|
|
29
|
-
"@imtbl/starkex": "0.0.0",
|
|
30
27
|
"@rollup/plugin-commonjs": "^24.0.1",
|
|
31
28
|
"@rollup/plugin-json": "^6.0.0",
|
|
32
29
|
"@rollup/plugin-node-resolve": "^15.0.2",
|
|
30
|
+
"@rollup/plugin-replace": "^5.0.2",
|
|
33
31
|
"@rollup/plugin-typescript": "^11.0.0",
|
|
34
32
|
"@yarnpkg/cli": "3.5.0",
|
|
35
33
|
"@yarnpkg/core": "3.5.0",
|
|
@@ -40,17 +38,22 @@
|
|
|
40
38
|
"typescript": "^4.9.5"
|
|
41
39
|
},
|
|
42
40
|
"engines": {
|
|
43
|
-
"node": ">=
|
|
41
|
+
"node": ">=16.11.0"
|
|
44
42
|
},
|
|
45
43
|
"exports": {
|
|
46
44
|
"./package.json": "./package.json",
|
|
47
45
|
".": {
|
|
48
|
-
"types": "./dist/
|
|
49
|
-
"import": "./dist/
|
|
46
|
+
"types": "./dist/index.d.ts",
|
|
47
|
+
"import": "./dist/index.js",
|
|
48
|
+
"require": "./dist/index.cjs"
|
|
50
49
|
},
|
|
51
|
-
"./
|
|
52
|
-
"types": "./dist/
|
|
53
|
-
"import": "./dist/
|
|
50
|
+
"./config": {
|
|
51
|
+
"types": "./dist/config.d.ts",
|
|
52
|
+
"import": "./dist/config.js"
|
|
53
|
+
},
|
|
54
|
+
"./immutablex_client": {
|
|
55
|
+
"types": "./dist/immutablex_client.d.ts",
|
|
56
|
+
"import": "./dist/immutablex_client.js"
|
|
54
57
|
},
|
|
55
58
|
"./passport": {
|
|
56
59
|
"types": "./dist/passport.d.ts",
|
|
@@ -75,13 +78,18 @@
|
|
|
75
78
|
"homepage": "https://github.com/immutable/ts-immutable-sdk#readme",
|
|
76
79
|
"license": "Apache-2.0",
|
|
77
80
|
"main": "./dist/index.cjs",
|
|
81
|
+
"module": "./dist/index.js",
|
|
78
82
|
"repository": "immutable/ts-immutable-sdk.git",
|
|
79
83
|
"scripts": {
|
|
80
|
-
"build": "yarn packageList && yarn updateDependencies && rm -rf dist && NODE_ENV=production rollup --config rollup.config.js && rm -rf dist/types",
|
|
84
|
+
"build": "yarn packageList && yarn updateDependencies && yarn regenModules && rm -rf dist && NODE_ENV=production rollup --config rollup.config.js && rm -rf dist/types",
|
|
85
|
+
"build:only": "rm -rf dist && NODE_ENV=production rollup --config rollup.config.js && rm -rf dist/types",
|
|
86
|
+
"generateIndex": "node scripts/generateIndex.js",
|
|
81
87
|
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
|
|
82
88
|
"packageList": "./scripts/generate-package-list.sh",
|
|
89
|
+
"regenModules": "yarn generateIndex && yarn updateExports",
|
|
83
90
|
"typecheck": "tsc --noEmit --jsx preserve",
|
|
84
|
-
"updateDependencies": "node ./scripts/updateDependencies.js"
|
|
91
|
+
"updateDependencies": "yarn packageList && node ./scripts/updateDependencies.js",
|
|
92
|
+
"updateExports": "node scripts/updateExports.js"
|
|
85
93
|
},
|
|
86
94
|
"type": "module",
|
|
87
95
|
"types": "./dist/index.d.ts"
|