Skip to content

Commit

Permalink
[PFT-1088] Add function to return position ids for order ids (#59)
Browse files Browse the repository at this point in the history
* Add function to return position ids for order ids

* 0.1.26
  • Loading branch information
sudeepb02 committed Jun 13, 2024
1 parent c04b7d8 commit 8434123
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@parifi/sdk",
"version": "0.1.25",
"version": "0.1.26",
"description": "Parifi SDK with common utility functions",
"files": [
"dist",
Expand Down
6 changes: 6 additions & 0 deletions src/subgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
getAllOrdersByUserAddress,
getAllPendingOrders,
getOrderById,
getPositionIdsFromOrderIds,
getPythPriceIdsForOrderIds,
getReferralDataForPartner,
} from './orders';
Expand Down Expand Up @@ -143,6 +144,11 @@ export class Subgraph {
return await getPythPriceIdsForOrderIds(subgraphEndpoint, orderIds);
}

public async getPositionIdsFromOrderIds(orderIds: string[]): Promise<{ orderId: string; positionId: string }[]> {
const subgraphEndpoint = this.getSubgraphEndpoint(this.rpcConfig.chainId);
return await getPositionIdsFromOrderIds(subgraphEndpoint, orderIds);
}

////////////////////////////////////////////////////////////////
////////////////////// POSITION //////////////////////////
////////////////////////////////////////////////////////////////
Expand Down
39 changes: 38 additions & 1 deletion src/subgraph/orders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import {
fetchOrdersByUserQuery,
fetchPartnerRewards,
fetchPendingOrdersQuery,
fetchPositionIdsForOrderIds,
fetchPriceIdsFromOrderIdsQuery,
} from './subgraphQueries';
import {
mapOrdersArrayToInterface,
mapReferralsArrayToInterface,
mapSingleOrderToInterface,
} from '../../common/subgraphMapper';
import { EMPTY_BYTES32, getPriceIdsForCollaterals, getUniqueValuesFromArray } from '../../common';
import { EMPTY_BYTES32, getUniqueValuesFromArray } from '../../common';
import { NotFoundError } from '../../error/not-found.error';

// Get all order by a user address
Expand Down Expand Up @@ -108,3 +109,39 @@ export const getReferralDataForPartner = async (
throw error;
}
};

// Returns the position id related to the order id. If the order ID or position Id does not exists
// EMPTY_BYTES32 (0x0000) is returned
export const getPositionIdsFromOrderIds = async (
subgraphEndpoint: string,
orderIds: string[],
): Promise<{ orderId: string; positionId: string }[]> => {
let result: { orderId: string; positionId: string }[] = [];

// Interface for subgraph response
interface ApiResponse {
orders: Array<{
id: string;
position: {
id: string;
} | null;
}>;
}

try {
const subgraphResponse: ApiResponse = await request(subgraphEndpoint, fetchPositionIdsForOrderIds(orderIds));

orderIds.forEach((id) => {
let pid: string = EMPTY_BYTES32;
const res = subgraphResponse.orders.find((order) => order.id === id);
if (res) {
pid = res.position?.id ?? EMPTY_BYTES32;
}
result.push({ orderId: id, positionId: pid });
});
} catch (error) {
throw error;
}
console.log(result);
return result;
};
17 changes: 15 additions & 2 deletions src/subgraph/orders/subgraphQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const fetchPendingOrdersQuery = (
export const fetchOrdersByIdQuery = (orderId: string) =>
gql`
{
order(id: "${orderId}") {
order(id: "${orderId.toLowerCase()}") {
id
user {
id
Expand Down Expand Up @@ -136,7 +136,7 @@ export const fetchPriceIdsFromOrderIdsQuery = (orderIds: string[]) =>
{
orders(
where: {
id_in: [${orderIds.map((id) => `"${id}"`).join(', ')}]
id_in: [${orderIds.map((id) => `"${id.toLowerCase()}"`).join(', ')}]
}
) {
id
Expand Down Expand Up @@ -170,3 +170,16 @@ export const fetchPartnerRewards = (partnerAddress: string, count: number = 20,
}
}
`;

export const fetchPositionIdsForOrderIds = (orderIds: string[]) => gql`
{
orders(
where: {
id_in: [${orderIds.map((id) => `"${id.toLowerCase()}"`).join(', ')}]
}
) {
id
position { id }
}
}
`;
14 changes: 13 additions & 1 deletion test/subgraph-tests/orders.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getParifiSdkInstanceForTesting } from '..';
import { OrderStatus } from '../../src';
import { EMPTY_BYTES32, OrderStatus } from '../../src';
import { TEST_ORDER_ID1, TEST_SETTLE_ORDER_ID } from '../common/constants';
import { zeroAddress } from 'viem';

describe('Order fetching logic from subgraph', () => {
it('should return correct order details', async () => {
Expand Down Expand Up @@ -30,4 +31,15 @@ describe('Order fetching logic from subgraph', () => {
const referralData = await parifiSdk.subgraph.getReferralDataForPartner(partnerAddress);
expect(referralData.length).not.toBe(0);
});

it('should return correct position id for an order id', async () => {
const parifiSdk = await getParifiSdkInstanceForTesting();
const orderIds = [TEST_ORDER_ID1, TEST_SETTLE_ORDER_ID, zeroAddress];

const response = await parifiSdk.subgraph.getPositionIdsFromOrderIds(orderIds);
expect(response.length).toBeGreaterThan(0);

// Invalid order id should have position id as Bytes32(0);
expect(response.at(2)?.positionId).toBe(EMPTY_BYTES32)
});
});

0 comments on commit 8434123

Please sign in to comment.