|
| 1 | +const fetch = require("node-fetch"); |
| 2 | + |
| 3 | +async function tenderlySync(taskArguments, hre) { |
| 4 | + const allDeployments = await hre.deployments.all(); |
| 5 | + const deployedContracts = Object.entries(allDeployments).map( |
| 6 | + ([name, deployment]) => ({ |
| 7 | + name, |
| 8 | + address: deployment.address, |
| 9 | + }) |
| 10 | + ); |
| 11 | + const chainId = parseInt( |
| 12 | + (await hre.network.provider.send("eth_chainId")).toString() |
| 13 | + ); |
| 14 | + const allTenderlyContracts = await fetchAllContractsFromTenderly(chainId); |
| 15 | + |
| 16 | + for (let i = 0; i < deployedContracts.length; i++) { |
| 17 | + let presentInTenderly = false; |
| 18 | + const deployedContract = deployedContracts[i]; |
| 19 | + for (let j = 0; j < allTenderlyContracts.length; j++) { |
| 20 | + if ( |
| 21 | + deployedContract.address.toLowerCase() == |
| 22 | + allTenderlyContracts[j].toLowerCase() |
| 23 | + ) { |
| 24 | + presentInTenderly = true; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + if (presentInTenderly) { |
| 29 | + console.log( |
| 30 | + `✓ contract ${deployedContract.name}[${deployedContract.address}] already detected by Tenderly` |
| 31 | + ); |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + await uploadContractToTenderly( |
| 36 | + deployedContract.address, |
| 37 | + deployedContract.name, |
| 38 | + chainId |
| 39 | + ); |
| 40 | + console.log( |
| 41 | + `✅ contract ${deployedContract.name}[${deployedContract.address}] added to Tenderly` |
| 42 | + ); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +async function uploadContractToTenderly(address, name, networkId) { |
| 47 | + if (!process.env.TENDERLY_ACCESS_TOKEN) { |
| 48 | + throw new Error("TENDERLY_ACCESS_TOKEN env var missing"); |
| 49 | + } |
| 50 | + |
| 51 | + const baseUrl = `https://api.tenderly.co/api/v1/account/origin-protocol/project/origin/address`; |
| 52 | + |
| 53 | + const payload = { |
| 54 | + network_id: `${networkId}`, |
| 55 | + address: address, |
| 56 | + display_name: name, |
| 57 | + }; |
| 58 | + |
| 59 | + const response = await fetch(baseUrl, { |
| 60 | + method: "POST", |
| 61 | + headers: { |
| 62 | + "X-Access-Key": `${process.env.TENDERLY_ACCESS_TOKEN}`, |
| 63 | + "Content-Type": "application/json", |
| 64 | + Accept: "application/json", |
| 65 | + }, |
| 66 | + body: JSON.stringify(payload), |
| 67 | + }); |
| 68 | + |
| 69 | + if (!response.ok) { |
| 70 | + throw new Error( |
| 71 | + `API request failed with status ${ |
| 72 | + response.status |
| 73 | + }: ${await response.text()}` |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + const data = await response.json(); |
| 78 | + return data; |
| 79 | +} |
| 80 | + |
| 81 | +async function fetchAllContractsFromTenderly() { |
| 82 | + if (!process.env.TENDERLY_ACCESS_TOKEN) { |
| 83 | + throw new Error("TENDERLY_ACCESS_TOKEN env var missing"); |
| 84 | + } |
| 85 | + |
| 86 | + const baseUrl = `https://api.tenderly.co/api/v1/account/origin-protocol/project/origin/contracts?accountType=contract`; |
| 87 | + |
| 88 | + let url = baseUrl; |
| 89 | + const response = await fetch(url, { |
| 90 | + method: "GET", |
| 91 | + headers: { |
| 92 | + "X-Access-Key": `${process.env.TENDERLY_ACCESS_TOKEN}`, |
| 93 | + Accept: "application/json", |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + if (!response.ok) { |
| 98 | + throw new Error( |
| 99 | + `API request failed with status ${ |
| 100 | + response.status |
| 101 | + }: ${await response.text()}` |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + const data = await response.json(); |
| 106 | + |
| 107 | + // return the array of contract addresses |
| 108 | + return data.map((contractData) => contractData.contract.address); |
| 109 | +} |
| 110 | + |
| 111 | +module.exports = { |
| 112 | + tenderlySync, |
| 113 | +}; |
0 commit comments