@firefly-exchange/library-sui
Version:
Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui
105 lines (104 loc) • 3.78 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toHex = exports.fromBase64 = exports.toBase64 = exports.hexToUint8Array = exports.writeJSONFile = exports.readJSONFile = exports.getCreatedObjectsIDs = exports.getEvent = exports.sleep = exports.USDC_DECIMALS = exports.TOKEN_DECIMALS = void 0;
const fs_1 = __importDefault(require("fs"));
// Decimals used by SUI and BLUE coin
exports.TOKEN_DECIMALS = 9;
// Decimals used by USDC coin
exports.USDC_DECIMALS = 6;
async function sleep(timeInMs) {
await new Promise(resolve => setTimeout(resolve, timeInMs));
}
exports.sleep = sleep;
function getEvent(txResponse, eventName) {
const events = [];
for (const event of txResponse.events || []) {
if (event.type.endsWith(eventName))
events.push(event);
}
return events;
}
exports.getEvent = getEvent;
function getCreatedObjectsIDs(txResponse) {
const objects = {};
for (const object of txResponse.objectChanges) {
if (object.type == "mutated")
continue;
// only Packages get published
if (object.type == "published") {
objects["Package"] = object.packageId;
}
else if (object.type == "created") {
try {
const type = (object.objectType.match(/^(?<pkg>[\w]+)::(?<mod>[\w]+)::(?<type>[\w]+)$/)?.groups)["type"];
objects[type] = object.objectId;
}
catch (e) {
// eslint-disable-next-line
const match = object.objectType.match(/(?<=\::)(.*?)(?=\<)/)[0];
const type = match.split("::")[1];
if (type == "CoinMetadata") {
objects["Currency"] =
// eslint-disable-next-line
object.objectType.match(/(?<=\<)(.*?)(?=\>)/)[0];
}
else {
objects[type] = object.objectId;
}
}
}
}
return objects;
}
exports.getCreatedObjectsIDs = getCreatedObjectsIDs;
function readJSONFile(filePath) {
return fs_1.default.existsSync(filePath)
? JSON.parse(fs_1.default.readFileSync(filePath).toString())
: {};
}
exports.readJSONFile = readJSONFile;
function writeJSONFile(filePath, content) {
fs_1.default.writeFileSync(filePath, JSON.stringify(content));
}
exports.writeJSONFile = writeJSONFile;
function hexToUint8Array(hex) {
// Remove the '0x' prefix if present
if (hex.startsWith("0x") || hex.startsWith("0X")) {
hex = hex.slice(2);
}
// Ensure even length
if (hex.length % 2 !== 0) {
hex = "0" + hex;
}
const byteArray = new Uint8Array(hex.length / 2);
for (let i = 0; i < byteArray.length; i++) {
byteArray[i] = parseInt(hex.substr(i * 2, 2), 16);
}
return byteArray;
}
exports.hexToUint8Array = hexToUint8Array;
const CHUNK_SIZE = 8192;
function toBase64(bytes) {
// Special-case the simple case for speed's sake.
if (bytes.length < CHUNK_SIZE) {
return btoa(String.fromCharCode(...bytes));
}
let output = "";
for (let i = 0; i < bytes.length; i += CHUNK_SIZE) {
const chunk = bytes.slice(i, i + CHUNK_SIZE);
output += String.fromCharCode(...chunk);
}
return btoa(output);
}
exports.toBase64 = toBase64;
function fromBase64(base64String) {
return Uint8Array.from(atob(base64String), char => char.charCodeAt(0));
}
exports.fromBase64 = fromBase64;
function toHex(bytes) {
return bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
}
exports.toHex = toHex;
;