Skip to content

Commit bbe271b

Browse files
authored
Merge pull request #675 from LF-Decentralized-Trust-labs/sdk-updates
Misc sdk updates
2 parents 36b9b5a + e0f50a9 commit bbe271b

File tree

8 files changed

+131
-27
lines changed

8 files changed

+131
-27
lines changed

example/event-listener/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ async function main(): Promise<boolean> {
9595
subscriptions: ["example-event-listener"],
9696
},
9797
async (sender, event) => {
98-
if (event.method === "ptx_subscription") {
98+
if (
99+
event.method === "ptx_subscription" &&
100+
"receipts" in event.params.result
101+
) {
99102
for (const receipt of event.params.result.receipts) {
100103
// Process each transaction receipt
101104
await processReceipt(receipt);

example/swap/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ async function main(): Promise<boolean> {
183183
},
184184
],
185185
});
186-
if (!checkReceipt(receipt)) return false;
187186
const preparedCashTransfer = await paladin3.pollForPreparedTransaction(
188187
txID,
189188
10000
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ethers } from "ethers";
2+
3+
export interface IBlockchainEventListener {
4+
name: string;
5+
sources: IBlockchainEventListenerSource[];
6+
options?: IBlockchainEventListenerOptions;
7+
}
8+
9+
export interface IBlockchainEventListenerOptions {
10+
batchSize?: number;
11+
batchTimeout?: string;
12+
fromBlock?: string;
13+
}
14+
15+
export interface IBlockchainEventListenerSource {
16+
abi: ethers.JsonFragment[];
17+
address?: string;
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export interface IIndexedBlock {
2+
number: number;
3+
hash: string;
4+
timestamp: number;
5+
}
6+
7+
export interface IIndexedTransaction {
8+
hash: string;
9+
blockNumber: number;
10+
transactionIndex: number;
11+
from?: string;
12+
to?: string;
13+
nonce: number;
14+
contractAddress?: string;
15+
result?: "failure" | "success";
16+
block?: IIndexedBlock;
17+
}
18+
19+
export interface IIndexedEvent {
20+
blockNumber: number;
21+
transactionIndex: number;
22+
logIndex: number;
23+
transactionHash: string;
24+
signature: string;
25+
transaction?: IIndexedTransaction;
26+
block?: IIndexedBlock;
27+
}
28+
29+
export interface IEventWithData extends IIndexedEvent {
30+
soliditySignature: string;
31+
address: string;
32+
data: any;
33+
}

sdk/typescript/src/interfaces/transaction.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,13 @@ export enum TransactionType {
154154
Public = "public",
155155
}
156156

157-
export interface IDecodedEvent {
157+
export interface IABIDecodedData {
158158
signature: string;
159159
definition: ethers.JsonFragment;
160160
data: any;
161161
summary: string; // errors only
162162
}
163163

164-
export interface IEventWithData {
165-
blockNumber: number;
166-
transactionIndex: number;
167-
logIndex: number;
168-
transactionHash: string;
169-
signature: string;
170-
soliditySignature: string;
171-
address: string;
172-
data: any;
173-
}
174-
175164
export interface IStoredABI {
176165
hash: string;
177166
abi: ethers.InterfaceAbi;

sdk/typescript/src/interfaces/websocket.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as http from "http";
22
import WebSocket from "ws";
3-
import { ITransactionReceipt } from "./transaction";
3+
import { IEventWithData } from "./blockindex";
44
import { Logger } from "./logger";
5+
import { ITransactionReceipt } from "./transaction";
56

67
export interface WebSocketSender {
78
send: (json: object) => void;
@@ -18,22 +19,35 @@ export interface WebSocketEventCallback {
1819

1920
export interface WebSocketClientOptions {
2021
url: string;
21-
subscriptions?: string[];
2222
username?: string;
2323
password?: string;
24+
subscriptions?: string[] | WebSocketSubscription[]; // TODO: deprecate string[]
2425
logger?: Logger;
2526
heartbeatInterval?: number;
2627
reconnectDelay?: number;
2728
afterConnect?: WebSocketConnectCallback;
2829
socketOptions?: WebSocket.ClientOptions | http.ClientRequestArgs;
2930
}
3031

32+
export interface WebSocketSubscription {
33+
type: "receipts" | "blockchainevents";
34+
name: string;
35+
}
36+
3137
export interface WebSocketEvent {
3238
method: "ptx_subscription" | undefined;
3339
params: {
3440
subscription: string;
35-
result: {
36-
receipts: ITransactionReceipt[];
37-
};
41+
result: TransactionReceiptBatch | TransactionEventBatch;
3842
};
3943
}
44+
45+
export interface TransactionReceiptBatch {
46+
batchId: number;
47+
receipts: ITransactionReceipt[];
48+
}
49+
50+
export interface TransactionEventBatch {
51+
batchId: number;
52+
events: IEventWithData[];
53+
}

sdk/typescript/src/paladin.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
StateStatus,
99
Verifiers,
1010
} from "./interfaces";
11+
import { IBlockchainEventListener } from "./interfaces/blockchainevent";
12+
import { IEventWithData } from "./interfaces/blockindex";
1113
import { Logger } from "./interfaces/logger";
1214
import {
1315
JsonRpcResult,
@@ -22,8 +24,7 @@ import {
2224
} from "./interfaces/privacygroups";
2325
import { IQuery } from "./interfaces/query";
2426
import {
25-
IDecodedEvent,
26-
IEventWithData,
27+
IABIDecodedData,
2728
INotoDomainReceipt,
2829
IPenteDomainReceipt,
2930
IPreparedTransaction,
@@ -212,8 +213,8 @@ export default class PaladinClient {
212213

213214
async resolveVerifier(
214215
lookup: string,
215-
algorithm: Algorithms,
216-
verifierType: Verifiers
216+
algorithm: Algorithms | string,
217+
verifierType: Verifiers | string
217218
) {
218219
const res = await this.post<JsonRpcResult<string>>("ptx_resolveVerifier", [
219220
lookup,
@@ -234,9 +235,17 @@ export default class PaladinClient {
234235
return res.data.result;
235236
}
236237

238+
async decodeCall(callData: string, dataFormat: string) {
239+
const res = await this.post<JsonRpcResult<IABIDecodedData>>(
240+
"ptx_decodeCall",
241+
[callData, dataFormat]
242+
);
243+
return res.data.result;
244+
}
245+
237246
async decodeEvent(topics: string[], data: string) {
238247
try {
239-
const res = await this.post<JsonRpcResult<IDecodedEvent>>(
248+
const res = await this.post<JsonRpcResult<IABIDecodedData>>(
240249
"ptx_decodeEvent",
241250
[topics, data, ""]
242251
);
@@ -351,4 +360,38 @@ export default class PaladinClient {
351360
);
352361
return res.data.result;
353362
}
363+
364+
async getReceiptListener(name: string) {
365+
const res = await this.post<JsonRpcResult<ITransactionReceiptListener>>(
366+
"ptx_getReceiptListener",
367+
[name],
368+
{ validateStatus: (status) => status < 300 || status === 404 }
369+
);
370+
return res.status === 404 ? undefined : res.data.result;
371+
}
372+
373+
async createBlockchainEventListener(listener: IBlockchainEventListener) {
374+
const res = await this.post<JsonRpcResult<boolean>>(
375+
"ptx_createBlockchainEventListener",
376+
[listener]
377+
);
378+
return res.data.result;
379+
}
380+
381+
async deleteBlockchainEventListener(name: string) {
382+
const res = await this.post<JsonRpcResult<boolean>>(
383+
"ptx_deleteBlockchainEventListener",
384+
[name]
385+
);
386+
return res.data.result;
387+
}
388+
389+
async getBlockchainEventListener(name: string) {
390+
const res = await this.post<JsonRpcResult<IBlockchainEventListener>>(
391+
"ptx_getBlockchainEventListener",
392+
[name],
393+
{ validateStatus: (status) => status < 300 || status === 404 }
394+
);
395+
return res.status === 404 ? undefined : res.data.result;
396+
}
354397
}

sdk/typescript/src/websocket.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
WebSocketClientOptions,
77
WebSocketEvent,
88
WebSocketEventCallback,
9+
WebSocketSubscription,
910
} from "./interfaces/websocket";
1011

1112
export class PaladinWebSocketClient {
@@ -54,10 +55,14 @@ export class PaladinWebSocketClient {
5455
this.logger.log("Connected");
5556
}
5657
this.schedulePing();
57-
for (const name of this.options.subscriptions ?? []) {
58+
for (const subOrName of this.options.subscriptions ?? []) {
5859
// Automatically connect subscriptions
59-
this.subscribe("receipts", name);
60-
this.logger.log(`Started listening on subscription ${name}`);
60+
const sub: WebSocketSubscription =
61+
typeof subOrName === "string"
62+
? { type: "receipts", name: subOrName }
63+
: subOrName;
64+
this.subscribe(sub.type, sub.name);
65+
this.logger.log(`Started listening on subscription ${sub.name}`);
6166
}
6267
if (this.options?.afterConnect !== undefined) {
6368
this.options.afterConnect(this);

0 commit comments

Comments
 (0)