1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00
metamask-extension/ui/hooks/flask/useTransactionInsightSnap.js
Guillaume Roux c9dc59ea2a
[FLASK] Snaps Insight (#15814)
Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com>
Co-authored-by: Hassan Malik <41640681+hmalik88@users.noreply.github.com>
2022-09-20 19:00:07 +02:00

38 lines
1.0 KiB
JavaScript

import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { handleSnapRequest } from '../../store/actions';
import { getPermissionSubjects } from '../../selectors';
const INSIGHT_PERMISSION = 'endowment:transaction-insight';
export function useTransactionInsightSnap({ transaction, chainId, snapId }) {
const subjects = useSelector(getPermissionSubjects);
if (!subjects[snapId]?.permissions[INSIGHT_PERMISSION]) {
throw new Error(
'This snap does not have the transaction insight endowment.',
);
}
const [data, setData] = useState(undefined);
useEffect(() => {
async function fetchInsight() {
const d = await handleSnapRequest({
snapId,
origin: 'test',
handler: 'onTransaction',
request: {
jsonrpc: '2.0',
method: ' ',
params: { transaction, chainId },
},
});
setData(d);
}
if (transaction) {
fetchInsight();
}
}, [snapId, transaction, chainId]);
return data;
}