Skip to content

Commit

Permalink
feat: Add transaction execution detailed response (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss authored Dec 15, 2023
2 parents 0c4b4c4 + a9b410a commit c510872
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { RoutePath } from '@router/path';
import { validateInjectionData } from '@inject/message/methods';
import BigNumber from 'bignumber.js';
import { useNetwork } from '@hooks/use-network';
import { TM2Error } from '@gnolang/tm2-js-client';
import { BroadcastTxCommitResult, TM2Error } from '@gnolang/tm2-js-client';

function mappedTransactionData(document: StdSignDoc): {
messages: readonly AminoMsg[];
Expand Down Expand Up @@ -176,36 +176,39 @@ const ApproveTransactionContainer: React.FC = () => {
setProcessType('PROCESSING');
const transaction = await transactionService.createTransaction(document, signature);
const hash = transactionService.createHash(transaction);
const responseHash = await new Promise<string>((resolve) => {
const response = await new Promise<BroadcastTxCommitResult | TM2Error | null>((resolve) => {
transactionService
.sendTransaction(transaction)
.then(resolve)
.catch((error: TM2Error) => {
const message = {
hash,
error: {
name: error.name,
message: error.message,
log: error.log,
},
};
setResponse(
InjectionMessageInstance.failure('TRANSACTION_FAILED', message, requestData?.key),
);
.catch((error: TM2Error | Error) => {
resolve(error);
});

checkHealth(currentNetwork.rpcUrl, requestData?.key);
});
if (hash === responseHash) {
if (!response) {
setResponse(
InjectionMessageInstance.success('TRANSACTION_SUCCESS', { hash }, requestData?.key),
InjectionMessageInstance.failure('TRANSACTION_FAILED', {
hash,
error: null,
}, requestData?.key),
);
return true;
} else {
}
if (response instanceof TM2Error || response instanceof Error) {
setResponse(
InjectionMessageInstance.failure('TRANSACTION_FAILED', { hash }, requestData?.key),
InjectionMessageInstance.failure('TRANSACTION_FAILED', {
hash,
error: response,
}, requestData?.key),
);
return true;
}

setResponse(
InjectionMessageInstance.success('TRANSACTION_SUCCESS', response, requestData?.key),
);
return true;
} catch (e) {
if (e instanceof Error) {
const message = e.message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,33 @@ const ApproveTransactionLedgerLoadingContainer: React.FC = () => {
.then(async (signature) => {
const transaction = await transactionService.createTransaction(document, signature);
const hash = transactionService.createHash(transaction);
const responseHash = await transactionService
const response = await transactionService
.sendTransaction(transaction)
.catch((error: TM2Error) => {
const message = {
hash,
error: {
name: error.name,
message: error.message,
log: error.log,
},
};
chrome.runtime.sendMessage(
InjectionMessageInstance.failure('TRANSACTION_FAILED', message, requestData?.key),
);
.catch((error: TM2Error | Error) => {
return error;
});

if (hash === responseHash) {
if (!response) {
chrome.runtime.sendMessage(
InjectionMessageInstance.success('TRANSACTION_SUCCESS', { hash }, requestData?.key),
InjectionMessageInstance.failure('TRANSACTION_FAILED', {
hash,
error: null,
}, requestData?.key),
);
return true;
}
if (response instanceof TM2Error || response instanceof Error) {
chrome.runtime.sendMessage(
InjectionMessageInstance.failure('TRANSACTION_FAILED', {
hash,
error: response,
}, requestData?.key),
);
return true;
}

chrome.runtime.sendMessage(
InjectionMessageInstance.failure('TRANSACTION_FAILED', { hash }, requestData?.key),
InjectionMessageInstance.success('TRANSACTION_SUCCESS', response, requestData?.key),
);
return true;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const TransferLedgerLoadingContainer = (): JSX.Element => {
.createSignatureWithLedger(currentAccount, document)
.then(async (signature) => {
const transaction = await transactionService.createTransaction(document, signature);
const hash = await transactionService.sendTransaction(transaction);
return hash;
const response = await transactionService.sendTransaction(transaction);
return response.hash;
})
.catch((error: Error) => {
if (error.message === 'Transaction signing request was rejected by the user') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { WalletService } from '..';
import { StdSignDoc } from 'adena-module/src';
import { Document } from 'adena-module/src/amino/document';
import { GnoProvider } from '@common/provider/gno/gno-provider';
import { BroadcastTxCommitResult } from '@gnolang/tm2-js-client';

export class TransactionService {
private walletService: WalletService;
Expand Down Expand Up @@ -141,11 +142,11 @@ export class TransactionService {
* @param gnoClient gno api client
* @param transaction created transaction
*/
public sendTransaction = async (transaction: Array<number>): Promise<string> => {
public sendTransaction = async (transaction: Array<number>): Promise<BroadcastTxCommitResult> => {
const provider = this.getGnoProvider();
const encodedTransaction = Buffer.from(transaction).toString('base64');
const response = await provider.sendTransactionCommit(encodedTransaction);
return response.hash;
return response;
};

public createHash(transaction: Array<number>): string {
Expand Down

0 comments on commit c510872

Please sign in to comment.