diff options
| author | Czarek Nakamoto <cyjan@mrcyjanek.net> | 2025-01-05 13:17:22 +0100 |
|---|---|---|
| committer | Czarek Nakamoto <cyjan@mrcyjanek.net> | 2025-01-05 13:17:22 +0100 |
| commit | 085d74b37b478be77bc873d66876247a751aa957 (patch) | |
| tree | d8434dd9c8c57df9b64ae93059d9ebb5a16b90f2 /impls/monero.ts/src/pending_transaction.ts | |
| parent | 8e7bc59509c40f00702ba568a0adcb3cf82e6e05 (diff) | |
| parent | c3dd64bdee37d361a2c1252d127fb575936e43e6 (diff) | |
Merge remote-tracking branch 'origin/develop' into rust-develop
Diffstat (limited to 'impls/monero.ts/src/pending_transaction.ts')
| -rw-r--r-- | impls/monero.ts/src/pending_transaction.ts | 117 |
1 files changed, 57 insertions, 60 deletions
diff --git a/impls/monero.ts/src/pending_transaction.ts b/impls/monero.ts/src/pending_transaction.ts index 169332f..d80b738 100644 --- a/impls/monero.ts/src/pending_transaction.ts +++ b/impls/monero.ts/src/pending_transaction.ts @@ -1,87 +1,84 @@ -import { CString, getSymbol, readCString, type Sanitizer } from "./utils.ts"; +import { fns } from "./bindings.ts"; +import { C_SEPARATOR, CString, maybeMultipleStrings, readCString } from "./utils.ts"; -export type PendingTransactionPtr = Deno.PointerObject<"transactionInfo">; +export type PendingTransactionPtr = Deno.PointerObject<"pendingTransaction">; -export class PendingTransaction { - #pendingTxPtr: PendingTransactionPtr; - sanitizer?: Sanitizer; +export class PendingTransaction<MultDest extends boolean = false> { + #ptr: PendingTransactionPtr; - constructor(pendingTxPtr: PendingTransactionPtr, sanitizer?: Sanitizer) { - this.sanitizer = sanitizer; - this.#pendingTxPtr = pendingTxPtr; - } + #amount!: bigint; + #dust!: bigint; + #fee!: bigint; + #txid!: string | string[] | null; + #txCount!: bigint; - async status(): Promise<number> { - return await getSymbol("PendingTransaction_status")(this.#pendingTxPtr); + constructor(ptr: PendingTransactionPtr) { + this.#ptr = ptr; } - async errorString(): Promise<string | null> { - if (!await this.status()) return null; + static async new(ptr: PendingTransactionPtr): Promise<PendingTransaction> { + const instance = new PendingTransaction(ptr); - const error = await getSymbol("PendingTransaction_errorString")(this.#pendingTxPtr); - if (!error) return null; + const [amount, dust, fee, txCount, txid] = await Promise.all([ + fns.PendingTransaction_amount(ptr), + fns.PendingTransaction_dust(ptr), + fns.PendingTransaction_fee(ptr), + fns.PendingTransaction_txCount(ptr), + fns.PendingTransaction_txid(ptr, C_SEPARATOR), + ]); - return await readCString(error) || null; - } + instance.#amount = amount; + instance.#dust = dust; + instance.#fee = fee; + instance.#txCount = txCount; + instance.#txid = maybeMultipleStrings(await readCString(txid)); - async throwIfError(sanitize = true): Promise<void> { - const maybeError = await this.errorString(); - if (maybeError) { - if (sanitize) this.sanitizer?.(); - throw new Error(maybeError); - } + return instance; } - async commit(fileName: string, overwrite: boolean, sanitize = true): Promise<boolean> { - const bool = await getSymbol("PendingTransaction_commit")( - this.#pendingTxPtr, - CString(fileName), - overwrite, - ); - await this.throwIfError(sanitize); - return bool; + get amount(): bigint { + return this.#amount; } - async commitUR(maxFragmentLength: number): Promise<string | null> { - const commitUR = getSymbol("PendingTransaction_commitUR"); - - if (!commitUR) { - return null; - } + get dust(): bigint { + return this.#dust; + } - const result = await commitUR( - this.#pendingTxPtr, - maxFragmentLength, - ); + get fee(): bigint { + return this.#fee; + } - if (!result) return null; - await this.throwIfError(); - return await readCString(result) || null; + get txCount(): bigint { + return this.#txCount; } - async amount(): Promise<bigint> { - return await getSymbol("PendingTransaction_amount")(this.#pendingTxPtr); + async commit(fileName: string, overwrite: boolean): Promise<boolean> { + return await fns.PendingTransaction_commit(this.#ptr, CString(fileName), overwrite); } - async dust(): Promise<bigint> { - return await getSymbol("PendingTransaction_dust")(this.#pendingTxPtr); + async commitUR(maxFragmentLength: number): Promise<string | null> { + const commitUR = fns.PendingTransaction_commitUR; + if (!commitUR) return null; + + return await readCString( + await commitUR(this.#ptr, maxFragmentLength), + ); } - async fee(): Promise<bigint> { - return await getSymbol("PendingTransaction_fee")(this.#pendingTxPtr); + async status(): Promise<number> { + return await fns.PendingTransaction_status(this.#ptr); } - async txid(separator: string, sanitize = true): Promise<string | null> { - const result = await getSymbol("PendingTransaction_txid")( - this.#pendingTxPtr, - CString(separator), - ); - if (!result) return null; - await this.throwIfError(sanitize); - return await readCString(result) || null; + async errorString(): Promise<string | null> { + if (!await this.status()) return null; + const error = await fns.PendingTransaction_errorString(this.#ptr); + return await readCString(error); } - async txCount(): Promise<bigint> { - return await getSymbol("PendingTransaction_txCount")(this.#pendingTxPtr); + async throwIfError(): Promise<void> { + const maybeError = await this.errorString(); + if (maybeError) { + throw new Error(maybeError); + } } } |
