|
| 1 | +import "dotenv/config"; |
| 2 | +import { |
| 3 | + ThirdwebSDK, |
| 4 | + computeCloneFactoryAddress, |
| 5 | + deployContractDeterministic, |
| 6 | + deployCreate2Factory, |
| 7 | + deployWithThrowawayDeployer, |
| 8 | + fetchAndCacheDeployMetadata, |
| 9 | + getCreate2FactoryAddress, |
| 10 | + getDeploymentInfo, |
| 11 | + getThirdwebContractAddress, |
| 12 | + isContractDeployed, |
| 13 | + resolveAddress, |
| 14 | +} from "@thirdweb-dev/sdk"; |
| 15 | +import { Signer } from "ethers"; |
| 16 | +import { apiMap, chainIdApiKey, contractsToDeploy } from "./constants"; |
| 17 | + |
| 18 | +////// To run this script: `npx ts-node scripts/deploy-prebuilt-deterministic/bootstrap-on-a-chain.ts` ////// |
| 19 | +///// MAKE SURE TO PUT IN THE RIGHT CONTRACT NAME HERE AFTER PUBLISHING IT ///// |
| 20 | +//// THE CONTRACT SHOULD BE PUBLISHED WITH THE NEW PUBLISH FLOW //// |
| 21 | + |
| 22 | +const publisherKey: string = process.env.THIRDWEB_PUBLISHER_PRIVATE_KEY as string; |
| 23 | +const deployerKey: string = process.env.PRIVATE_KEY as string; |
| 24 | + |
| 25 | +const polygonSDK = ThirdwebSDK.fromPrivateKey(publisherKey, "polygon"); |
| 26 | + |
| 27 | +const chainId = "8453"; // update here |
| 28 | +const networkName = "base"; // update here |
| 29 | + |
| 30 | +async function main() { |
| 31 | + const publisher = await polygonSDK.wallet.getAddress(); |
| 32 | + |
| 33 | + const sdk = ThirdwebSDK.fromPrivateKey(deployerKey, chainId); // can also hardcode the chain here |
| 34 | + const signer = sdk.getSigner() as Signer; |
| 35 | + |
| 36 | + console.log("balance: ", await sdk.wallet.balance()); |
| 37 | + |
| 38 | + // Deploy CREATE2 factory (if not already exists) |
| 39 | + const create2FactoryAddress = await getCreate2FactoryAddress(sdk.getProvider()); |
| 40 | + if (await isContractDeployed(create2FactoryAddress, sdk.getProvider())) { |
| 41 | + console.log(`-- Create2 factory already present at ${create2FactoryAddress}\n`); |
| 42 | + } else { |
| 43 | + console.log(`-- Deploying Create2 factory at ${create2FactoryAddress}\n`); |
| 44 | + await deployCreate2Factory(signer, {}); |
| 45 | + } |
| 46 | + |
| 47 | + // TWStatelessFactory (Clone factory) |
| 48 | + const cloneFactoryAddress = await computeCloneFactoryAddress(sdk.getProvider(), sdk.storage, create2FactoryAddress); |
| 49 | + if (await isContractDeployed(cloneFactoryAddress, sdk.getProvider())) { |
| 50 | + console.log(`-- TWCloneFactory present at ${cloneFactoryAddress}\n`); |
| 51 | + } |
| 52 | + |
| 53 | + for (const publishedContractName of contractsToDeploy) { |
| 54 | + const latest = await polygonSDK.getPublisher().getLatest(publisher, publishedContractName); |
| 55 | + |
| 56 | + if (latest && latest.metadataUri) { |
| 57 | + const { extendedMetadata } = await fetchAndCacheDeployMetadata(latest?.metadataUri, polygonSDK.storage); |
| 58 | + |
| 59 | + const isNetworkEnabled = |
| 60 | + extendedMetadata?.networksForDeployment?.networksEnabled.includes(parseInt(chainId)) || |
| 61 | + extendedMetadata?.networksForDeployment?.allNetworks; |
| 62 | + |
| 63 | + if (extendedMetadata?.networksForDeployment && !isNetworkEnabled) { |
| 64 | + console.log(`Deployment of ${publishedContractName} disabled on ${networkName}\n`); |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + console.log(`Deploying ${publishedContractName} on ${networkName}`); |
| 69 | + |
| 70 | + // const chainId = (await sdk.getProvider().getNetwork()).chainId; |
| 71 | + |
| 72 | + try { |
| 73 | + const implAddr = await getThirdwebContractAddress(publishedContractName, parseInt(chainId), sdk.storage); |
| 74 | + if (implAddr) { |
| 75 | + console.log(`implementation ${implAddr} already deployed on chainId: ${chainId}`); |
| 76 | + console.log(); |
| 77 | + continue; |
| 78 | + } |
| 79 | + } catch (error) {} |
| 80 | + |
| 81 | + try { |
| 82 | + // any evm deployment flow |
| 83 | + |
| 84 | + // get deployment info for any evm |
| 85 | + const deploymentInfo = await getDeploymentInfo( |
| 86 | + latest.metadataUri, |
| 87 | + sdk.storage, |
| 88 | + sdk.getProvider(), |
| 89 | + create2FactoryAddress, |
| 90 | + ); |
| 91 | + |
| 92 | + const implementationAddress = deploymentInfo.find(i => i.type === "implementation")?.transaction |
| 93 | + .predictedAddress as string; |
| 94 | + |
| 95 | + const isDeployed = await isContractDeployed(implementationAddress, sdk.getProvider()); |
| 96 | + if (isDeployed) { |
| 97 | + console.log(`implementation ${implementationAddress} already deployed on chainId: ${chainId}`); |
| 98 | + console.log(); |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + console.log("Deploying as", await signer?.getAddress()); |
| 103 | + // filter out already deployed contracts (data is empty) |
| 104 | + const transactionsToSend = deploymentInfo.filter(i => i.transaction.data && i.transaction.data.length > 0); |
| 105 | + const transactionsforDirectDeploy = transactionsToSend |
| 106 | + .filter(i => { |
| 107 | + return i.type !== "infra"; |
| 108 | + }) |
| 109 | + .map(i => i.transaction); |
| 110 | + const transactionsForThrowawayDeployer = transactionsToSend |
| 111 | + .filter(i => { |
| 112 | + return i.type === "infra"; |
| 113 | + }) |
| 114 | + .map(i => i.transaction); |
| 115 | + |
| 116 | + // deploy via throwaway deployer, multiple infra contracts in one transaction |
| 117 | + if (transactionsForThrowawayDeployer.length > 0) { |
| 118 | + console.log("-- Deploying Infra"); |
| 119 | + await deployWithThrowawayDeployer(signer, transactionsForThrowawayDeployer, {}); |
| 120 | + } |
| 121 | + |
| 122 | + const resolvedImplementationAddress = await resolveAddress(implementationAddress); |
| 123 | + |
| 124 | + console.log(`-- Deploying ${publishedContractName} at ${resolvedImplementationAddress}`); |
| 125 | + // send each transaction directly to Create2 factory |
| 126 | + await Promise.all( |
| 127 | + transactionsforDirectDeploy.map(tx => { |
| 128 | + return deployContractDeterministic(signer, tx, {}); |
| 129 | + }), |
| 130 | + ); |
| 131 | + console.log(); |
| 132 | + } catch (e) { |
| 133 | + console.log("Error while deploying: ", e); |
| 134 | + console.log(); |
| 135 | + continue; |
| 136 | + } |
| 137 | + } else { |
| 138 | + console.log("No previous release found"); |
| 139 | + return; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + console.log("Deployments done."); |
| 144 | + console.log(); |
| 145 | + |
| 146 | + console.log("---------- Verification ---------"); |
| 147 | + console.log(); |
| 148 | + for (const publishedContractName of contractsToDeploy) { |
| 149 | + try { |
| 150 | + await sdk.verifier.verifyThirdwebContract( |
| 151 | + publishedContractName, |
| 152 | + apiMap[parseInt(chainId)], |
| 153 | + chainIdApiKey[parseInt(chainId)] as string, |
| 154 | + ); |
| 155 | + console.log(); |
| 156 | + } catch (error) { |
| 157 | + console.log(error); |
| 158 | + console.log(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + console.log("All done."); |
| 163 | +} |
| 164 | + |
| 165 | +main() |
| 166 | + .then(() => process.exit(0)) |
| 167 | + .catch(e => { |
| 168 | + console.error(e); |
| 169 | + process.exit(1); |
| 170 | + }); |
0 commit comments