UNPKG

client-aftermath-ts-sdk

Version:
129 lines (128 loc) 6.19 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.Casting = void 0; const bcs_1 = require("@mysten/sui/bcs"); const suiFrensApiCasting_1 = require("../../packages/suiFrens/api/suiFrensApiCasting"); const faucetApiCasting_1 = require("../../packages/faucet/api/faucetApiCasting"); const nftAmmApiCasting_1 = require("../../packages/nftAmm/api/nftAmmApiCasting"); const poolsApiCasting_1 = require("../../packages/pools/api/poolsApiCasting"); const stakingApiCasting_1 = require("../../packages/staking/api/stakingApiCasting"); const routerApiCasting_1 = require("../../packages/router/api/routerApiCasting"); const fixedUtils_1 = require("./fixedUtils"); const iFixedUtils_1 = require("./iFixedUtils"); const perpetualsApiCasting_1 = require("../../packages/perpetuals/api/perpetualsApiCasting"); const farmsApiCasting_1 = require("../../packages/farms/api/farmsApiCasting"); const leveragedStakingApiCasting_1 = require("../../packages/leveragedStaking/api/leveragedStakingApiCasting"); const __1 = require("../.."); const nftsApiCasting_1 = require("../nfts/nftsApiCasting"); const oracleApiCasting_1 = require("../../packages/oracle/api/oracleApiCasting"); /** * Utility class for casting and conversion functions. * @class */ class Casting { // ========================================================================= // Fixed // ========================================================================= static percentageToBps(percentage) { // Convert decimal percentage to basis points const bps = percentage * 10000; // Convert basis points to bigint const bpsBigint = BigInt(Math.round(bps)); return bpsBigint; } static bpsToPercentage(bps) { // Convert bigint basis points to number const bpsNumber = Number(bps); // Convert basis points to decimal percentage const percentage = bpsNumber / 10000; return percentage; } static bcsBytesFromSuiObjectResponse(suiObjectResponse) { var _b, _c; const rawData = (_b = suiObjectResponse.data) === null || _b === void 0 ? void 0 : _b.bcs; if (rawData && "bcsBytes" in rawData) return rawData.bcsBytes; throw new Error(`no bcs bytes found on object: ${(_c = suiObjectResponse.data) === null || _c === void 0 ? void 0 : _c.objectId}`); } } exports.Casting = Casting; _a = Casting; // ========================================================================= // Api Casting // ========================================================================= Casting.pools = poolsApiCasting_1.PoolsApiCasting; Casting.suiFrens = suiFrensApiCasting_1.SuiFrensApiCasting; Casting.faucet = faucetApiCasting_1.FaucetApiCasting; Casting.staking = stakingApiCasting_1.StakingApiCasting; Casting.leveragedStaking = leveragedStakingApiCasting_1.LeveragedStakingApiCasting; Casting.nftAmm = nftAmmApiCasting_1.NftAmmApiCasting; Casting.router = routerApiCasting_1.RouterApiCasting; Casting.perpetuals = perpetualsApiCasting_1.PerpetualsApiCasting; Casting.oracle = oracleApiCasting_1.OracleApiCasting; Casting.farms = farmsApiCasting_1.FarmsApiCasting; Casting.nfts = nftsApiCasting_1.NftsApiCasting; // ========================================================================= // Constants // ========================================================================= // ========================================================================= // Fixed / IFixed // ========================================================================= Casting.Fixed = fixedUtils_1.FixedUtils; Casting.IFixed = iFixedUtils_1.IFixedUtils; Casting.u64MaxBigInt = BigInt("0xFFFFFFFFFFFFFFFF"); // ========================================================================= // Functions // ========================================================================= // ========================================================================= // Fixed // ========================================================================= // TODO: only use fixed class for these functions, remove from here Casting.numberToFixedBigInt = (a) => BigInt(Math.floor(a * _a.Fixed.fixedOneN)); Casting.bigIntToFixedNumber = (a) => Number(a) / _a.Fixed.fixedOneN; Casting.scaleNumberByBigInt = (scalar, int) => BigInt(Math.floor(scalar * Number(int))); // ========================================================================= // Bytes / BCS // ========================================================================= Casting.stringFromBytes = (bytes) => String.fromCharCode.apply(null, bytes); Casting.bigIntFromBytes = (bytes) => BigInt("0x" + bytes .reverse() .map((byte) => byte.toString(16).padStart(2, "0")) .join("")); Casting.addressFromBcsBytes = (bytes) => __1.Helpers.addLeadingZeroesToType(bcs_1.bcs.Address.parse(new Uint8Array(bytes))); Casting.addressFromBytes = (bytes) => __1.Helpers.addLeadingZeroesToType("0x" + bytes .map((byte) => { const hex = byte.toString(16); return hex.length === 1 ? "0" + hex : hex; }) .join("")); Casting.addressFromStringBytes = (bytes) => _a.addressFromBytes(_a.bytesFromStringBytes(bytes)); Casting.bytesFromStringBytes = (bytes) => bytes.map((byte) => Number(byte)); Casting.unwrapDeserializedOption = (deserializedData) => { // return "vec" in deserializedData // ? deserializedData.vec.length > 0 // ? deserializedData.vec[0] // : undefined // : undefined; return "Some" in deserializedData ? deserializedData.Some : undefined; }; Casting.u8VectorFromString = (str) => { const textEncode = new TextEncoder(); const encodedStr = textEncode.encode(str); let uint8s = []; for (const uint8 of encodedStr.values()) { uint8s.push(uint8); } return uint8s; }; Casting.normalizeSlippageTolerance = (slippageTolerance) => { return slippageTolerance / 100; }; Casting.castObjectBcs = (inputs) => { const { suiObjectResponse, bcsType, fromDeserialized } = inputs; const deserialized = bcsType.fromBase64(_a.bcsBytesFromSuiObjectResponse(suiObjectResponse)); return fromDeserialized(deserialized); };