feat: Add generics of TransactionType for TxResponse (#2332)

* add generics of TransactionType for TxResponse

* add generics to submitAndWait

* add xrpl.js history
This commit is contained in:
tequ
2023-06-08 22:21:37 +09:00
committed by GitHub
parent 6fbfab4fa4
commit 456eac0628
3 changed files with 17 additions and 11 deletions

View File

@@ -13,6 +13,7 @@ Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xr
* Fixed the location of `signer_lists` in the `account_info` response so that it matches rippled * Fixed the location of `signer_lists` in the `account_info` response so that it matches rippled
* Guard check for signing algorithm used in `Wallet.generate()` * Guard check for signing algorithm used in `Wallet.generate()`
* Null and undefined values in transactions are now treated as though the field was not passed in. * Null and undefined values in transactions are now treated as though the field was not passed in.
* Improved the type definition of the return value of `submitAndWait()`.
### Removed ### Removed
* RPCs and utils related to the old sidechain design * RPCs and utils related to the old sidechain design

View File

@@ -1,4 +1,5 @@
import { Transaction, TransactionMetadata } from '../transactions' import { Transaction, TransactionMetadata } from '../transactions'
import { BaseTransaction } from '../transactions/common'
import { BaseRequest, BaseResponse } from './baseMethod' import { BaseRequest, BaseResponse } from './baseMethod'
@@ -38,7 +39,8 @@ export interface TxRequest extends BaseRequest {
* *
* @category Responses * @category Responses
*/ */
export interface TxResponse extends BaseResponse { export interface TxResponse<T extends BaseTransaction = Transaction>
extends BaseResponse {
result: { result: {
/** The SHA-512 hash of the transaction. */ /** The SHA-512 hash of the transaction. */
hash: string hash: string
@@ -55,7 +57,7 @@ export interface TxResponse extends BaseResponse {
* This number measures the number of seconds since the "Ripple Epoch" of January 1, 2000 (00:00 UTC) * This number measures the number of seconds since the "Ripple Epoch" of January 1, 2000 (00:00 UTC)
*/ */
date?: number date?: number
} & Transaction } & T
/** /**
* If true, the server was able to search all of the specified ledger * If true, the server was able to search all of the specified ledger
* versions, and the transaction was in none of them. If false, the server did * versions, and the transaction was in none of them. If false, the server did

View File

@@ -3,8 +3,9 @@ import { decode, encode } from 'ripple-binary-codec'
import type { Client, SubmitRequest, SubmitResponse, Wallet } from '..' import type { Client, SubmitRequest, SubmitResponse, Wallet } from '..'
import { ValidationError, XrplError } from '../errors' import { ValidationError, XrplError } from '../errors'
import { Signer } from '../models/common' import { Signer } from '../models/common'
import { TxResponse } from '../models/methods' import { TxRequest, TxResponse } from '../models/methods'
import { Transaction } from '../models/transactions' import { Transaction } from '../models/transactions'
import { BaseTransaction } from '../models/transactions/common'
import { hashes } from '../utils' import { hashes } from '../utils'
/** Approximate time for a ledger to close, in milliseconds */ /** Approximate time for a ledger to close, in milliseconds */
@@ -104,9 +105,9 @@ async function submit(
* ledger, the promise returned by `submitAndWait()` will be rejected with an error. * ledger, the promise returned by `submitAndWait()` will be rejected with an error.
* @returns A promise that contains TxResponse, that will return when the transaction has been validated. * @returns A promise that contains TxResponse, that will return when the transaction has been validated.
*/ */
async function submitAndWait( async function submitAndWait<T extends Transaction = Transaction>(
this: Client, this: Client,
transaction: Transaction | string, transaction: T | string,
opts?: { opts?: {
// If true, autofill a transaction. // If true, autofill a transaction.
autofill?: boolean autofill?: boolean
@@ -115,7 +116,7 @@ async function submitAndWait(
// A wallet to sign a transaction. It must be provided when submitting an unsigned transaction. // A wallet to sign a transaction. It must be provided when submitting an unsigned transaction.
wallet?: Wallet wallet?: Wallet
}, },
): Promise<TxResponse> { ): Promise<TxResponse<T>> {
const signedTx = await getSignedTx(this, transaction, opts) const signedTx = await getSignedTx(this, transaction, opts)
const lastLedger = getLastLedgerSequence(signedTx) const lastLedger = getLastLedgerSequence(signedTx)
@@ -167,12 +168,14 @@ async function submitRequest(
* latest ledger sequence (meaning it will never be included in a validated ledger). * latest ledger sequence (meaning it will never be included in a validated ledger).
*/ */
// eslint-disable-next-line max-params, max-lines-per-function -- this function needs to display and do with more information. // eslint-disable-next-line max-params, max-lines-per-function -- this function needs to display and do with more information.
async function waitForFinalTransactionOutcome( async function waitForFinalTransactionOutcome<
T extends BaseTransaction = Transaction,
>(
client: Client, client: Client,
txHash: string, txHash: string,
lastLedger: number, lastLedger: number,
submissionResult: string, submissionResult: string,
): Promise<TxResponse> { ): Promise<TxResponse<T>> {
await sleep(LEDGER_CLOSE_TIME) await sleep(LEDGER_CLOSE_TIME)
const latestLedger = await client.getLedgerIndex() const latestLedger = await client.getLedgerIndex()
@@ -185,7 +188,7 @@ async function waitForFinalTransactionOutcome(
} }
const txResponse = await client const txResponse = await client
.request({ .request<TxRequest, TxResponse<T>>({
command: 'tx', command: 'tx',
transaction: txHash, transaction: txHash,
}) })
@@ -194,7 +197,7 @@ async function waitForFinalTransactionOutcome(
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions,@typescript-eslint/no-unsafe-member-access -- ^ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions,@typescript-eslint/no-unsafe-member-access -- ^
const message = error?.data?.error as string const message = error?.data?.error as string
if (message === 'txnNotFound') { if (message === 'txnNotFound') {
return waitForFinalTransactionOutcome( return waitForFinalTransactionOutcome<T>(
client, client,
txHash, txHash,
lastLedger, lastLedger,
@@ -212,7 +215,7 @@ async function waitForFinalTransactionOutcome(
return txResponse return txResponse
} }
return waitForFinalTransactionOutcome( return waitForFinalTransactionOutcome<T>(
client, client,
txHash, txHash,
lastLedger, lastLedger,