All files / src/modules/debug transactions.ts

66.66% Statements 18/27
0% Branches 0/1
60% Functions 3/5
66.66% Lines 18/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 11133x   33x 33x 33x   33x             33x 24x         24x 24x   24x                 33x       1x         1x   1x       22x   22x                                     33x                                           33x                                          
import { Types } from 'cafe-utility'
import { BeeRequestOptions, NumberString, TransactionInfo } from '../../types'
import { http } from '../../utils/http'
import { asNumberString } from '../../utils/type'
import { TransactionId } from '../../utils/typed-bytes'
 
const transactionsEndpoint = 'transactions'
 
/**
 * Get list of all pending transactions
 *
 * @param requestOptions Options for making requests
 */
export async function getAllTransactions(requestOptions: BeeRequestOptions): Promise<TransactionInfo[]> {
  const response = await http<unknown>(requestOptions, {
    url: transactionsEndpoint,
    responseType: 'json',
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
  const pendingTransactions = Types.asArray(body.pendingTransactions, { name: 'pendingTransactions' })
 
  return pendingTransactions.map(toTransaction)
}
 
/**
 * Get information for specific pending transactions
 *
 * @param requestOptions Options for making requests
 * @param transactionHash Hash of the transaction
 */
export async function getTransaction(
  requestOptions: BeeRequestOptions,
  transactionHash: TransactionId,
): Promise<TransactionInfo> {
  const response = await http<unknown>(requestOptions, {
    url: `${transactionsEndpoint}/${transactionHash}`,
    responseType: 'json',
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  return toTransaction(body)
}
 
function toTransaction(value: unknown) {
  const object = Types.asObject(value, { name: 'transaction' })
 
  return {
    transactionHash: new TransactionId(Types.asString(object.transactionHash, { name: 'transactionHash' })),
    to: Types.asString(object.to, { name: 'to' }),
    nonce: Types.asNumber(object.nonce, { name: 'nonce' }),
    gasPrice: asNumberString(object.gasPrice, { name: 'gasPrice' }),
    gasLimit: Types.asNumber(object.gasLimit, { name: 'gasLimit' }),
    data: Types.asString(object.data, { name: 'data' }),
    created: Types.asString(object.created, { name: 'created' }),
    description: Types.asString(object.description, { name: 'description' }),
    value: asNumberString(object.value, { name: 'value' }),
  }
}
 
/**
 * Rebroadcast existing transaction
 *
 * @param requestOptions Options for making requests
 * @param transactionHash Hash of the transaction
 */
export async function rebroadcastTransaction(
  requestOptions: BeeRequestOptions,
  transactionHash: TransactionId,
): Promise<TransactionId> {
  const response = await http<unknown>(requestOptions, {
    method: 'post',
    url: `${transactionsEndpoint}/${transactionHash}`,
    responseType: 'json',
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  return new TransactionId(Types.asString(body.transactionHash, { name: 'transactionHash' }))
}
 
/**
 * Cancel existing transaction
 *
 * @param requestOptions Options for making requests
 * @param transactionHash Hash of the transaction
 * @param gasPrice Optional gas price
 */
export async function cancelTransaction(
  requestOptions: BeeRequestOptions,
  transactionHash: TransactionId,
  gasPrice?: NumberString | string | bigint,
): Promise<TransactionId> {
  const headers: Record<string, string | number> = {}
 
  Iif (gasPrice) {
    headers['gas-price'] = gasPrice.toString()
  }
  const response = await http<unknown>(requestOptions, {
    method: 'delete',
    headers,
    url: `${transactionsEndpoint}/${transactionHash}`,
    responseType: 'json',
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  return new TransactionId(Types.asString(body.transactionHash, { name: 'transactionHash' }))
}