Skip to content

Commit 5e096b1

Browse files
committed
finish tests and fix bugs
1 parent c7ff5c9 commit 5e096b1

File tree

13 files changed

+624
-201
lines changed

13 files changed

+624
-201
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: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { SnapComponent } from '@metamask/snaps-sdk/jsx';
22
import { Box, Container } from '@metamask/snaps-sdk/jsx';
33

4-
import type { Account, Currency } from '../types';
4+
import type { Currency } from '../types';
55
import { SendFlowFooter } from './SendFlowFooter';
66
import { SendFlowHeader } from './SendFlowHeader';
77
import { SendForm } from './SendForm';
@@ -10,8 +10,6 @@ import { TransactionSummary } from './TransactionSummary';
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.
@@ -20,8 +18,6 @@ import { TransactionSummary } from './TransactionSummary';
2018
* @property errors - The form errors.
2119
*/
2220
export type SendFlowProps = {
23-
accounts: Account[];
24-
selectedAccount: string;
2521
selectedCurrency: 'BTC' | '$';
2622
total: Currency;
2723
fees: Currency;
@@ -37,8 +33,6 @@ export type SendFlowProps = {
3733
* A send flow component, which shows the user a form to send funds to another.
3834
*
3935
* @param props - The component props.
40-
* @param props.accounts - The available accounts.
41-
* @param props.selectedAccount - The currently selected account.
4236
* @param props.selectedCurrency - The selected currency to display.
4337
* @param props.total - The total cost of the transaction.
4438
* @param props.errors - The form errors.
@@ -48,8 +42,6 @@ export type SendFlowProps = {
4842
* @returns The SendFlow component.
4943
*/
5044
export const SendFlow: SnapComponent<SendFlowProps> = ({
51-
accounts,
52-
selectedAccount,
5345
selectedCurrency,
5446
total,
5547
fees,
@@ -62,8 +54,6 @@ export const SendFlow: SnapComponent<SendFlowProps> = ({
6254
<Box>
6355
<SendFlowHeader />
6456
<SendForm
65-
selectedAccount={selectedAccount}
66-
accounts={accounts}
6757
selectedCurrency={selectedCurrency}
6858
flushToAddress={flushToAddress}
6959
displayClearIcon={displayClearIcon}

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,23 @@ import {
77
Image,
88
Input,
99
Text,
10+
AccountSelector,
1011
type SnapComponent,
1112
} from '@metamask/snaps-sdk/jsx';
1213

1314
import btcIcon from '../images/btc.svg';
1415
import jazzicon3 from '../images/jazzicon3.svg';
15-
import type { Account, SendFormErrors } from '../types';
16-
import { AccountSelector } from './AccountSelector';
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 displayClearIcon - Whether to display the clear icon or not.
2624
* @property flushToAddress - Whether to flush the address field or not.
2725
*/
2826
export type SendFormProps = {
29-
selectedAccount: string;
30-
accounts: Account[];
3127
errors?: SendFormErrors;
3228
selectedCurrency: 'BTC' | '$';
3329
displayClearIcon: boolean;
@@ -38,24 +34,22 @@ export type SendFormProps = {
3834
* A component that shows the send form.
3935
*
4036
* @param props - The component props.
41-
* @param props.selectedAccount - The currently selected account.
42-
* @param props.accounts - The available accounts.
4337
* @param props.errors - The form errors.
4438
* @param props.selectedCurrency - The selected currency to display.
4539
* @param props.displayClearIcon - Whether to display the clear icon or not.
4640
* @param props.flushToAddress - Whether to flush the address field or not.
4741
* @returns The SendForm component.
4842
*/
4943
export const SendForm: SnapComponent<SendFormProps> = ({
50-
selectedAccount,
51-
accounts,
5244
errors,
5345
selectedCurrency,
5446
displayClearIcon,
5547
flushToAddress,
5648
}) => (
5749
<Form name="sendForm">
58-
<AccountSelector selectedAccount={selectedAccount} accounts={accounts} />
50+
<Field label="From account">
51+
<AccountSelector name="accountSelector" switchSelectedAccount />
52+
</Field>
5953
<Field label="Send amount" error={errors?.amount}>
6054
<Box>
6155
<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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
} from '@metamask/snaps-sdk';
1010

1111
import { SendFlow } from './components';
12-
import { accountsArray, accounts } from './data';
1312
import type { SendFormState, SendFlowContext } from './types';
1413
import { formValidation, generateSendFlow } from './utils';
1514

@@ -28,8 +27,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
2827
switch (request.method) {
2928
case 'display': {
3029
const interfaceId = await generateSendFlow({
31-
accountsArray,
32-
accounts,
3330
fees: { amount: 1.0001, fiat: 1.23 },
3431
});
3532

@@ -58,8 +55,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
5855
*/
5956
export const onHomePage: OnHomePageHandler = async () => {
6057
const interfaceId = await generateSendFlow({
61-
accountsArray,
62-
accounts,
6358
fees: { amount: 1.0001, fiat: 1.23 },
6459
});
6560

@@ -90,7 +85,7 @@ export const onUserInput: OnUserInputHandler = async ({
9085

9186
const sendForm = state.sendForm as SendFormState;
9287

93-
const formErrors = formValidation(sendForm, context as SendFlowContext);
88+
const formErrors = formValidation(sendForm);
9489

9590
const total = {
9691
amount: Number(sendForm.amount ?? 0) + fees.amount,
@@ -108,8 +103,6 @@ export const onUserInput: OnUserInputHandler = async ({
108103
id,
109104
ui: (
110105
<SendFlow
111-
accounts={accountsArray}
112-
selectedAccount={sendForm.accountSelector}
113106
selectedCurrency={selectedCurrency}
114107
total={total}
115108
fees={fees}
@@ -134,8 +127,6 @@ export const onUserInput: OnUserInputHandler = async ({
134127
id,
135128
ui: (
136129
<SendFlow
137-
accounts={accountsArray}
138-
selectedAccount={sendForm.accountSelector}
139130
selectedCurrency={selectedCurrency}
140131
total={total}
141132
fees={fees}

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
@@ -1,47 +1,30 @@
11
import { SendFlow } from './components';
2-
import type {
3-
Account,
4-
Currency,
5-
SendFlowContext,
6-
SendFormErrors,
7-
SendFormState,
8-
} from './types';
2+
import type { Currency, SendFormErrors, SendFormState } from './types';
93

104
export type GenerateSendFlowParams = {
11-
accountsArray: Account[];
12-
accounts: Record<string, Account>;
135
fees: Currency;
146
};
157

168
/**
179
* Generate the send flow.
1810
*
1911
* @param params - The parameters for the send form.
20-
* @param params.accountsArray - The available accounts as an array.
21-
* @param params.accounts - The available accounts.
2212
* @param params.fees - The fees for the transaction.
2313
* @returns The interface ID.
2414
*/
25-
export async function generateSendFlow({
26-
accountsArray,
27-
accounts,
28-
fees,
29-
}: GenerateSendFlowParams) {
15+
export async function generateSendFlow({ fees }: GenerateSendFlowParams) {
3016
return await snap.request({
3117
method: 'snap_createInterface',
3218
params: {
3319
ui: (
3420
<SendFlow
35-
accounts={accountsArray}
36-
selectedAccount={accountsArray[0].address}
3721
selectedCurrency="BTC"
3822
total={{ amount: 0, fiat: 0 }}
3923
fees={fees}
4024
displayClearIcon={false}
4125
/>
4226
),
4327
context: {
44-
accounts,
4528
selectedCurrency: 'BTC',
4629
fees,
4730
},
@@ -53,27 +36,15 @@ export async function generateSendFlow({
5336
* Validate the send form.
5437
*
5538
* @param formState - The state of the send form.
56-
* @param context - The context of the interface.
5739
* @returns The form errors.
5840
*/
59-
export function formValidation(
60-
formState: SendFormState,
61-
context: SendFlowContext,
62-
): SendFormErrors {
41+
export function formValidation(formState: SendFormState): SendFormErrors {
6342
const errors: Partial<SendFormErrors> = {};
6443

6544
if (formState.to === 'invalid address') {
6645
errors.to = 'Invalid address';
6746
}
6847

69-
if (
70-
formState.amount &&
71-
Number(formState.amount) >
72-
context.accounts[formState.accountSelector].balance.amount
73-
) {
74-
errors.amount = 'Insufficient funds';
75-
}
76-
7748
return errors;
7849
}
7950

0 commit comments

Comments
 (0)