import { resolveAccountRef } from "../utils.js";
function normalizeOrderId(orderId) {
if (typeof orderId === "bigint") return orderId.toString();
if (typeof orderId === "string") return orderId;
throw new TypeError(
`PSWAP orderId must be a string or bigint; got ${typeof orderId}`
);
}
export class PswapResource {
#inner;
#getWasm;
#client;
constructor(inner, getWasm, client) {
this.#inner = inner;
this.#getWasm = getWasm;
this.#client = client;
}
async lineages() {
this.#client.assertNotTerminated();
return await this.#inner.getPswapLineages();
}
async lineagesFor(account) {
this.#client.assertNotTerminated();
const wasm = await this.#getWasm();
const accountId = resolveAccountRef(account, wasm);
return await this.#inner.getPswapLineagesFor(accountId);
}
async lineage(orderId) {
this.#client.assertNotTerminated();
const result = await this.#inner.getPswapLineage(normalizeOrderId(orderId));
return result ?? null;
}
async cancelByOrder(opts) {
this.#client.assertNotTerminated();
const orderId = normalizeOrderId(opts.orderId);
const lineage = await this.#inner.getPswapLineage(orderId);
if (!lineage) {
throw new Error(`No PSWAP lineage tracked for order ${orderId}`);
}
const accountId = lineage.creatorAccountId();
const request = await this.#inner.buildPswapCancelByOrder(orderId);
const { txId, result } = await this.#client.transactions.submit(
accountId,
request,
{ prover: opts.prover }
);
if (opts.waitForConfirmation) {
await this.#client.transactions.waitFor(txId.toHex(), {
timeout: opts.timeout,
});
}
return { txId, result };
}
}