@moonwell-fi/moonwell-sdk
Version:
TypeScript Interface for Moonwell
255 lines • 10.9 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LunarIndexerClient = exports.DEFAULT_LUNAR_TIMEOUT_MS = exports.LunarIndexerError = void 0;
exports.shouldFallback = shouldFallback;
exports.createLunarIndexerClient = createLunarIndexerClient;
const axios_1 = __importDefault(require("axios"));
const retry_js_1 = require("./retry.js");
class LunarIndexerError extends Error {
constructor(message, statusCode, endpoint, originalError) {
super(message);
Object.defineProperty(this, "statusCode", {
enumerable: true,
configurable: true,
writable: true,
value: statusCode
});
Object.defineProperty(this, "endpoint", {
enumerable: true,
configurable: true,
writable: true,
value: endpoint
});
Object.defineProperty(this, "originalError", {
enumerable: true,
configurable: true,
writable: true,
value: originalError
});
this.name = "LunarIndexerError";
}
}
exports.LunarIndexerError = LunarIndexerError;
function shouldFallback(error) {
if (axios_1.default.isAxiosError(error)) {
const axiosError = error;
const isNetworkError = !axiosError.response;
const is5xxError = !!axiosError.response && axiosError.response.status >= 500;
const is404Error = !!axiosError.response && axiosError.response.status === 404;
if (isNetworkError || is5xxError || is404Error) {
return true;
}
return false;
}
return true;
}
exports.DEFAULT_LUNAR_TIMEOUT_MS = 10_000;
class LunarIndexerClient {
constructor(config) {
Object.defineProperty(this, "client", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "stakingClient", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "vaultsClient", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.client = axios_1.default.create({
baseURL: `${config.baseUrl}/api/v1/core`,
timeout: config.timeout || exports.DEFAULT_LUNAR_TIMEOUT_MS,
headers: {
"Content-Type": "application/json",
},
});
this.stakingClient = axios_1.default.create({
baseURL: `${config.baseUrl}/api/v1/staking`,
timeout: config.timeout || exports.DEFAULT_LUNAR_TIMEOUT_MS,
headers: {
"Content-Type": "application/json",
},
});
this.vaultsClient = axios_1.default.create({
baseURL: `${config.baseUrl}/api/v1/vaults`,
timeout: config.timeout || exports.DEFAULT_LUNAR_TIMEOUT_MS,
headers: {
"Content-Type": "application/json",
},
});
(0, retry_js_1.attachRetryInterceptor)(this.client);
(0, retry_js_1.attachRetryInterceptor)(this.stakingClient);
(0, retry_js_1.attachRetryInterceptor)(this.vaultsClient);
}
async getComptroller(chainId) {
try {
const response = await this.client.get(`/comptroller/${chainId}`);
return response.data;
}
catch (error) {
throw new LunarIndexerError(`Failed to fetch comptroller for chain ${chainId}`, axios_1.default.isAxiosError(error) ? error.response?.status : undefined, `/comptroller/${chainId}`, error);
}
}
async listMarkets(chainId, options) {
try {
const params = {};
if (options?.limit)
params.limit = options.limit.toString();
if (options?.cursor)
params.cursor = options.cursor;
const response = await this.client.get(`/markets/${chainId}`, { params });
return response.data;
}
catch (error) {
throw new LunarIndexerError(`Failed to list markets for chain ${chainId}`, axios_1.default.isAxiosError(error) ? error.response?.status : undefined, `/markets/${chainId}`, error);
}
}
async getMarket(marketId) {
try {
const response = await this.client.get(`/market/${marketId}`);
return response.data;
}
catch (error) {
throw new LunarIndexerError(`Failed to fetch market ${marketId}`, axios_1.default.isAxiosError(error) ? error.response?.status : undefined, `/market/${marketId}`, error);
}
}
async getMarketSnapshots(marketId, options) {
try {
const params = {};
if (options?.limit)
params.limit = options.limit.toString();
if (options?.cursor)
params.cursor = options.cursor;
if (options?.granularity)
params.granularity = options.granularity;
if (options?.startTime)
params.startTime = options.startTime.toString();
if (options?.endTime)
params.endTime = options.endTime.toString();
const response = await this.client.get(`/market/${marketId}/snapshots`, { params });
return response.data;
}
catch (error) {
throw new LunarIndexerError(`Failed to fetch market snapshots for ${marketId}`, axios_1.default.isAxiosError(error) ? error.response?.status : undefined, `/market/${marketId}/snapshots`, error);
}
}
async listTokens(chainId, options) {
try {
const params = {};
if (options?.limit)
params.limit = options.limit.toString();
if (options?.cursor)
params.cursor = options.cursor;
const response = await this.client.get(`/tokens/${chainId}`, { params });
return response.data;
}
catch (error) {
throw new LunarIndexerError(`Failed to list tokens for chain ${chainId}`, axios_1.default.isAxiosError(error) ? error.response?.status : undefined, `/tokens/${chainId}`, error);
}
}
async getToken(tokenId) {
try {
const response = await this.client.get(`/token/${tokenId}`);
return response.data;
}
catch (error) {
throw new LunarIndexerError(`Failed to fetch token ${tokenId}`, axios_1.default.isAxiosError(error) ? error.response?.status : undefined, `/token/${tokenId}`, error);
}
}
async getAccountPortfolio(accountAddress, options) {
try {
const params = {
startTime: options.startTime.toString(),
endTime: options.endTime.toString(),
};
if (options.granularity)
params.granularity = options.granularity;
if (options.chainId)
params.chainId = options.chainId.toString();
if (options.market)
params.market = options.market;
const response = await this.client.get(`/account/${accountAddress.toLowerCase()}/portfolio`, { params });
return response.data;
}
catch (error) {
throw new LunarIndexerError(`Failed to fetch portfolio for account ${accountAddress}`, axios_1.default.isAxiosError(error) ? error.response?.status : undefined, `/account/${accountAddress}/portfolio`, error);
}
}
async getStakingSnapshots(chainId, options) {
try {
const params = {};
if (options?.limit)
params.limit = options.limit.toString();
if (options?.cursor)
params.cursor = options.cursor;
if (options?.granularity)
params.granularity = options.granularity;
if (options?.startTime)
params.startTime = options.startTime.toString();
if (options?.endTime)
params.endTime = options.endTime.toString();
const response = await this.stakingClient.get(`/snapshots/${chainId}`, { params });
return response.data;
}
catch (error) {
throw new LunarIndexerError(`Failed to fetch staking snapshots for chain ${chainId}`, axios_1.default.isAxiosError(error) ? error.response?.status : undefined, `/snapshots/${chainId}`, error);
}
}
async getVaultAccountPortfolio(accountAddress, options) {
try {
const params = {
startTime: options.startTime.toString(),
endTime: options.endTime.toString(),
};
if (options.granularity)
params.granularity = options.granularity;
if (options.chainId)
params.chainId = options.chainId.toString();
if (options.vault)
params.vault = options.vault;
const response = await this.vaultsClient.get(`/account/${accountAddress.toLowerCase()}/portfolio`, { params });
return response.data;
}
catch (error) {
throw new LunarIndexerError(`Failed to fetch vault portfolio for account ${accountAddress}`, axios_1.default.isAxiosError(error) ? error.response?.status : undefined, `/account/${accountAddress}/portfolio`, error);
}
}
async getVaultStakingSnapshots(chainId, options) {
try {
const params = {};
if (options?.limit)
params.limit = options.limit.toString();
if (options?.cursor)
params.cursor = options.cursor;
if (options?.granularity)
params.granularity = options.granularity;
if (options?.startTime)
params.startTime = options.startTime.toString();
if (options?.endTime)
params.endTime = options.endTime.toString();
if (options?.vaultAddress)
params.vaultAddress = options.vaultAddress;
const response = await this.stakingClient.get(`/vault-snapshots/${chainId}`, { params });
return response.data;
}
catch (error) {
throw new LunarIndexerError(`Failed to fetch vault staking snapshots for chain ${chainId}`, axios_1.default.isAxiosError(error) ? error.response?.status : undefined, `/vault-snapshots/${chainId}`, error);
}
}
}
exports.LunarIndexerClient = LunarIndexerClient;
function createLunarIndexerClient(config) {
return new LunarIndexerClient(config);
}
//# sourceMappingURL=lunar-indexer-client.js.map