Skip to content

Commit 7692629

Browse files
committed
[WIP] update
1 parent 782a40a commit 7692629

File tree

12 files changed

+329
-154
lines changed

12 files changed

+329
-154
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import type {
2+
AccountSelectorState,
3+
AssetSelectorState,
4+
} from '@metamask/snaps-sdk';
15
import type { SnapComponent } from '@metamask/snaps-sdk/jsx';
26
import { Box, Container } from '@metamask/snaps-sdk/jsx';
37

48
import { SendFlowFooter } from './SendFlowFooter';
59
import { SendFlowHeader } from './SendFlowHeader';
610
import { SendForm } from './SendForm';
711
import { TransactionSummary } from './TransactionSummary';
8-
import type { Account, Currency } from '../types';
12+
import type { Currency } from '../types';
913

1014
/**
1115
* The props for the {@link SendFlow} component.
@@ -17,42 +21,50 @@ import type { Account, Currency } from '../types';
1721
* @property displayAvatar - Whether to display the avatar of the address.
1822
*/
1923
export type SendFlowProps = {
20-
selectedCurrency: 'BTC' | '$';
24+
displayAvatar?: boolean;
25+
useFiat: boolean;
26+
account: AccountSelectorState | null;
27+
asset: AssetSelectorState | null;
2128
total: Currency;
2229
fees: Currency;
2330
errors?: {
2431
amount?: string;
2532
to?: string;
2633
};
27-
displayAvatar?: boolean | undefined;
2834
};
2935

3036
/**
3137
* A send flow component, which shows the user a form to send funds to another.
3238
*
3339
* @param props - The component props.
34-
* @param props.selectedCurrency - The selected currency to display.
3540
* @param props.total - The total cost of the transaction.
3641
* @param props.errors - The form errors.
3742
* @param props.fees - The fees for the transaction.
43+
* @param props.useFiat - Whether to use fiat currency.
44+
* @param props.account - The account state.
45+
* @param props.asset - The asset state.
3846
* @param props.displayAvatar - Whether to display the avatar of the address.
3947
* @returns The SendFlow component.
4048
*/
4149
export const SendFlow: SnapComponent<SendFlowProps> = ({
42-
selectedCurrency,
4350
total,
4451
fees,
4552
errors,
53+
useFiat,
4654
displayAvatar,
55+
account,
56+
asset,
4757
}) => {
4858
return (
4959
<Container>
5060
<Box>
5161
<SendFlowHeader />
5262
<SendForm
53-
selectedCurrency={selectedCurrency}
54-
errors={errors}
5563
displayAvatar={displayAvatar}
64+
useFiat={useFiat}
65+
account={account}
66+
asset={asset}
67+
errors={errors}
5668
/>
5769
<TransactionSummary fees={fees} total={total} />
5870
</Box>

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

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
import type {
2+
AccountSelectorState,
3+
AssetSelectorState,
4+
} from '@metamask/snaps-sdk';
15
import {
26
Box,
37
Button,
48
Field,
59
Form,
610
Icon,
7-
Image,
811
Input,
912
AddressInput,
1013
Text,
1114
AccountSelector,
1215
type SnapComponent,
16+
AssetSelector,
1317
} from '@metamask/snaps-sdk/jsx';
1418

15-
import btcIcon from '../images/btc.svg';
1619
import type { SendFormErrors } from '../types';
1720

1821
/**
@@ -23,8 +26,10 @@ import type { SendFormErrors } from '../types';
2326
* @property displayAvatar - Whether to display the avatar of the address.
2427
*/
2528
export type SendFormProps = {
29+
useFiat: boolean;
2630
errors?: SendFormErrors;
27-
selectedCurrency: 'BTC' | '$';
31+
account: AccountSelectorState | null;
32+
asset: AssetSelectorState | null;
2833
displayAvatar?: boolean | undefined;
2934
};
3035

@@ -33,31 +38,48 @@ export type SendFormProps = {
3338
*
3439
* @param props - The component props.
3540
* @param props.errors - The form errors.
36-
* @param props.selectedCurrency - The selected currency to display.
3741
* @param props.displayAvatar - Whether to display the avatar of the address.
42+
* @param props.account - The account state.
43+
* @param props.asset - The asset state.
44+
* @param props.useFiat - Whether to use fiat currency.
3845
* @returns The SendForm component.
3946
*/
4047
export const SendForm: SnapComponent<SendFormProps> = ({
4148
errors,
42-
selectedCurrency,
49+
useFiat,
50+
account,
51+
asset,
4352
displayAvatar,
4453
}) => (
4554
<Form name="sendForm">
4655
<Field label="From account">
47-
<AccountSelector name="accountSelector" switchGlobalAccount />
48-
</Field>
49-
<Field label="Send amount" error={errors?.amount}>
50-
<Box>
51-
<Image src={btcIcon} />
52-
</Box>
53-
<Input name="amount" type="number" placeholder="Enter amount to send" />
54-
<Box direction="horizontal" center>
55-
<Text color="alternative">{selectedCurrency}</Text>
56-
<Button name="swap">
57-
<Icon name="swap-vertical" color="primary" size="md" />
58-
</Button>
59-
</Box>
56+
<AccountSelector
57+
name="account"
58+
switchGlobalAccount
59+
chainIds={[
60+
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
61+
'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1',
62+
]}
63+
/>
6064
</Field>
65+
<Box direction="horizontal">
66+
{account ? (
67+
<Field label="Asset">
68+
<AssetSelector name="asset" addresses={account.addresses} />
69+
</Field>
70+
) : null}
71+
<Field label="Send amount" error={errors?.amount}>
72+
<Input name="amount" type="number" placeholder="Enter amount to send" />
73+
<Box direction="horizontal" center>
74+
<Text color="alternative" size="sm">
75+
{useFiat ? '$' : (asset?.symbol ?? 'SOL')}
76+
</Text>
77+
<Button name="swap" size="sm">
78+
<Icon name="swap-vertical" color="primary" size="inherit" />
79+
</Button>
80+
</Box>
81+
</Field>
82+
</Box>
6183
<Field label="To account" error={errors?.to}>
6284
<AddressInput
6385
name="to"

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

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
OnHomePageHandler,
44
OnUserInputHandler,
55
OnRpcRequestHandler,
6+
AssetSelectorState,
67
} from '@metamask/snaps-sdk';
78
import { UserInputEventType } from '@metamask/snaps-sdk';
89

@@ -74,7 +75,7 @@ export const onUserInput: OnUserInputHandler = async ({
7475
id,
7576
context,
7677
}) => {
77-
const { selectedCurrency, fees } = context as SendFlowContext;
78+
const { useFiat, fees } = context as SendFlowContext;
7879

7980
const state = await snap.request({
8081
method: 'snap_getInterfaceState',
@@ -93,47 +94,30 @@ export const onUserInput: OnUserInputHandler = async ({
9394
if (event.type === UserInputEventType.InputChangeEvent) {
9495
switch (event.name) {
9596
case 'amount':
96-
case 'to': {
97+
case 'to':
98+
case 'account':
99+
case 'asset': {
97100
await snap.request({
98101
method: 'snap_updateInterface',
99102
params: {
100103
id,
101104
ui: (
102105
<SendFlow
103-
accounts={accountsArray}
104-
selectedAccount={sendForm.accountSelector}
105-
selectedCurrency={selectedCurrency}
106+
asset={state.asset as AssetSelectorState}
107+
account={sendForm.account}
108+
useFiat={useFiat}
106109
total={total}
107110
fees={fees}
108111
errors={formErrors}
109112
// For testing purposes, we display the avatar if the address is
110113
// a valid hex checksum address.
111-
displayAvatar={isCaipHexAddress(event.value)}
114+
displayAvatar={isCaipHexAddress(state.to)}
112115
/>
113116
),
114117
},
115118
});
116119
break;
117120
}
118-
119-
case 'accountSelector': {
120-
await snap.request({
121-
method: 'snap_updateInterface',
122-
params: {
123-
id,
124-
ui: (
125-
<SendFlow
126-
selectedCurrency={selectedCurrency}
127-
total={total}
128-
fees={fees}
129-
errors={formErrors}
130-
/>
131-
),
132-
},
133-
});
134-
135-
break;
136-
}
137121
default:
138122
break;
139123
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { AccountSelectorValue } from '@metamask/snaps-sdk';
1+
import type {
2+
AccountSelectorState,
3+
AssetSelectorState,
4+
} from '@metamask/snaps-sdk';
25

36
/**
47
* The state of the send form.
@@ -10,7 +13,8 @@ import type { AccountSelectorValue } from '@metamask/snaps-sdk';
1013
export type SendFormState = {
1114
to: string;
1215
amount: string;
13-
accountSelector: AccountSelectorValue;
16+
account: AccountSelectorState | null;
17+
asset: AssetSelectorState | null;
1418
};
1519

1620
/**
@@ -43,6 +47,6 @@ export type Currency = {
4347
* @property fees - The fees for the transaction.
4448
*/
4549
export type SendFlowContext = {
46-
selectedCurrency: 'BTC' | '$';
50+
useFiat: boolean;
4751
fees: Currency;
4852
};

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export async function generateSendFlow({ fees }: GenerateSendFlowParams) {
2626
params: {
2727
ui: (
2828
<SendFlow
29-
selectedCurrency="BTC"
29+
useFiat={false}
30+
account={null}
31+
asset={null}
3032
total={{ amount: 0, fiat: 0 }}
3133
fees={fees}
3234
/>

packages/snaps-controllers/src/interface/SnapInterfaceController.test.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { MOCK_SNAP_ID } from '@metamask/snaps-utils/test-utils';
2828

2929
import { SnapInterfaceController } from './SnapInterfaceController';
3030
import {
31+
MOCK_ACCOUNT_ID,
3132
MockApprovalController,
3233
getRestrictedSnapInterfaceControllerMessenger,
3334
getRootSnapInterfaceControllerMessenger,
@@ -225,7 +226,7 @@ describe('SnapInterfaceController', () => {
225226
<AccountSelector
226227
name="baz"
227228
switchGlobalAccount
228-
value="eip155:0:0x1234567890123456789012345678901234567890"
229+
value="solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv"
229230
/>
230231
</Field>
231232
<Field label="foobar">
@@ -256,13 +257,13 @@ describe('SnapInterfaceController', () => {
256257
expect(rootMessenger.call).toHaveBeenNthCalledWith(
257258
4,
258259
'AccountsController:getAccountByAddress',
259-
'0x1234567890123456789012345678901234567890',
260+
'7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
260261
);
261262

262263
expect(rootMessenger.call).toHaveBeenNthCalledWith(
263264
5,
264265
'AccountsController:setSelectedAccount',
265-
'foo',
266+
MOCK_ACCOUNT_ID,
266267
);
267268

268269
expect(rootMessenger.call).toHaveBeenNthCalledWith(
@@ -275,11 +276,13 @@ describe('SnapInterfaceController', () => {
275276
foo: {
276277
bar: null,
277278
baz: {
278-
accountId: 'foo',
279-
addresses: ['eip155:0:0x1234567890123456789012345678901234567890'],
279+
accountId: MOCK_ACCOUNT_ID,
280+
addresses: [
281+
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:7S3P4HxJpyyigGzodYwHtCxZyUQe9JiBMHyRWXArAaKv',
282+
],
280283
},
281284
foobar: {
282-
accountId: 'foo',
285+
accountId: MOCK_ACCOUNT_ID,
283286
addresses: ['eip155:0:0x1234567890123456789012345678901234567890'],
284287
},
285288
},
@@ -1361,7 +1364,7 @@ describe('SnapInterfaceController', () => {
13611364

13621365
// TODO: Either fix this lint violation or explain why it's necessary to
13631366
// ignore.
1364-
1367+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
13651368
rootMessenger.call(
13661369
'SnapInterfaceController:resolveInterface',
13671370
MOCK_SNAP_ID,

0 commit comments

Comments
 (0)