@imtbl/sdk 1.39.2 → 1.39.3-alpha.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/dist/blockchain_data.js +2 -2
- package/dist/browser/checkout/sdk.js +4 -4
- package/dist/checkout.js +4 -4
- package/dist/config.js +1 -1
- package/dist/index.browser.js +4 -4
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +627 -15
- package/dist/index.d.ts +145 -7
- package/dist/index.js +626 -16
- package/dist/minting_backend.d.ts +3534 -0
- package/dist/minting_backend.js +6615 -0
- package/dist/orderbook.js +1 -1
- package/dist/passport.js +4 -4
- package/dist/webhook.d.ts +11 -0
- package/dist/webhook.js +388 -0
- package/dist/x.js +3 -3
- package/package.json +10 -2
package/dist/index.cjs
CHANGED
|
@@ -35,6 +35,7 @@ var solidity = require('@ethersproject/solidity');
|
|
|
35
35
|
var routerSdk = require('@uniswap/router-sdk');
|
|
36
36
|
var units = require('@ethersproject/units');
|
|
37
37
|
var bignumber = require('@ethersproject/bignumber');
|
|
38
|
+
var MessageValidator = require('sns-validator');
|
|
38
39
|
|
|
39
40
|
function _interopNamespaceDefault(e) {
|
|
40
41
|
var n = Object.create(null);
|
|
@@ -184,7 +185,7 @@ const flattenProperties$1 = (properties) => {
|
|
|
184
185
|
};
|
|
185
186
|
|
|
186
187
|
// WARNING: DO NOT CHANGE THE STRING BELOW. IT GETS REPLACED AT BUILD TIME.
|
|
187
|
-
const SDK_VERSION$1 = '1.39.
|
|
188
|
+
const SDK_VERSION$1 = '1.39.3-alpha.1';
|
|
188
189
|
const getFrameParentDomain$1 = () => {
|
|
189
190
|
if (isNode$1()) {
|
|
190
191
|
return '';
|
|
@@ -14211,7 +14212,7 @@ class MultiRollupApiClients {
|
|
|
14211
14212
|
}
|
|
14212
14213
|
|
|
14213
14214
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
14214
|
-
const defaultHeaders$2 = { 'x-sdk-version': 'ts-immutable-sdk-1.39.
|
|
14215
|
+
const defaultHeaders$2 = { 'x-sdk-version': 'ts-immutable-sdk-1.39.3-alpha.1' };
|
|
14215
14216
|
const createConfig$1 = ({ basePath, headers, }) => {
|
|
14216
14217
|
if (!basePath.trim()) {
|
|
14217
14218
|
throw Error('basePath can not be empty');
|
|
@@ -14283,7 +14284,7 @@ class APIError extends Error {
|
|
|
14283
14284
|
|
|
14284
14285
|
/* eslint-disable implicit-arrow-linebreak */
|
|
14285
14286
|
const defaultHeaders$1 = {
|
|
14286
|
-
sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-1.39.
|
|
14287
|
+
sdkVersion: 'ts-immutable-sdk-multi-rollup-api-client-1.39.3-alpha.1',
|
|
14287
14288
|
};
|
|
14288
14289
|
/**
|
|
14289
14290
|
* createAPIConfiguration to create a custom Configuration
|
|
@@ -14757,7 +14758,7 @@ var blockchain_data = /*#__PURE__*/Object.freeze({
|
|
|
14757
14758
|
/* eslint-disable @typescript-eslint/naming-convention */
|
|
14758
14759
|
class ApiConfiguration extends index$2.Configuration {
|
|
14759
14760
|
}
|
|
14760
|
-
const defaultHeaders = { 'x-sdk-version': 'ts-immutable-sdk-1.39.
|
|
14761
|
+
const defaultHeaders = { 'x-sdk-version': 'ts-immutable-sdk-1.39.3-alpha.1' };
|
|
14761
14762
|
/**
|
|
14762
14763
|
* @dev use createImmutableXConfiguration instead
|
|
14763
14764
|
*/
|
|
@@ -14798,7 +14799,7 @@ const createImmutableXConfiguration = ({ basePath, chainID, coreContractAddress,
|
|
|
14798
14799
|
coreContractAddress,
|
|
14799
14800
|
registrationContractAddress,
|
|
14800
14801
|
registrationV4ContractAddress,
|
|
14801
|
-
sdkVersion: 'ts-immutable-sdk-1.39.
|
|
14802
|
+
sdkVersion: 'ts-immutable-sdk-1.39.3-alpha.1',
|
|
14802
14803
|
baseConfig,
|
|
14803
14804
|
});
|
|
14804
14805
|
const production = ({ baseConfig }) => createImmutableXConfiguration({
|
|
@@ -26301,12 +26302,12 @@ function assertOptions$1(options, schema, allowUnknown) {
|
|
|
26301
26302
|
}
|
|
26302
26303
|
}
|
|
26303
26304
|
|
|
26304
|
-
var validator$
|
|
26305
|
+
var validator$2 = {
|
|
26305
26306
|
assertOptions: assertOptions$1,
|
|
26306
26307
|
validators: validators$1$1
|
|
26307
26308
|
};
|
|
26308
26309
|
|
|
26309
|
-
const validators$2 = validator$
|
|
26310
|
+
const validators$2 = validator$2.validators;
|
|
26310
26311
|
|
|
26311
26312
|
/**
|
|
26312
26313
|
* Create a new instance of Axios
|
|
@@ -26347,7 +26348,7 @@ let Axios$2 = class Axios {
|
|
|
26347
26348
|
const {transitional, paramsSerializer, headers} = config;
|
|
26348
26349
|
|
|
26349
26350
|
if (transitional !== undefined) {
|
|
26350
|
-
validator$
|
|
26351
|
+
validator$2.assertOptions(transitional, {
|
|
26351
26352
|
silentJSONParsing: validators$2.transitional(validators$2.boolean),
|
|
26352
26353
|
forcedJSONParsing: validators$2.transitional(validators$2.boolean),
|
|
26353
26354
|
clarifyTimeoutError: validators$2.transitional(validators$2.boolean)
|
|
@@ -26360,7 +26361,7 @@ let Axios$2 = class Axios {
|
|
|
26360
26361
|
serialize: paramsSerializer
|
|
26361
26362
|
};
|
|
26362
26363
|
} else {
|
|
26363
|
-
validator$
|
|
26364
|
+
validator$2.assertOptions(paramsSerializer, {
|
|
26364
26365
|
encode: validators$2.function,
|
|
26365
26366
|
serialize: validators$2.function
|
|
26366
26367
|
}, true);
|
|
@@ -35079,12 +35080,12 @@ function assertOptions(options, schema, allowUnknown) {
|
|
|
35079
35080
|
}
|
|
35080
35081
|
}
|
|
35081
35082
|
|
|
35082
|
-
var validator = {
|
|
35083
|
+
var validator$1 = {
|
|
35083
35084
|
assertOptions,
|
|
35084
35085
|
validators: validators$1
|
|
35085
35086
|
};
|
|
35086
35087
|
|
|
35087
|
-
const validators = validator.validators;
|
|
35088
|
+
const validators = validator$1.validators;
|
|
35088
35089
|
|
|
35089
35090
|
/**
|
|
35090
35091
|
* Create a new instance of Axios
|
|
@@ -35125,7 +35126,7 @@ class Axios {
|
|
|
35125
35126
|
const {transitional, paramsSerializer, headers} = config;
|
|
35126
35127
|
|
|
35127
35128
|
if (transitional !== undefined) {
|
|
35128
|
-
validator.assertOptions(transitional, {
|
|
35129
|
+
validator$1.assertOptions(transitional, {
|
|
35129
35130
|
silentJSONParsing: validators.transitional(validators.boolean),
|
|
35130
35131
|
forcedJSONParsing: validators.transitional(validators.boolean),
|
|
35131
35132
|
clarifyTimeoutError: validators.transitional(validators.boolean)
|
|
@@ -35138,7 +35139,7 @@ class Axios {
|
|
|
35138
35139
|
serialize: paramsSerializer
|
|
35139
35140
|
};
|
|
35140
35141
|
} else {
|
|
35141
|
-
validator.assertOptions(paramsSerializer, {
|
|
35142
|
+
validator$1.assertOptions(paramsSerializer, {
|
|
35142
35143
|
encode: validators.function,
|
|
35143
35144
|
serialize: validators.function
|
|
35144
35145
|
}, true);
|
|
@@ -60897,7 +60898,7 @@ const flattenProperties = (properties) => {
|
|
|
60897
60898
|
};
|
|
60898
60899
|
|
|
60899
60900
|
// WARNING: DO NOT CHANGE THE STRING BELOW. IT GETS REPLACED AT BUILD TIME.
|
|
60900
|
-
const SDK_VERSION = '1.39.
|
|
60901
|
+
const SDK_VERSION = '1.39.3-alpha.1';
|
|
60901
60902
|
const getFrameParentDomain = () => {
|
|
60902
60903
|
if (isNode()) {
|
|
60903
60904
|
return '';
|
|
@@ -66190,7 +66191,7 @@ const IMMUTABLE_ZKVEM_GAS_OVERRIDES = {
|
|
|
66190
66191
|
maxPriorityFeePerGas: ethers.BigNumber.from(10e9),
|
|
66191
66192
|
};
|
|
66192
66193
|
|
|
66193
|
-
const SDK_VERSION_MARKER = '1.39.
|
|
66194
|
+
const SDK_VERSION_MARKER = '1.39.3-alpha.1';
|
|
66194
66195
|
// This SDK version is replaced by the `yarn build` command ran on the root level
|
|
66195
66196
|
const globalPackageVersion = () => SDK_VERSION_MARKER;
|
|
66196
66197
|
|
|
@@ -71947,9 +71948,620 @@ var x = /*#__PURE__*/Object.freeze({
|
|
|
71947
71948
|
starkEcOrder: starkEcOrder
|
|
71948
71949
|
});
|
|
71949
71950
|
|
|
71951
|
+
const validator = new MessageValidator();
|
|
71952
|
+
const allowedTopicArnPrefix = {
|
|
71953
|
+
[Environment$1.PRODUCTION]: 'arn:aws:sns:us-east-2:362750628221:',
|
|
71954
|
+
[Environment$1.SANDBOX]: 'arn:aws:sns:us-east-2:783421985614:'
|
|
71955
|
+
};
|
|
71956
|
+
const init = async (body, env, handlers) => {
|
|
71957
|
+
const msg = await new Promise((resolve, reject) => {
|
|
71958
|
+
validator.validate(body, (err, message) => {
|
|
71959
|
+
if (err) {
|
|
71960
|
+
return reject(err);
|
|
71961
|
+
}
|
|
71962
|
+
// check for topic arn prefix
|
|
71963
|
+
if (!message.TopicArn.startsWith(allowedTopicArnPrefix[env])) {
|
|
71964
|
+
throw new Error('Invalid topic arn');
|
|
71965
|
+
}
|
|
71966
|
+
if (message?.Type === 'SubscriptionConfirmation') {
|
|
71967
|
+
fetch(message.SubscribeURL).then(() => {
|
|
71968
|
+
resolve(message);
|
|
71969
|
+
}).catch((e) => {
|
|
71970
|
+
reject(e);
|
|
71971
|
+
});
|
|
71972
|
+
}
|
|
71973
|
+
return resolve(message);
|
|
71974
|
+
});
|
|
71975
|
+
});
|
|
71976
|
+
if (msg.Type === 'Notification') {
|
|
71977
|
+
const event = JSON.parse(msg.Message);
|
|
71978
|
+
switch (event.event_name) {
|
|
71979
|
+
case 'imtbl_zkevm_mint_request_updated':
|
|
71980
|
+
await handlers.zkevmMintRequestUpdated(event);
|
|
71981
|
+
break;
|
|
71982
|
+
default:
|
|
71983
|
+
if (handlers.others) {
|
|
71984
|
+
await handlers.others(event);
|
|
71985
|
+
}
|
|
71986
|
+
break;
|
|
71987
|
+
}
|
|
71988
|
+
}
|
|
71989
|
+
};
|
|
71990
|
+
|
|
71991
|
+
var webhook = /*#__PURE__*/Object.freeze({
|
|
71992
|
+
__proto__: null,
|
|
71993
|
+
init: init
|
|
71994
|
+
});
|
|
71995
|
+
|
|
71996
|
+
const moduleName = 'minting_backend_sdk';
|
|
71997
|
+
const trackInitializePersistencePG = () => {
|
|
71998
|
+
try {
|
|
71999
|
+
track(moduleName, 'initializePersistencePG');
|
|
72000
|
+
}
|
|
72001
|
+
catch {
|
|
72002
|
+
// ignore
|
|
72003
|
+
}
|
|
72004
|
+
};
|
|
72005
|
+
const trackInitializePersistencePrismaSqlite = () => {
|
|
72006
|
+
try {
|
|
72007
|
+
track(moduleName, 'initializePersistencePrismaSqlite');
|
|
72008
|
+
}
|
|
72009
|
+
catch {
|
|
72010
|
+
// ignore
|
|
72011
|
+
}
|
|
72012
|
+
};
|
|
72013
|
+
const trackSubmitMintingRequests = () => {
|
|
72014
|
+
try {
|
|
72015
|
+
track(moduleName, 'submitMintingRequests');
|
|
72016
|
+
}
|
|
72017
|
+
catch {
|
|
72018
|
+
// ignore
|
|
72019
|
+
}
|
|
72020
|
+
};
|
|
72021
|
+
const trackProcessMint = () => {
|
|
72022
|
+
try {
|
|
72023
|
+
track(moduleName, 'processMint');
|
|
72024
|
+
}
|
|
72025
|
+
catch {
|
|
72026
|
+
// ignore
|
|
72027
|
+
}
|
|
72028
|
+
};
|
|
72029
|
+
const trackRecordMint = () => {
|
|
72030
|
+
try {
|
|
72031
|
+
track(moduleName, 'recordMint');
|
|
72032
|
+
}
|
|
72033
|
+
catch {
|
|
72034
|
+
// ignore
|
|
72035
|
+
}
|
|
72036
|
+
};
|
|
72037
|
+
|
|
72038
|
+
const mintingPersistence$1 = (client) => {
|
|
72039
|
+
trackInitializePersistencePG();
|
|
72040
|
+
return {
|
|
72041
|
+
recordMint: async (request) => {
|
|
72042
|
+
const r = await client.query(`
|
|
72043
|
+
INSERT INTO im_assets (asset_id, contract_address, owner_address, metadata, amount, token_id)
|
|
72044
|
+
VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (asset_id, contract_address) DO NOTHING;
|
|
72045
|
+
`, [
|
|
72046
|
+
request.asset_id, request.contract_address, request.owner_address,
|
|
72047
|
+
request.metadata, request.amount, request.token_id
|
|
72048
|
+
]);
|
|
72049
|
+
if (r.rowCount === 0) {
|
|
72050
|
+
throw new Error('Duplicated mint');
|
|
72051
|
+
}
|
|
72052
|
+
},
|
|
72053
|
+
getNextBatchForSubmission: async (limit) => {
|
|
72054
|
+
const res = await client.query(`
|
|
72055
|
+
UPDATE im_assets SET minting_status = 'submitting' WHERE minting_status IS NULL and id in (
|
|
72056
|
+
select id from im_assets where minting_status is null limit $1 for update skip locked
|
|
72057
|
+
) returning *;
|
|
72058
|
+
`, [limit]);
|
|
72059
|
+
return res.rows;
|
|
72060
|
+
},
|
|
72061
|
+
updateMintingStatusToSubmitted: async (ids) => {
|
|
72062
|
+
await client.query(`
|
|
72063
|
+
UPDATE im_assets SET minting_status = $2 WHERE id = ANY($1);
|
|
72064
|
+
`, [ids, 'submitted']);
|
|
72065
|
+
},
|
|
72066
|
+
syncMintingStatus: async (submittedMintRequest) => {
|
|
72067
|
+
// doing a upsert just in case the row has not been created yet
|
|
72068
|
+
await client.query(`
|
|
72069
|
+
INSERT INTO im_assets (
|
|
72070
|
+
asset_id,
|
|
72071
|
+
contract_address,
|
|
72072
|
+
owner_address,
|
|
72073
|
+
token_id,
|
|
72074
|
+
minting_status,
|
|
72075
|
+
metadata_id,
|
|
72076
|
+
last_imtbl_zkevm_mint_request_updated_id,
|
|
72077
|
+
error,
|
|
72078
|
+
amount
|
|
72079
|
+
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) ON CONFLICT (asset_id, contract_address)
|
|
72080
|
+
DO UPDATE SET
|
|
72081
|
+
owner_address = $3,
|
|
72082
|
+
token_id = $4,
|
|
72083
|
+
minting_status = $5,
|
|
72084
|
+
metadata_id = $6,
|
|
72085
|
+
last_imtbl_zkevm_mint_request_updated_id = $7,
|
|
72086
|
+
error = $8
|
|
72087
|
+
where (
|
|
72088
|
+
im_assets.last_imtbl_zkevm_mint_request_updated_id < $7 OR
|
|
72089
|
+
im_assets.last_imtbl_zkevm_mint_request_updated_id is null
|
|
72090
|
+
);
|
|
72091
|
+
`, [
|
|
72092
|
+
submittedMintRequest.assetId,
|
|
72093
|
+
submittedMintRequest.contractAddress,
|
|
72094
|
+
submittedMintRequest.ownerAddress,
|
|
72095
|
+
submittedMintRequest.tokenId,
|
|
72096
|
+
submittedMintRequest.status,
|
|
72097
|
+
submittedMintRequest.metadataId,
|
|
72098
|
+
submittedMintRequest.imtblZkevmMintRequestUpdatedId,
|
|
72099
|
+
submittedMintRequest.error,
|
|
72100
|
+
submittedMintRequest.amount
|
|
72101
|
+
]);
|
|
72102
|
+
},
|
|
72103
|
+
markAsConflict: async (assetIds, contractAddress) => {
|
|
72104
|
+
await client.query(`
|
|
72105
|
+
UPDATE im_assets
|
|
72106
|
+
SET minting_status = 'conflicting'
|
|
72107
|
+
WHERE asset_id = ANY($1)
|
|
72108
|
+
AND contract_address = $2;
|
|
72109
|
+
`, [assetIds, contractAddress]);
|
|
72110
|
+
},
|
|
72111
|
+
resetMintingStatus: async (ids) => {
|
|
72112
|
+
await client.query(`
|
|
72113
|
+
UPDATE im_assets SET minting_status = null WHERE id = ANY($1);
|
|
72114
|
+
`, [ids]);
|
|
72115
|
+
},
|
|
72116
|
+
markForRetry: async (ids) => {
|
|
72117
|
+
await client.query(`
|
|
72118
|
+
UPDATE im_assets
|
|
72119
|
+
SET minting_status = null, tried_count = tried_count + 1 WHERE id = ANY($1);
|
|
72120
|
+
`, [ids]);
|
|
72121
|
+
},
|
|
72122
|
+
updateMintingStatusToSubmissionFailed: async (ids) => {
|
|
72123
|
+
await client.query(`
|
|
72124
|
+
UPDATE im_assets SET minting_status = 'submission_failed' WHERE id = ANY($1);
|
|
72125
|
+
`, [ids]);
|
|
72126
|
+
},
|
|
72127
|
+
getMintingRequest: async (contractAddress, referenceId) => {
|
|
72128
|
+
const res = await client.query(`
|
|
72129
|
+
SELECT * FROM im_assets WHERE contract_address = $1 and asset_id = $2;
|
|
72130
|
+
`, [contractAddress, referenceId]);
|
|
72131
|
+
return res.rows[0] || null;
|
|
72132
|
+
}
|
|
72133
|
+
};
|
|
72134
|
+
};
|
|
72135
|
+
|
|
72136
|
+
/* eslint-disable no-await-in-loop */
|
|
72137
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
72138
|
+
// client is a PrismaClient instance. it needs to be generated with
|
|
72139
|
+
// additional schema defined inside (TODO: point to repo) file.
|
|
72140
|
+
const mintingPersistence = (client) => {
|
|
72141
|
+
trackInitializePersistencePrismaSqlite();
|
|
72142
|
+
return {
|
|
72143
|
+
recordMint: async (request) => {
|
|
72144
|
+
const result = await client.imAssets.upsert({
|
|
72145
|
+
where: {
|
|
72146
|
+
im_assets_uindex: {
|
|
72147
|
+
assetId: request.asset_id, contractAddress: request.contract_address
|
|
72148
|
+
}
|
|
72149
|
+
},
|
|
72150
|
+
update: {},
|
|
72151
|
+
create: {
|
|
72152
|
+
assetId: request.asset_id,
|
|
72153
|
+
contractAddress: request.contract_address,
|
|
72154
|
+
ownerAddress: request.owner_address,
|
|
72155
|
+
metadata: JSON.stringify(request.metadata),
|
|
72156
|
+
amount: request.amount || null,
|
|
72157
|
+
tokenId: request.token_id || null,
|
|
72158
|
+
},
|
|
72159
|
+
});
|
|
72160
|
+
// Since upsert does not throw on update and does not tell directly if it updated or inserted,
|
|
72161
|
+
// we need to add additional logic to handle this if "result" does not provide enough info.
|
|
72162
|
+
if (!result) {
|
|
72163
|
+
throw new Error('Duplicated mint');
|
|
72164
|
+
}
|
|
72165
|
+
},
|
|
72166
|
+
// WARNING: this is NOT concurrency safe. Please only call this method one at a time.
|
|
72167
|
+
getNextBatchForSubmission: async (limit) => {
|
|
72168
|
+
const assets = await client.imAssets.findMany({
|
|
72169
|
+
where: {
|
|
72170
|
+
mintingStatus: null
|
|
72171
|
+
},
|
|
72172
|
+
take: limit
|
|
72173
|
+
});
|
|
72174
|
+
const assetIds = assets.map((asset) => asset.id);
|
|
72175
|
+
await client.imAssets.updateMany({
|
|
72176
|
+
where: {
|
|
72177
|
+
id: {
|
|
72178
|
+
in: assetIds
|
|
72179
|
+
}
|
|
72180
|
+
},
|
|
72181
|
+
data: {
|
|
72182
|
+
mintingStatus: 'submitting'
|
|
72183
|
+
}
|
|
72184
|
+
});
|
|
72185
|
+
const updatedAssets = await client.imAssets.findMany({
|
|
72186
|
+
where: {
|
|
72187
|
+
id: {
|
|
72188
|
+
in: assetIds
|
|
72189
|
+
}
|
|
72190
|
+
}
|
|
72191
|
+
});
|
|
72192
|
+
return updatedAssets.map((asset) => ({
|
|
72193
|
+
id: asset.id,
|
|
72194
|
+
contract_address: asset.contractAddress,
|
|
72195
|
+
wallet_address: asset.ownerAddress,
|
|
72196
|
+
asset_id: asset.assetId,
|
|
72197
|
+
metadata: asset.metadata ? JSON.parse(asset.metadata) : null,
|
|
72198
|
+
owner_address: asset.ownerAddress,
|
|
72199
|
+
tried_count: asset.triedCount,
|
|
72200
|
+
amount: asset.amount || null,
|
|
72201
|
+
token_id: asset.tokenId || null,
|
|
72202
|
+
}));
|
|
72203
|
+
},
|
|
72204
|
+
updateMintingStatusToSubmitted: async (ids) => {
|
|
72205
|
+
await client.imAssets.updateMany({
|
|
72206
|
+
where: {
|
|
72207
|
+
id: {
|
|
72208
|
+
in: ids
|
|
72209
|
+
}
|
|
72210
|
+
},
|
|
72211
|
+
data: {
|
|
72212
|
+
mintingStatus: 'submitted'
|
|
72213
|
+
}
|
|
72214
|
+
});
|
|
72215
|
+
},
|
|
72216
|
+
syncMintingStatus: async (submittedMintRequest) => {
|
|
72217
|
+
// First, attempt to retrieve the existing asset
|
|
72218
|
+
const existingAsset = await client.imAssets.findUnique({
|
|
72219
|
+
where: {
|
|
72220
|
+
im_assets_uindex: {
|
|
72221
|
+
assetId: submittedMintRequest.assetId,
|
|
72222
|
+
contractAddress: submittedMintRequest.contractAddress,
|
|
72223
|
+
},
|
|
72224
|
+
}
|
|
72225
|
+
});
|
|
72226
|
+
// Check if the asset exists and the condition for updating is met
|
|
72227
|
+
if (existingAsset && (existingAsset.lastImtblZkevmMintRequestUpdatedId === null
|
|
72228
|
+
|| existingAsset.lastImtblZkevmMintRequestUpdatedId < submittedMintRequest.imtblZkevmMintRequestUpdatedId)) {
|
|
72229
|
+
// Perform update if the existing record's lastImtblZkevmMintRequestUpdatedId is less than the new one or is null
|
|
72230
|
+
await client.imAssets.update({
|
|
72231
|
+
where: {
|
|
72232
|
+
im_assets_uindex: {
|
|
72233
|
+
assetId: submittedMintRequest.assetId,
|
|
72234
|
+
contractAddress: submittedMintRequest.contractAddress,
|
|
72235
|
+
},
|
|
72236
|
+
},
|
|
72237
|
+
data: {
|
|
72238
|
+
ownerAddress: submittedMintRequest.ownerAddress,
|
|
72239
|
+
tokenId: submittedMintRequest.tokenId,
|
|
72240
|
+
mintingStatus: submittedMintRequest.status,
|
|
72241
|
+
metadataId: submittedMintRequest.metadataId,
|
|
72242
|
+
lastImtblZkevmMintRequestUpdatedId: submittedMintRequest.imtblZkevmMintRequestUpdatedId,
|
|
72243
|
+
error: submittedMintRequest.error,
|
|
72244
|
+
amount: submittedMintRequest.amount || null
|
|
72245
|
+
}
|
|
72246
|
+
});
|
|
72247
|
+
}
|
|
72248
|
+
else if (!existingAsset) {
|
|
72249
|
+
// Perform insert if no existing record is found
|
|
72250
|
+
await client.imAssets.create({
|
|
72251
|
+
data: {
|
|
72252
|
+
assetId: submittedMintRequest.assetId,
|
|
72253
|
+
contractAddress: submittedMintRequest.contractAddress,
|
|
72254
|
+
ownerAddress: submittedMintRequest.ownerAddress,
|
|
72255
|
+
tokenId: submittedMintRequest.tokenId,
|
|
72256
|
+
mintingStatus: submittedMintRequest.status,
|
|
72257
|
+
metadataId: submittedMintRequest.metadataId,
|
|
72258
|
+
lastImtblZkevmMintRequestUpdatedId: submittedMintRequest.imtblZkevmMintRequestUpdatedId,
|
|
72259
|
+
error: submittedMintRequest.error,
|
|
72260
|
+
amount: submittedMintRequest.amount || null
|
|
72261
|
+
}
|
|
72262
|
+
});
|
|
72263
|
+
}
|
|
72264
|
+
// If existing asset does not meet the update condition, do nothing
|
|
72265
|
+
},
|
|
72266
|
+
updateMintingStatusToSubmissionFailed: async (ids) => {
|
|
72267
|
+
await client.imAssets.updateMany({
|
|
72268
|
+
where: {
|
|
72269
|
+
id: {
|
|
72270
|
+
in: ids
|
|
72271
|
+
}
|
|
72272
|
+
},
|
|
72273
|
+
data: {
|
|
72274
|
+
mintingStatus: 'submission_failed'
|
|
72275
|
+
}
|
|
72276
|
+
});
|
|
72277
|
+
},
|
|
72278
|
+
markAsConflict: async (assetIds, contractAddress) => {
|
|
72279
|
+
await client.imAssets.updateMany({
|
|
72280
|
+
where: {
|
|
72281
|
+
assetId: {
|
|
72282
|
+
in: assetIds // Targets assets where assetId is in the provided list
|
|
72283
|
+
},
|
|
72284
|
+
contractAddress // Additional condition for contract address
|
|
72285
|
+
},
|
|
72286
|
+
data: {
|
|
72287
|
+
mintingStatus: 'conflicting' // Set the new status
|
|
72288
|
+
}
|
|
72289
|
+
});
|
|
72290
|
+
},
|
|
72291
|
+
resetMintingStatus: async (ids) => {
|
|
72292
|
+
await client.imAssets.updateMany({
|
|
72293
|
+
where: {
|
|
72294
|
+
id: {
|
|
72295
|
+
in: ids // Condition to match ids
|
|
72296
|
+
}
|
|
72297
|
+
},
|
|
72298
|
+
data: {
|
|
72299
|
+
mintingStatus: null // Setting minting_status to null
|
|
72300
|
+
}
|
|
72301
|
+
});
|
|
72302
|
+
},
|
|
72303
|
+
// this method is not concurrency safe
|
|
72304
|
+
markForRetry: async (ids) => {
|
|
72305
|
+
// Retrieve the current values of tried_count for the specified ids
|
|
72306
|
+
const assets = await client.imAssets.findMany({
|
|
72307
|
+
where: {
|
|
72308
|
+
id: {
|
|
72309
|
+
in: ids
|
|
72310
|
+
}
|
|
72311
|
+
},
|
|
72312
|
+
select: {
|
|
72313
|
+
id: true,
|
|
72314
|
+
triedCount: true // Assuming the field is named triedCount
|
|
72315
|
+
}
|
|
72316
|
+
});
|
|
72317
|
+
// Update each asset with the new tried_count and nullify minting_status
|
|
72318
|
+
for (const asset of assets) {
|
|
72319
|
+
await client.imAssets.update({
|
|
72320
|
+
where: {
|
|
72321
|
+
id: asset.id
|
|
72322
|
+
},
|
|
72323
|
+
data: {
|
|
72324
|
+
mintingStatus: null,
|
|
72325
|
+
triedCount: asset.triedCount + 1
|
|
72326
|
+
}
|
|
72327
|
+
});
|
|
72328
|
+
}
|
|
72329
|
+
},
|
|
72330
|
+
getMintingRequest: async (contractAddress, referenceId) => {
|
|
72331
|
+
// Retrieve the asset from the database based on contract_address and asset_id
|
|
72332
|
+
const asset = await client.imAssets.findFirst({
|
|
72333
|
+
where: {
|
|
72334
|
+
contractAddress,
|
|
72335
|
+
assetId: referenceId
|
|
72336
|
+
}
|
|
72337
|
+
});
|
|
72338
|
+
if (!asset) {
|
|
72339
|
+
return null;
|
|
72340
|
+
}
|
|
72341
|
+
return {
|
|
72342
|
+
asset_id: asset.assetId,
|
|
72343
|
+
contract_address: asset.contractAddress,
|
|
72344
|
+
id: asset.id,
|
|
72345
|
+
metadata: asset.metadata ? JSON.parse(asset.metadata) : null,
|
|
72346
|
+
owner_address: asset.ownerAddress,
|
|
72347
|
+
tried_count: asset.triedCount,
|
|
72348
|
+
wallet_address: asset.ownerAddress,
|
|
72349
|
+
amount: asset.amount || null
|
|
72350
|
+
};
|
|
72351
|
+
}
|
|
72352
|
+
};
|
|
72353
|
+
};
|
|
72354
|
+
|
|
72355
|
+
// TODO: expose metrics
|
|
72356
|
+
// - submitting status count, conflicting status count
|
|
72357
|
+
// - failed events count
|
|
72358
|
+
const recordMint = async (mintingPersistence, mintRequest) => {
|
|
72359
|
+
trackRecordMint();
|
|
72360
|
+
mintingPersistence.recordMint(mintRequest);
|
|
72361
|
+
};
|
|
72362
|
+
const defaultMintingDelay = 1000;
|
|
72363
|
+
// This function can be called in different node process and will run in parallel to mint assets.
|
|
72364
|
+
// Each process will NOT mint the same asset.
|
|
72365
|
+
// Monitor your database to see if parallel processes are saturating the database CPU and triggering excessive vacuuming.
|
|
72366
|
+
// default batch size will be treated as 100 when it exceeds 100 because minting API has a limit of 100 assets per request.
|
|
72367
|
+
const submitMintingRequests = async (mintingPersistence, blockchainDataSDKClient, { defaultBatchSize = 1000, chainName = 'imtbl-zkevm-testnet', maxNumberOfTries = 3, }, logger = console) => {
|
|
72368
|
+
trackSubmitMintingRequests();
|
|
72369
|
+
let mintingResponse;
|
|
72370
|
+
// eslint-disable-next-line no-constant-condition
|
|
72371
|
+
while (true) {
|
|
72372
|
+
await new Promise((resolve) => {
|
|
72373
|
+
setTimeout(resolve, defaultMintingDelay);
|
|
72374
|
+
});
|
|
72375
|
+
let batchSize = Math.min(mintingResponse?.imx_remaining_mint_requests
|
|
72376
|
+
? parseInt(mintingResponse.imx_remaining_mint_requests, 10)
|
|
72377
|
+
: defaultBatchSize, defaultBatchSize);
|
|
72378
|
+
if (batchSize === 0
|
|
72379
|
+
&& mintingResponse
|
|
72380
|
+
&& new Date(mintingResponse.imx_mint_requests_limit_reset) > new Date()) {
|
|
72381
|
+
logger.info(`minting limit reached, waiting for reset at ${mintingResponse?.imx_mint_requests_limit_reset}`);
|
|
72382
|
+
continue;
|
|
72383
|
+
}
|
|
72384
|
+
if (batchSize === 0) {
|
|
72385
|
+
logger.info(`minting limit has been reset, use default batch size: ${defaultBatchSize}`);
|
|
72386
|
+
mintingResponse = undefined;
|
|
72387
|
+
batchSize = defaultBatchSize;
|
|
72388
|
+
}
|
|
72389
|
+
// get mints to be submitted
|
|
72390
|
+
const pendingMints = await mintingPersistence.getNextBatchForSubmission(batchSize);
|
|
72391
|
+
if (pendingMints.length === 0) {
|
|
72392
|
+
logger.info('no assets to mint');
|
|
72393
|
+
continue;
|
|
72394
|
+
}
|
|
72395
|
+
// chunk assets by every 100 assets. every chunk should all be for the same contract address.
|
|
72396
|
+
const chunkedAssets = pendingMints.sort((a, b) => (a.contract_address > b.contract_address ? 1 : -1)).reduce((acc, row) => {
|
|
72397
|
+
if (acc.length === 0) {
|
|
72398
|
+
return [{ contractAddress: row.contract_address, assets: [row] }];
|
|
72399
|
+
}
|
|
72400
|
+
const lastBatch = acc[acc.length - 1];
|
|
72401
|
+
if (lastBatch.contractAddress === row.contract_address && lastBatch.assets.length < 100) {
|
|
72402
|
+
return [...acc.slice(0, -1), { ...lastBatch, assets: [...lastBatch.assets, row] }];
|
|
72403
|
+
}
|
|
72404
|
+
return [...acc, { contractAddress: row.contract_address, assets: [row] }];
|
|
72405
|
+
}, []);
|
|
72406
|
+
// submit minting request for each contract address in parallel
|
|
72407
|
+
const mintingResults = await Promise.allSettled(chunkedAssets.map(async ({ contractAddress, assets }) => {
|
|
72408
|
+
const mintingRequest = {
|
|
72409
|
+
chainName,
|
|
72410
|
+
contractAddress,
|
|
72411
|
+
createMintRequestRequest: {
|
|
72412
|
+
assets: assets.map((row) => ({
|
|
72413
|
+
reference_id: row.asset_id,
|
|
72414
|
+
owner_address: row.owner_address,
|
|
72415
|
+
metadata: row.metadata,
|
|
72416
|
+
token_id: row.token_id,
|
|
72417
|
+
amount: row.amount ? `${row.amount}` : null,
|
|
72418
|
+
})),
|
|
72419
|
+
},
|
|
72420
|
+
};
|
|
72421
|
+
try {
|
|
72422
|
+
const response = await blockchainDataSDKClient.createMintRequest(mintingRequest);
|
|
72423
|
+
logger.info(`mintingResponse: ${JSON.stringify(response, null, 2)}`);
|
|
72424
|
+
// update minting status to submitted
|
|
72425
|
+
await mintingPersistence.updateMintingStatusToSubmitted(assets.map(({ id }) => id));
|
|
72426
|
+
return response;
|
|
72427
|
+
}
|
|
72428
|
+
catch (e) {
|
|
72429
|
+
logger.error(e);
|
|
72430
|
+
if (e.code === 'CONFLICT_ERROR'
|
|
72431
|
+
&& e.details?.id === 'reference_id') {
|
|
72432
|
+
try {
|
|
72433
|
+
// mark the assets as conflicting, so that we don't send them again.
|
|
72434
|
+
await mintingPersistence.markAsConflict(e.details.values, contractAddress);
|
|
72435
|
+
// remove non conflicting assets status so that they can be retried.
|
|
72436
|
+
await mintingPersistence.resetMintingStatus(assets
|
|
72437
|
+
.map(({ id }) => id)
|
|
72438
|
+
.filter((id) => !e.details.values.includes(id)));
|
|
72439
|
+
}
|
|
72440
|
+
catch (e2) {
|
|
72441
|
+
logger.error(e2);
|
|
72442
|
+
}
|
|
72443
|
+
}
|
|
72444
|
+
else {
|
|
72445
|
+
// separate assets into "need to be retied" and "exceeded max number of tries."
|
|
72446
|
+
const { assetsToRetry, assetsExceededMaxNumberOfTries } = assets.reduce((acc, { tried_count = 0, id }) => {
|
|
72447
|
+
if (tried_count < maxNumberOfTries) {
|
|
72448
|
+
acc.assetsToRetry.push(id);
|
|
72449
|
+
}
|
|
72450
|
+
else {
|
|
72451
|
+
acc.assetsExceededMaxNumberOfTries.push(id);
|
|
72452
|
+
}
|
|
72453
|
+
return acc;
|
|
72454
|
+
}, {
|
|
72455
|
+
assetsToRetry: [],
|
|
72456
|
+
assetsExceededMaxNumberOfTries: [],
|
|
72457
|
+
});
|
|
72458
|
+
// remove all assets status so that they can be retried.
|
|
72459
|
+
await mintingPersistence.markForRetry(assetsToRetry);
|
|
72460
|
+
// mark assets that have exceeded the max number of tries as submission_failed
|
|
72461
|
+
await mintingPersistence.updateMintingStatusToSubmissionFailed(assetsExceededMaxNumberOfTries);
|
|
72462
|
+
}
|
|
72463
|
+
return e;
|
|
72464
|
+
}
|
|
72465
|
+
}));
|
|
72466
|
+
mintingResponse = mintingResults
|
|
72467
|
+
.reverse()
|
|
72468
|
+
.find((r) => r.status === 'fulfilled')?.value;
|
|
72469
|
+
}
|
|
72470
|
+
};
|
|
72471
|
+
const processMint = async (mintingPersistence, event, logger = console) => {
|
|
72472
|
+
trackProcessMint();
|
|
72473
|
+
if (event.event_name !== 'imtbl_zkevm_mint_request_updated') {
|
|
72474
|
+
logger.info(`${event.event_name} is not imtbl_zkevm_mint_request_updated, skip.`);
|
|
72475
|
+
return;
|
|
72476
|
+
}
|
|
72477
|
+
const referenceId = event.data.reference_id;
|
|
72478
|
+
if (!referenceId) {
|
|
72479
|
+
throw new Error('reference_id not found in webhook event');
|
|
72480
|
+
}
|
|
72481
|
+
const contractAddress = event.data.contract_address;
|
|
72482
|
+
if (!contractAddress) {
|
|
72483
|
+
throw new Error('contract_address not found in webhook event');
|
|
72484
|
+
}
|
|
72485
|
+
if (event.data.status === 'failed') {
|
|
72486
|
+
logger.error(`mint failed: ${JSON.stringify(event.data, null, 2)}`);
|
|
72487
|
+
}
|
|
72488
|
+
const mintReq = await mintingPersistence.getMintingRequest(contractAddress, referenceId);
|
|
72489
|
+
if (!mintReq) {
|
|
72490
|
+
logger.info(`minting request not found in the database, ${JSON.stringify(event.data)}`);
|
|
72491
|
+
}
|
|
72492
|
+
const ownerAddress = mintReq?.wallet_address || event.data.owner_address;
|
|
72493
|
+
if (!ownerAddress) {
|
|
72494
|
+
logger.error('owner_address missing');
|
|
72495
|
+
throw new Error('owner_address missing');
|
|
72496
|
+
}
|
|
72497
|
+
await mintingPersistence.syncMintingStatus({
|
|
72498
|
+
tokenId: event.data.token_id,
|
|
72499
|
+
status: event.data.status,
|
|
72500
|
+
assetId: referenceId,
|
|
72501
|
+
contractAddress,
|
|
72502
|
+
ownerAddress,
|
|
72503
|
+
metadataId: event.data.metadata_id,
|
|
72504
|
+
imtblZkevmMintRequestUpdatedId: event.event_id,
|
|
72505
|
+
error: event.data.error ? JSON.stringify(event.data.error) : null,
|
|
72506
|
+
amount: event.data.amount || null,
|
|
72507
|
+
});
|
|
72508
|
+
};
|
|
72509
|
+
|
|
72510
|
+
const noopHandlers = {
|
|
72511
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
72512
|
+
zkevmMintRequestUpdated: async (event) => { },
|
|
72513
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
72514
|
+
others: async (..._args) => { }
|
|
72515
|
+
};
|
|
72516
|
+
class MintingBackendModule {
|
|
72517
|
+
baseConfig;
|
|
72518
|
+
persistence;
|
|
72519
|
+
blockchainDataClient;
|
|
72520
|
+
logger;
|
|
72521
|
+
constructor(config) {
|
|
72522
|
+
this.baseConfig = config.baseConfig;
|
|
72523
|
+
this.persistence = config.persistence;
|
|
72524
|
+
this.logger = config.logger || console;
|
|
72525
|
+
this.blockchainDataClient = new BlockchainData({
|
|
72526
|
+
baseConfig: config.baseConfig
|
|
72527
|
+
});
|
|
72528
|
+
setEnvironment(this.baseConfig.environment);
|
|
72529
|
+
if (this.baseConfig.publishableKey) {
|
|
72530
|
+
setPublishableApiKey(this.baseConfig.publishableKey);
|
|
72531
|
+
}
|
|
72532
|
+
}
|
|
72533
|
+
async recordMint(mintRequest) {
|
|
72534
|
+
await recordMint(this.persistence, mintRequest);
|
|
72535
|
+
}
|
|
72536
|
+
async submitMintingRequests(config) {
|
|
72537
|
+
await submitMintingRequests(this.persistence, this.blockchainDataClient, config);
|
|
72538
|
+
}
|
|
72539
|
+
async processMint(body, otherHandlers = noopHandlers) {
|
|
72540
|
+
await init(body, this.baseConfig.environment, {
|
|
72541
|
+
zkevmMintRequestUpdated: async (event) => {
|
|
72542
|
+
await processMint(this.persistence, event, this.logger);
|
|
72543
|
+
otherHandlers.zkevmMintRequestUpdated(event);
|
|
72544
|
+
},
|
|
72545
|
+
others: otherHandlers.others
|
|
72546
|
+
});
|
|
72547
|
+
}
|
|
72548
|
+
}
|
|
72549
|
+
|
|
72550
|
+
var minting_backend = /*#__PURE__*/Object.freeze({
|
|
72551
|
+
__proto__: null,
|
|
72552
|
+
MintingBackendModule: MintingBackendModule,
|
|
72553
|
+
mintingPersistencePg: mintingPersistence$1,
|
|
72554
|
+
mintingPersistencePrismaSqlite: mintingPersistence,
|
|
72555
|
+
processMint: processMint,
|
|
72556
|
+
recordMint: recordMint,
|
|
72557
|
+
submitMintingRequests: submitMintingRequests
|
|
72558
|
+
});
|
|
72559
|
+
|
|
71950
72560
|
exports.blockchainData = blockchain_data;
|
|
71951
72561
|
exports.checkout = checkout;
|
|
71952
72562
|
exports.config = config;
|
|
72563
|
+
exports.mintingBackend = minting_backend;
|
|
71953
72564
|
exports.orderbook = orderbook;
|
|
71954
72565
|
exports.passport = passport;
|
|
72566
|
+
exports.webhook = webhook;
|
|
71955
72567
|
exports.x = x;
|