import { OnRpcRequestHandler } from '@metamask/snaps-types'; import { panel, text, divider } from '@metamask/snaps-ui'; // import { text } from '@metamask/snaps-ui'; const API_ENDPOINT = 'https://api.getriff.xyz/ths/transaction/'; // const API_ENDPOINT = 'http://localhost:5010/ths/transaction/'; // import { getInsights } from './insights'; /** * Handle an incoming transaction, and return any insights. * * @param args - The request handler args as object. * @param args.transactionOrigin - The transaction origin object * @param args.transaction - The transaction object. * @param args.chainId - The chainId object. * @returns The transaction insights. */ export const onTransaction = async ({ transactionOrigin, transaction, chainId, }) => { // console.log('onTransactionRan:', transactionOrigin, transaction, chainId); const Toaddress: string = transaction.to; const dataStr: string = transaction.data; // console.log(chainId); const chainIDInt: number = parseInt(chainId.split(':')[1], 16); const myHeaders = new Headers(); myHeaders.append('Content-Type', 'application/json'); const raw = JSON.stringify({ networkId: chainIDInt, networkName: '', contractAddress: Toaddress, method: '', stateMutability: 'nonpayable', }); const requestOptions: RequestInit = { method: 'POST', headers: myHeaders, body: raw, redirect: 'follow', }; // console.log(requestOptions); const response = await fetch(API_ENDPOINT, requestOptions); const jsonResp = await response.json(); const myHeaders2 = new Headers(); myHeaders2.append('Content-Type', 'application/json'); const raw2 = JSON.stringify({ networkId: chainIDInt, networkName: '', contractAddress: Toaddress, method: '', methodId: '', stateMutability: 'nonpayable', rawTransaction: dataStr, }); const requestOptions2: RequestInit = { method: 'POST', headers: myHeaders2, body: raw2, redirect: 'follow', }; let explainStr = 'Explaination not available at this time!'; try { const response2 = await fetch(`${API_ENDPOINT}explain/`, requestOptions2); const responseJson = await response2.json(); explainStr = responseJson?.data[0]; } catch (err) { console.log(err); } // console.log(responseJson); const ver = jsonResp?.data[0]?.verified === '1' ? 'Yes' : 'No'; // console.log({ // insights: { // 'Total Transactions': jsonResp?.data[0]?.total_txns, // 'Is Contract Verified': ver, // 'Safety Tag': jsonResp?.data[0]?.tag, // Explaination: explainStr, // 'Top Transactors': jsonResp?.data[0]?.top_transactor, // 'Function Distribution Info': jsonResp?.data[0]?.func_dist, // 'Method Distribution Info': jsonResp?.data[0]?.method_dist, // }, // }); const content = panel([ text(`**MetaMask Transaction Insights**`), divider(), text(`**Total Transactions:**`), text(`${jsonResp?.data[0]?.total_txns}`), text(`**Is Contract Verified:**`), text(`${ver}`), text(`**Safety Tag:**`), text(`${jsonResp?.data[0]?.tag}`), text(`**Explaination:**`), text(`${explainStr}`), text(`**Top Transactors:**`), text(`${JSON.stringify(jsonResp?.data[0]?.top_transactor, null, '\n')}`), text(`**Function Distribution Info:**`), text(`${JSON.stringify(jsonResp?.data[0]?.func_dist, null, '\n')}`), text(`**Method Distribution Info:**`), text(`${JSON.stringify(jsonResp?.data[0]?.method_dist, null, '\n')}`), ]); return { content, }; };