diff --git a/packages/docs/src/tools/stack/blockchain-functions.ts b/packages/docs/src/tools/stack/blockchain-functions.ts index c6ca05b..589bd77 100644 --- a/packages/docs/src/tools/stack/blockchain-functions.ts +++ b/packages/docs/src/tools/stack/blockchain-functions.ts @@ -1,11 +1,17 @@ -import { BlockchainElectrum, encodeExtendedJson, Transaction } from '@xocash/stack'; +import { + BlockchainElectrum, + encodeExtendedJson, + Transaction, +} from '@xocash/stack'; export const electrumClient = { from: [ { label: 'From Servers', inputs: ['Servers'], - models: ['bch.imaginary.cash, cashnode.bch.ninja, electrum.imaginary.cash'], + models: [ + 'bch.imaginary.cash, cashnode.bch.ninja, electrum.imaginary.cash', + ], callback: async (args: [string]) => { const electrum = await BlockchainElectrum.from({ opts: { @@ -39,11 +45,11 @@ export const electrumClient = { }, }, { - label: 'Fetch Transactions', + label: 'Fetch Address History', inputs: ['Address'], models: ['Address'], callback: async (electrum: BlockchainElectrum, args: [string]) => { - const transactions = await electrum.fetchTransactions(args[0]); + const transactions = await electrum.fetchAddressHistory(args[0]); return encodeExtendedJson(transactions.toArray(), 2); }, }, @@ -57,4 +63,4 @@ export const electrumClient = { }, }, ], -}; \ No newline at end of file +}; diff --git a/packages/stack/src/blockchain/base-blockchain.ts b/packages/stack/src/blockchain/base-blockchain.ts index ac7fd8a..95c6bee 100644 --- a/packages/stack/src/blockchain/base-blockchain.ts +++ b/packages/stack/src/blockchain/base-blockchain.ts @@ -83,7 +83,15 @@ export abstract class BaseBlockchain extends EventEmitter { * @returns {Promise} A promise that resolves with the list of transactions * @throws {Error} If the address is invalid or the query fails */ - abstract fetchTransactions(address: string): Promise; + abstract fetchAddressHistory( + address: string, + ): Promise; + + // TODO: We may have a fetchTokenHistory in future, but Fulcrum does not currently support indexing tokens. + // There are actually a few functional discrepancies with the adapters: different adapters grant different capabilities. + // For example, Chaingraph can fetch token and address history. But BCHN P2P network cannot even fetch address history. + // We might want to handle this by having multiple BlockchainInterfaces (e.g. IBlockchainAddressSubscriptions) that describe capabilities. + // But we should return to that later. /** * Broadcasts a raw transaction to the blockchain network. diff --git a/packages/stack/src/blockchain/blockchain-electrum.ts b/packages/stack/src/blockchain/blockchain-electrum.ts index 16912d8..5a5deb2 100644 --- a/packages/stack/src/blockchain/blockchain-electrum.ts +++ b/packages/stack/src/blockchain/blockchain-electrum.ts @@ -69,9 +69,7 @@ export class BlockchainElectrum extends BaseBlockchain { store: BaseStore; opts: BlockchainElectrumOpts; - constructor( - public readonly params: BlockchainElectrumParams = {}, - ) { + constructor(public readonly params: BlockchainElectrumParams = {}) { super(); // Set the store, or use the memory store by default. @@ -266,7 +264,7 @@ export class BlockchainElectrum extends BaseBlockchain { return transaction; } - async fetchTransactions(address: string) { + async fetchAddressHistory(address: string) { // Wait for connection to be established before attempting to subscribe. // TODO: Consider doing this at a higher level. if (!this.isConnected) { diff --git a/packages/stack/src/blockchain/blockchain-local.ts b/packages/stack/src/blockchain/blockchain-local.ts index 1baf3af..5f02e5c 100644 --- a/packages/stack/src/blockchain/blockchain-local.ts +++ b/packages/stack/src/blockchain/blockchain-local.ts @@ -71,7 +71,7 @@ export class BlockchainLocal extends BaseBlockchain { return tx.transaction; } - async fetchTransactions(address: string) { + async fetchAddressHistory(address: string) { return this.transactions.filter((tx) => { // Check if any input or output involves the address // This is a simplified check and might need to be more sophisticated in a real implementation diff --git a/packages/stack/src/blockchain/blockchain.ts b/packages/stack/src/blockchain/blockchain.ts index f3ab834..4f44eb2 100644 --- a/packages/stack/src/blockchain/blockchain.ts +++ b/packages/stack/src/blockchain/blockchain.ts @@ -60,11 +60,11 @@ export class Blockchain extends BaseBlockchain { } } - async fetchTransactions(address: string) { + async fetchAddressHistory(address: string) { if (this.blockchainElectrum.isConnected) { - return await this.blockchainElectrum.fetchTransactions(address); + return await this.blockchainElectrum.fetchAddressHistory(address); } else { - return await this.blockchainLocal.fetchTransactions(address); + return await this.blockchainLocal.fetchAddressHistory(address); } } diff --git a/packages/stack/src/wallet/wallet-p2pkh.ts b/packages/stack/src/wallet/wallet-p2pkh.ts index b571c61..37ad9ac 100644 --- a/packages/stack/src/wallet/wallet-p2pkh.ts +++ b/packages/stack/src/wallet/wallet-p2pkh.ts @@ -241,7 +241,7 @@ export class WalletP2PKH extends BaseWallet { const address = await this.getReceivingAddress(); // Get transactions involving this address. - const transactions = await this.blockchain.fetchTransactions( + const transactions = await this.blockchain.fetchAddressHistory( address.toCashAddr(), );