Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PFT-1796] Add changes for the snx subgraph code and order #75

Merged
merged 23 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
270 changes: 19 additions & 251 deletions src/common/subgraphMapper.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,40 @@
import {
Account,
Market,
Order,
Position,
PriceFeedSnapshot,
PythData,
Referral,
Token,
Vault,
VaultCooldown,
VaultPosition,
Wallet,
} from '../interfaces/subgraphTypes';

////////////////////////////////////////////////////////////////
////////////////////// ACCOUNT ////////////////////////////
////////////////////////////////////////////////////////////////

export const mapSubgraphResponseToAccountInterface = (response: any): Account | undefined => {
export const mapSubgraphResponseToAccountInterface = (response: any): Wallet | undefined => {
Akhilleshgoswami marked this conversation as resolved.
Show resolved Hide resolved
if (response === null) return undefined;
try {
return {
id: response.id,
totalOrdersCount: response.totalOrdersCount,
openPositionsCount: response.openPositionsCount,
totalPositionsCount: response.totalPositionsCount,
totalReferralsCount: response.totalReferralsCount,
totalReferralRewardsInUsd: response.totalReferralRewardsInUsd,
unclaimedReferralRewardsWeth: response.unclaimedReferralRewardsWeth,
unclaimedReferralRewardsUsdc: response.unclaimedReferralRewardsUsdc,
totalRealizedPnlPositions: response.totalRealizedPnlPositions,
totalRealizedPnlVaults: response.totalRealizedPnlVaults,
openPositionCount:response.openPositionCount,
countProfitablePositions: response.countProfitablePositions,
countLossPositions: response.countLossPositions,
countLiquidatedPositions: response.countLiquidatedPositions,
totalVolumeInUsd: response.totalVolumeInUsd,
totalVolumeInUsdLongs: response.totalVolumeInUsdLongs,
totalVolumeInUsdShorts: response.totalVolumeInUsdShorts,
totalAccruedBorrowingFeesInUsd: response.totalAccruedBorrowingFeesInUsd,
totalStaked: response.totalStaked,
iamamitkumar0512 marked this conversation as resolved.
Show resolved Hide resolved
esPRFBalance: response.esPRFBalance,
};
} catch (error) {
console.log('Error while mapping data', error);
throw error;
}
};

export const mapAccountsArrayToInterface = (response: any): Account[] | undefined => {
Akhilleshgoswami marked this conversation as resolved.
Show resolved Hide resolved
if (response === null) return undefined;
try {
return response.accounts.map((account: Account) => {
return mapSubgraphResponseToAccountInterface(account);
});
} catch (error) {
console.log('Error while mapping data', error);
throw error;
}
};

////////////////////////////////////////////////////////////////
////////////////////// MARKET ////////////////////////////
Expand All @@ -67,53 +45,18 @@ export const mapSingleMarketToInterface = (response: any): Market | undefined =>
try {
return {
id: response.id,
name: response.name,
vaultAddress: response.vaultAddress,
depositToken: response.depositToken ? mapSubgraphResponseToTokenInterface(response.depositToken) : undefined,
isLive: response.isLive,
marketDecimals: response.marketDecimals,
liquidationThreshold: response.liquidationThreshold,
minCollateral: response.minCollateral,
maxLeverage: response.maxLeverage,
openingFee: response.openingFee,
closingFee: response.closingFee,
liquidationFee: response.liquidationFee,
maxPriceDeviation: response.maxPriceDeviation,
createdTimestamp: response.createdTimestamp,
lastUpdated: response.lastUpdated,
maxOpenInterest: response.maxOpenInterest,
totalLongs: response.totalLongs,
avgPriceLongs: response.avgPriceLongs,
pnlLongs: response.pnlLongs,
totalShorts: response.totalShorts,
avgPriceShorts: response.avgPriceShorts,
pnlShorts: response.pnlShorts,
netPnl: response.netPnl,
netPnlDec: response.netPnlDec,
totalOI: response.totalOI,
totalOIAssets: response.totalOIAssets,
accumulatedOILongs: response.accumulatedOILongs,
accumulatedOIShorts: response.accumulatedOIShorts,
closeOnlyMode: response.closeOnlyMode,
feeLastUpdatedTimestamp: response.feeLastUpdatedTimestamp,
priceDeviationLongs: response.priceDeviationLongs,
priceDeviationShorts: response.priceDeviationShorts,
utilizationLongs: response.utilizationLongs,
utilizationShorts: response.utilizationShorts,
marketSkew: response.marketSkew,
baseFeeCumulativeLongs: response.baseFeeCumulativeLongs,
baseFeeCumulativeShorts: response.baseFeeCumulativeShorts,
dynamicFeeCumulativeLongs: response.dynamicFeeCumulativeLongs,
dynamicFeeCumulativeShorts: response.dynamicFeeCumulativeShorts,
deviationCoeff: response.deviationCoeff,
deviationConst: response.deviationConst,
baseCoeff: response.baseCoeff,
baseConst: response.baseConst,
maxDynamicBorrowFee: response.maxDynamicBorrowFee,
dynamicCoeff: response.dynamicCoeff,
transactionHash: response.transactionHash,
senderAddress: response.senderAddress,
pyth: response.pyth ? mapSubgraphResponseToPythDataInterface(response.pyth) : undefined,
name: response.marketName,
symbol : response.marketSymbol,
feedId: response.feedId,
size:response.size,
currentFundingRate: response.currentFundingRate,
currentFundingVelocity: response.currentFundingVelocity,
maxFundingVelocity:response.maxFundingVelocity,
skewScale:response.skewScale,
makerFee:response.makerFee,
takerFee:response.takerFee,
skew:response.skew

};
} catch (error) {
console.log('Error while mapping data', error);
Expand Down Expand Up @@ -142,21 +85,15 @@ export const mapSingleOrderToInterface = (response: any): Order | undefined => {
try {
return {
id: response.id,
market: response.market ? mapSingleMarketToInterface(response.market) : undefined,
user: response.user ? mapSubgraphResponseToAccountInterface(response.user) : undefined,
orderType: response.orderType,
isLong: response.isLong,
market: mapSingleMarketToInterface(response.market),
user: mapSubgraphResponseToAccountInterface(response.user),
isLimitOrder: response.isLimitOrder,
triggerAbove: response.triggerAbove,
deadline: response.deadline,
deadlineISO: response.deadlineISO,
deadline: response.expirationTime,
deltaCollateral: response.deltaCollateral,
deltaSize: response.deltaSize,
deltaSizeUsd: response.deltaSizeUsd,
expectedPrice: response.expectedPrice,
maxSlippage: response.maxSlippage,
partnerAddress: response.partnerAddress,
executionFee: response.executionFee,
executionFee: response.collectedFees,
txHash: response.txHash,
createdTimestamp: response.createdTimestamp,
status: response.status,
Expand All @@ -165,7 +102,6 @@ export const mapSingleOrderToInterface = (response: any): Order | undefined => {
settledTimestampISO: response.settledTimestampISO,
executionPrice: response.executionPrice,
settledBy: response.settledBy ? mapSubgraphResponseToAccountInterface(response.settledBy) : undefined,
cancellationTxHash: response.cancellationTxHash,
positionId: response.position ? response.position.id : undefined,
};
} catch (error) {
Expand Down Expand Up @@ -202,25 +138,18 @@ export const mapSinglePositionToInterface = (response: any): Position | undefine
positionSize: response.positionSize,
avgPrice: response.avgPrice,
avgPriceDec: response.avgPriceDec,
lastCumulativeFee: response.lastCumulativeFee,
status: response.status,
txHash: response.txHash,
liquidationTxHash: response.liquidationTxHash,
closingPrice: response.closingPrice,
realizedPnl: response.realizedPnl,
realizedPnlCollateral: response.realizedPnlCollateral,
realizedFee: response.realizedFee,
realizedFeeCollateral: response.realizedFeeCollateral,
netRealizedPnl: response.netRealizedPnl,
createdTimestamp: response.createdTimestamp,
lastRefresh: response.lastRefresh,
lastRefreshISO: response.lastRefreshISO,
netUnrealizedPnlInCollateral: response.netUnrealizedPnlInCollateral,
netUnrealizedPnlInUsd: response.netUnrealizedPnlInUsd,
liquidationNetPnlInCollateral: response.liquidationNetPnlInCollateral,
accruedBorrowingFeesInCollateral: response.accruedBorrowingFeesInCollateral,
canBeLiquidated: response.canBeLiquidated,
lossToCollateralRatioPercent: response.lossToCollateralRatioPercent,
};
} catch (error) {
console.log('Error while mapping data', error);
Expand Down Expand Up @@ -302,164 +231,3 @@ export const mapSubgraphResponseToPythDataInterface = (response: any): PythData
}
};

////////////////////////////////////////////////////////////////
////////////////////// VAULTS ////////////////////////////
////////////////////////////////////////////////////////////////

export const mapSingleVaultToInterface = (response: any): Vault | undefined => {
if (response === null) return undefined;

try {
return {
id: response.id,
vaultName: response.vaultName,
vaultSymbol: response.vaultSymbol,
vaultDecimals: response.vaultDecimals,
depositToken: response.depositToken ? mapSubgraphResponseToTokenInterface(response.depositToken) : undefined,
isPaused: response.isPaused,
feeManagerAddress: response.feeManagerAddress,
totalAssets: response.totalAssets,
totalShares: response.totalShares,
assetsPerShare: response.assetsPerShare,
assetsPerShareDec: response.assetsPerShareDec,
sharesPerAsset: response.sharesPerAsset,
sharesPerAssetDec: response.sharesPerAssetDec,
withdrawalFee: response.withdrawalFee,
profitFromTraderLosses: response.profitFromTraderLosses,
lossFromTraderProfits: response.lossFromTraderProfits,
cooldownPeriod: response.cooldownPeriod,
withdrawalWindow: response.withdrawalWindow,
daysPassed: response.daysPassed,
cumulativeAPRs: response.cumulativeAPRs,
allTimeApr: response.allTimeApr,
};
} catch (error) {
console.log('Error while mapping data', error);
throw error;
}
};

export const mapVaultsArrayToInterface = (response: any): Vault[] | undefined => {
if (response === null) return undefined;

try {
return response.vaults.map((vault: Vault) => {
return mapSingleVaultToInterface(vault);
});
} catch (error) {
console.log('Error while mapping data', error);
throw error;
}
};

export const mapVaultPositionToInterface = (response: any): VaultPosition | undefined => {
if (response === null) return undefined;

try {
return {
id: response.id,
vault: response.vault ? mapSingleVaultToInterface(response.vault) : undefined,
user: response.user ? mapSubgraphResponseToAccountInterface(response.user) : undefined,
sharesBalance: response.sharesBalance,
totalMinted: response.totalMinted,
totalRedeemed: response.totalRedeemed,
totalDeposited: response.totalDeposited,
totalWithdrawn: response.totalWithdrawn,
avgMintPrice: response.avgMintPrice,
avgMintPriceDec: response.avgMintPriceDec,
realizedPNL: response.realizedPNL,
realizedPNLInUsd: response.realizedPNLInUsd,
unrealizedPNL: response.unrealizedPNL,
timestamp: response.timestamp,
cooldownInitiatedTimestamp: response.cooldownInitiatedTimestamp,
cooldownEnd: response.cooldownEnd,
withdrawalEnds: response.withdrawalEnds,
};
} catch (error) {
console.log('Error while mapping data', error);
throw error;
}
};

export const mapVaultPositionsArrayToInterface = (response: any): VaultPosition[] | undefined => {
if (response === null) return undefined;

try {
return response.vaultPositions.map((vaultPosition: VaultPosition) => {
return mapVaultPositionToInterface(vaultPosition);
});
} catch (error) {
console.log('Error while mapping data', error);
throw error;
}
};

export const mapVaultCooldownToInterface = (response: any): VaultCooldown | undefined => {
if (response === null) return undefined;

try {
return {
id: response.Id,
vault: response.vault ? mapSingleVaultToInterface(response.vault) : undefined,
user: response.user ? mapSubgraphResponseToAccountInterface(response.user) : undefined,
amountAssets: response.amountAssets,
cooldownEnd: response.cooldownEnd,
withdrawalEnds: response.withdrawalEnds,
timestamp: response.timestamp,
};
} catch (error) {
console.log('Error while mapping data', error);
throw error;
}
};

export const mapVaultCooldownArrayToInterface = (response: any): VaultCooldown[] | undefined => {
if (response === null) return undefined;

try {
return response.vaultCooldowns.map((cooldown: VaultCooldown) => {
return mapVaultCooldownToInterface(cooldown);
});
} catch (error) {
console.log('Error while mapping data', error);
throw error;
}
};

////////////////////////////////////////////////////////////////
////////////////////// OTHERS ///////////////////////////
////////////////////////////////////////////////////////////////

export const mapReferralDataToInterface = (response: any): Referral | undefined => {
if (response === null) return undefined;

try {
return {
id: response.id,
partner: response.partner ? mapSubgraphResponseToAccountInterface(response.partner) : undefined,
referredUser: response.partner ? mapSubgraphResponseToAccountInterface(response.partner) : undefined,
sizeInUsd: response.sizeInUsd,
timestamp: response.timestamp,
txHash: response.txHash,
rewardToken: response.rewardToken ? mapSubgraphResponseToTokenInterface(response.rewardToken) : undefined,
referralRewardsInUsd: response.referralRewardsInUsd,
referralRewardsInToken: response.referralRewardsInToken,
};
} catch (error) {
console.log('Error while mapping data', error);
throw error;
}
};

export const mapReferralsArrayToInterface = (response: any): Referral[] | undefined => {
if (response === null) return undefined;

try {
return response.referrals.map((referral: Referral) => {
return mapReferralDataToInterface(referral);
});
} catch (error) {
console.log('Error while mapping data', error);
throw error;
}
};
Loading
Loading