Skip to content

Commit

Permalink
PR feedback, fix amounts issue on unbond
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jul 27, 2023
1 parent 14943c3 commit ed9917b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 61 deletions.
75 changes: 19 additions & 56 deletions apps/extension/src/Approvals/ApproveTx/ConfirmTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@ import {
} from "Approvals/Approvals.components";
import { Ports } from "router";
import { useRequester } from "hooks/useRequester";
import {
SubmitApprovedBondMsg,
SubmitApprovedTransferMsg,
SubmitApprovedUnbondMsg,
SubmitApprovedWithdrawMsg,
} from "background/approvals";
import { Address } from "App/Accounts/AccountListing.components";
import { closeCurrentTab } from "utils";
import { FetchAndStoreMaspParamsMsg, HasMaspParamsMsg } from "provider";
import { TxTypeLabel } from "Approvals/types";
import { ApproveMsg, SupportedTx, TxTypeLabel, txMap } from "Approvals/types";

type Props = {
details?: ApprovalDetails;
Expand All @@ -50,64 +44,33 @@ export const ConfirmTx: React.FC<Props> = ({ details }) => {
);

try {
const Msg: ApproveMsg | undefined = txMap.get(txType as SupportedTx);
if (!msgId) {
throw new Error("msgId was not provided!");
}
switch (txType) {
case TxType.Bond: {
await requester.sendMessage(
Ports.Background,
new SubmitApprovedBondMsg(msgId, password)
);
setStatusInfo("");
setStatus(Status.Completed);
break;
}
case TxType.Transfer: {
const hasMaspParams = await requester.sendMessage(
Ports.Background,
new HasMaspParamsMsg()
);
if (!Msg) {
throw new Error("Unsupported transaction!");
}

if (!hasMaspParams) {
setStatusInfo("Fetching MASP parameters...");
try {
await requester.sendMessage(
Ports.Background,
new FetchAndStoreMaspParamsMsg()
);
} catch (e) {
setError(`Fetching MASP parameters failed: ${e}`);
setStatus(Status.Failed);
}
}
await requester.sendMessage(
Ports.Background,
new SubmitApprovedTransferMsg(msgId, password)
);
setStatusInfo("");
setStatus(Status.Completed);
break;
}
case TxType.Unbond: {
await requester.sendMessage(
Ports.Background,
new SubmitApprovedUnbondMsg(msgId, password)
);
setStatusInfo("");
setStatus(Status.Completed);
break;
}
case TxType.Withdraw: {
const hasMaspParams = await requester.sendMessage(
Ports.Background,
new HasMaspParamsMsg()
);

if (!hasMaspParams) {
setStatusInfo("Fetching MASP parameters...");
try {
await requester.sendMessage(
Ports.Background,
new SubmitApprovedWithdrawMsg(msgId, password)
new FetchAndStoreMaspParamsMsg()
);
setStatusInfo("");
setStatus(Status.Completed);
break;
} catch (e) {
setError(`Fetching MASP parameters failed: ${e}`);
setStatus(Status.Failed);
}
}

await requester.sendMessage(Ports.Background, new Msg(msgId, password));
} catch (e) {
console.info(e);
setError(`${e}`);
Expand Down
22 changes: 22 additions & 0 deletions apps/extension/src/Approvals/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { TxType } from "@namada/shared";
import {
SubmitApprovedBondMsg,
SubmitApprovedTransferMsg,
SubmitApprovedUnbondMsg,
SubmitApprovedWithdrawMsg,
} from "background/approvals";
import { Message } from "router";

export enum TopLevelRoute {
Default = "/",
Expand All @@ -19,3 +26,18 @@ export const TxTypeLabel: Record<TxType, string> = {
[TxType.Withdraw]: "withdraw",
[TxType.RevealPK]: "reveal-pk",
};

export type SupportedTx = Extract<
TxType,
TxType.Bond | TxType.Unbond | TxType.Transfer | TxType.Withdraw
>;

export type ApproveMsg = new (msgId: string, password: string) => unknown &
Message<void>;

export const txMap: Map<SupportedTx, ApproveMsg> = new Map([
[TxType.Bond, SubmitApprovedBondMsg],
[TxType.Unbond, SubmitApprovedUnbondMsg],
[TxType.Transfer, SubmitApprovedTransferMsg],
[TxType.Withdraw, SubmitApprovedWithdrawMsg],
]);
3 changes: 1 addition & 2 deletions apps/namada-interface/.prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"bracketSpacing": false
"singleQuote": false
}
4 changes: 1 addition & 3 deletions packages/shared/lib/src/sdk/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,8 @@ pub fn unbond_tx_args(tx_msg: &[u8], password: Option<String>) -> Result<args::U

let source = Address::from_str(&source)?;
let validator = Address::from_str(&validator)?;
let amount: Amount = DenominatedAmount::from_str(&amount)
.expect(format!("Amount has to be valid. Received {}", amount).as_str())
.into();

let amount = Amount::from_str(&amount, NATIVE_MAX_DECIMAL_PLACES)?;
let args = args::Unbond {
tx: tx_msg_into_args(tx, password)?,
validator,
Expand Down

0 comments on commit ed9917b

Please sign in to comment.