Getting OrderId and userOrderId
Fetch order details for an user based on orderId and userOrderId
Orders are instructions which decide how trades should execute on a decentralized trading platform. The following snippet illustrates how we can query order data related to particular user account, based on a particular order Id.
You can directly copy paste this code on replit and see it in action.
Fetch orders associated to a user account based orderId and userOrderId
const SHYFT_API_KEY = "YOUR-API-KEY";
async function getOrderDataByGraphQl(authorityAddress) {
const operationsDoc = `
query MyQuery {
drift_User(
where: {authority: {_eq: ${JSON.stringify(authorityAddress)}}}
) {
authority
orders
delegate
hasOpenOrder
subAccountId
status
pubkey
}
}
`; //graphQl query
const result = await fetch(
`https://programs.shyft.to/v0/graphql/accounts?api_key=${SHYFT_API_KEY}&network=mainnet-beta`, //SHYFT's GQL endpoint
{
method: "POST",
body: JSON.stringify({
query: operationsDoc,
variables: {},
operationName: "MyQuery",
}),
}
);
return await result.json();
}
async function main(authorityPubkey, orderId) {
const { errors, data } = await getOrderDataByGraphQl(authorityPubkey);
if (errors) {
console.error(errors);
console.log("Some Error Occured, please check your API key or try again");
}
let allMatchedOrder = [];
for (let index = 0; index < data.drift_User.length; index++) {
const currentUserDetails = data.drift_User[index];
const matchedOrder = currentUserDetails.orders.find(
(order) => order.orderId == orderId
);
if (!matchedOrder) {
// console.log("Order not found");
continue;
}
allMatchedOrder.push(matchedOrder);
}
console.log("Order found: ");
console.dir(allMatchedOrder, { depth: null });
}
main("BgGAVukE1j8JDsvXwcnneuoN8LTpDuiRUtYfQWociQjL", 112)
In the above case, we first get the particular userAccount based on Authority (user wallet), and then filter the order field to get the order of a particular orderId. Alternatively, we can also fetch orders based on userOrderId, just by filter the order using the userOrderId field, instead of the orderId field.
const matchedOrder = currentUserDetails.orders.find(
(order) => order.userorderId == orderId
); //userOrderId to be taken as input from the user
Last updated
Was this helpful?