Skip to content

Commit 1963874

Browse files
committed
fix: handle a simulate errors
1 parent 8101915 commit 1963874

File tree

8 files changed

+62
-7
lines changed

8 files changed

+62
-7
lines changed

packages/adena-extension/src/common/provider/gno/gno-provider.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class GnoProvider extends GnoJSONRPCProvider {
157157
return response;
158158
}
159159

160-
async estimateGas(tx: Tx): Promise<number> {
160+
async simulateTx(tx: Tx): Promise<ResponseDeliverTx> {
161161
const encodedTx = uint8ArrayToBase64(Tx.encode(tx).finish());
162162
const params = {
163163
request: newRequest(ABCIEndpoint.ABCI_QUERY, ['.app/simulate', `${encodedTx}`, '0', false]),
@@ -175,12 +175,17 @@ export class GnoProvider extends GnoJSONRPCProvider {
175175
}
176176

177177
const simulateResult = parseProto(responseValue, ResponseDeliverTx.decode);
178-
console.info('simulateResult', simulateResult);
179178

180179
if (simulateResult.responseBase?.error) {
181180
throw new Error(simulateResult.responseBase.error.typeUrl);
182181
}
183182

184-
return simulateResult.gasUsed.toInt();
183+
return simulateResult;
184+
}
185+
186+
async estimateGas(tx: Tx): Promise<number> {
187+
return this.simulateTx(tx).then((response) => {
188+
return response.gasUsed.toInt();
189+
});
185190
}
186191
}

packages/adena-extension/src/hooks/wallet/transaction-gas/use-get-estimate-gas-price-tiers.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { GasToken } from '@common/constants/token.constant';
77
import { useAdenaContext } from '@hooks/use-context';
88
import { useQuery, UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
99
import { NetworkFeeSettingInfo, NetworkFeeSettingType } from '@types';
10-
import { Document } from 'adena-module';
10+
import { Document, documentToDefaultTx } from 'adena-module';
1111
import BigNumber from 'bignumber.js';
1212
import { useGetGasPriceTier } from './use-get-gas-price';
1313

@@ -39,10 +39,27 @@ function makeGasInfoBy(
3939
};
4040
}
4141

42+
function modifyDocument(document: Document, gasWanted: number, gasFee: number): Document {
43+
return {
44+
...document,
45+
fee: {
46+
...document.fee,
47+
gas: gasWanted.toString(),
48+
amount: [
49+
{
50+
denom: GasToken.denom,
51+
amount: gasFee.toString(),
52+
},
53+
],
54+
},
55+
};
56+
}
57+
4258
export const useGetEstimateGasPriceTiers = (
4359
document: Document | null | undefined,
4460
gasUsed: number | undefined,
4561
gasAdjustment: string,
62+
isSuccessSimulate = true,
4663
options?: UseQueryOptions<NetworkFeeSettingInfo[] | null, Error>,
4764
): UseQueryResult<NetworkFeeSettingInfo[] | null> => {
4865
const { transactionGasService } = useAdenaContext();
@@ -80,16 +97,30 @@ export const useGetEstimateGasPriceTiers = (
8097
const { gasWanted: resultGasWanted, gasFee: resultGasFee } = makeGasInfoBy(
8198
gasUsed,
8299
adjustedGasPrice,
83-
GAS_FEE_SAFETY_MARGIN,
100+
isSuccessSimulate ? GAS_FEE_SAFETY_MARGIN : 2,
84101
);
85102

103+
const modifiedDocument = modifyDocument(document, resultGasWanted, resultGasFee);
104+
105+
const isSuccess = await transactionGasService
106+
.simulateTx(documentToDefaultTx(modifiedDocument))
107+
.then(() => true)
108+
.catch((e: Error) => {
109+
if (e?.message === '/std.InvalidPubKeyError') {
110+
return true;
111+
}
112+
113+
return false;
114+
});
115+
86116
return {
87117
settingType: tier,
88118
gasInfo: {
89119
gasFee: resultGasFee,
90120
gasUsed,
91121
gasWanted: resultGasWanted,
92122
gasPrice: adjustedGasPrice,
123+
hasError: !isSuccess,
93124
},
94125
};
95126
}),

packages/adena-extension/src/hooks/wallet/use-network-fee.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const useNetworkFee = (
5555
document,
5656
gasInfo?.gasUsed || estimatedGasInfo?.gasUsed,
5757
gasAdjustment,
58+
!estimatedGasInfo?.hasError,
5859
);
5960

6061
const isLoading = useMemo(() => {
@@ -97,7 +98,7 @@ export const useNetworkFee = (
9798
}
9899

99100
const current = gasPriceTiers.find((setting) => setting.settingType === currentSettingType);
100-
if (current?.gasInfo?.hasError) {
101+
if (!current?.gasInfo) {
101102
return {
102103
gasFee: 0,
103104
gasUsed: 0,

packages/adena-extension/src/pages/popup/wallet/approve-transaction-main/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ const ApproveTransactionContainer: React.FC = () => {
363363
WalletResponseFailureType.TRANSACTION_FAILED,
364364
{
365365
hash,
366-
error: response?.name || response.message || '',
366+
error: response?.toString(),
367367
},
368368
requestData?.key,
369369
),

packages/adena-extension/src/repositories/transaction/transaction-gas.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { GnoProvider } from '@common/provider/gno/gno-provider';
2+
import { ResponseDeliverTx } from '@common/provider/gno/proto/tm2/abci';
23
import { makeIndexerRPCRequest } from '@common/utils/fetch-utils';
34
import { Tx } from '@gnolang/tm2-js-client';
45
import { NetworkMetainfo } from '@types';
@@ -47,6 +48,14 @@ export class TransactionGasRepository implements ITransactionGasRepository {
4748
return response;
4849
}
4950

51+
public async simulateTx(tx: Tx): Promise<ResponseDeliverTx> {
52+
if (!this.gnoProvider) {
53+
throw new Error('GnoProvider is not initialized');
54+
}
55+
56+
return this.gnoProvider.simulateTx(tx);
57+
}
58+
5059
public async estimateGasByTx(tx: Tx): Promise<number> {
5160
if (!this.gnoProvider) {
5261
throw new Error('GnoProvider is not initialized');
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { ResponseDeliverTx } from '@common/provider/gno/proto/tm2/abci';
12
import { Tx } from '@gnolang/tm2-js-client';
23
import { TransactionGasResponse } from './response/transaction-gas-response';
34

45
export interface ITransactionGasRepository {
56
fetchGasPrices: () => Promise<TransactionGasResponse[]>;
7+
simulateTx: (tx: Tx) => Promise<ResponseDeliverTx>;
68
estimateGasByTx: (tx: Tx) => Promise<number>;
79
}

packages/adena-extension/src/services/transaction/transaction-gas.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ResponseDeliverTx } from '@common/provider/gno/proto/tm2/abci';
12
import { Tx } from '@gnolang/tm2-js-client';
23
import { ITransactionGasRepository } from '@repositories/transaction/types';
34
import { GasPriceTierInfo } from '@types';
@@ -21,6 +22,10 @@ export class TransactionGasService implements ITransactionGasService {
2122
return gasPrice;
2223
}
2324

25+
public async simulateTx(tx: Tx): Promise<ResponseDeliverTx> {
26+
return this.gasRepository.simulateTx(tx);
27+
}
28+
2429
public async estimateGas(tx: Tx): Promise<number> {
2530
return this.gasRepository.estimateGasByTx(tx);
2631
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { ResponseDeliverTx } from '@common/provider/gno/proto/tm2/abci';
12
import { Tx } from '@gnolang/tm2-js-client';
23
import { GasPriceTierInfo } from '@types';
34

45
export interface ITransactionGasService {
56
getGasPrice: (denomination: string) => Promise<GasPriceTierInfo | null>;
7+
simulateTx(tx: Tx): Promise<ResponseDeliverTx>;
68
estimateGas(tx: Tx): Promise<number>;
79
}

0 commit comments

Comments
 (0)