-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): introduce injectable datasource dependency (#67)
* feat: add abstract class to define datasource base * feat: add JSON RPC datasource class * refactor: replace OrditApi w/ JsonRpc datasource * refasctor: move datasource to very top in inheritance chain * refactor: accept & bind datasource in instant-trade classes * refactor: replace OrditApi w/ datasource * feat: decouple utils to separate class * fix: pass exact fee set by user * refactor: set ds type to abstract class * refactor: deprecate OrditApi & replace w/ datasource * fix: fetch unspents w/ pagination * refactor!: rename class to BaseDatasource and convert to abtract class * refactor: pass datasource to processInput * fix: rename derived fn wrt. abstract class
- Loading branch information
1 parent
32770c2
commit eb8f19c
Showing
13 changed files
with
311 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Transaction as BTCTransaction } from "bitcoinjs-lib" | ||
|
||
import { Inscription } from ".." | ||
import { FetchSpendablesOptions, FetchTxOptions, FetchUnspentUTXOsOptions } from "../api/types" | ||
import { Network } from "../config/types" | ||
import { Transaction, UTXOLimited } from "../transactions/types" | ||
import { DatasourceUtility } from "." | ||
|
||
interface BaseDatasourceOptions { | ||
network: Network | ||
} | ||
|
||
export default abstract class BaseDatasource { | ||
protected readonly network: Network | ||
|
||
constructor({ network }: BaseDatasourceOptions) { | ||
this.network = network | ||
} | ||
|
||
abstract getBalance(address?: string): Promise<number> | ||
|
||
abstract getInscription(id?: string, decodeMetadata?: boolean): Promise<Inscription> | ||
|
||
abstract getInscriptions(outpoint?: string, decodeMetadata?: boolean): Promise<Inscription[]> | ||
|
||
abstract getSpendables(args?: FetchSpendablesOptions): Promise<UTXOLimited[]> | ||
|
||
abstract getTransaction(args: FetchTxOptions): Promise<{ tx: Transaction; rawTx?: BTCTransaction }> | ||
|
||
abstract getUnspents( | ||
args: FetchUnspentUTXOsOptions | ||
): Promise<ReturnType<typeof DatasourceUtility.segregateUTXOsBySpendStatus>> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { decodeObject, Inscription } from ".." | ||
import { UTXO } from "../transactions/types" | ||
|
||
interface SegregateUTXOsBySpendStatusArgOptions { | ||
utxos: UTXO[] | ||
decodeMetadata?: boolean | ||
} | ||
|
||
export default class DatasourceUtility { | ||
static transformInscriptions(inscriptions?: Inscription[]) { | ||
if (!inscriptions) return [] | ||
|
||
return inscriptions.map((inscription) => { | ||
inscription.meta = inscription.meta ? decodeObject(inscription.meta) : inscription.meta | ||
return inscription | ||
}) | ||
} | ||
|
||
static segregateUTXOsBySpendStatus({ utxos, decodeMetadata }: SegregateUTXOsBySpendStatusArgOptions) { | ||
const { spendableUTXOs, unspendableUTXOs } = utxos.reduce( | ||
(acc, utxo) => { | ||
if (utxo.inscriptions?.length && !utxo.safeToSpend) { | ||
utxo.inscriptions = decodeMetadata ? this.transformInscriptions(utxo.inscriptions) : utxo.inscriptions | ||
|
||
acc.unspendableUTXOs.push(utxo) | ||
} else { | ||
acc.spendableUTXOs.push(utxo) | ||
} | ||
|
||
return acc | ||
}, | ||
{ | ||
spendableUTXOs: [], | ||
unspendableUTXOs: [] | ||
} as Record<string, UTXO[]> | ||
) | ||
|
||
return { | ||
totalUTXOs: utxos.length, | ||
spendableUTXOs, | ||
unspendableUTXOs | ||
} | ||
} | ||
} |
Oops, something went wrong.