116 lines
4.0 KiB
TypeScript
116 lines
4.0 KiB
TypeScript
import { ClaimKey, User } from '@coinweb/contract-kit';
|
|
import { Client } from 'cwap-cm-lib/offchain';
|
|
|
|
import {
|
|
createActiveOrderIndexFirstPart,
|
|
createBestByQuoteActiveIndexFirstPart,
|
|
createBestByQuoteIndexFirstPart,
|
|
createDateIndexFirstPart,
|
|
createL1TxInfoFirstPart,
|
|
createOrderStateKey,
|
|
createOwnerlessIndexFirstPart,
|
|
createUniquenessFirstPart,
|
|
createUserIndexFirstPart,
|
|
} from './shared/keys';
|
|
import { BtcChainData, L1TxInfoData, OrderStateClaimBody } from './shared/types';
|
|
import { OrderData } from './types';
|
|
|
|
export const getActiveOrderIds = async (client: Client): Promise<string[]> => {
|
|
const claimsResponse = await client.fetchClaims(createActiveOrderIndexFirstPart(), null);
|
|
|
|
return claimsResponse.map(({ content }) => ((content.key as ClaimKey).second_part as [number, string])[1]);
|
|
};
|
|
|
|
export const getBestOrderIds = async (client: Client): Promise<string[]> => {
|
|
const claimsResponse = await client.fetchClaims(createBestByQuoteIndexFirstPart(), null);
|
|
|
|
return claimsResponse.map(({ content }) => ((content.key as ClaimKey).second_part as [number, string])[1]);
|
|
};
|
|
|
|
export const getBestActiveOrderIds = async (client: Client): Promise<string[]> => {
|
|
const claimsResponse = await client.fetchClaims(createBestByQuoteActiveIndexFirstPart(), null);
|
|
|
|
return claimsResponse.map(({ content }) => ((content.key as ClaimKey).second_part as [number, string])[1]);
|
|
};
|
|
|
|
export const getLastOrderIds = async (client: Client): Promise<string[]> => {
|
|
const claimsResponse = await client.fetchClaims(createDateIndexFirstPart(), null);
|
|
|
|
return claimsResponse.map(({ content }) => ((content.key as ClaimKey).second_part as [number, string])[1]);
|
|
};
|
|
|
|
export const getUserOrderIds = async (client: Client, user: User): Promise<string[]> => {
|
|
const claimsResponse = await client.fetchClaims(createUserIndexFirstPart(user), null);
|
|
|
|
return claimsResponse.map(({ content }) => ((content.key as ClaimKey).second_part as [number, string])[1]);
|
|
};
|
|
|
|
export const getOwnerlessOrderIds = async (client: Client): Promise<string[]> => {
|
|
const claimsResponse = await client.fetchClaims(createOwnerlessIndexFirstPart(), null);
|
|
|
|
return claimsResponse.map(({ content }) => ((content.key as ClaimKey).second_part as [string])[0]);
|
|
};
|
|
|
|
export const getUtxosInUse = async (client: Client): Promise<Pick<BtcChainData, 'l1TxId' | 'vout'>[]> => {
|
|
const claimsResponse = await client.fetchClaims(createUniquenessFirstPart(), null);
|
|
|
|
return claimsResponse.map(({ content: { key } }) => {
|
|
const [l1TxId, vout] = (key as ClaimKey).second_part as [BtcChainData['l1TxId'], BtcChainData['vout']];
|
|
|
|
return {
|
|
l1TxId,
|
|
vout,
|
|
};
|
|
});
|
|
};
|
|
|
|
export const getOrderById = async (client: Client, id: string) => {
|
|
const key = createOrderStateKey(id);
|
|
|
|
const claimResponse = (await client.fetchClaims(key.first_part, key.second_part))[0];
|
|
|
|
if (!claimResponse) {
|
|
throw new Error('Order not found');
|
|
}
|
|
|
|
const data = claimResponse.content.body as OrderStateClaimBody;
|
|
|
|
return {
|
|
id,
|
|
baseAmount: BigInt(data.baseAmount),
|
|
l1Amount: BigInt(data.l1Amount),
|
|
minL1Amount: BigInt(data.minL1Amount),
|
|
recipient: data.recipient,
|
|
createdAt: data.createdAt,
|
|
activityStatus: data.activityStatus,
|
|
paymentStatus: data.paymentStatus,
|
|
funds: BigInt(data.funds),
|
|
chainData: data.chainData,
|
|
txId: data.txId,
|
|
error: data.error,
|
|
expirationDate: data.expirationDate,
|
|
history: data.history,
|
|
isOwnerless: data.isOwnerless,
|
|
} satisfies OrderData;
|
|
};
|
|
|
|
export const getOwnerlessOrders = async (client: Client) => {
|
|
const ids = await getOwnerlessOrderIds(client);
|
|
|
|
try {
|
|
const orders = await Promise.all(ids.map((id) => getOrderById(client, id)));
|
|
|
|
return orders;
|
|
} catch (e) {
|
|
console.error({ e });
|
|
|
|
return [];
|
|
}
|
|
};
|
|
|
|
export const getL1TxInfo = async (client: Client, user: User): Promise<L1TxInfoData[]> => {
|
|
const claimsResponse = await client.fetchClaims(createL1TxInfoFirstPart(user), null);
|
|
|
|
return claimsResponse.map((claim) => claim.content.body as L1TxInfoData);
|
|
};
|