Skip to content

Commit

Permalink
Merge pull request #4 from bugbytesinc/vnext-0.31.7
Browse files Browse the repository at this point in the history
Mirror Updates and Bug Fixes
  • Loading branch information
bugbytesinc authored Dec 6, 2022
2 parents 0a97655 + 78b99f7 commit a5c9e70
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 95 deletions.
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/hapi-connect/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bugbytes/hapi-connect",
"version": "0.31.6",
"version": "0.31.7",
"description": "Lightweight HashConnect Browser Side Client Library",
"main": "./lib/index.js",
"exports": "./lib/index.js",
Expand All @@ -25,8 +25,8 @@
},
"homepage": "https://github.com/bugbytesinc/hapi-proto#readme",
"peerDependencies": {
"@bugbytes/hapi-proto": "0.31.6",
"@bugbytes/hapi-util": "0.31.6",
"@bugbytes/hapi-proto": "0.31.7",
"@bugbytes/hapi-util": "0.31.7",
"long": ">=5.2.0",
"protobufjs": ">=7.1.2",
"simple-crypto-js": "^3.0.1",
Expand Down
7 changes: 5 additions & 2 deletions packages/hapi-connect/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ declare global {
declare type Handler = (value: RelayMessage) => boolean;

export class HashConnectClient {

private readonly relay: Relay;
private topics = new Map<string, string>();
private pending: Handler[] = [];
private readonly topics: Map<string, string>;
private readonly pending: Handler[];

constructor(relay = new Relay()) {
this.topics = new Map<string, string>();
this.pending = [];
this.relay = relay;
this.relay.connected.on(() => {
for (const topic of this.topics.keys()) {
Expand Down
22 changes: 15 additions & 7 deletions packages/hapi-connect/src/relay.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { createEmitter } from "ts-typed-events";
import { createEmitter, Emitter, SealedEvent } from "ts-typed-events";


export class Relay {

private readonly url: string;
private socket!: WebSocket;
private readonly emitConnected = createEmitter();
private readonly emitDisconnected = createEmitter();
private readonly emitReceived = createEmitter<string>();
public readonly received = this.emitReceived.event;
public readonly connected = this.emitConnected.event;
public readonly disconnected = this.emitDisconnected.event;
private readonly emitConnected: Emitter<undefined, SealedEvent<undefined>>;
private readonly emitDisconnected: Emitter<undefined, SealedEvent<undefined>>;
private readonly emitReceived: Emitter<string, SealedEvent<string>>;
public readonly received: SealedEvent<string>;
public readonly connected: SealedEvent<undefined>;
public readonly disconnected: SealedEvent<undefined>;

public get isOpen() {
return this.socket.readyState === WebSocket.OPEN;
}

constructor(url = "wss://hashconnect.hashpack.app") {
this.url = url;
this.emitConnected = createEmitter();
this.emitDisconnected = createEmitter();
this.emitReceived = createEmitter<string>();
this.received = this.emitReceived.event;
this.connected = this.emitConnected.event;
this.disconnected = this.emitDisconnected.event;
this.connect();
}

Expand Down
6 changes: 3 additions & 3 deletions packages/hapi-mirror/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bugbytes/hapi-mirror",
"version": "0.31.6",
"version": "0.31.7",
"description": "Hedera Mirror Node Client",
"main": "./lib/index.js",
"exports": "./lib/index.js",
Expand All @@ -24,8 +24,8 @@
},
"homepage": "https://github.com/bugbytesinc/hapi-proto#readme",
"peerDependencies": {
"@bugbytes/hapi-proto": "0.31.6",
"@bugbytes/hapi-util": "0.31.6",
"@bugbytes/hapi-proto": "0.31.7",
"@bugbytes/hapi-util": "0.31.7",
"long": ">=5.2.0",
"protobufjs": ">=7.1.2"
}
Expand Down
44 changes: 43 additions & 1 deletion packages/hapi-mirror/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ export type ContractResult = components["schemas"]["ContractResult"];
export type MessageInfo = components["schemas"]["TopicMessage"];
export type TransactionInfo = components["schemas"]["Transaction"];
export type TokenBalanceInfo = { timestamp: TimestampKeyString, account: EntityIdKeyString, token: EntityIdKeyString, balance: number };
export type TokenRelationship = components["schemas"]["TokenRelationship"]
export type TokenRelationshipIterator = AsyncGenerator<TokenRelationship, void, unknown>;
export type Nft = components["schemas"]["Nft"]
export type NftIterator = AsyncGenerator<Nft, void, unknown>;

type NodeInfoListResponse = components["schemas"]["NetworkNodesResponse"];
type MessageInfoListResponse = components["schemas"]["TopicMessagesResponse"];
type TokenBalanceListResponse = components["schemas"]["TokenBalancesResponse"];
type TransactionsListResponse = components["schemas"]["TransactionsResponse"];
type TokenRelationshipListResponse = components["schemas"]["TokenRelationshipResponse"];
type NftListResponse = components["schemas"]["Nfts"];

export class MirrorRestClient {

private readonly mirrorHostname: string;

constructor(mirrorHostname: string) {
Expand Down Expand Up @@ -67,6 +74,42 @@ export class MirrorRestClient {
return await response.json() as AccountInfo;
}

async * getAccountTokens(accountId: EntityIdKeyString | AccountID): TokenRelationshipIterator {
const accountKey = (typeof accountId === 'string') ? accountId : accountID_to_keyString(accountId);
let path = `/api/v1/accounts/${accountKey}/tokens`;
while (path) {
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new MirrorError(response.statusText, response.status);
}
const payload = await response.json() as TokenRelationshipListResponse;
if (payload.tokens) {
for (const item of payload.tokens) {
yield item;
}
}
path = payload.links?.next || '';
}
}

async * getAccountNfts(accountId: EntityIdKeyString | AccountID): NftIterator {
const accountKey = (typeof accountId === 'string') ? accountId : accountID_to_keyString(accountId);
let path = `/api/v1/accounts/${accountKey}/nfts`;
while (path) {
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new MirrorError(response.statusText, response.status);
}
const payload = await response.json() as NftListResponse;
if (payload.nfts) {
for (const item of payload.nfts) {
yield item;
}
}
path = payload.links?.next || '';
}
}

async getContractInfo(contractId: EntityIdKeyString | ContractID): Promise<ContractInfo> {
const contractKey = (typeof contractId === 'string') ? contractId : contractID_to_keyString(contractId);
const path = `/api/v1/contracts/${contractKey}`;
Expand Down Expand Up @@ -147,5 +190,4 @@ export class MirrorRestClient {
}
throw new MirrorError('No Messages Found.', 404);
}

}
1 change: 1 addition & 0 deletions packages/hapi-mirror/src/mirror-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export class MirrorError extends Error {

readonly status: number;
readonly innerError: Error | undefined

Expand Down
Loading

0 comments on commit a5c9e70

Please sign in to comment.