Skip to content

Commit 7db29c5

Browse files
author
victor-134
committed
feat(sdk): implement transaction progress streaming (MettaChain#167)
1 parent c686c82 commit 7db29c5

5 files changed

Lines changed: 254 additions & 96 deletions

File tree

sdk/frontend/src/client/EscrowClient.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import type { PropertyRegistryClient, Signer } from './PropertyRegistryClient';
11-
import type { EscrowInfo, TxResult } from '../types';
11+
import type { EscrowInfo, TxResult, TxProgressCallback } from '../types';
1212

1313
/**
1414
* Focused client for escrow operations.
@@ -56,8 +56,9 @@ export class EscrowClient {
5656
buyer: string,
5757
seller: string,
5858
amount: bigint,
59+
onProgress?: TxProgressCallback,
5960
): Promise<{ escrowId: number } & TxResult> {
60-
return this.registryClient.createEscrow(signer, propertyId, buyer, seller, amount);
61+
return this.registryClient.createEscrow(signer, propertyId, buyer, seller, amount, onProgress);
6162
}
6263

6364
/**
@@ -66,8 +67,8 @@ export class EscrowClient {
6667
* @param signer - Authorized account
6768
* @param escrowId - Escrow to release
6869
*/
69-
async release(signer: Signer, escrowId: number): Promise<TxResult> {
70-
return this.registryClient.releaseEscrow(signer, escrowId);
70+
async release(signer: Signer, escrowId: number, onProgress?: TxProgressCallback): Promise<TxResult> {
71+
return this.registryClient.releaseEscrow(signer, escrowId, onProgress);
7172
}
7273

7374
/**
@@ -76,8 +77,8 @@ export class EscrowClient {
7677
* @param signer - Authorized account
7778
* @param escrowId - Escrow to refund
7879
*/
79-
async refund(signer: Signer, escrowId: number): Promise<TxResult> {
80-
return this.registryClient.refundEscrow(signer, escrowId);
80+
async refund(signer: Signer, escrowId: number, onProgress?: TxProgressCallback): Promise<TxResult> {
81+
return this.registryClient.refundEscrow(signer, escrowId, onProgress);
8182
}
8283

8384
/**

sdk/frontend/src/client/OracleClient.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import type {
1919
VolatilityMetrics,
2020
PropertyType,
2121
OracleSource,
22-
TxResult,
2322
ContractEvent,
2423
ClientOptions,
24+
TxProgressCallback,
25+
TxProgressStatus,
2526
} from '../types';
2627
import { decodeContractError, TransactionError, GasEstimationError } from '../utils/errors';
2728
import { decodeTransactionEvents } from '../utils/events';
@@ -131,8 +132,9 @@ export class OracleClient {
131132
async requestValuation(
132133
signer: Signer,
133134
propertyId: number,
135+
onProgress?: TxProgressCallback,
134136
): Promise<{ requestId: number } & TxResult> {
135-
const txResult = await this.submitTx(signer, 'request_valuation', [propertyId]);
137+
const txResult = await this.submitTx(signer, 'request_valuation', [propertyId], onProgress);
136138
return { requestId: 0, ...txResult };
137139
}
138140

@@ -145,8 +147,9 @@ export class OracleClient {
145147
async batchRequestValuations(
146148
signer: Signer,
147149
propertyIds: number[],
150+
onProgress?: TxProgressCallback,
148151
): Promise<TxResult> {
149-
return this.submitTx(signer, 'batch_request_valuations', [propertyIds]);
152+
return this.submitTx(signer, 'batch_request_valuations', [propertyIds], onProgress);
150153
}
151154

152155
// ==========================================================================
@@ -156,15 +159,15 @@ export class OracleClient {
156159
/**
157160
* Adds an oracle source (admin only).
158161
*/
159-
async addSource(signer: Signer, source: OracleSource): Promise<TxResult> {
160-
return this.submitTx(signer, 'add_source', [source]);
162+
async addSource(signer: Signer, source: OracleSource, onProgress?: TxProgressCallback): Promise<TxResult> {
163+
return this.submitTx(signer, 'add_source', [source], onProgress);
161164
}
162165

163166
/**
164167
* Removes an oracle source (admin only).
165168
*/
166-
async removeSource(signer: Signer, sourceId: string): Promise<TxResult> {
167-
return this.submitTx(signer, 'remove_source', [sourceId]);
169+
async removeSource(signer: Signer, sourceId: string, onProgress?: TxProgressCallback): Promise<TxResult> {
170+
return this.submitTx(signer, 'remove_source', [sourceId], onProgress);
168171
}
169172

170173
/**
@@ -211,6 +214,7 @@ export class OracleClient {
211214
signer: Signer,
212215
method: string,
213216
args: unknown[],
217+
onProgress?: TxProgressCallback,
214218
): Promise<TxResult> {
215219
const signerAddress = typeof signer === 'string' ? signer : signer.address;
216220

@@ -246,7 +250,26 @@ export class OracleClient {
246250
signer as KeyringPair,
247251
{},
248252
({ status, events: rawEvents, dispatchError }) => {
253+
if (status.isReady && onProgress) {
254+
onProgress({ status: TxProgressStatus.Ready, txHash: tx.hash.toString() });
255+
} else if (status.isBroadcast && onProgress) {
256+
onProgress({ status: TxProgressStatus.Broadcast, txHash: tx.hash.toString() });
257+
} else if (status.isInBlock && onProgress) {
258+
onProgress({
259+
status: TxProgressStatus.InBlock,
260+
txHash: tx.hash.toString(),
261+
blockHash: status.asInBlock.toString()
262+
});
263+
}
264+
249265
if (dispatchError) {
266+
if (onProgress) {
267+
onProgress({
268+
status: TxProgressStatus.Error,
269+
txHash: tx.hash.toString(),
270+
message: dispatchError.toString()
271+
});
272+
}
250273
reject(
251274
new TransactionError(
252275
`Transaction failed: ${dispatchError.toString()}`,
@@ -259,6 +282,14 @@ export class OracleClient {
259282

260283
if (status.isFinalized) {
261284
const blockHash = status.asFinalized.toString();
285+
if (onProgress) {
286+
onProgress({
287+
status: TxProgressStatus.Finalized,
288+
txHash: tx.hash.toString(),
289+
blockHash
290+
});
291+
}
292+
262293
const decodedEvents: ContractEvent[] = decodeTransactionEvents(
263294
this.abi,
264295
rawEvents as unknown as Array<{

0 commit comments

Comments
 (0)