UNPKG

hm-aftermath-ts-sdk

Version:
197 lines (196 loc) 10.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectsApiHelpers = void 0; const utils_1 = require("../utils"); class ObjectsApiHelpers { // ========================================================================= // Constructor // ========================================================================= constructor(Provider) { this.Provider = Provider; // ========================================================================= // Public Methods // ========================================================================= // ========================================================================= // Fetching // ========================================================================= this.fetchDoesObjectExist = (objectId) => __awaiter(this, void 0, void 0, function* () { const object = yield this.Provider.provider.getObject({ id: objectId }); return object.error === undefined; }); this.fetchIsObjectOwnedByAddress = (inputs) => __awaiter(this, void 0, void 0, function* () { var _a; const { objectId, walletAddress } = inputs; const object = yield this.fetchObject({ objectId }); const objectOwner = (_a = object.data) === null || _a === void 0 ? void 0 : _a.owner; if (!objectOwner || typeof objectOwner !== "object") return false; if ("AddressOwner" in objectOwner && objectOwner.AddressOwner === walletAddress) return true; if ("ObjectOwner" in objectOwner && objectOwner.ObjectOwner === walletAddress) return true; return false; }); this.fetchObjectsOfTypeOwnedByAddress = (inputs) => __awaiter(this, void 0, void 0, function* () { return this.fetchOwnedObjects(Object.assign(Object.assign({}, inputs), { filter: { StructType: utils_1.Helpers.stripLeadingZeroesFromType(inputs.objectType), } })); }); this.fetchOwnedObjects = (inputs) => __awaiter(this, void 0, void 0, function* () { var _b; const { walletAddress, withDisplay, filter } = inputs; let allObjectData = []; let cursor = undefined; do { const paginatedObjects = yield this.Provider.provider.getOwnedObjects({ owner: walletAddress, options: (_b = inputs.options) !== null && _b !== void 0 ? _b : { showContent: true, showDisplay: withDisplay, showOwner: true, showType: true, }, limit: ObjectsApiHelpers.constants.maxObjectFetchingLimit, cursor, filter, }); const objectData = paginatedObjects.data; allObjectData = [...allObjectData, ...objectData]; if (paginatedObjects.data.length === 0 || !paginatedObjects.hasNextPage || !paginatedObjects.nextCursor) return allObjectData; cursor = paginatedObjects.nextCursor; } while (true); }); this.fetchObject = (inputs) => __awaiter(this, void 0, void 0, function* () { const { objectId, withDisplay } = inputs; return yield this.fetchObjectGeneral({ objectId, options: { showContent: true, showDisplay: withDisplay, showOwner: true, showType: true, }, }); }); this.fetchObjectGeneral = (inputs) => __awaiter(this, void 0, void 0, function* () { var _c; const { objectId, options } = inputs; const object = yield this.Provider.provider.getObject({ id: objectId, options, }); if (object.error !== undefined) throw new Error(`an error occured fetching object: ${(_c = object.error) === null || _c === void 0 ? void 0 : _c.code}`); return object; }); this.fetchCastObject = (inputs) => __awaiter(this, void 0, void 0, function* () { return inputs.objectFromSuiObjectResponse(yield this.fetchObject(inputs)); }); this.fetchCastObjectGeneral = (inputs) => __awaiter(this, void 0, void 0, function* () { const { objectId, objectFromSuiObjectResponse, options } = inputs; return objectFromSuiObjectResponse(yield this.fetchObjectGeneral({ objectId, options })); }); this.fetchObjectBatch = (inputs) => __awaiter(this, void 0, void 0, function* () { const { objectIds, options } = inputs; let objectIdsBatches = []; let endIndex = 0; while (true) { const newEndIndex = endIndex + ObjectsApiHelpers.constants.maxObjectFetchingLimit; if (newEndIndex >= objectIds.length) { objectIdsBatches.push(objectIds.slice(endIndex, objectIds.length)); break; } objectIdsBatches.push(objectIds.slice(endIndex, newEndIndex)); endIndex = newEndIndex; } const objectBatches = yield Promise.all(objectIdsBatches.map((objectIds) => this.Provider.provider.multiGetObjects({ ids: objectIds, options: options === undefined ? { showContent: true, showOwner: true, showType: true, } : options, }))); const objectBatch = objectBatches.reduce((acc, objects) => [...acc, ...objects], []); // const objectDataResponses = objectBatch.filter( // (data) => data.error !== undefined // ); // REVIEW: throw error on any objects that don't exist ? // or don't throw any errors and return empty array ? return objectBatch; }); this.fetchCastObjectBatch = (inputs) => __awaiter(this, void 0, void 0, function* () { return (yield this.fetchObjectBatch(inputs)).map((SuiObjectResponse) => { return inputs.objectFromSuiObjectResponse(SuiObjectResponse); }); }); this.fetchCastObjectsOwnedByAddressOfType = (inputs) => __awaiter(this, void 0, void 0, function* () { const objects = (yield this.fetchObjectsOfTypeOwnedByAddress(inputs)).map((SuiObjectResponse) => { return inputs.objectFromSuiObjectResponse(SuiObjectResponse); }); return objects; }); // ========================================================================= // BCS // ========================================================================= this.fetchObjectBcs = (objectId) => __awaiter(this, void 0, void 0, function* () { var _d; const objectResponse = yield this.Provider.provider.getObject({ id: objectId, options: { showBcs: true }, }); if (objectResponse.error !== undefined) throw new Error(`an error occured fetching object: ${(_d = objectResponse.error) === null || _d === void 0 ? void 0 : _d.code}`); return objectResponse; }); this.fetchCastObjectBcs = (inputs) => __awaiter(this, void 0, void 0, function* () { const { objectId } = inputs; const suiObjectResponse = yield this.Provider.Objects().fetchObjectBcs(objectId); return utils_1.Casting.castObjectBcs(Object.assign(Object.assign({}, inputs), { suiObjectResponse: suiObjectResponse })); }); // ========================================================================= // Transactions // ========================================================================= this.burnObjectTx = (inputs) => __awaiter(this, void 0, void 0, function* () { const { tx, object } = inputs; return tx.transferObjects([object], // not using constants because of strange build bug on frontend otherwise // tx.pure(Sui.constants.addresses.zero) "0x0"); }); this.publicShareObjectTx = (inputs) => __awaiter(this, void 0, void 0, function* () { const { tx, object, objectType } = inputs; return tx.moveCall({ target: utils_1.Helpers.transactions.createTxTarget( // not using constants because of strange build bug on frontend otherwise // Sui.constants.addresses.suiPackageId, "0x2", "transfer", "public_share_object"), typeArguments: [objectType], arguments: [object], }); }); } } exports.ObjectsApiHelpers = ObjectsApiHelpers; // ========================================================================= // Private Static Constants // ========================================================================= ObjectsApiHelpers.constants = { maxObjectFetchingLimit: 50, };