Skip to content

Commit

Permalink
Hooking up Withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jul 26, 2023
1 parent cefbc58 commit a16b480
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 27 deletions.
10 changes: 7 additions & 3 deletions apps/extension/src/Approvals/ApproveTx/ApproveTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const ApproveTx: React.FC<Props> = ({ setDetails }) => {
const amount = query.get("amount") || "";
const source = query.get("source") || "";
const target = query.get("target") || "";
const validator = query.get("validator") || "";
const tokenAddress = query.get("token") || "";
const tokenType =
Object.values(Tokens).find((token) => token.address === tokenAddress)
Expand Down Expand Up @@ -87,9 +88,12 @@ export const ApproveTx: React.FC<Props> = ({ setDetails }) => {
<Address>{shortenAddress(target)}</Address>
</>
)}
<p>
Amount: {amount} {tokenType}
</p>
{amount && (
<p>
Amount: {amount} {tokenType}
</p>
)}
{validator && <p>Validator: {shortenAddress(validator)}</p>}
<ButtonContainer>
<Button onClick={handleApproveClick} variant={ButtonVariant.Contained}>
Approve
Expand Down
10 changes: 10 additions & 0 deletions apps/extension/src/Approvals/ApproveTx/ConfirmTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
SubmitApprovedBondMsg,
SubmitApprovedTransferMsg,
SubmitApprovedUnbondMsg,
SubmitApprovedWithdrawMsg,
} from "background/approvals";
import { Address } from "App/Accounts/AccountListing.components";
import { closeCurrentTab } from "utils";
Expand Down Expand Up @@ -94,6 +95,15 @@ export const ConfirmTx: React.FC<Props> = ({ details }) => {
setStatus(Status.Completed);
break;
}
case TxType.Withdraw: {
await requester.sendMessage(
Ports.Background,
new SubmitApprovedWithdrawMsg(msgId, password)
);
setStatusInfo("");
setStatus(Status.Completed);
break;
}
}
} catch (e) {
console.info(e);
Expand Down
35 changes: 33 additions & 2 deletions apps/extension/src/background/approvals/handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Handler, Env, Message, InternalHandler } from "router";
import { ApprovalsService } from "./service";
import { ApproveBondMsg, ApproveUnbondMsg, ApproveTransferMsg } from "provider";
import {
ApproveBondMsg,
ApproveUnbondMsg,
ApproveTransferMsg,
ApproveWithdrawMsg,
} from "provider";
import {
RejectTxMsg,
SubmitApprovedTransferMsg,
SubmitApprovedBondMsg,
SubmitApprovedUnbondMsg,
SubmitApprovedWithdrawMsg,
} from "./messages";

export const getHandler: (service: ApprovalsService) => Handler = (service) => {
Expand All @@ -20,6 +26,11 @@ export const getHandler: (service: ApprovalsService) => Handler = (service) => {
return handleApproveBondMsg(service)(env, msg as ApproveBondMsg);
case ApproveUnbondMsg:
return handleApproveUnbondMsg(service)(env, msg as ApproveUnbondMsg);
case ApproveWithdrawMsg:
return handleApproveWithdrawMsg(service)(
env,
msg as ApproveWithdrawMsg
);
case RejectTxMsg:
return handleRejectTxMsg(service)(env, msg as RejectTxMsg);
case SubmitApprovedTransferMsg:
Expand All @@ -37,7 +48,11 @@ export const getHandler: (service: ApprovalsService) => Handler = (service) => {
env,
msg as SubmitApprovedUnbondMsg
);

case SubmitApprovedWithdrawMsg:
return handleSubmitApprovedWithdrawMsg(service)(
env,
msg as SubmitApprovedUnbondMsg
);
default:
throw new Error("Unknown msg type");
}
Expand Down Expand Up @@ -84,6 +99,14 @@ const handleApproveUnbondMsg: (
};
};

const handleApproveWithdrawMsg: (
service: ApprovalsService
) => InternalHandler<ApproveWithdrawMsg> = (service) => {
return async (_, { txMsg, accountType }) => {
return await service.approveWithdraw(txMsg, accountType);
};
};

const handleSubmitApprovedBondMsg: (
service: ApprovalsService
) => InternalHandler<SubmitApprovedBondMsg> = (service) => {
Expand All @@ -99,3 +122,11 @@ const handleSubmitApprovedUnbondMsg: (
return await service.submitUnbond(msgId, password);
};
};

const handleSubmitApprovedWithdrawMsg: (
service: ApprovalsService
) => InternalHandler<SubmitApprovedWithdrawMsg> = (service) => {
return async (_, { msgId, password }) => {
return await service.submitWithdraw(msgId, password);
};
};
66 changes: 53 additions & 13 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
AccountType,
SubmitBondMsgValue,
SubmitUnbondMsgValue,
SubmitWithdrawMsgValue,
TransferMsgValue,
} from "@namada/types";
import { TxType } from "@namada/shared";
Expand All @@ -23,7 +24,7 @@ export class ApprovalsService {
protected readonly connectedTabsStore: KVStore<TabStore[]>,
protected readonly keyRingService: KeyRingService,
protected readonly ledgerService: LedgerService
) {}
) { }

// Deserialize transfer details and prompt user
async approveTransfer(txMsg: string, type?: AccountType): Promise<void> {
Expand All @@ -41,9 +42,8 @@ export class ApprovalsService {
tx: { publicKey = "" },
} = txDetails;
const amount = new BigNumber(amountBN.toString());
const baseUrl = `${browser.runtime.getURL("approvals.html")}#/approve-tx/${
TxType.Transfer
}`;
const baseUrl = `${browser.runtime.getURL("approvals.html")}#/approve-tx/${TxType.Transfer
}`;

const url = paramsToUrl(baseUrl, {
id,
Expand Down Expand Up @@ -74,9 +74,8 @@ export class ApprovalsService {
tx: { publicKey = "" },
} = txDetails;
const amount = new BigNumber(amountBN.toString());
const baseUrl = `${browser.runtime.getURL("approvals.html")}#/approve-tx/${
TxType.Bond
}`;
const baseUrl = `${browser.runtime.getURL("approvals.html")}#/approve-tx/${TxType.Bond
}`;

const url = paramsToUrl(baseUrl, {
id,
Expand Down Expand Up @@ -105,9 +104,8 @@ export class ApprovalsService {
tx: { publicKey = "" },
} = txDetails;
const amount = new BigNumber(amountBN.toString());
const baseUrl = `${browser.runtime.getURL("approvals.html")}#/approve-tx/${
TxType.Unbond
}`;
const baseUrl = `${browser.runtime.getURL("approvals.html")}#/approve-tx/${TxType.Unbond
}`;

const url = paramsToUrl(baseUrl, {
id,
Expand All @@ -120,6 +118,33 @@ export class ApprovalsService {
this._launchApprovalWindow(url);
}

// Deserialize withdraw details and prompt user
async approveWithdraw(txMsg: string, type: AccountType): Promise<void> {
const txMsgBuffer = Buffer.from(fromBase64(txMsg));
const id = uuid();
await this.txStore.set(id, txMsg);

// Decode tx details and launch approval screen
const txDetails = deserialize(txMsgBuffer, SubmitWithdrawMsgValue);

const {
source,
validator,
tx: { publicKey = "" },
} = txDetails;
const baseUrl = `${browser.runtime.getURL("approvals.html")}#/approve-tx/${TxType.Withdraw
}`;

const url = paramsToUrl(baseUrl, {
id,
source,
validator,
publicKey,
accountType: type as string,
});

this._launchApprovalWindow(url);
}
// Remove pending transaction from storage
async rejectTx(msgId: string): Promise<void> {
await this._clearPendingTx(msgId);
Expand All @@ -138,7 +163,7 @@ export class ApprovalsService {
return await this._clearPendingTx(msgId);
}

throw new Error("Pending transfer not found!");
throw new Error("Pending Transfer tx not found!");
}

// Authenticate keyring and submit approved bond transaction from storage
Expand All @@ -155,7 +180,7 @@ export class ApprovalsService {
return await this._clearPendingTx(msgId);
}

throw new Error("Pending bond not found!");
throw new Error("Pending Bond tx not found!");
}

async submitUnbond(msgId: string, password: string): Promise<void> {
Expand All @@ -170,7 +195,22 @@ export class ApprovalsService {
return await this._clearPendingTx(msgId);
}

throw new Error("Pending bond not found!");
throw new Error("Pending Unbond tx not found!");
}

async submitWithdraw(msgId: string, password: string): Promise<void> {
await this.keyRingService.unlock(password);

// Fetch pending bond tx
const tx = await this.txStore.get(msgId);

if (tx) {
await this.keyRingService.submitWithdraw(tx, msgId);

return await this._clearPendingTx(msgId);
}

throw new Error("Pending Withdraw tx not found!");
}

private async _clearPendingTx(msgId: string): Promise<void> {
Expand Down
3 changes: 2 additions & 1 deletion apps/extension/src/background/keyring/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ export class KeyRingService {
}
}

async submitWithdraw(txMsg: string): Promise<void> {
async submitWithdraw(txMsg: string, msgId: string): Promise<void> {
console.log(`TODO: Broadcast notification for ${msgId}`);
try {
await this._keyRing.submitWithdraw(fromBase64(txMsg));
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/provider/Namada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Namada implements INamada {
constructor(
private readonly _version: string,
protected readonly requester?: MessageRequester
) {}
) { }

public async connect(chainId: string): Promise<void> {
return await this.requester?.sendMessage(
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/provider/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export class ApproveWithdrawMsg extends Message<void> {
}

type(): string {
return ApproveUnbondMsg.type();
return ApproveWithdrawMsg.type();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export const postNewWithdraw = createAsyncThunk<
const integration = getIntegration(chainId);
const signer = integration.signer() as Signer;
const {
details: { type },
details: { type, publicKey },
} = derived[chainId][owner];

await signer.submitWithdraw(
Expand All @@ -316,6 +316,7 @@ export const postNewWithdraw = createAsyncThunk<
feeAmount: new BigNumber(0),
gasLimit: new BigNumber(0),
chainId,
publicKey,
},
},
type
Expand Down
15 changes: 10 additions & 5 deletions packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,16 @@ impl Sdk {
.map_err(JsError::from)?;
unbond.0
}
_ => {
return Err(JsError::new(&format!(
"TxType \"{:?}\" not implemented!",
tx_type,
)))
TxType::Withdraw => {
let args = tx::withdraw_tx_args(tx_msg, None)?;
let unbond = namada::ledger::tx::build_withdraw(
&self.client,
&mut self.wallet,
args.clone(),
)
.await
.map_err(JsError::from)?;
unbond.0
}
};

Expand Down

0 comments on commit a16b480

Please sign in to comment.