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