Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): add GetTransactions + export more types #105

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/sdk/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./jsonrpc"
export * from "./types"
2 changes: 1 addition & 1 deletion packages/sdk/src/api/jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fetch from "cross-fetch"

import { apiConfig } from "../config"

class JsonRpc {
export class JsonRpc {
constructor(readonly url: string) {}

/**
Expand Down
12 changes: 11 additions & 1 deletion packages/sdk/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ export interface GetUnspentsResponse {
unspendableUTXOs: UTXO[]
}

export interface GetTxOptions {
export interface GetTransactionOptions {
txId: string
ordinals?: boolean
hex?: boolean
witness?: boolean
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
Expand All @@ -47,6 +56,7 @@ export type GetInscriptionsOptions = RequireAtLeastOne<{
limit?: number
next?: string | null
decodeMetadata?: boolean
includePostage?: boolean
}

export interface GetInscriptionOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/inscription/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface Inscription {
mediaSize: number
mediaContent: string
meta?: Record<string, any>
value?: number // postage
}

export interface InputsToSign {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/modules/BaseDatasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
GetInscriptionsOptions,
GetInscriptionUTXOOptions,
GetSpendablesOptions,
GetTxOptions,
GetTransactionOptions,
GetUnspentsOptions,
GetUnspentsResponse,
RelayOptions
Expand Down Expand Up @@ -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<GetUnspentsResponse>

Expand Down
55 changes: 50 additions & 5 deletions packages/sdk/src/modules/JsonRpcDatasource.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Transaction as BTCTransaction } from "bitcoinjs-lib"

import { Inscription } from ".."
import { GetTransactionsOptions, Inscription, Transactions } from ".."
import { rpc } from "../api/jsonrpc"
import {
GetBalanceOptions,
GetInscriptionOptions,
GetInscriptionsOptions,
GetInscriptionUTXOOptions,
GetSpendablesOptions,
GetTxOptions,
GetTransactionOptions,
GetUnspentsOptions,
GetUnspentsResponse,
RelayOptions
Expand Down Expand Up @@ -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 {
Expand All @@ -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
)
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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<Transactions>(
"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",
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion packages/sdk/src/transactions/Inscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/transactions/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./Inscriber"
export * from "./psbt"
export * from "./types"
28 changes: 24 additions & 4 deletions packages/sdk/src/transactions/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Inscription, Ordinal } from "../inscription/types"
import { JsonRpcPagination } from "../modules"

export type Vout = {
value: number
n: number
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
}
}
Expand All @@ -24,8 +26,9 @@ export type Vin = {
asm: string
hex: string
}
txinwitness: string[]
txinwitness?: string[]
sequence: number
value: number
}

export type Transaction = {
Expand All @@ -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<Transaction, 'blockhash' | 'blockheight' | 'blocktime'> & {
blockHash: string
blockHeight: number
blockTime: number
}

export type Transactions = {
transactions: TransactionV2[]
options: {
ord: boolean
hex: boolean
witness: boolean
}
pagination: JsonRpcPagination
}

export interface ScriptPubKey {
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/utxos/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./types"
export { default as UTXOManager } from "./UTXOManager"