@parifi/sdk
Version:
Parifi SDK with common utility functions
152 lines (148 loc) • 4.45 kB
JavaScript
// src/subgraph/markets/index.ts
import { request } from "graphql-request";
// src/subgraph/markets/subgraphQueries.ts
import { gql } from "graphql-request";
var fetchAllMarketsDataQuery = gql`
{
markets {
id
marketName
marketSymbol
marketPrice
feedId
skew
size
maxOpenInterest
maxMarketValue
interestRate
currentFundingRate
currentFundingVelocity
indexPrice
skewScale
maxFundingVelocity
makerFee
takerFee
marketPrice
initialMarginRatioD18
minimumPositionMargin
maintenanceMarginRatioD18
minimumInitialMarginRatioD18
}
}
`;
var fetchMarketByIdQuery = (marketId) => gql`
{
market(id: "${marketId}") {
id
marketName
marketSymbol
marketPrice
feedId
skew
size
maxOpenInterest
maxMarketValue
interestRate
currentFundingRate
currentFundingVelocity
indexPrice
skewScale
maxFundingVelocity
makerFee
takerFee
marketPrice
initialMarginRatioD18
minimumPositionMargin
maintenanceMarginRatioD18
minimumInitialMarginRatioD18
}
}
`;
// src/common/subgraphMapper.ts
var mapResponseToMarket = (response) => {
if (!response) return void 0;
if (response?.marketName === "" || response?.marketSymbol === "") return void 0;
try {
return {
id: response?.id ?? "",
marketName: response?.marketName ?? "",
marketSymbol: response?.marketSymbol ?? "",
feedId: response?.feedId ?? "",
skew: response?.skew ?? "0",
size: response?.size ?? "0",
maxOpenInterest: response?.maxOpenInterest ?? "0",
interestRate: response?.interestRate ?? "0",
currentFundingRate: response?.currentFundingRate ?? "0",
currentFundingVelocity: response?.currentFundingVelocity ?? "0",
indexPrice: response?.indexPrice ?? "0",
skewScale: response?.skewScale ?? "0",
maxFundingVelocity: response?.maxFundingVelocity ?? "0",
makerFee: response?.makerFee ?? "0",
takerFee: response?.takerFee ?? "0",
maxMarketValue: response?.maxMarketValue ?? "0",
maxMarketSize: response?.maxMarketSize ?? "0",
marketPrice: response?.marketPrice ?? "0",
initialMarginRatioD18: response?.initialMarginRatioD18 ?? "0",
maintenanceMarginRatioD18: response?.maintenanceMarginRatioD18 ?? "0",
minimumInitialMarginRatioD18: response?.minimumInitialMarginRatioD18 ?? "0",
flagRewardRatioD18: response?.flagRewardRatioD18 ?? "0",
minimumPositionMargin: response?.minimumPositionMargin ?? "0",
openInterestUsd: response?.openInterestUsd ?? "0"
};
} catch (error) {
console.log("Error while mapping data", error);
return void 0;
}
};
var mapResponseToMarketArray = (response) => {
if (!response) return void 0;
try {
return response.map((market) => {
return mapResponseToMarket(market);
});
} catch (error) {
console.log("Error while mapping data", error);
return void 0;
}
};
// src/error/not-found.error.ts
var NotFoundError = class extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
}
};
// src/subgraph/markets/index.ts
var getAllMarketsFromSubgraph = async (subgraphEndpoint) => {
try {
const subgraphResponse = await request(subgraphEndpoint, fetchAllMarketsDataQuery);
if (!subgraphResponse) throw Error(`Error while fetching markets`);
const markets = mapResponseToMarketArray(subgraphResponse.markets)?.filter((market) => !!market);
if (markets) return markets;
throw new NotFoundError("Markets not found");
} catch (error) {
console.log("Markets not found. Subgraph request failed", error);
return [];
}
};
var getMarketById = async (subgraphEndpoint, marketId) => {
try {
const subgraphResponse = await request(subgraphEndpoint, fetchMarketByIdQuery(marketId));
if (!subgraphResponse) throw Error(`Error while fetching markets`);
if (subgraphResponse) {
const market = mapResponseToMarket(subgraphResponse.market);
if (market && market.id === marketId) {
return market;
}
}
throw Error(`Market with Market Id ${marketId} Not Found`);
} catch (error) {
console.log(`Market with Market Id ${marketId} Not Found`, error);
throw error;
}
};
export {
getAllMarketsFromSubgraph,
getMarketById
};
//# sourceMappingURL=index.mjs.map