0.1.0 release

This commit is contained in:
Mononaut 2024-03-01 17:25:23 +00:00
parent 0b54d97e75
commit 3e692c8444
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E
3 changed files with 15 additions and 7 deletions

View File

@ -9,7 +9,7 @@ Mwck uses websocket push notifications to discover new address transaction event
## Quick start
```typescript
const mwck = require('./mwck/index.js');
const mwck = require('mwck');
const wallet = new mwck.MempoolWallet({
hostname: 'mempool.space'
});

View File

@ -1,6 +1,6 @@
{
"name": "mwck",
"version": "0.0.1",
"version": "0.1.0",
"description": "mempool.space wallet connector kit",
"main": "dist/index.js",
"module": "dist/index.esm.js",

View File

@ -6,9 +6,17 @@ import { AddressTracker } from './address';
const walletEventTypes = ['addressReady', 'txAdded', 'txConfirmed', 'txRemoved', 'txEvent', 'wsConnected', 'wsDisconnected', 'wsError'] as const;
type WalletEvent = typeof walletEventTypes[number];
export interface SubscriptionEvent {
event?: 'added' | 'confirmed' | 'removed';
address?: string;
tx?: Transaction;
state?: AddressState;
err?: any;
}
type ObserverDict = {
[event in WalletEvent]: {
[oid: number]: (args: unknown) => void;
[oid: number]: (args: SubscriptionEvent) => void;
};
};
@ -42,7 +50,7 @@ export class MempoolWallet {
this.ws.off('error');
}
private notifyObservers(event: WalletEvent, args: unknown): void {
private notifyObservers(event: WalletEvent, args: SubscriptionEvent): void {
Object.values(this.observers[event]).forEach(observer => observer(args));
}
@ -89,7 +97,7 @@ export class MempoolWallet {
await this.resync();
}
private async fetchAddressBacklog(address: string) {
private async fetchAddressBacklog(address: string, lookBack = 1) {
const addressState = this.tracking[address]?.getState();
// fetch history back one block beyond the last known confirmed transaction
@ -98,7 +106,7 @@ export class MempoolWallet {
let lastHeight;
if (lastConfirmed) {
lastTxid = lastConfirmed.txid;
lastHeight = (lastConfirmed.status?.block_height || 1) - 1;
lastHeight = (lastConfirmed.status?.block_height || 1) - lookBack;
}
const fetchedTxids = new Set();
const initialTransactions = await this.api.getAddressTransactions(address, lastTxid, lastHeight);
@ -155,7 +163,7 @@ export class MempoolWallet {
// register a handler for an event type
// returns an unsubscribe function
public subscribe(event: WalletEvent, fn: (args: unknown) => void): () => void {
public subscribe(event: WalletEvent, fn: (args: SubscriptionEvent) => void): () => void {
const oid = this.observerId++;
this.observers[event][oid] = fn;
return () => { delete this.observers[event][oid]; };