Skip to content

Commit 446feb2

Browse files
committed
finish tests and fix bugs
1 parent 122cd3e commit 446feb2

File tree

13 files changed

+121
-165
lines changed

13 files changed

+121
-165
lines changed

packages/examples/packages/dialogs/src/components/custom.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
AccountSelector,
32
Box,
43
Button,
54
Container,
@@ -23,7 +22,7 @@ export const CustomDialog: SnapComponent = () => (
2322
This is a custom dialog. It has a custom Footer and can be resolved to
2423
any value.
2524
</Text>
26-
<AccountSelector name="account" switchSelectedAccount />
25+
<Input name="custom-input" placeholder="Enter something..." />
2726
</Box>
2827
<Footer>
2928
<Button name="cancel">Cancel</Button>

packages/examples/packages/send-flow/src/components/AccountSelector.tsx

Lines changed: 0 additions & 54 deletions
This file was deleted.

packages/examples/packages/send-flow/src/components/SendFlow.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@ import type { Account, Currency } from '../types';
1010
/**
1111
* The props for the {@link SendFlow} component.
1212
*
13-
* @property accounts - The available accounts.
14-
* @property selectedAccount - The currently selected account.
1513
* @property selectedCurrency - The selected currency to display.
1614
* @property total - The total cost of the transaction.
1715
* @property fees - The fees for the transaction.
1816
* @property errors - The form errors.
1917
* @property displayAvatar - Whether to display the avatar of the address.
2018
*/
2119
export type SendFlowProps = {
22-
accounts: Account[];
23-
selectedAccount: string;
2420
selectedCurrency: 'BTC' | '$';
2521
total: Currency;
2622
fees: Currency;
@@ -35,8 +31,6 @@ export type SendFlowProps = {
3531
* A send flow component, which shows the user a form to send funds to another.
3632
*
3733
* @param props - The component props.
38-
* @param props.accounts - The available accounts.
39-
* @param props.selectedAccount - The currently selected account.
4034
* @param props.selectedCurrency - The selected currency to display.
4135
* @param props.total - The total cost of the transaction.
4236
* @param props.errors - The form errors.
@@ -45,8 +39,6 @@ export type SendFlowProps = {
4539
* @returns The SendFlow component.
4640
*/
4741
export const SendFlow: SnapComponent<SendFlowProps> = ({
48-
accounts,
49-
selectedAccount,
5042
selectedCurrency,
5143
total,
5244
fees,
@@ -58,8 +50,6 @@ export const SendFlow: SnapComponent<SendFlowProps> = ({
5850
<Box>
5951
<SendFlowHeader />
6052
<SendForm
61-
selectedAccount={selectedAccount}
62-
accounts={accounts}
6353
selectedCurrency={selectedCurrency}
6454
errors={errors}
6555
displayAvatar={displayAvatar}

packages/examples/packages/send-flow/src/components/SendForm.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,21 @@ import {
88
Input,
99
AddressInput,
1010
Text,
11+
AccountSelector,
1112
type SnapComponent,
1213
} from '@metamask/snaps-sdk/jsx';
1314

14-
import { AccountSelector } from './AccountSelector';
1515
import btcIcon from '../images/btc.svg';
16-
import type { Account, SendFormErrors } from '../types';
16+
import type { SendFormErrors } from '../types';
1717

1818
/**
1919
* The props for the {@link SendForm} component.
2020
*
21-
* @property selectedAccount - The currently selected account.
22-
* @property accounts - The available accounts.
2321
* @property errors - The form errors.
2422
* @property selectedCurrency - The selected currency to display.
2523
* @property displayAvatar - Whether to display the avatar of the address.
2624
*/
2725
export type SendFormProps = {
28-
selectedAccount: string;
29-
accounts: Account[];
3026
errors?: SendFormErrors;
3127
selectedCurrency: 'BTC' | '$';
3228
displayAvatar?: boolean | undefined;
@@ -36,22 +32,20 @@ export type SendFormProps = {
3632
* A component that shows the send form.
3733
*
3834
* @param props - The component props.
39-
* @param props.selectedAccount - The currently selected account.
40-
* @param props.accounts - The available accounts.
4135
* @param props.errors - The form errors.
4236
* @param props.selectedCurrency - The selected currency to display.
4337
* @param props.displayAvatar - Whether to display the avatar of the address.
4438
* @returns The SendForm component.
4539
*/
4640
export const SendForm: SnapComponent<SendFormProps> = ({
47-
selectedAccount,
48-
accounts,
4941
errors,
5042
selectedCurrency,
5143
displayAvatar,
5244
}) => (
5345
<Form name="sendForm">
54-
<AccountSelector selectedAccount={selectedAccount} accounts={accounts} />
46+
<Field label="From account">
47+
<AccountSelector name="accountSelector" switchSelectedAccount />
48+
</Field>
5549
<Field label="Send amount" error={errors?.amount}>
5650
<Box>
5751
<Image src={btcIcon} />

packages/examples/packages/send-flow/src/data.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

packages/examples/packages/send-flow/src/index.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
import { UserInputEventType } from '@metamask/snaps-sdk';
88

99
import { SendFlow } from './components';
10-
import { accountsArray, accounts } from './data';
1110
import type { SendFormState, SendFlowContext } from './types';
1211
import { formValidation, generateSendFlow, isCaipHexAddress } from './utils';
1312

@@ -26,8 +25,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
2625
switch (request.method) {
2726
case 'display': {
2827
const interfaceId = await generateSendFlow({
29-
accountsArray,
30-
accounts,
3128
fees: { amount: 1.0001, fiat: 1.23 },
3229
});
3330

@@ -56,8 +53,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
5653
*/
5754
export const onHomePage: OnHomePageHandler = async () => {
5855
const interfaceId = await generateSendFlow({
59-
accountsArray,
60-
accounts,
6156
fees: { amount: 1.0001, fiat: 1.23 },
6257
});
6358

@@ -88,7 +83,7 @@ export const onUserInput: OnUserInputHandler = async ({
8883

8984
const sendForm = state.sendForm as SendFormState;
9085

91-
const formErrors = formValidation(sendForm, context as SendFlowContext);
86+
const formErrors = formValidation(sendForm);
9287

9388
const total = {
9489
amount: Number(sendForm.amount ?? 0) + fees.amount,
@@ -128,8 +123,6 @@ export const onUserInput: OnUserInputHandler = async ({
128123
id,
129124
ui: (
130125
<SendFlow
131-
accounts={accountsArray}
132-
selectedAccount={sendForm.accountSelector}
133126
selectedCurrency={selectedCurrency}
134127
total={total}
135128
fees={fees}
@@ -141,7 +134,6 @@ export const onUserInput: OnUserInputHandler = async ({
141134

142135
break;
143136
}
144-
145137
default:
146138
break;
147139
}

packages/examples/packages/send-flow/src/types.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { AccountSelectorValue } from '@metamask/snaps-sdk';
2+
13
/**
24
* The state of the send form.
35
*
@@ -8,7 +10,7 @@
810
export type SendFormState = {
911
to: string;
1012
amount: string;
11-
accountSelector: string;
13+
accountSelector: AccountSelectorValue;
1214
};
1315

1416
/**
@@ -22,21 +24,6 @@ export type SendFormErrors = {
2224
amount?: string;
2325
};
2426

25-
/**
26-
* An Account of the send flow interface.
27-
*
28-
* @property name - The name of the account.
29-
* @property address - The address of the account.
30-
* @property balance - The balance of the account.
31-
* @property icon - The icon of the account.
32-
*/
33-
export type Account = {
34-
name: string;
35-
address: string;
36-
balance: Currency;
37-
icon: string;
38-
};
39-
4027
/**
4128
* A currency value.
4229
*
@@ -56,7 +43,6 @@ export type Currency = {
5643
* @property fees - The fees for the transaction.
5744
*/
5845
export type SendFlowContext = {
59-
accounts: Record<string, Account>;
6046
selectedCurrency: 'BTC' | '$';
6147
fees: Currency;
6248
};

packages/examples/packages/send-flow/src/utils.tsx

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,31 @@ import {
77
} from '@metamask/utils';
88

99
import { SendFlow } from './components';
10-
import type {
11-
Account,
12-
Currency,
13-
SendFlowContext,
14-
SendFormErrors,
15-
SendFormState,
16-
} from './types';
10+
import type { Currency, SendFormErrors, SendFormState } from './types';
1711

1812
export type GenerateSendFlowParams = {
19-
accountsArray: Account[];
20-
accounts: Record<string, Account>;
2113
fees: Currency;
2214
};
2315

2416
/**
2517
* Generate the send flow.
2618
*
2719
* @param params - The parameters for the send form.
28-
* @param params.accountsArray - The available accounts as an array.
29-
* @param params.accounts - The available accounts.
3020
* @param params.fees - The fees for the transaction.
3121
* @returns The interface ID.
3222
*/
33-
export async function generateSendFlow({
34-
accountsArray,
35-
accounts,
36-
fees,
37-
}: GenerateSendFlowParams) {
23+
export async function generateSendFlow({ fees }: GenerateSendFlowParams) {
3824
return await snap.request({
3925
method: 'snap_createInterface',
4026
params: {
4127
ui: (
4228
<SendFlow
43-
accounts={accountsArray}
44-
selectedAccount={accountsArray[0].address}
4529
selectedCurrency="BTC"
4630
total={{ amount: 0, fiat: 0 }}
4731
fees={fees}
4832
/>
4933
),
5034
context: {
51-
accounts,
5235
selectedCurrency: 'BTC',
5336
fees,
5437
},
@@ -60,27 +43,15 @@ export async function generateSendFlow({
6043
* Validate the send form.
6144
*
6245
* @param formState - The state of the send form.
63-
* @param context - The context of the interface.
6446
* @returns The form errors.
6547
*/
66-
export function formValidation(
67-
formState: SendFormState,
68-
context: SendFlowContext,
69-
): SendFormErrors {
48+
export function formValidation(formState: SendFormState): SendFormErrors {
7049
const errors: Partial<SendFormErrors> = {};
7150

7251
if (formState.to === 'invalid address') {
7352
errors.to = 'Invalid address';
7453
}
7554

76-
if (
77-
formState.amount &&
78-
Number(formState.amount) >
79-
context.accounts[formState.accountSelector].balance.amount
80-
) {
81-
errors.amount = 'Insufficient funds';
82-
}
83-
8455
return errors;
8556
}
8657

0 commit comments

Comments
 (0)