Skip to content

Commit

Permalink
Store error when persistTransaction fails.
Browse files Browse the repository at this point in the history
  * This tells the user to stop polling /getConfirmedTransaction because
    the transaction will never be confirmed.
  • Loading branch information
MantisClone committed Oct 27, 2023
1 parent fdd8a32 commit 39a1fcb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
18 changes: 14 additions & 4 deletions packages/request-node/src/request/confirmedTransactionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import Keyv, { Store } from 'keyv';
* The client can call the getConfirmed entry point, to get the confirmed event.
*/
export default class ConfirmedTransactionStore {
private store: Keyv<DataAccessTypes.IReturnPersistTransactionRaw>;
private store: Keyv<DataAccessTypes.IReturnPersistTransactionRaw | Error>;

/**
* Confirmed transactions store constructor
*/
constructor(store?: Store<DataAccessTypes.IReturnPersistTransaction>) {
this.store = new Keyv<DataAccessTypes.IReturnPersistTransaction>({
constructor(store?: Store<DataAccessTypes.IReturnPersistTransaction | Error>) {
this.store = new Keyv<DataAccessTypes.IReturnPersistTransaction | Error>({
namespace: 'ConfirmedTransactions',
store,
});
}

public async getConfirmedTransaction(
transactionHash: string,
): Promise<DataAccessTypes.IReturnPersistTransactionRaw | undefined> {
): Promise<DataAccessTypes.IReturnPersistTransactionRaw | Error | undefined> {
return this.store.get(transactionHash);
}

Expand All @@ -37,4 +37,14 @@ export default class ConfirmedTransactionStore {
): Promise<void> {
await this.store.set(transactionHash, result);
}

/**
* Stores the error
*
* @param transactionHash hash of the transaction
* @param error error of the event "error"
*/
public async addFailedTransaction(transactionHash: string, error: Error): Promise<void> {
await this.store.set(transactionHash, error);
}
}
6 changes: 5 additions & 1 deletion packages/request-node/src/request/persistTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ export default class PersistTransactionHandler {
});

// when the transaction fails, log an error
dataAccessResponse.on('error', async (e) => {
dataAccessResponse.on('error', async (e: unknown) => {
await this.confirmedTransactionStore.addFailedTransaction(
transactionHash.value,
e as Error,
);
this.logger.error(`persistTransaction error: ${e}\n
transactionHash: ${transactionHash.value}, channelId: ${
clientRequest.body.channelId
Expand Down

0 comments on commit 39a1fcb

Please sign in to comment.