aftermath-ts-sdk
Version:
Aftermath TypeScript SDK
219 lines (218 loc) • 10.4 kB
JavaScript
"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.SuiFrens = void 0;
const suiFren_1 = require("./suiFren");
const stakedSuiFren_1 = require("./stakedSuiFren");
const caller_1 = require("../../general/utils/caller");
const coin_1 = require("../coin");
const utils_1 = require("../../general/utils");
class SuiFrens extends caller_1.Caller {
// =========================================================================
// Constructor
// =========================================================================
constructor(config, Provider) {
super(config, "sui-frens");
this.Provider = Provider;
// =========================================================================
// Private Helpers
// =========================================================================
this.useProvider = () => {
var _a;
const provider = (_a = this.Provider) === null || _a === void 0 ? void 0 : _a.SuiFrens();
if (!provider)
throw new Error("missing AftermathApi Provider");
return provider;
};
}
// =========================================================================
// Public Methods
// =========================================================================
// =========================================================================
// Calculations
// =========================================================================
static calcTotalInternalMixFee(inputs) {
const { mixFee1, mixFee2 } = inputs;
if (mixFee1 === undefined && mixFee2 === undefined)
return this.constants.protocolFees.mixOwned;
if (mixFee1 !== undefined && mixFee2 !== undefined) {
return (this.calcMixFeeForStakedSuiFren({ mixFee: mixFee1 }) +
this.calcMixFeeForStakedSuiFren({ mixFee: mixFee2 }));
}
return mixFee1 !== undefined
? this.calcMixFeeForStakedSuiFren({ mixFee: mixFee1 })
: mixFee2 !== undefined
? this.calcMixFeeForStakedSuiFren({ mixFee: mixFee2 })
: (() => {
// to make TS happy :)
throw new Error("unreachable");
})();
}
static calcMixFeeForStakedSuiFren(inputs) {
const { mixFee } = inputs;
return (mixFee +
utils_1.Helpers.maxBigInt(this.constants.protocolFees.minMixStaked, mixFee /
BigInt(Math.floor(this.constants.protocolFees.mixStakedPercentage *
100))));
}
// =========================================================================
// Class Objects
// =========================================================================
getSuiFren(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const suiFrens = yield this.getSuiFrens({
suiFrenObjectIds: [inputs.suiFrenObjectId],
});
return suiFrens[0];
});
}
getSuiFrens(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const suiFrens = yield this.fetchApi(`${JSON.stringify(inputs.suiFrenObjectIds)}`);
return suiFrens.map((suiFren) => new suiFren_1.SuiFren(suiFren, this.config));
});
}
getOwnedSuiFrens(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const ownedSuiFrens = yield this.fetchApi(`owned-sui-frens`, inputs);
return ownedSuiFrens.map((suiFren) => new suiFren_1.SuiFren(suiFren, this.config, false, true));
});
}
getOwnedStakedSuiFrens(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const stakesInfo = yield this.fetchApi(`owned-staked-sui-frens`, inputs);
return stakesInfo.map((info) => new stakedSuiFren_1.StakedSuiFren(info, this.config, true));
});
}
getAllStakedSuiFrens(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const stakesInfoWithCursor = yield this.fetchApi(`filtered-staked-sui-frens/${SuiFrens.createStakedSuiFrensQueryString(inputs)}`, inputs);
const suiFrens = stakesInfoWithCursor.dynamicFieldObjects.map((info) => new stakedSuiFren_1.StakedSuiFren(info, this.config));
return {
dynamicFieldObjects: suiFrens,
nextCursor: stakesInfoWithCursor.nextCursor,
};
});
}
getStakedSuiFrens(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const suiFrenInfos = yield this.fetchApi(`staked-sui-frens/${JSON.stringify(inputs.stakedSuiFrenIds)}`);
return suiFrenInfos.map((info) => new stakedSuiFren_1.StakedSuiFren(info, this.config));
});
}
// =========================================================================
// Objects
// =========================================================================
getCapyLabsApp() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi(`capy-labs-app`);
});
}
getOwnedAccessories(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("owned-accessories", inputs);
});
}
// =========================================================================
// Events
// =========================================================================
getHarvestFeesEvents(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiEvents("events/harvest-fees", inputs);
});
}
getMixEvents(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiEvents("events/mix", inputs);
});
}
getStakeEvents(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiEvents("events/stake", inputs);
});
}
getUnstakeEvents(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiEvents("events/unstake", inputs);
});
}
// =========================================================================
// Transactions
// =========================================================================
getMixTransaction(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchBuildMixTx(inputs);
});
}
getHarvestFeesTransaction(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchBuildHarvestFeesTx(inputs);
});
}
// =========================================================================
// Inspections
// =========================================================================
getStats() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("stats");
});
}
// =========================================================================
// Public Static Methods
// =========================================================================
// =========================================================================
// Helpers
// =========================================================================
static suiFren(suiFren) {
return suiFren instanceof suiFren_1.SuiFren ? suiFren : suiFren === null || suiFren === void 0 ? void 0 : suiFren.suiFren;
}
static suiFrenId(suiFren) {
var _a, _b;
return (suiFren === null || suiFren === void 0 ? void 0 : suiFren.suiFren) instanceof suiFren_1.SuiFren
? (_a = suiFren === null || suiFren === void 0 ? void 0 : suiFren.suiFren) === null || _a === void 0 ? void 0 : _a.suiFren.objectId
: (_b = suiFren === null || suiFren === void 0 ? void 0 : suiFren.suiFren) === null || _b === void 0 ? void 0 : _b.objectId;
}
static mixFee(suiFren) {
return suiFren instanceof stakedSuiFren_1.StakedSuiFren ? suiFren === null || suiFren === void 0 ? void 0 : suiFren.mixFee() : undefined;
}
// =========================================================================
// Private Static Methods
// =========================================================================
// =========================================================================
// Helpers
// =========================================================================
static createStakedSuiFrensQueryString(inputs) {
const { attributes, sortBy } = inputs;
const startStr = sortBy ? `?sort=${sortBy}` : "";
return Object.keys(attributes).length === 0
? startStr
: (startStr === "" ? "?" : startStr) +
Object.entries(attributes)
.map(([key, val], i) => `${i === 0 && startStr === "" ? "" : "&"}${key}=${val}`)
.reduce((acc, curr) => acc + curr, "");
}
}
exports.SuiFrens = SuiFrens;
// =========================================================================
// Constants
// =========================================================================
SuiFrens.constants = {
mixingFeeCoinType: coin_1.Coin.constants.suiCoinType,
protocolFees: {
mint: BigInt(250000000), // 0.25 SUI
mixOwned: BigInt(250000000), // 0.25 SUI
minMixStaked: BigInt(250000000), // 0.25 SUI
mixStakedPercentage: 0.1, // 10%
},
suifrenFees: {
mint: BigInt(8000000000), // 8 SUI
},
};