Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Added additional metamask dialog box when querying to hedera ledger node
Browse files Browse the repository at this point in the history
  • Loading branch information
kpachhai committed Oct 6, 2023
1 parent 2c43a12 commit 8bf30cc
Show file tree
Hide file tree
Showing 22 changed files with 471 additions and 153 deletions.
8 changes: 7 additions & 1 deletion packages/site/src/components/cards/GetAccountInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ import ExternalAccount, {

type Props = {
network: string;
mirrorNodeUrl: string;
setAccountInfo: React.Dispatch<React.SetStateAction<Account>>;
};

const GetAccountInfo: FC<Props> = ({ network, setAccountInfo }) => {
const GetAccountInfo: FC<Props> = ({
network,
mirrorNodeUrl,
setAccountInfo,
}) => {
const [state, dispatch] = useContext(MetaMaskContext);
const [loading, setLoading] = useState(false);
const { showModal } = useModal();
Expand All @@ -32,6 +37,7 @@ const GetAccountInfo: FC<Props> = ({ network, setAccountInfo }) => {

const response: any = await getAccountInfo(
network,
mirrorNodeUrl,
accountId,
externalAccountParams,
);
Expand Down
9 changes: 7 additions & 2 deletions packages/site/src/components/cards/SendHelloMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ import { Card, SendHelloButton } from '../base';

type Props = {
network: string;
mirrorNodeUrl: string;
setAccountInfo: React.Dispatch<React.SetStateAction<Account>>;
};

const SendHelloHessage: FC<Props> = ({ network, setAccountInfo }) => {
const SendHelloHessage: FC<Props> = ({
network,
mirrorNodeUrl,
setAccountInfo,
}) => {
const [state, dispatch] = useContext(MetaMaskContext);

const handleSendHelloClick = async () => {
try {
const response: any = await sendHello(network);
const response: any = await sendHello(network, mirrorNodeUrl);
setAccountInfo(response.currentAccount);
} catch (e) {
console.error(e);
Expand Down
8 changes: 7 additions & 1 deletion packages/site/src/components/cards/TransferCrypto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ import ExternalAccount, {

type Props = {
network: string;
mirrorNodeUrl: string;
setAccountInfo: React.Dispatch<React.SetStateAction<Account>>;
};

const TransferCrypto: FC<Props> = ({ network, setAccountInfo }) => {
const TransferCrypto: FC<Props> = ({
network,
mirrorNodeUrl,
setAccountInfo,
}) => {
const [state, dispatch] = useContext(MetaMaskContext);
const [loading, setLoading] = useState(false);
const { showModal } = useModal();
Expand Down Expand Up @@ -46,6 +51,7 @@ const TransferCrypto: FC<Props> = ({ network, setAccountInfo }) => {

const response: any = await transferCrypto(
network,
mirrorNodeUrl,
transfers,
sendMemo,
undefined,
Expand Down
14 changes: 13 additions & 1 deletion packages/site/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useContext, useState } from 'react';
import { Col, Container, Row } from 'react-bootstrap';
import { Col, Container, Form, Row } from 'react-bootstrap';
import Select from 'react-select';
import { Card, InstallFlaskButton } from '../components/base';
import { ConnectPulseSnap } from '../components/cards/ConnectPulseSnap';
Expand All @@ -26,6 +26,7 @@ import { connectSnap, getSnap } from '../utils';
const Index = () => {
const [state, dispatch] = useContext(MetaMaskContext);
const [currentNetwork, setCurrentNetwork] = useState(networkOptions[0]);
const [mirrorNodeUrl, setMirrorNodeUrl] = useState('');
const [accountInfo, setAccountInfo] = useState<Account>({} as Account);

const handleNetworkChange = (network: any) => {
Expand Down Expand Up @@ -71,6 +72,14 @@ const Index = () => {
}),
}}
/>
<Form.Label>Enter your own Mirror Node URL to use(Optional)</Form.Label>
<Form.Control
size="lg"
type="text"
placeholder="eg. https://testnet.mirrornode.hedera.com"
style={{ marginBottom: 8 }}
onChange={(e) => setMirrorNodeUrl(e.target.value)}
/>
</Subtitle>
<Container>
<Row>
Expand Down Expand Up @@ -116,16 +125,19 @@ const Index = () => {

<SendHelloHessage
network={currentNetwork.value}
mirrorNodeUrl={mirrorNodeUrl}
setAccountInfo={setAccountInfo}
/>

<GetAccountInfo
network={currentNetwork.value}
mirrorNodeUrl={mirrorNodeUrl}
setAccountInfo={setAccountInfo}
/>

<TransferCrypto
network={currentNetwork.value}
mirrorNodeUrl={mirrorNodeUrl}
setAccountInfo={setAccountInfo}
/>

Expand Down
17 changes: 13 additions & 4 deletions packages/site/src/utils/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ export const getSnap = async (version?: string): Promise<Snap | undefined> => {
* Invoke the "hello" method from the snap.
*/

export const sendHello = async (network: string) => {
export const sendHello = async (network: string, mirrorNodeUrl: string) => {
return await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId: defaultSnapOrigin,
request: {
method: 'hello',
params: { network },
params: { network, mirrorNodeUrl },
},
},
});
Expand All @@ -102,6 +102,7 @@ export const sendHello = async (network: string) => {

export const getAccountInfo = async (
network: string,
mirrorNodeUrl: string,
accountId?: string,
externalAccountparams?: ExternalAccountParams,
) => {
Expand All @@ -111,7 +112,7 @@ export const getAccountInfo = async (
snapId: defaultSnapOrigin,
request: {
method: 'getAccountInfo',
params: { network, accountId, ...externalAccountparams },
params: { network, mirrorNodeUrl, accountId, ...externalAccountparams },
},
},
});
Expand Down Expand Up @@ -143,6 +144,7 @@ export const getAccountBalance = async (

export const transferCrypto = async (
network: string,
mirrorNodeUrl: string,
transfers: SimpleTransfer[],
memo?: string,
maxFee?: BigNumber,
Expand All @@ -154,7 +156,14 @@ export const transferCrypto = async (
snapId: defaultSnapOrigin,
request: {
method: 'transferCrypto',
params: { network, transfers, memo, maxFee, ...externalAccountparams },
params: {
network,
mirrorNodeUrl,
transfers,
memo,
maxFee,
...externalAccountparams,
},
},
},
});
Expand Down
4 changes: 3 additions & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"@metamask/snaps-ui": "^1.0.1",
"bignumber.js": "^9.1.1",
"ethers": "^6.3.0",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"normalize-url": "^8.0.0"
},
"devDependencies": {
"@babel/core": "^7.21.0",
Expand All @@ -75,6 +76,7 @@
"@metamask/snaps-webpack-plugin": "^1.0.2",
"@types/jest": "^29.5.0",
"@types/lodash.clonedeep": "^4.5.7",
"@types/normalize-url": "^4.2.0",
"@typescript-eslint/eslint-plugin": "^5.57.1",
"@typescript-eslint/parser": "^5.57.1",
"babel-loader": "^9.1.2",
Expand Down
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/tuum-tech/hedera-pulse.git"
},
"source": {
"shasum": "kD2bjW2YMOHS3iNSirqRXwv+H04ff49m15cMvlflOxw=",
"shasum": "RbrLNX82IPT/uPvT8D7VjrbQzLlyym0aEw9wOVmvCtU=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
12 changes: 11 additions & 1 deletion packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getSnapStateUnchecked } from './snap/state';
import { PulseSnapParams } from './types/state';
import { init } from './utils/init';
import {
getMirrorNodeFlagIfExists,
isExternalAccountFlagSet,
isValidGetAccountInfoRequest,
isValidTransferCryptoParams,
Expand Down Expand Up @@ -55,14 +56,23 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
isExternalAccount = true;
}

await setCurrentAccount(origin, state, request.params, isExternalAccount);
const mirrorNodeUrl = getMirrorNodeFlagIfExists(request.params);

await setCurrentAccount(
origin,
state,
request.params,
mirrorNodeUrl,
isExternalAccount,
);
console.log(
`Current account: ${JSON.stringify(state.currentAccount, null, 4)}`,
);

const pulseSnapParams: PulseSnapParams = {
origin,
state,
mirrorNodeUrl,
};

switch (request.method) {
Expand Down
11 changes: 7 additions & 4 deletions packages/snap/src/rpc/account/getAccountBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { updateSnapState } from '../../snap/state';
import { PulseSnapParams } from '../../types/state';

/**
* Get balance of an account.
* A query that returns the account balance for the specified account.
* Requesting an account balance is currently free of charge. Queries do
* not change the state of the account or require network consensus. The
* information is returned from a single node processing the query.
*
* @param pulseSnapParams - Pulse snap params.
* @returns Account Balance.
Expand All @@ -24,12 +27,12 @@ export async function getAccountBalance(
);

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
state.accountState[hederaEvmAddress][network].accountInfo.balance!.hbars =
state.accountState[hederaEvmAddress][network].accountInfo.balance.hbars =
await hederaClient.getAccountBalance();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
state.accountState[hederaEvmAddress][
network
].accountInfo.balance!.timestamp = new Date().toISOString();
].accountInfo.balance.timestamp = new Date().toISOString();
await updateSnapState(state);
} catch (error: any) {
console.error(
Expand All @@ -41,5 +44,5 @@ export async function getAccountBalance(
}

return state.accountState[hederaEvmAddress][network].accountInfo.balance
?.hbars as number;
.hbars;
}
Loading

0 comments on commit 8bf30cc

Please sign in to comment.