Skip to content

Commit

Permalink
feat: support show all monitored addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoosocool committed Oct 20, 2023
1 parent 8ea73d8 commit 4962b80
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 55 deletions.
93 changes: 61 additions & 32 deletions packages/site/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useContext, useState } from 'react';
import styled from 'styled-components';
import { MetamaskActions, MetaMaskContext } from '../hooks';
import {
addOwnWalletAddress,
// addOwnWalletAddress,
connectSnap,
getSnap,
isLocalSnap,
Expand All @@ -12,6 +12,7 @@ import {
shouldDisplayReconnectButton,
showAlert,
showAllActivities,
showAllMonitoredAddresses,
showLastUpdated,
} from '../utils';
import {
Expand Down Expand Up @@ -178,32 +179,32 @@ const Index = () => {
}
};

const handleSendGetStateClick = async () => {
try {
const resp = await sendGetState();
await showAlert('Get State', JSON.stringify(resp, null, 2));
} catch (e) {
console.error(e);
dispatch({ type: MetamaskActions.SetError, payload: e });
}
};

const handleSendAddYourWalletClick = async () => {
try {
const resp = await addOwnWalletAddress();
if (resp && (resp as string[]).length > 0) {
await showAlert('Monitored', JSON.stringify(resp, null, 2));
} else {
await showAlert(
'Already Monitored',
'the wallet address is already monitored.',
);
}
} catch (e) {
console.error(e);
dispatch({ type: MetamaskActions.SetError, payload: e });
}
};
// const handleSendGetStateClick = async () => {
// try {
// const resp = await sendGetState();
// await showAlert('Get State', JSON.stringify(resp, null, 2));
// } catch (e) {
// console.error(e);
// dispatch({ type: MetamaskActions.SetError, payload: e });
// }
// };

// const handleSendAddYourWalletClick = async () => {
// try {
// const resp = await addOwnWalletAddress();
// if (resp && (resp as string[]).length > 0) {
// await showAlert('Monitored', JSON.stringify(resp, null, 2));
// } else {
// await showAlert(
// 'Already Monitored',
// 'the wallet address is already monitored.',
// );
// }
// } catch (e) {
// console.error(e);
// dispatch({ type: MetamaskActions.SetError, payload: e });
// }
// };

const handleSendSetStateClick = async () => {
if (
Expand Down Expand Up @@ -287,6 +288,15 @@ const Index = () => {
}
};

const handleShowAllAddressesClick = async () => {
try {
await showAllMonitoredAddresses();
} catch (e) {
console.error(e);
dispatch({ type: MetamaskActions.SetError, payload: e });
}
};

return (
<Container>
<Heading>
Expand All @@ -300,12 +310,12 @@ const Index = () => {
3. When there is a new activity produced by any of your monitored
addresses, <Span>you will be notified</Span>.
</SubHeading>
{state.error && (
<ErrorMessage>
<b>An error happened:</b> {state.error.message}
</ErrorMessage>
)}
<CardContainer>
{state.error && (
<ErrorMessage>
<b>An error happened:</b> {state.error.message}
</ErrorMessage>
)}
{!isMetaMaskReady && (
<Card
content={{
Expand Down Expand Up @@ -447,6 +457,25 @@ const Index = () => {
}
/>

<Card
content={{
title: 'Show All Addresses',
description: 'View all addresses from all the addresses monitored.',
button: (
<SendHelloButton
onClick={handleShowAllAddressesClick}
disabled={!state.installedSnap}
/>
),
}}
disabled={!state.installedSnap}
fullWidth={
isMetaMaskReady &&
Boolean(state.installedSnap) &&
!shouldDisplayReconnectButton(state.installedSnap)
}
/>

<Card
content={{
title: 'Monitor Any Address',
Expand Down
13 changes: 13 additions & 0 deletions packages/site/src/utils/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,17 @@ export const showAllActivities = async () => {
},
});
};

export const showAllMonitoredAddresses = async () => {
await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId: defaultSnapOrigin,
request: {
method: 'showAllMonitoredAddresses',
},
},
});
};

export const isLocalSnap = (snapId: string) => snapId.startsWith('local:');
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/NaturalSelectionLabs/RSS3-MetaMask-Snap.git"
},
"source": {
"shasum": "qJEtxAAKUamn/Xa5MfMU/SU3PDyLZOpkE6De75PhsGs=",
"shasum": "VR2VgcEbiaPiSNYV3HP1YCB2L7UWDQ7PAdgAOrVk7PI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
74 changes: 52 additions & 22 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export type FetchSocialCountParams = {
*/
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
switch (request.method) {
/** Basic function */
// set the state
case 'setState': {
const { socialActivities } = request.params as State;
const state = await getState();
Expand All @@ -41,15 +43,53 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
return true;
}

// get the state
case 'getState': {
return await getState();
}

// clear the state
case 'clearState': {
await clearState();
return true;
}

// get accounts by own wallet
case 'getAccounts': {
return await getAccounts();
}

// add account to state
case 'addAccount': {
const { account } = request.params as { account: string | undefined };
if (!account) {
return true;
}
return await addAddressToState(account);
}

// add multiple accounts by own wallet to state
case 'addOwnWalletAddresses': {
const accounts = await getAccounts();
return await addMultipleAddressesToState(accounts);
}

// User-defined Dialog.Alert
case 'showAlert': {
const { title, content } = request.params as {
title: string;
content: string;
};
return snap.request({
method: 'snap_dialog',
params: {
type: DialogType.Alert,
content: panel([heading(title), text(content)]),
},
});
}

// fetch the social activities by @rss3/js-sdk.
case 'fetchSocialCount': {
const params = request.params as FetchSocialCountParams | undefined;
assert(
Expand All @@ -63,15 +103,9 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
return await Promise.all(resultPromise);
}

case 'getAccounts': {
return await getAccounts();
}

case 'addOwnWalletAddresses': {
const accounts = await getAccounts();
return await addMultipleAddressesToState(accounts);
}
/** Helper function */

// show the last updated activities
case 'showLastUpdated': {
const state = await getState();
const content: any = [heading('Last Updated')];
Expand All @@ -91,6 +125,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
});
}

// show the all activities
case 'showAllActivities': {
const state = await getState();
const content: any = [heading('All Activities')];
Expand All @@ -110,24 +145,19 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
});
}

case 'addAccount': {
const { account } = request.params as { account: string | undefined };
if (!account) {
return true;
}
return await addAddressToState(account);
}

case 'showAlert': {
const { title, content } = request.params as {
title: string;
content: string;
};
// show the all monitored addresses
case 'showAllMonitoredAddresses': {
const state = await getState();
const content: any = [heading('All Monitored Addresses')];
state.socialActivities.forEach((activity) => {
content.push(text(activity.address));
content.push(divider());
});
return snap.request({
method: 'snap_dialog',
params: {
type: DialogType.Alert,
content: panel([heading(title), text(content)]),
content: panel(content),
},
});
}
Expand Down

0 comments on commit 4962b80

Please sign in to comment.