diff --git a/packages/sdk/src/api/index.ts b/packages/sdk/src/api/index.ts new file mode 100644 index 00000000..8099eef1 --- /dev/null +++ b/packages/sdk/src/api/index.ts @@ -0,0 +1,2 @@ +export * from "./jsonrpc" +export * from "./types" diff --git a/packages/sdk/src/api/jsonrpc.ts b/packages/sdk/src/api/jsonrpc.ts index c3e2852e..3234f164 100644 --- a/packages/sdk/src/api/jsonrpc.ts +++ b/packages/sdk/src/api/jsonrpc.ts @@ -2,7 +2,7 @@ import fetch from "cross-fetch" import { apiConfig } from "../config" -class JsonRpc { +export class JsonRpc { constructor(readonly url: string) {} /** diff --git a/packages/sdk/src/api/types.ts b/packages/sdk/src/api/types.ts index b0d15681..435a03d3 100644 --- a/packages/sdk/src/api/types.ts +++ b/packages/sdk/src/api/types.ts @@ -19,7 +19,7 @@ export interface GetUnspentsResponse { unspendableUTXOs: UTXO[] } -export interface GetTxOptions { +export interface GetTransactionOptions { txId: string ordinals?: boolean hex?: boolean @@ -27,6 +27,15 @@ export interface GetTxOptions { decodeMetadata?: boolean } +export interface GetTransactionsOptions { + address: string + ordinals?: boolean + hex?: boolean + witness?: boolean + limit?: number + next?: string | null +} + export interface FetchTxResponse { tx: Transaction rawTx?: BTCTransaction @@ -47,6 +56,7 @@ export type GetInscriptionsOptions = RequireAtLeastOne<{ limit?: number next?: string | null decodeMetadata?: boolean + includePostage?: boolean } export interface GetInscriptionOptions { diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 36c1a9b1..3e5b2144 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -25,6 +25,7 @@ export const ordit = { } export * from "./addresses" +export * from "./api" export * as metamask from "./browser-wallets/metamask" export * as unisat from "./browser-wallets/unisat" export * as xverse from "./browser-wallets/xverse" diff --git a/packages/sdk/src/inscription/types.ts b/packages/sdk/src/inscription/types.ts index 644ba580..8b271575 100644 --- a/packages/sdk/src/inscription/types.ts +++ b/packages/sdk/src/inscription/types.ts @@ -39,6 +39,7 @@ export interface Inscription { mediaSize: number mediaContent: string meta?: Record + value?: number // postage } export interface InputsToSign { diff --git a/packages/sdk/src/modules/BaseDatasource.ts b/packages/sdk/src/modules/BaseDatasource.ts index 64fac76b..ef9c12b2 100644 --- a/packages/sdk/src/modules/BaseDatasource.ts +++ b/packages/sdk/src/modules/BaseDatasource.ts @@ -7,7 +7,7 @@ import { GetInscriptionsOptions, GetInscriptionUTXOOptions, GetSpendablesOptions, - GetTxOptions, + GetTransactionOptions, GetUnspentsOptions, GetUnspentsResponse, RelayOptions @@ -52,7 +52,7 @@ export default abstract class BaseDatasource { hex, witness, decodeMetadata - }: GetTxOptions): Promise<{ tx: Transaction; rawTx?: BTCTransaction }> + }: GetTransactionOptions): Promise<{ tx: Transaction; rawTx?: BTCTransaction }> abstract getUnspents({ address, type, rarity, sort, limit, next }: GetUnspentsOptions): Promise diff --git a/packages/sdk/src/modules/JsonRpcDatasource.ts b/packages/sdk/src/modules/JsonRpcDatasource.ts index d4a62fa2..c6db3395 100644 --- a/packages/sdk/src/modules/JsonRpcDatasource.ts +++ b/packages/sdk/src/modules/JsonRpcDatasource.ts @@ -1,6 +1,6 @@ import { Transaction as BTCTransaction } from "bitcoinjs-lib" -import { Inscription } from ".." +import { GetTransactionsOptions, Inscription, Transactions } from ".." import { rpc } from "../api/jsonrpc" import { GetBalanceOptions, @@ -8,7 +8,7 @@ import { GetInscriptionsOptions, GetInscriptionUTXOOptions, GetSpendablesOptions, - GetTxOptions, + GetTransactionOptions, GetUnspentsOptions, GetUnspentsResponse, RelayOptions @@ -70,7 +70,8 @@ export default class JsonRpcDatasource extends BaseDatasource { decodeMetadata, sort = "asc", limit = 25, - next = null + next = null, + includePostage = false // return postage for each inscription }: GetInscriptionsOptions) { let inscriptions: Inscription[] = [] do { @@ -82,7 +83,8 @@ export default class JsonRpcDatasource extends BaseDatasource { { filter: { creator, owner, mimeType, mimeSubType, outpoint }, sort: { number: sort }, - pagination: { limit, next } + pagination: { limit, next }, + include: includePostage ? ["value"] : undefined }, rpc.id ) @@ -118,7 +120,13 @@ export default class JsonRpcDatasource extends BaseDatasource { ) } - async getTransaction({ txId, ordinals = true, hex = false, witness = true, decodeMetadata = true }: GetTxOptions) { + async getTransaction({ + txId, + ordinals = true, + hex = false, + witness = true, + decodeMetadata = true + }: GetTransactionOptions) { if (!txId) { throw new OrditSDKError("Invalid request") } @@ -149,6 +157,43 @@ export default class JsonRpcDatasource extends BaseDatasource { } } + async getTransactions({ + address, + ordinals = true, + hex = false, + witness = false, + limit = 10, + next + }: GetTransactionsOptions) { + if (!address) { + throw new OrditSDKError("Invalid address") + } + + const response = await rpc[this.network].call( + "Address.GetTransactions", + { + address, + options: { + ord: ordinals, + hex, + witness + }, + pagination: { + limit, + next + } + }, + rpc.id + ) + + return { + transactions: response.transactions, + pagination: { + next: response.pagination.next + } + } + } + async getUnspents({ address, type = "spendable", diff --git a/packages/sdk/src/modules/index.ts b/packages/sdk/src/modules/index.ts index 3c0379f9..702a4a06 100644 --- a/packages/sdk/src/modules/index.ts +++ b/packages/sdk/src/modules/index.ts @@ -1,3 +1,4 @@ export { default as BaseDatasource } from "./BaseDatasource" export { default as DatasourceUtility } from "./DatasourceUtility" export { default as JsonRpcDatasource } from "./JsonRpcDatasource" +export * from "./types" diff --git a/packages/sdk/src/transactions/Inscriber.ts b/packages/sdk/src/transactions/Inscriber.ts index 69ed6f1e..82aa91a4 100644 --- a/packages/sdk/src/transactions/Inscriber.ts +++ b/packages/sdk/src/transactions/Inscriber.ts @@ -32,7 +32,7 @@ export class Inscriber extends PSBTBuilder { private payment: bitcoin.payments.Payment | null = null private suitableUnspent: UTXOLimited | null = null private recovery = false - private recoverAmount: number = 0 + private recoverAmount = 0 private safeMode: OnOffUnion private encodeMetadata: boolean private previewMode = false diff --git a/packages/sdk/src/transactions/index.ts b/packages/sdk/src/transactions/index.ts index fcf8fff2..ffd98016 100644 --- a/packages/sdk/src/transactions/index.ts +++ b/packages/sdk/src/transactions/index.ts @@ -1,2 +1,3 @@ export * from "./Inscriber" export * from "./psbt" +export * from "./types" diff --git a/packages/sdk/src/transactions/types.ts b/packages/sdk/src/transactions/types.ts index ca18b07e..cc973818 100644 --- a/packages/sdk/src/transactions/types.ts +++ b/packages/sdk/src/transactions/types.ts @@ -1,4 +1,5 @@ import { Inscription, Ordinal } from "../inscription/types" +import { JsonRpcPagination } from "../modules" export type Vout = { value: number @@ -6,13 +7,14 @@ export type Vout = { ordinals: Ordinal[] inscriptions: Inscription[] spent: string | false + sats: number scriptPubKey: { asm: string desc: string hex: string reqSigs?: number type: string - addresses: string[] + addresses?: string[] address?: string } } @@ -24,8 +26,9 @@ export type Vin = { asm: string hex: string } - txinwitness: string[] + txinwitness?: string[] sequence: number + value: number } export type Transaction = { @@ -39,12 +42,29 @@ export type Transaction = { vin: Vin[] vout: Vout[] blockhash: string + blockheight: number + blocktime: number confirmations: number time: number - blocktime: number weight: number fee: number - blockheight: number +} + +// used in Address.GetTransactions RPC, needed due to response not matching Transaction type (ex. blockhash vs blockHash) +export type TransactionV2 = Omit & { + blockHash: string + blockHeight: number + blockTime: number +} + +export type Transactions = { + transactions: TransactionV2[] + options: { + ord: boolean + hex: boolean + witness: boolean + } + pagination: JsonRpcPagination } export interface ScriptPubKey { diff --git a/packages/sdk/src/utxos/index.ts b/packages/sdk/src/utxos/index.ts index 2769f153..d80e5179 100644 --- a/packages/sdk/src/utxos/index.ts +++ b/packages/sdk/src/utxos/index.ts @@ -1 +1,2 @@ +export * from "./types" export { default as UTXOManager } from "./UTXOManager"