blub-sdk
Version:
A modular SDK for interacting with the BLUB ecosystem on the Sui blockchain.
176 lines (175 loc) • 5.74 kB
JavaScript
"use strict";
/* eslint-disable @typescript-eslint/no-explicit-any */
/* -------------------------------------------------------------------------- */
/* Helper functions */
/* -------------------------------------------------------------------------- */
Object.defineProperty(exports, "__esModule", { value: true });
exports.GAS_TYPE_ARG_LONG = exports.GAS_TYPE_ARG = void 0;
exports.getSuiObjectData = getSuiObjectData;
exports.getObjectDeletedResponse = getObjectDeletedResponse;
exports.getObjectNotExistsResponse = getObjectNotExistsResponse;
exports.getObjectReference = getObjectReference;
exports.getObjectId = getObjectId;
exports.getObjectVersion = getObjectVersion;
exports.isSuiObjectResponse = isSuiObjectResponse;
exports.getMovePackageContent = getMovePackageContent;
exports.getMoveObject = getMoveObject;
exports.getMoveObjectType = getMoveObjectType;
exports.getObjectType = getObjectType;
exports.getObjectPreviousTransactionDigest = getObjectPreviousTransactionDigest;
exports.getObjectOwner = getObjectOwner;
exports.getObjectDisplay = getObjectDisplay;
exports.getObjectFields = getObjectFields;
exports.hasPublicTransfer = hasPublicTransfer;
exports.completionCoin = completionCoin;
exports.GAS_TYPE_ARG = "0x2::sui::SUI";
exports.GAS_TYPE_ARG_LONG = "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI";
/* -------------------------- SuiObjectResponse ------------------------- */
function getSuiObjectData(resp) {
return resp.data;
}
function getObjectDeletedResponse(resp) {
if (resp.error &&
"object_id" in resp.error &&
"version" in resp.error &&
"digest" in resp.error) {
const { error } = resp;
return {
objectId: error.object_id,
version: error.version,
digest: error.digest,
};
}
return undefined;
}
function getObjectNotExistsResponse(resp) {
if (resp.error &&
"object_id" in resp.error &&
!("version" in resp.error) &&
!("digest" in resp.error)) {
return resp.error.object_id;
}
return undefined;
}
function getObjectReference(resp) {
if ("reference" in resp) {
return resp.reference;
}
const exists = getSuiObjectData(resp);
if (exists) {
return {
objectId: exists.objectId,
version: exists.version,
digest: exists.digest,
};
}
return getObjectDeletedResponse(resp);
}
/* ------------------------------ SuiObjectRef ------------------------------ */
function getObjectId(data) {
if ("objectId" in data) {
return data.objectId;
}
return (getObjectReference(data)?.objectId ??
getObjectNotExistsResponse(data));
}
function getObjectVersion(data) {
if ("version" in data) {
return data.version;
}
return getObjectReference(data)?.version;
}
/* -------------------------------- SuiObject ------------------------------- */
function isSuiObjectResponse(resp) {
return resp.data !== undefined;
}
function isSuiObjectDataWithContent(data) {
return data.content !== undefined;
}
function getMovePackageContent(data) {
const suiObject = getSuiObjectData(data);
if (suiObject?.content?.dataType !== "package") {
return undefined;
}
return suiObject.content.disassembled;
}
function getMoveObject(data) {
const suiObject = "data" in data ? getSuiObjectData(data) : data;
if (!suiObject ||
!isSuiObjectDataWithContent(suiObject) ||
suiObject.content.dataType !== "moveObject") {
return undefined;
}
return suiObject.content;
}
function getMoveObjectType(resp) {
return getMoveObject(resp)?.type;
}
/**
* Deriving the object type from the object response
* @returns 'package' if the object is a package, move object type(e.g., 0x2::coin::Coin<0x2::sui::SUI>)
* if the object is a move object
*/
function getObjectType(resp) {
const data = isSuiObjectResponse(resp) ? resp.data : resp;
if (!data?.type && "data" in resp) {
if (data?.content?.dataType === "package") {
return "package";
}
return getMoveObjectType(resp);
}
return data?.type;
}
function getObjectPreviousTransactionDigest(resp) {
return getSuiObjectData(resp)?.previousTransaction;
}
function getObjectOwner(resp) {
return getSuiObjectData(resp)?.owner;
}
function getObjectDisplay(resp) {
const display = getSuiObjectData(resp)?.display;
if (!display) {
return { data: null, error: null };
}
return display;
}
/**
* Get the fields of a sui object response or data. The dataType of the object must be moveObject.
* @param {SuiObjectResponse | SuiObjectData}object The object to get the fields from.
* @returns {any} The fields of the object.
*/
function getObjectFields(object) {
const fields = getMoveObject(object)?.fields;
if (fields) {
if ("fields" in fields) {
return fields.fields;
}
return fields;
}
return undefined;
}
/**
* Return hasPublicTransfer of a move object.
* @param {SuiObjectResponse | SuiObjectData}data
* @returns
*/
function hasPublicTransfer(data) {
return getMoveObject(data)?.hasPublicTransfer ?? false;
}
function completionCoin(s) {
const index = s.indexOf("::");
if (index === -1) {
return s;
}
const prefix = s.substring(0, index);
const rest = s.substring(index);
if (prefix.startsWith("0x")) {
return s;
}
const hexStr = prefix.substring(2);
if (hexStr.length > 64) {
return s;
}
const paddedHexStr = hexStr.padStart(64, "0");
return `0x${paddedHexStr}${rest}`;
}