Skip to content

Commit fe9798f

Browse files
committed
feat: improve error message on wasm errors
1 parent 1ee6ee9 commit fe9798f

File tree

9 files changed

+53
-21
lines changed

9 files changed

+53
-21
lines changed

apps/namadillo/src/App/Staking/ClaimRewardsSubmitModalStage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type ClaimRewardsPanelProps = {
1818
rewardsToClaim: ClaimRewardsMsgValue[];
1919
isClaimAndStake: boolean;
2020
feeProps: TransactionFeeProps;
21-
onClaim: () => void;
21+
onClaim: () => Promise<void>;
2222
isClaiming: boolean;
2323
isEnabled: boolean;
2424
error?: string;

apps/namadillo/src/App/Staking/IncrementBonding.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ const IncrementBonding = (): JSX.Element => {
115115
return getTopValidatorsAddresses(validators?.data ?? []);
116116
}, [validators]);
117117

118-
const onSubmit = (e: React.FormEvent): void => {
118+
const onSubmit = async (e: React.FormEvent): Promise<void> => {
119119
e.preventDefault();
120-
performBonding();
120+
await performBonding();
121121
};
122122

123123
const errorMessage = ((): string => {

apps/namadillo/src/App/Staking/ReDelegate.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const ReDelegate = (): JSX.Element => {
8787
});
8888
};
8989

90-
const onSubmit = (e: React.FormEvent): void => {
90+
const onSubmit = async (e: React.FormEvent): Promise<void> => {
9191
e.preventDefault();
9292
// Go to next page or do nothing
9393
if (step === "remove" && totalToRedelegate) {
@@ -96,7 +96,7 @@ export const ReDelegate = (): JSX.Element => {
9696
}
9797
return;
9898
}
99-
performRedelegate();
99+
await performRedelegate();
100100
};
101101

102102
const totalAssignedAmounts = BigNumber.sum(

apps/namadillo/src/App/Staking/StakingRewards.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ export const StakingRewards = (): JSX.Element => {
9191
return "Confirm Claim";
9292
}, [shouldClaimAndStake, rewardsToClaim]);
9393

94-
const onSubmitClaim = (): void => {
94+
const onSubmitClaim = async (): Promise<void> => {
9595
if (shouldClaimAndStake) {
96-
claimRewardsAndStake();
96+
await claimRewardsAndStake();
9797
} else {
98-
claimRewards();
98+
await claimRewards();
9999
}
100100
};
101101

apps/namadillo/src/App/Staking/StakingWithdrawModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export const StakingWithdrawModal = (): JSX.Element => {
108108
<ActionButton
109109
outlineColor="pink"
110110
textColor="pink"
111-
onClick={() => withdraw()}
111+
onClick={async () => await withdraw()}
112112
disabled={
113113
totalWithdrawableAmount.eq(0) ||
114114
!withdrawTxEnabled ||

apps/namadillo/src/App/Staking/Unstake.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ export const Unstake = (): JSX.Element => {
8282
);
8383
};
8484

85-
const onSubmit = (e: FormEvent): void => {
85+
const onSubmit = async (e: FormEvent): Promise<void> => {
8686
e.preventDefault();
87-
performUnbond();
87+
await performUnbond();
8888
};
8989

9090
const validationMessage = ((): string => {

apps/namadillo/src/utils/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ export const toBaseAmount = (
124124
return displayAmount.shiftedBy(displayUnit.exponent);
125125
};
126126

127+
const toGasMsg = (error: string, gasLimit: BigNumber): string => {
128+
return `${error.toString()} Please raise the Gas Amount above the previously provided ${gasLimit} in the fee options for your transaction.`;
129+
};
130+
127131
/**
128132
* Returns formatted error message based on tx props and error code
129133
*/
@@ -132,14 +136,21 @@ export const toErrorDetail = (
132136
error: BroadcastTxError
133137
): string => {
134138
try {
135-
const { code } = error.toProps();
139+
const { code, info } = error.toProps();
140+
const { args } = tx[0];
136141
// TODO: Over time we may expand this to format errors for more result codes
137142
switch (code) {
138143
case ResultCode.TxGasLimit:
139-
const { gasLimit } = tx[0].args;
140-
return `${error.toString()} Please raise the Gas Amount above the previously provided ${gasLimit} in the fee options for your transaction.`;
144+
return toGasMsg(error.toString(), args.gasLimit);
145+
case ResultCode.WasmRuntimeError:
146+
// We can only check error type by reading the error message
147+
if (info.includes("Gas error:")) {
148+
return toGasMsg(error.toString(), args.gasLimit);
149+
}
150+
return error.toString() + ` ${info}`;
151+
141152
default:
142-
return error.toString();
153+
return error.toString() + ` ${info}`;
143154
}
144155
} catch (_e) {
145156
return `${error.toString()}`;

packages/shared/lib/src/sdk/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use namada_sdk::string_encoding::Format;
4040
use namada_sdk::tendermint_rpc::Url;
4141
use namada_sdk::token::{Amount, DenominatedAmount, MaspEpoch};
4242
use namada_sdk::token::{MaspTxId, OptionExt};
43-
use namada_sdk::tx::data::TxType;
43+
use namada_sdk::tx::data::{ResultCode, TxType};
4444
use namada_sdk::tx::Section;
4545
use namada_sdk::tx::{
4646
build_batch, build_bond, build_claim_rewards, build_ibc_transfer, build_redelegation,
@@ -338,6 +338,11 @@ impl Sdk {
338338
}
339339

340340
pub async fn broadcast_tx(&self, tx_bytes: &[u8], deadline: u64) -> Result<JsValue, JsValue> {
341+
#[derive(serde::Serialize)]
342+
struct TxErrResponse {
343+
pub code: u32,
344+
pub info: String,
345+
}
341346
let tx = Tx::try_from_slice(tx_bytes).expect("Should be able to deserialize a Tx");
342347
let cmts = tx.commitments().clone();
343348
let wrapper_hash = tx.wrapper_hash();
@@ -349,8 +354,10 @@ impl Sdk {
349354
Ok(res) => {
350355
if res.clone().code != 0.into() {
351356
return Err(JsValue::from(
352-
&serde_json::to_string(&res)
353-
.map_err(|e| JsValue::from_str(&e.to_string()))?,
357+
&serde_json::to_string(&TxErrResponse {
358+
code: res.code.into(),
359+
info: String::from(""),
360+
}).map_err(|e| JsValue::from_str(&e.to_string()))?,
354361
));
355362
}
356363

@@ -361,6 +368,15 @@ impl Sdk {
361368
.map_err(|e| JsValue::from_str(&e.to_string()))?;
362369
let tx_response = TxResponse::from_event(event);
363370

371+
if tx_response.code != ResultCode::Ok {
372+
return Err(JsValue::from(
373+
&serde_json::to_string(&TxErrResponse {
374+
code: tx_response.code.into(),
375+
info: tx_response.info,
376+
}).map_err(|e| JsValue::from_str(&e.to_string()))?,
377+
));
378+
}
379+
364380
let mut batch_tx_results: Vec<tx::BatchTxResult> = vec![];
365381
let code =
366382
u8::try_from(tx_response.code.to_usize()).expect("Code should fit in u8");

packages/types/src/errors.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { ResultCode, ResultCodes, TxResponseProps } from "./tx";
1+
import { ResultCode, ResultCodes } from "./tx";
2+
3+
export type BroadcastTxErrorProps = {
4+
code: ResultCode;
5+
info: string;
6+
};
27

38
/**
49
* Custom error for Broadcast Tx
@@ -32,9 +37,9 @@ export class BroadcastTxError extends Error {
3237
/**
3338
* @returns TxResponseProps
3439
*/
35-
toProps(): TxResponseProps {
40+
toProps(): BroadcastTxErrorProps {
3641
try {
37-
const props = JSON.parse(this.message) as TxResponseProps;
42+
const props = JSON.parse(this.message) as BroadcastTxErrorProps;
3843
return props;
3944
} catch (e) {
4045
throw new Error(`${e}`);

0 commit comments

Comments
 (0)