All files endpoints.ts

93.47% Statements 43/46
82.92% Branches 34/41
100% Functions 10/10
93.47% Lines 43/46

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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 29938x                     1x                                                                                                                                                                                 1x                             86x 86x 86x 86x 86x 86x       19x     19x             1x     1x                     89x       89x                         89x         86x                                                       1x 7x                   4x   4x       4x 3x 3x   3x 3x   3x 1x   3x 1x     1x 1x 1x     4x           4x 2x     4x                         69x                         2x 2x           2x                   2x                   1x  
import {
    API,
    APIClient,
    Checksum256,
    Checksum256Type,
    Int32,
    Int32Type,
    Name,
    NameType,
    UInt32,
    UInt32Type,
} from '@wharfkit/antelope'
 
/**
 * Options for querying account actions
 */
export interface GetActionOptions {
    /** Starting position (default: -1 for latest) */
    start?: Int32Type
    /** Number of actions to retrieve */
    limit?: Int32Type
    /** Reverse the order of results */
    reverse?: boolean
}
 
/**
 * Options for querying account activity (v2)
 */
export interface ActivityOptions {
    /** Number of actions to retrieve per page (default: 100) */
    limit?: number
    /** Sort order by sequence number: "asc" or "desc" (default) */
    order?: 'asc' | 'desc'
    /** Pagination cursor from previous response */
    cursor?: string
    /** Filter by contract account */
    contract?: string
    /** Filter by action name (requires contract) */
    action?: string
    /** Filter by single date YYYY-MM-DD */
    date?: string
    /** Filter by date range start YYYY-MM-DD */
    start_date?: string
    /** Filter by date range end YYYY-MM-DD */
    end_date?: string
    /** Enable ABI decoding of action data (default: true) */
    decode?: boolean
    /** Omit null fields from response (default: false) */
    omit_null_fields?: boolean
}
 
/**
 * Parameters sent to the get_actions API
 */
interface GetActionsParams {
    account_name: NameType
    pos?: Int32Type
    offset?: Int32Type
}
 
/**
 * Parameters sent to the activity API (query/body params, not including path param)
 */
interface ActivityParams extends ActivityOptions {
    limit: number
    order: 'asc' | 'desc'
    decode: boolean
}
 
/**
 * Options for querying transactions (v2)
 */
export interface TransactionOptions {
    /** Include action traces in response (default: true) */
    traces?: boolean
}
 
/**
 * Options for querying transactions (legacy v1)
 */
export interface GetTransactionOptions {
    /** Block number hint for faster lookup */
    blockNumHint?: UInt32Type
    /** Include action traces in response */
    traces?: boolean
}
 
/**
 * Raw response from activity API (v2)
 */
interface ActivityAPIResponse {
    results: API.v1.OrderedActionsResult[]
    next_cursor?: string
    prev_cursor?: string
}
 
/**
 * Cursor for paginating through account activity results.
 * Returned by RoborovskiClient.activity()
 */
export class ActivityCursor {
    readonly results: API.v1.OrderedActionsResult[]
    readonly next_cursor?: string
    readonly prev_cursor?: string
 
    private readonly client: APIClient
    private readonly account: Name
    private readonly options: ActivityOptions
 
    constructor(
        client: APIClient,
        account: Name,
        options: ActivityOptions,
        response: ActivityAPIResponse
    ) {
        this.client = client
        this.account = account
        this.options = options
        this.results = response.results
        this.next_cursor = response.next_cursor
        this.prev_cursor = response.prev_cursor
    }
 
    async next(): Promise<ActivityCursor> {
        Iif (!this.next_cursor) {
            throw new Error('No next page available')
        }
        return ActivityCursor.fetch(this.client, this.account, {
            ...this.options,
            cursor: this.next_cursor,
        })
    }
 
    async prev(): Promise<ActivityCursor> {
        Iif (!this.prev_cursor) {
            throw new Error('No previous page available')
        }
        return ActivityCursor.fetch(this.client, this.account, {
            ...this.options,
            cursor: this.prev_cursor,
        })
    }
 
    static async fetch(
        client: APIClient,
        account: Name,
        options: ActivityOptions = {}
    ): Promise<ActivityCursor> {
        Iif (options.action && !options.contract) {
            throw new Error('action filter requires contract to be specified')
        }
 
        const params: ActivityParams = {
            limit: options.limit !== undefined ? options.limit : 100,
            order: options.order || 'desc',
            decode: options.decode !== undefined ? options.decode : true,
            cursor: options.cursor,
            contract: options.contract,
            action: options.action,
            date: options.date,
            start_date: options.start_date,
            end_date: options.end_date,
            omit_null_fields: options.omit_null_fields,
        }
 
        const response = (await client.call({
            path: `/account/${account}/activity`,
            params,
        })) as ActivityAPIResponse
 
        return new ActivityCursor(client, account, options, response)
    }
}
 
/**
 * Response type for transaction API (v2)
 */
export interface TransactionResponse {
    /** Transaction ID */
    id: string
    /** Block number containing the transaction */
    block_num: number
    /** Block timestamp */
    block_time: string
    /** Current head block number */
    head_block_num: number
    /** Last irreversible block number */
    last_irreversible_block: number
    /** Whether the transaction is irreversible */
    irreversible: boolean
    /** Transaction position within the block */
    transaction_num: number
    /** Action traces (null if not requested) */
    traces: unknown[] | null
    /** Transaction receipt and body */
    trx: unknown
}
 
export class RoborovskiClient {
    constructor(private client: APIClient) {}
 
    /**
     * Query account actions
     * Uses pos/offset parameters with negation for reverse
     */
    async get_actions(
        accountName: NameType,
        options?: GetActionOptions
    ): Promise<API.v1.GetActionsResponse> {
        let reverse = options ? options.reverse : undefined
 
        const params: GetActionsParams = {
            account_name: Name.from(accountName),
        }
 
        if (options) {
            if (options.start !== undefined) {
                params.pos = Int32.from(options.start)
            }
            if (options.limit) {
                params.offset = Int32.from(options.limit)
            }
            if (options.reverse && params.pos) {
                params.pos = Int32.from(params.pos).multiplying(-1)
            }
            if (options.reverse && params.offset) {
                params.offset = Int32.from(params.offset).multiplying(-1)
            }
        } else {
            params.pos = Int32.from(-1)
            params.offset = Int32.from(-100)
            reverse = true
        }
 
        const result = await this.client.call({
            path: '/v1/history/get_actions',
            params,
            responseType: API.v1.GetActionsResponse,
        })
 
        if (reverse) {
            result.actions.reverse()
        }
 
        return result
    }
 
    /**
     * Query account activity (v2)
     * Returns a cursor for paginating through results
     *
     * @param accountName - Account to query
     * @param options - Query options
     * @returns ActivityCursor with results and pagination methods
     * @throws {Error} If action is specified without contract
     */
    async activity(accountName: NameType, options?: ActivityOptions): Promise<ActivityCursor> {
        return ActivityCursor.fetch(this.client, Name.from(accountName), options)
    }
 
    /**
     * Get transaction by ID (v2)
     *
     * @param id - Transaction ID
     * @param options - Query options
     */
    async transaction(
        id: Checksum256Type,
        options: TransactionOptions = {}
    ): Promise<TransactionResponse> {
        const txid = Checksum256.from(id)
        const response = (await this.client.call({
            path: `/transaction/${txid}`,
            params: {
                traces: options.traces !== undefined ? options.traces : true,
            },
        })) as {results: TransactionResponse}
        return response.results
    }
 
    /**
     * Get transaction by ID (legacy v1)
     *
     * @param id - Transaction ID
     * @param options - Query options
     */
    async get_transaction(id: Checksum256Type, options: GetTransactionOptions = {}) {
        return this.client.call({
            path: '/v1/history/get_transaction',
            params: {
                id: Checksum256.from(id),
                block_num_hint: options.blockNumHint && UInt32.from(options.blockNumHint),
                traces: options.traces !== undefined ? options.traces : false,
            },
            responseType: API.v1.GetTransactionResponse,
        })
    }
}