-
Notifications
You must be signed in to change notification settings - Fork 94
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 6 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,18 +1,19 @@ | ||
| import { BigNumber } from 'bignumber.js'; | ||
| import { BigNumber as BigNumEth } from 'ethers'; | ||
| import { PropsWithChildren, ReactNode, useState } from 'react'; | ||
|
|
||
| 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 { logger } from '../../../utils/logger'; | ||
| 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 { LabelAndCodeBlock } from './CodeBlock'; | ||
| import { KeyValueRow } from './KeyValueRow'; | ||
|
|
||
|
|
@@ -40,6 +41,7 @@ export function DestinationTransactionCard({ | |
| isStatusFetching, | ||
| isPiMsg, | ||
| blur, | ||
| message | ||
| }: { | ||
| chainId: ChainId; | ||
| status: MessageStatus; | ||
|
|
@@ -48,6 +50,7 @@ export function DestinationTransactionCard({ | |
| isStatusFetching: boolean; | ||
| isPiMsg?: boolean; | ||
| blur: boolean; | ||
| message:Message; | ||
| }) { | ||
| let content: ReactNode; | ||
| if (transaction) { | ||
|
|
@@ -70,7 +73,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 +87,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 +210,20 @@ 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.
|
||
| 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 +249,59 @@ 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' | ||
| > | ||
| {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}){ | ||
|
|
||
|
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. Wrap the function contents here in a
Author
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. Didn't wrap this in |
||
|
|
||
|
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. Spacing? Please run |
||
| const data:SimulateBody={ | ||
| save: true, | ||
| save_if_fails: true, | ||
| simulation_type: 'full', | ||
|
jmrossy marked this conversation as resolved.
Outdated
|
||
| network_id: chainId, | ||
|
jmrossy marked this conversation as resolved.
Outdated
|
||
| from: '0x0000000000000000000000000000000000000000',//can be any address, doesn't matter | ||
| to: contract, | ||
| input:handleCalldata, | ||
| gas: BigNumEth.from(message.totalGasAmount).toNumber(), | ||
| gas_price: Number(computeAvgGasPrice("wei",message.totalGasAmount,message.totalPayment)), | ||
| value: 0, | ||
| } | ||
| const resp=await fetch( | ||
| `/api/simulation`,{ | ||
| method:'POST', | ||
| body:JSON.stringify(data), | ||
| } | ||
| ) | ||
|
|
||
| const simulationId=await resp.json().then((data)=>data.data) | ||
| window.open(`https://dashboard.tenderly.co/shared/simulation/${simulationId}`) | ||
|
|
||
| } | ||
|
|
||
| function computeAvgGasPrice(unit: string, gasAmount?: BigNumber.Value, payment?: BigNumber.Value) { | ||
|
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. Why did you duplicate this function here? Please de-dupe with the copy in GasDetailsCard and move it to |
||
| try { | ||
| if (!gasAmount || !payment) return null; | ||
| const gasBN = new BigNumber(gasAmount); | ||
| const paymentBN = new BigNumber(payment); | ||
| if (gasBN.isZero() || paymentBN.isZero()) return null; | ||
| const wei = paymentBN.div(gasAmount).toFixed(0); | ||
| return wei; | ||
| } catch (error) { | ||
| logger.debug('Error computing avg gas price', error); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| 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,31 @@ | ||
| import { TENDERLY_ACCESS_KEY, TENDERLY_PROJECT, TENDERLY_USER } from "../../consts/config" | ||
| import { successResult } from "../../features/api/utils" | ||
| export default async function handler(req,res){ | ||
| const data=req.body | ||
|
jmrossy marked this conversation as resolved.
Outdated
|
||
|
|
||
| if(!TENDERLY_ACCESS_KEY || !TENDERLY_PROJECT || !TENDERLY_USER){ | ||
| console.log("ENV not defined") | ||
| return null | ||
| } | ||
| const resp = await fetch( | ||
| `https://api.tenderly.co/api/v1/account/${TENDERLY_USER}/project/${TENDERLY_PROJECT}/simulate`, | ||
|
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. Check that these env vars are defined and throw a helpful here at the top if they're not |
||
| { | ||
| 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)) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.