Skip to content

Commit

Permalink
Pass account type for approveBond, hook up routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jul 11, 2023
1 parent 187a307 commit 187fed5
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 53 deletions.
15 changes: 15 additions & 0 deletions apps/extension/src/Approvals/Approvals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ApproveTransfer, ConfirmTransfer } from "./ApproveTransfer";
import { ApproveConnection } from "./ApproveConnection";
import { TopLevelRoute } from "Approvals/types";
import { ConfirmLedgerTransfer } from "./ApproveTransfer/ConfirmLedgerTransfer";
import { ApproveBond, ConfirmBond, ConfirmLedgerBond } from "./ApproveBond";

export enum Status {
Completed,
Expand Down Expand Up @@ -50,6 +51,20 @@ export const Approvals: React.FC = () => {
path={TopLevelRoute.ConfirmLedgerTransfer}
element={<ConfirmLedgerTransfer msgId={msgId} />}
/>
<Route
path={TopLevelRoute.ApproveBond}
element={
<ApproveBond setMsgId={setMsgId} setAddress={setAddress} />
}
/>
<Route
path={TopLevelRoute.ConfirmBond}
element={<ConfirmBond msgId={msgId} address={address} />}
/>
<Route
path={TopLevelRoute.ConfirmLedgerBond}
element={<ConfirmLedgerBond msgId={msgId} />}
/>

<Route
path={TopLevelRoute.ApproveConnection}
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/approvals/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const handleRejectTxMsg: (
service: ApprovalsService
) => InternalHandler<RejectTxMsg> = (service) => {
return async (_, { msgId }) => {
return await service.rejectTransfer(msgId);
return await service.rejectTx(msgId);
};
};

Expand Down
30 changes: 13 additions & 17 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ export class ApprovalsService {
"approvals.html"
)}#/approve-transfer?type=${type}&id=${id}&source=${source}&target=${target}&token=${token}&amount=${amount}`;

browser.windows.create({
url,
width: 415,
height: 510,
type: "popup",
});

return;
this._launchApprovalWindow(url);
}

// Deserialize bond details and prompt user
Expand All @@ -65,22 +58,16 @@ export class ApprovalsService {
);
const { source, token, amount: amountBN } = txDetails;
const amount = new BigNumber(amountBN.toString());

const url = `${browser.runtime.getURL(
"approvals.html"
)}#/approve-bond?type=${type}&id=${id}&source=${source}&token=${token}&amount=${amount}`;

browser.windows.create({
url,
width: 415,
height: 510,
type: "popup",
});

return;
this._launchApprovalWindow(url);
}

// Remove pending transaction from storage
async rejectTransfer(msgId: string): Promise<void> {
async rejectTx(msgId: string): Promise<void> {
await this._clearPendingTx(msgId);
}

Expand Down Expand Up @@ -126,4 +113,13 @@ export class ApprovalsService {
private async _clearPendingTx(msgId: string): Promise<void> {
return await this.txStore.set(msgId, null);
}

private _launchApprovalWindow = (url: string): void => {
browser.windows.create({
url,
width: 415,
height: 510,
type: "popup",
});
};
}
6 changes: 5 additions & 1 deletion apps/extension/src/provider/Anoma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ export class Anoma implements IAnoma {
);
}

public async submitBond(txMsg: string, type?: AccountType): Promise<void> {
public async submitBond(props: {
txMsg: string;
type?: AccountType;
}): Promise<void> {
const { txMsg, type } = props;
return await this.requester?.sendMessage(
Ports.Background,
new ApproveBondMsg(txMsg, type)
Expand Down
14 changes: 12 additions & 2 deletions apps/extension/src/provider/InjectedAnoma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,18 @@ export class InjectedAnoma implements IAnoma {
return new Signer(chainId, this);
}

public async submitBond(txMsg: string): Promise<void> {
return await InjectedProxy.requestMethod<string, void>("submitBond", txMsg);
public async submitBond(props: {
txMsg: string;
type: AccountType;
}): Promise<void> {
const { txMsg, type } = props;
return await InjectedProxy.requestMethod<
{ txMsg: string; type: AccountType },
void
>("submitBond", {
txMsg,
type,
});
}

public async submitUnbond(txMsg: string): Promise<void> {
Expand Down
7 changes: 5 additions & 2 deletions apps/extension/src/provider/Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ export class Signer implements ISigner {
/**
* Submit bond transaction
*/
public async submitBond(args: SubmitBondProps): Promise<void> {
public async submitBond(
args: SubmitBondProps,
type: AccountType
): Promise<void> {
const msgValue = new SubmitBondMsgValue(args);

const msg = new Message<SubmitBondMsgValue>();
const encoded = msg.encode(SubmitBondMsgSchema, msgValue);

return await this._anoma.submitBond(toBase64(encoded));
return await this._anoma.submitBond({ txMsg: toBase64(encoded), type });
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ export const NewBondingPosition = (props: Props): JSX.Element => {
const currentBondingPosition = currentBondingPositions.find(
(pos) => pos.owner === currentAccount?.details.address
);
const stakedAmount: BigNumber = new BigNumber(currentBondingPosition?.stakedAmount || "0");
const currentNAMBalance: BigNumber = currentAccount.balance["NAM"] || new BigNumber(0);
const stakedAmount: BigNumber = new BigNumber(
currentBondingPosition?.stakedAmount || "0"
);
const currentNAMBalance: BigNumber =
currentAccount.balance["NAM"] || new BigNumber(0);

const handleAddressChange = (
e: React.ChangeEvent<HTMLSelectElement>
Expand Down Expand Up @@ -167,6 +170,7 @@ export const NewBondingPosition = (props: Props): JSX.Element => {
owner: currentAddress,
validatorId: currentBondingPositions[0].validatorId,
};
console.log({ changeInStakingPosition });
confirmBonding(changeInStakingPosition);
}}
disabled={isEntryIncorrectOrEmpty}
Expand Down
36 changes: 22 additions & 14 deletions apps/namada-interface/src/slices/StakingAndGovernance/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ const toMyValidators = (
index == -1
? (arr: MyValidators[]) => arr
: (arr: MyValidators[], idx: number) => [
...arr.slice(0, idx),
...arr.slice(idx + 1),
];
...arr.slice(0, idx),
...arr.slice(idx + 1),
];

const stakedAmount = new BigNumber(stake)
.plus(new BigNumber(v?.stakedAmount || 0))
Expand Down Expand Up @@ -159,20 +159,28 @@ export const postNewBonding = createAsyncThunk<
{ state: RootState }
>(POST_NEW_STAKING, async (change, thunkApi) => {
const { chainId } = thunkApi.getState().settings;
const { derived } = thunkApi.getState().accounts;
const integration = getIntegration(chainId);
const signer = integration.signer() as Signer;
await signer.submitBond({
source: change.owner,
validator: change.validatorId,
amount: new BigNumber(change.amount),
nativeToken: Tokens.NAM.address || "",
tx: {
token: Tokens.NAM.address || "",
feeAmount: new BigNumber(0),
gasLimit: new BigNumber(0),
chainId,
const { owner, validatorId, amount } = change;
const account = derived[chainId][owner];
const type = account.details.type;

await signer.submitBond(
{
source: owner,
validator: validatorId,
amount: new BigNumber(amount),
nativeToken: Tokens.NAM.address || "",
tx: {
token: Tokens.NAM.address || "",
feeAmount: new BigNumber(0),
gasLimit: new BigNumber(0),
chainId,
},
},
});
type
);
});

// we post an unstake transaction
Expand Down
24 changes: 12 additions & 12 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type TimeoutOpts = {
error?: string;
};

const DEFAULT_TIMEOUT = 5000;
const DEFAULT_TIMEOUT = 60000;

const DEFAULT_OPTS: TimeoutOpts = {
timeout: DEFAULT_TIMEOUT,
Expand All @@ -23,19 +23,19 @@ const promiseWithTimeout =
fn: (...args: U) => Promise<T>,
opts?: TimeoutOpts
) =>
(...args: U): Promise<T> => {
const { timeout, error } = { ...DEFAULT_OPTS, ...opts };
(...args: U): Promise<T> => {
const { timeout, error } = { ...DEFAULT_OPTS, ...opts };

return new Promise(async (resolve, reject) => {
const t = setTimeout(() => {
reject(error);
}, timeout);
return new Promise(async (resolve, reject) => {
const t = setTimeout(() => {
reject(error);
}, timeout);

const res = await fn(...args);
clearTimeout(t);
resolve(res);
});
};
const res = await fn(...args);
clearTimeout(t);
resolve(res);
});
};

export class Query extends RustQuery {
query_balance = promiseWithTimeout(super.query_balance.bind(this), {
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/anoma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Anoma {
suggestChain(chainConfig: Chain): Promise<void>;
chain: (chainId: string) => Promise<Chain | undefined>;
chains: () => Promise<Chain[] | undefined>;
submitBond: (txMsg: string) => Promise<void>;
submitBond: (props: { txMsg: string; type: AccountType }) => Promise<void>;
submitUnbond: (txMsg: string) => Promise<void>;
submitTransfer: (props: {
txMsg: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

export interface Signer {
accounts: () => Promise<Account[] | undefined>;
submitBond(args: SubmitBondProps): Promise<void>;
submitBond(args: SubmitBondProps, type: AccountType): Promise<void>;
submitUnbond(args: SubmitUnbondProps): Promise<void>;
submitTransfer(args: TransferProps, type: AccountType): Promise<void>;
submitIbcTransfer(args: IbcTransferProps): Promise<void>;
Expand Down

0 comments on commit 187fed5

Please sign in to comment.