client-aftermath-ts-sdk
Version:
Client Aftermath TypeScript SDK
245 lines (244 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.Staking = void 0;
const caller_1 = require("../../general/utils/caller");
const utils_1 = require("../../general/utils");
/**
* The Staking class provides an interface for interacting with the Staking contract.
*/
class Staking extends caller_1.Caller {
// =========================================================================
// Constructor
// =========================================================================
/**
* Creates a new instance of the Staking class.
* @param network - The network to use for interacting with the Staking contract.
*/
constructor(config, Provider) {
super(config, "staking");
this.Provider = Provider;
// =========================================================================
// Private Helpers
// =========================================================================
this.useProvider = () => {
var _a;
const provider = (_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Staking();
if (!provider)
throw new Error("missing AftermathApi Provider");
return provider;
};
}
// =========================================================================
// Objects
// =========================================================================
/**
* Fetches the list of active validators.
* @returns A Promise that resolves to an array of `SuiValidatorSummary` objects.
*/
getActiveValidators() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("active-validators");
});
}
/**
* Fetches the APYs for all validators.
* @returns A Promise that resolves to a `ValidatorsApy` object.
*/
getValidatorApys() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("validator-apys");
});
}
/**
* Fetches the configuration for all validators.
* @returns A Promise that resolves to an array of `ValidatorConfigObject` objects.
*/
getValidatorConfigs() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("validator-configs");
});
}
/**
* Fetches the staking positions for a given account.
* @param inputs - An object containing the account address and optional pagination parameters.
* @returns A Promise that resolves to an array of `StakingPosition` objects.
*/
getStakingPositions(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("staking-positions", inputs);
});
}
/**
* Fetches the delegated stakes for a given validator.
* @param inputs - An object containing the validator address and optional pagination parameters.
* @returns A Promise that resolves to an array of `SuiDelegatedStake` objects.
*/
getDelegatedStakes(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("delegated-stakes", inputs);
});
}
/**
* Fetches the operation caps for a given validator.
* @param inputs - An object containing the validator address and optional pagination parameters.
* @returns A Promise that resolves to an array of `ValidatorOperationCapObject` objects.
*/
getValidatorOperationCaps(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("validator-operation-caps", inputs);
});
}
// =========================================================================
// Transactions
// =========================================================================
/**
* Fetches the transaction object for staking SUI.
* @param inputs - An object containing the staker address and the amount of SUI to stake.
* @returns A Promise that resolves to a transaction object.
*/
getStakeTransaction(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchBuildStakeTx(inputs);
});
}
/**
* Fetches the transaction object for unstaking SUI.
* @param inputs - An object containing the staker address and the amount of SUI to unstake.
* @returns A Promise that resolves to a transaction object.
*/
getUnstakeTransaction(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchBuildUnstakeTx(inputs);
});
}
/**
* Fetches the transaction object for staking stakedSUI.
* @param inputs - An object containing the staker address and the amount of stakedSUI to stake.
* @returns A Promise that resolves to a transaction object.
*/
getStakeStakedSuiTransaction(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.useProvider().fetchBuildStakeStakedSuiTx(inputs);
});
}
/**
* Fetches the transaction object for updating a validator's fee.
* @param inputs - An object containing the validator address and the new fee percentage.
* @returns A Promise that resolves to a transaction object.
*/
getUpdateValidatorFeeTransaction(inputs) {
return this.useProvider().buildUpdateValidatorFeeTx(inputs);
}
getCrankAfSuiTransaction(inputs) {
return this.useProvider().buildEpochWasChangedTx(inputs);
}
// =========================================================================
// Inspections
// =========================================================================
/**
* Fetches the total value locked in SUI.
* @returns A Promise that resolves to a `Balance` object.
*/
getSuiTvl() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("sui-tvl");
});
}
/**
* Fetches the exchange rate between afSUI and SUI.
* @returns A Promise that resolves to a number.
*/
getAfSuiToSuiExchangeRate() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("afsui-exchange-rate");
});
}
/**
* Fetches the state of the stakedSUI vault.
* @returns A Promise that resolves to a `StakedSuiVaultStateObject` object.
*/
getStakedSuiVaultState() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("staked-sui-vault-state");
});
}
/**
* Fetches the current APY for staking SUI.
* @returns A Promise that resolves to a number.
*/
getApy() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("apy");
});
}
getHistoricalApy(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("historical-apy", inputs);
});
}
// =========================================================================
// Public Static Methods
// =========================================================================
// =========================================================================
// Calculations
// =========================================================================
/**
* Calculates the atomic unstake fee for a given stakedSUI vault state.
* @param inputs - An object containing the stakedSUI vault state.
* @returns A `Percentage` object representing the atomic unstake fee.
*/
static calcAtomicUnstakeFee(inputs) {
const { stakedSuiVaultState } = inputs;
// iia. Calculate the `atomic_unstake_fee`.
if (stakedSuiVaultState.atomicUnstakeSuiReserves >=
stakedSuiVaultState.atomicUnstakeSuiReservesTargetValue) {
// Atomic unstakes that keep the `atomic_unstake_sui_reserves` larger than the desired target
// value receive the minimum fee.
return utils_1.Casting.bigIntToFixedNumber(stakedSuiVaultState.minAtomicUnstakeFee);
}
else {
// Atomic unstakes that shift the `atomic_unstake_sui_reserves` below the desired target value
// receive a variable fee dependent on the distance from the target the unstake brings the
// `atomic_unstake_sui_reserves` to:
// e.g. fee = max_fee - ((max_fee - min_fee) * liquidity_after / target_liquidity_value)
const atomicFeeDelta = stakedSuiVaultState.maxAtomicUnstakeFee -
stakedSuiVaultState.minAtomicUnstakeFee;
return utils_1.Casting.bigIntToFixedNumber(stakedSuiVaultState.maxAtomicUnstakeFee -
(atomicFeeDelta *
stakedSuiVaultState.atomicUnstakeSuiReserves) /
stakedSuiVaultState.atomicUnstakeSuiReservesTargetValue);
}
}
}
exports.Staking = Staking;
// =========================================================================
// Constants
// =========================================================================
/**
* Constants related to staking.
*/
Staking.constants = {
fees: {
protocolUnstake: 0.05,
defaultValidator: 0,
maxValidator: 0.05, // 5%
},
bounds: {
minStake: BigInt("1000000000"),
minUnstake: BigInt("1000000000"),
/**
* Max fee percentage that third parties can charge on staking/unstaking transactions
*/
maxExternalFeePercentage: 0.5, // 50%
},
defaultValidatorFee: 0, // 0%
};