@parcl-finance/product-sdk
Version:
TypeScript SDK for interacting with Parcl's product APIs
503 lines • 20 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.User = void 0;
const defaults_1 = require("./constants/defaults");
const httpClient_1 = require("./httpClient");
const utils_1 = require("./utils");
class User {
_baseUrl;
user;
headers;
client;
expressClient;
constructor({ baseUrl, user, accessToken, env }) {
const headers = {};
if (accessToken !== undefined) {
headers["Authorization"] = "Bearer " + accessToken;
}
this.headers = headers;
this._baseUrl = baseUrl.concat("/user/");
this.user = user;
this.client = new httpClient_1.HttpClient(this._baseUrl + (user ?? "").toString(), this.headers);
this.expressClient = new httpClient_1.HttpClient((0, defaults_1.getDefaultExpressApiUrl)(env), this.headers);
}
setUser(user) {
this.user = user;
}
get baseUrl() {
if (this.user === undefined) {
throw new Error("Missing user address: user routes require a user address to be set.");
}
return this._baseUrl + this.user.toString();
}
async isHighRisk(refresh) {
const { isHighRisk } = await this.client.get({
path: "/risk-screening",
params: { refresh },
});
return isHighRisk;
}
async getSolBalance(refresh) {
const { solBalance } = await this.client.get({
path: "/sol-balance",
params: { refresh },
});
return (0, utils_1.formatAmount)(solBalance, 9);
}
async getCollateralBalance(refresh) {
const { collateralBalance } = await this.client.get({
path: "/collateral-balance",
params: { refresh },
});
return (0, utils_1.formatCollateralAmount)(collateralBalance);
}
async getPrclBalance(refresh) {
const { prclBalance } = await this.client.get({
path: "/prcl-balance",
params: { refresh },
});
return (0, utils_1.formatPrclAmount)(prclBalance);
}
async getMarginAccount(params) {
const res = await this.client.get({
path: "/margin-account",
params,
});
return (0, utils_1.formatMarginAccountInfo)(res.marginAccount);
}
async getMarginAccounts(params) {
const res = await this.client.get({
path: "/margin-accounts",
params,
});
return (0, utils_1.formatMarginAccountsInfo)(res.marginAccounts);
}
async getLpAccount(params) {
const res = await this.client.get({ path: "/lp-account", params });
return (0, utils_1.formatLpAccountInfo)(res.lpAccount);
}
async getLpPosition(params) {
const res = await this.client.get({ path: "/lp-position", params });
return (0, utils_1.formatLpPositionInfo)(res.lpPosition);
}
async getLpPositions(params) {
const res = await this.client.get({ path: "/lp-positions", params });
return (0, utils_1.formatLpPositionsInfo)(res.lpPositions);
}
async getSettlementRequests() {
const res = await this.client.get({ path: "/settlement-requests" });
return res.settlementRequests.map((info) => (0, utils_1.formatSettlementRequestInfo)(info));
}
async getUpgradeLpAccount(payload) {
const res = await this.client.post({
path: "/transaction/upgrade-lp-account",
body: { ...payload },
});
return (0, utils_1.deserializeVersionedTransaction)(res.info.tx);
}
async getAddLiquidityTransaction(payload) {
const res = await this.client.post({
path: "/transaction/add-liquidity",
body: { ...payload, input: (0, utils_1.parseCollateralAmount)(payload.input) },
});
return (0, utils_1.deserializeVersionedTransaction)(res.info.tx);
}
async getRemoveLiquidityTransaction(payload) {
const res = await this.client.post({
path: "/transaction/remove-liquidity",
body: { ...payload, input: (0, utils_1.parseCollateralAmount)(payload.input) },
});
return (0, utils_1.deserializeVersionedTransaction)(res.info.tx);
}
async getCloseLpPositionTransaction(payload) {
const res = await this.client.post({
path: "/transaction/close-lp-position",
body: payload,
});
return (0, utils_1.deserializeVersionedTransaction)(res.info.tx);
}
async getCreateMarginAccountTransaction(payload) {
const res = await this.client.post({
path: "/transaction/create-margin-account",
body: { ...payload },
});
return {
marginAccountId: res.marginAccountId,
tx: (0, utils_1.deserializeVersionedTransaction)(res.info.tx),
};
}
async getDepositMarginTransaction(payload) {
const res = await this.client.post({
path: "/transaction/deposit-margin",
body: { ...payload, margin: (0, utils_1.parseCollateralAmount)(payload.margin) },
});
return (0, utils_1.deserializeVersionedTransaction)(res.info.tx);
}
async getWithdrawMarginTransaction(payload) {
const res = await this.client.post({
path: "/transaction/withdraw-margin",
body: { ...payload, margin: (0, utils_1.parseCollateralAmount)(payload.margin) },
});
return (0, utils_1.deserializeVersionedTransaction)(res.info.tx);
}
async getModifyPositionTransaction(payload) {
(0, utils_1.validateModifyPositionInput)(payload.sizeDelta);
const res = await this.client.post({
path: "/transaction/modify-position",
body: { ...payload, sizeDelta: (0, utils_1.parseSize)(payload.sizeDelta) },
});
return (0, utils_1.deserializeVersionedTransaction)(res.info.tx);
}
async getClosePositionTransaction(payload) {
const res = await this.client.post({
path: "/transaction/close-position",
body: payload,
});
return (0, utils_1.deserializeVersionedTransaction)(res.info.tx);
}
async getCloseMarginAccountTransaction(payload) {
const res = await this.client.post({
path: "/transaction/close-margin-account",
body: payload,
});
return (0, utils_1.deserializeVersionedTransaction)(res.info.tx);
}
async getProcessSettlementRequestsTransactions() {
const res = await this.client.post({
path: "/transaction/process-settlement-requests",
body: {},
});
return res.infos.map(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(info) => (0, utils_1.deserializeVersionedTransaction)(info.tx));
}
async getAddLiquidityQuote(params) {
const { quote } = await this.client.get({
path: "quote/add-liquidity",
params: { ...params, input: (0, utils_1.parseCollateralAmount)(params.input) },
});
return (0, utils_1.formatAddLiquidityQuote)(quote);
}
async getRemoveLiquidityQuote(params) {
const { quote } = await this.client.get({
path: "quote/remove-liquidity",
params: { ...params, input: (0, utils_1.parseCollateralAmount)(params.input) },
});
return (0, utils_1.formatRemoveLiquidityQuote)(quote);
}
async getWithdrawMarginQuote(params) {
const { quote } = await this.client.get({
path: "quote/withdraw-margin",
params,
});
return { maxWithdrawableAmount: (0, utils_1.formatCollateralAmount)(quote.maxWithdrawableAmount) };
}
async getModifyPositionQuote(payload, params) {
// all inputs must be gt 0
(0, utils_1.validateModifyPositionInput)(payload.input);
const input = (0, utils_1.parseModifyPositionInput)(payload.input, payload.inputKind);
const { quote } = await this.client.post({
path: "quote/modify-position",
params,
body: { ...payload, input },
});
return (0, utils_1.formatModifyPositionQuote)(quote);
}
async getClosePositionQuote(payload, params) {
// all inputs must be gt 0
(0, utils_1.validateClosePositionInput)(payload.input);
const input = (0, utils_1.parseClosePositionInput)(payload.input, payload.inputKind);
const { quote } = await this.client.post({
path: "quote/close-position",
params,
body: { ...payload, input },
});
return (0, utils_1.formatModifyPositionQuote)(quote);
}
async getPositionModificationLimitsQuote(payload, params) {
const { quote } = await this.client.post({
path: "quote/position-modification-limits",
params,
body: payload,
});
return (0, utils_1.formatPositionModificationLimitsQuote)(quote);
}
async updateProfileSettings(settingsUpdate, accessToken) {
const update = {
username: settingsUpdate.username ?? null,
defaultLeverage: settingsUpdate.defaultLeverage ?? null,
defaultTheme: settingsUpdate.defaultTheme ?? null,
tradeMode: settingsUpdate.tradeMode ?? null,
interests: settingsUpdate.interests ?? null,
};
const { settings } = await this.client.put({
path: "/profile/settings",
body: { update },
accessToken,
});
return settings;
}
async getProfileSettings(refresh, accessToken) {
const { settings } = await this.client.get({
path: "/profile/settings",
params: { refresh },
accessToken,
});
return {
username: settings.username,
defaultLeverage: settings.defaultLeverage,
defaultTheme: settings.defaultTheme,
tradeMode: settings.tradeMode,
interests: settings.interests ?? undefined,
};
}
async getProfileCommunicationMethods(refresh, accessToken) {
const { communicationMethods } = await this.client.get({
path: "/profile/communication-methods",
params: { refresh },
accessToken,
});
return communicationMethods;
}
async updateProfileCommunicationMethods(communicationMethodsUpdate, accessToken) {
const update = {
email: communicationMethodsUpdate.email ?? null,
discord: communicationMethodsUpdate.discord ?? null,
twitter: communicationMethodsUpdate.twitter ?? null,
};
const { communicationMethods } = await this.client.put({
path: "/profile/communication-methods",
body: { update },
accessToken,
});
return communicationMethods;
}
async getMarginAccountIds(refresh, accessToken) {
const { marginAccountIds } = await this.client.get({
path: "/profile/margin-account-ids",
params: { refresh },
accessToken,
});
return marginAccountIds;
}
async updateMarginAccountIds(marginAccountIdsUpdate, accessToken) {
const { marginAccountIds } = await this.client.put({
path: "/profile/margin-account-ids",
body: { update: marginAccountIdsUpdate },
accessToken,
});
return marginAccountIds;
}
async removeMarginAccountId(marginAccountId, accessToken) {
const { marginAccountIds } = await this.client.post({
path: "/profile/margin-account-ids/remove",
body: { marginAccountId },
accessToken,
});
return marginAccountIds;
}
async getLiquidationHistory(params, accessToken) {
const { history } = await this.client.get({
path: "/history/liquidation",
params,
accessToken,
});
return (0, utils_1.formatLiquidationHistory)(history);
}
async getTradeHistory(params, accessToken) {
const { history } = await this.client.get({
path: "/history/trade",
params,
accessToken,
});
return { ...history, events: (0, utils_1.formatTradeHistoryEvents)(history.events) };
}
async getLpHistory(params, accessToken) {
const { history } = await this.client.get({
path: "/history/lp",
params,
accessToken,
});
return { ...history, events: (0, utils_1.formatLpHistoryEvents)(history.events) };
}
async getMarginTransferHistory(params, accessToken) {
const { history } = await this.client.get({
path: "/history/margin-transfer",
params,
accessToken,
});
return { ...history, events: (0, utils_1.formatMarginTransferHistoryEvents)(history.events) };
}
async isOnboarded(accessToken) {
const { onboarded } = await this.client.get({
path: "/profile/onboard",
accessToken,
});
return onboarded;
}
async onboard(accessToken) {
const { onboarded } = await this.client.post({
path: "/profile/onboard",
accessToken,
});
return onboarded;
}
async getAirdropEventSummary(accessToken) {
const airdropInfo = await this.client.get({
path: "/event/airdrop_claim",
accessToken,
});
return airdropInfo;
}
async getStakingAddresses() {
const { stakingAddresses } = await this.client.get({
path: "/staking-addresses",
});
return stakingAddresses;
}
async getStakingBalance() {
if (this.user === undefined) {
throw new Error("Missing user address: staking requires a user address");
}
const stakingBalance = await this.expressClient.get({
path: `/staking/${this.user.toString()}/balance`,
});
return (0, utils_1.formatStakingBalances)(stakingBalance);
}
async getStakingCurrentEpochPeriod() {
if (this.user === undefined) {
throw new Error("Missing user address: staking requires a user address");
}
const currentEpochPeriod = await this.expressClient.get({
path: `/staking/${this.user.toString()}/epoch-period`,
});
return {
endDate: currentEpochPeriod.endDate,
startDate: currentEpochPeriod.startDate,
};
}
async getStakingLockedEpochPeriod(unlockingEpoch) {
if (this.user === undefined) {
throw new Error("Missing user address: staking requires a user address");
}
const stakingEpochPeriod = await this.expressClient.get({
path: `/staking/${this.user.toString()}/locked-epoch-period`,
params: { unlockingEpoch },
});
return {
endDate: stakingEpochPeriod.lockedPeriodEndDate,
startDate: stakingEpochPeriod.lockedPeriodStartDate,
};
}
async getStakingLockedEpochPeriodEstimate({ addOneEpoch, }) {
if (this.user === undefined) {
throw new Error("Missing user address: staking requires a user address");
}
const stakingEpochPeriod = await this.expressClient.get({
path: `/staking/${this.user.toString()}/locked-epoch-period-estimate`,
params: { addOneEpoch },
});
return {
endDate: stakingEpochPeriod.lockedPeriodEndDate,
startDate: stakingEpochPeriod.lockedPeriodStartDate,
};
}
async getCreateStakingAccountTransactions() {
if (this.user === undefined) {
throw new Error("Missing user address: staking requires a user address");
}
const { transactions } = await this.expressClient.post({
path: `/staking/${this.user.toString()}/create-account`,
});
return transactions.map((tx) => (0, utils_1.deserializeVersionedTransaction)(tx));
}
async getStakeTransactions(stakingAmount, createAccount = false) {
if (this.user === undefined) {
throw new Error("Missing user address: staking requires a user address");
}
const { transactions } = await this.expressClient.post({
path: `/staking/${this.user.toString()}/stake`,
body: { amount: stakingAmount, createAccount },
});
return transactions.map((tx) => (0, utils_1.deserializeVersionedTransaction)(tx));
}
async getUnStakeTransactions(stakingAmount) {
if (this.user === undefined) {
throw new Error("Missing user address: staking requires a user address");
}
const { transactions } = await this.expressClient.post({
path: `/staking/${this.user.toString()}/unstake`,
body: { amount: stakingAmount },
});
return transactions.map((tx) => (0, utils_1.deserializeVersionedTransaction)(tx));
}
async getStakingWithdrawTransactions(stakingAmount) {
if (this.user === undefined) {
throw new Error("Missing user address: staking requires a user address");
}
const { transactions } = await this.expressClient.post({
path: `/staking/${this.user.toString()}/withdraw`,
body: { amount: stakingAmount },
});
return transactions.map((tx) => (0, utils_1.deserializeVersionedTransaction)(tx));
}
async getHistoricalPortfolio({ userAddress, }) {
const { rollup, portfolioPnlByDate, liquidationsByDateByAccount, marginTransfersByDateByAccount, } = await this.expressClient.get({
path: `/portfolio/${userAddress.toString()}`,
});
return {
rollup,
portfolioPnlByDate,
liquidationsByDateByAccount,
marginTransfersByDateByAccount,
};
}
async getLiquidationHistoryV2(params) {
if (this.user === undefined) {
throw new Error("Missing user address: transaction history requires a user address");
}
const history = await this.expressClient.get({
path: `/portfolio/${this.user.toString()}/history/liquidation`,
params,
});
return { ...history, events: (0, utils_1.formatLiquidationEventV2)(history.events) };
}
async getTradeHistoryV2(params) {
if (this.user === undefined) {
throw new Error("Missing user address: transaction history requires a user address");
}
const history = await this.expressClient.get({
path: `/portfolio/${this.user.toString()}/history/trade`,
params,
});
return { ...history, events: (0, utils_1.formatTradeHistoryEventsV2)(history.events) };
}
async getLpHistoryV2(params) {
if (this.user === undefined) {
throw new Error("Missing user address: transaction history requires a user address");
}
const history = await this.expressClient.get({
path: `/portfolio/${this.user.toString()}/history/lp`,
params,
});
return { ...history, events: (0, utils_1.formatLpHistoryEventsV2)(history.events) };
}
async getMarginTransferHistoryV2(params) {
if (this.user === undefined) {
throw new Error("Missing user address: transaction history requires a user address");
}
const history = await this.expressClient.get({
path: `/portfolio/${this.user.toString()}/history/margin-transfer`,
params,
});
return { ...history, events: (0, utils_1.formatMarginTransferHistoryEventsV2)(history.events) };
}
async getHoaImage({ user }) {
const { image } = await this.expressClient.get({
path: `/hoa-staking/${user}`,
});
return image;
}
}
exports.User = User;
//# sourceMappingURL=user.js.map