-
Notifications
You must be signed in to change notification settings - Fork 95
Adding tenderly simulation #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9cb9315
b637643
cb1ac3e
dd164e9
f3625c2
d511ea0
276ad83
41f1041
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,20 @@ | ||
| import { BigNumber as BigNumEth } from 'ethers'; | ||
| import { PropsWithChildren, ReactNode, useState } from 'react'; | ||
| import { toast } from 'react-toastify'; | ||
|
|
||
| import { Spinner } from '../../../components/animations/Spinner'; | ||
| import { ChainLogo } from '../../../components/icons/ChainLogo'; | ||
| import { HelpIcon } from '../../../components/icons/HelpIcon'; | ||
| import { Card } from '../../../components/layout/Card'; | ||
| import { Modal } from '../../../components/layout/Modal'; | ||
| import { links } from '../../../consts/links'; | ||
| import { MessageStatus, MessageTx } from '../../../types'; | ||
| import { Message, MessageStatus, MessageTx, SimulateBody } from '../../../types'; | ||
| import { getDateTimeString, getHumanReadableTimeString } from '../../../utils/time'; | ||
| import { getChainDisplayName } from '../../chains/utils'; | ||
| import { debugStatusToDesc } from '../../debugger/strings'; | ||
| import { MessageDebugResult } from '../../debugger/types'; | ||
| import { useMultiProvider } from '../../providers/multiProvider'; | ||
| import { computeAvgGasPrice } from '../utils'; | ||
|
|
||
| import { LabelAndCodeBlock } from './CodeBlock'; | ||
| import { KeyValueRow } from './KeyValueRow'; | ||
|
|
@@ -40,6 +43,7 @@ export function DestinationTransactionCard({ | |
| isStatusFetching, | ||
| isPiMsg, | ||
| blur, | ||
| message, | ||
| }: { | ||
| chainId: ChainId; | ||
| status: MessageStatus; | ||
|
|
@@ -48,6 +52,7 @@ export function DestinationTransactionCard({ | |
| isStatusFetching: boolean; | ||
| isPiMsg?: boolean; | ||
| blur: boolean; | ||
| message: Message; | ||
| }) { | ||
| let content: ReactNode; | ||
| if (transaction) { | ||
|
|
@@ -70,7 +75,7 @@ export function DestinationTransactionCard({ | |
| {debugResult.description} | ||
| </div> | ||
| )} | ||
| <CallDataModal debugResult={debugResult} /> | ||
| <CallDataModal debugResult={debugResult} chainId={chainId} message={message} /> | ||
| </DeliveryStatus> | ||
| ); | ||
| } else if (status === MessageStatus.Pending) { | ||
|
|
@@ -84,7 +89,7 @@ export function DestinationTransactionCard({ | |
| </div> | ||
| )} | ||
| <Spinner classes="my-4 scale-75" /> | ||
| <CallDataModal debugResult={debugResult} /> | ||
| <CallDataModal debugResult={debugResult} chainId={chainId} message={message} /> | ||
| </div> | ||
| </DeliveryStatus> | ||
| ); | ||
|
|
@@ -207,10 +212,27 @@ function DeliveryStatus({ children }: PropsWithChildren<unknown>) { | |
| ); | ||
| } | ||
|
|
||
| function CallDataModal({ debugResult }: { debugResult?: MessageDebugResult }) { | ||
| function CallDataModal({ | ||
| debugResult, | ||
| chainId, | ||
| message, | ||
| }: { | ||
| debugResult?: MessageDebugResult; | ||
| chainId: ChainId; | ||
| message: Message; | ||
| }) { | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const [loading, setLoading] = useState(false); | ||
| const [buttonText, setButtonText] = useState('Simulate call with Tenderly'); | ||
| if (!debugResult?.calldataDetails) return null; | ||
| const { contract, handleCalldata } = debugResult.calldataDetails; | ||
| const handleClick = async () => { | ||
| setButtonText('Simulating'); | ||
jmrossy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| setLoading(true); | ||
| await simulateCall({ contract, handleCalldata, chainId, message }); | ||
| setButtonText('Simulate call with Tenderly'); | ||
| setLoading(false); //using !loading is not setting the states properly and the state stays true | ||
| }; | ||
| return ( | ||
| <> | ||
| <button onClick={() => setIsOpen(true)} className={`mt-5 ${styles.textLink}`}> | ||
|
|
@@ -236,11 +258,51 @@ function CallDataModal({ debugResult }: { debugResult?: MessageDebugResult }) { | |
| </p> | ||
| <LabelAndCodeBlock label="Recipient contract address:" value={contract} /> | ||
| <LabelAndCodeBlock label="Handle function input calldata:" value={handleCalldata} /> | ||
| <button onClick={handleClick} disabled={loading} className="underline text-blue-400"> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Show only when not loading (see feedback on discord). |
||
| {buttonText} | ||
| </button> | ||
| {loading && <Spinner classes="mt-4 scale-75 self-center" />} | ||
| </div> | ||
| </Modal> | ||
| </> | ||
| ); | ||
| } | ||
| async function simulateCall({ | ||
| contract, | ||
| handleCalldata, | ||
| chainId, | ||
| message, | ||
| }: { | ||
| contract: string; | ||
| handleCalldata: string; | ||
| chainId: ChainId; | ||
| message: Message; | ||
| }) { | ||
| const gasPrice = computeAvgGasPrice('wei', message.totalGasAmount, message.totalPayment); | ||
| const data: SimulateBody = { | ||
| save: true, | ||
| save_if_fails: true, | ||
| simulation_type: 'full', | ||
| network_id: chainId, | ||
| from: '0x0000000000000000000000000000000000000000', //can be any address, doesn't matter | ||
| to: contract, | ||
| input: handleCalldata, | ||
| gas: BigNumEth.from(message.totalGasAmount).toNumber(), | ||
| gas_price: Number(gasPrice?.wei), | ||
| value: 0, | ||
| }; | ||
| const resp = await fetch(`/api/simulation`, { | ||
| method: 'POST', | ||
| body: JSON.stringify(data), | ||
| }); | ||
| const respMessage = await resp.json(); | ||
| if (respMessage.success === true) { | ||
| const simulationId = respMessage.data; | ||
| window.open(`https://dashboard.tenderly.co/shared/simulation/${simulationId}`); | ||
| } else { | ||
| toast.error(respMessage.error); | ||
| } | ||
| } | ||
|
|
||
| const helpText = { | ||
| origin: 'Info about the transaction that initiated the message placement into the outbox.', | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { TENDERLY_ACCESS_KEY, TENDERLY_PROJECT, TENDERLY_USER } from '../../consts/config'; | ||
| import { failureResult, successResult } from '../../features/api/utils'; | ||
|
|
||
| export default async function handler(req, res) { | ||
| const data = req.body; | ||
| if (!TENDERLY_ACCESS_KEY || !TENDERLY_PROJECT || !TENDERLY_USER) { | ||
| console.log('ENV not defined'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use logger instead of console, I believe this will raise a linter warning
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change log message to something more descriptive: |
||
| res.json(failureResult('Explorer Issues')); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to 'Tenderly credentials missing' |
||
| } | ||
| try { | ||
| const resp = await fetch( | ||
| `https://api.tenderly.co/api/v1/account/${TENDERLY_USER}/project/${TENDERLY_PROJECT}/simulate`, | ||
| { | ||
| method: 'POST', | ||
| body: data, | ||
| headers: { | ||
| 'X-Access-Key': TENDERLY_ACCESS_KEY as string, | ||
| }, | ||
| }, | ||
| ); | ||
| const simulationId = await resp.json().then((data) => data.simulation.id); | ||
| await fetch( | ||
| `https://api.tenderly.co/api/v1/account/${TENDERLY_USER}/project/${TENDERLY_PROJECT}/simulations/${simulationId}/share`, | ||
| { | ||
| method: 'POST', | ||
| headers: { | ||
| 'X-Access-Key': TENDERLY_ACCESS_KEY as string, | ||
| }, | ||
| }, | ||
| ); | ||
| res.json(successResult(simulationId)); | ||
| } catch (error) { | ||
| res.json(failureResult('Error preparing Tenderly simulation')); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.