Skip to content

Commit

Permalink
script for deploying prebuilts on a network
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaryash90 committed Jul 11, 2023
1 parent 0d44311 commit 0549e98
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@openzeppelin/contracts": "4.7.3",
"@openzeppelin/contracts-upgradeable": "4.7.3",
"@primitivefi/hardhat-dodoc": "^0.2.0",
"@thirdweb-dev/sdk": "3.10.20",
"@thirdweb-dev/sdk": "3.10.29",
"@typechain/ethers-v5": "^10.0.0",
"@typechain/hardhat": "^4.0.0",
"@types/fs-extra": "^9.0.13",
Expand Down
161 changes: 161 additions & 0 deletions scripts/deploy-prebuilt-deterministic/bootstrap-on-a-chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import "dotenv/config";
import {
ThirdwebSDK,
computeCloneFactoryAddress,
deployContractDeterministic,
deployCreate2Factory,
deployWithThrowawayDeployer,
fetchAndCacheDeployMetadata,
getCreate2FactoryAddress,
getDeploymentInfo,
getThirdwebContractAddress,
isContractDeployed,
resolveAddress,
} from "@thirdweb-dev/sdk";
import { Signer } from "ethers";
import { apiMap, chainIdApiKey, contractsToDeploy } from "./constants";

////// To run this script: `npx ts-node scripts/deploy-prebuilt-deterministic/bootstrap-on-a-chain.ts` //////
///// MAKE SURE TO PUT IN THE RIGHT CONTRACT NAME HERE AFTER PUBLISHING IT /////
//// THE CONTRACT SHOULD BE PUBLISHED WITH THE NEW PUBLISH FLOW ////

const publisherKey: string = process.env.THIRDWEB_PUBLISHER_PRIVATE_KEY as string;
const deployerKey: string = process.env.PRIVATE_KEY as string;

const polygonSDK = ThirdwebSDK.fromPrivateKey(publisherKey, "polygon");

const chainId = "84531"; // update here
const networkName = "base-goerli"; // update here

async function main() {
const publisher = await polygonSDK.wallet.getAddress();

const sdk = ThirdwebSDK.fromPrivateKey(deployerKey, chainId); // can also hardcode the chain here
const signer = sdk.getSigner() as Signer;

// Deploy CREATE2 factory (if not already exists)
const create2FactoryAddress = await getCreate2FactoryAddress(sdk.getProvider());
if (await isContractDeployed(create2FactoryAddress, sdk.getProvider())) {
console.log(`-- Create2 factory already present at ${create2FactoryAddress}\n`);
} else {
console.log(`-- Deploying Create2 factory at ${create2FactoryAddress}\n`);
await deployCreate2Factory(signer, {});
}

// TWStatelessFactory (Clone factory)
const cloneFactoryAddress = await computeCloneFactoryAddress(sdk.getProvider(), sdk.storage, create2FactoryAddress);
if (await isContractDeployed(cloneFactoryAddress, sdk.getProvider())) {
console.log(`-- TWCloneFactory present at ${cloneFactoryAddress}\n`);
}

for (const publishedContractName of contractsToDeploy) {
const latest = await polygonSDK.getPublisher().getLatest(publisher, publishedContractName);

if (latest && latest.metadataUri) {
const { extendedMetadata } = await fetchAndCacheDeployMetadata(latest?.metadataUri, polygonSDK.storage);

const isNetworkEnabled =
extendedMetadata?.networksForDeployment?.networksEnabled.includes(parseInt(chainId)) ||
extendedMetadata?.networksForDeployment?.allNetworks;

if (extendedMetadata?.networksForDeployment && !isNetworkEnabled) {
console.log(`Deployment of ${publishedContractName} disabled on ${networkName}\n`);
continue;
}

console.log(`Deploying ${publishedContractName} on ${networkName}`);

// const chainId = (await sdk.getProvider().getNetwork()).chainId;

try {
const implAddr = await getThirdwebContractAddress(publishedContractName, parseInt(chainId), sdk.storage);
if (implAddr) {
console.log(`implementation ${implAddr} already deployed on chainId: ${chainId}`);
console.log();
continue;
}
} catch (error) {}

try {
console.log("Deploying as", await signer?.getAddress());
// any evm deployment flow

// get deployment info for any evm
const deploymentInfo = await getDeploymentInfo(
latest.metadataUri,
sdk.storage,
sdk.getProvider(),
create2FactoryAddress,
);

const implementationAddress = deploymentInfo.find(i => i.type === "implementation")?.transaction
.predictedAddress as string;

// filter out already deployed contracts (data is empty)
const transactionsToSend = deploymentInfo.filter(i => i.transaction.data && i.transaction.data.length > 0);
const transactionsforDirectDeploy = transactionsToSend
.filter(i => {
return i.type !== "infra";
})
.map(i => i.transaction);
const transactionsForThrowawayDeployer = transactionsToSend
.filter(i => {
return i.type === "infra";
})
.map(i => i.transaction);

// deploy via throwaway deployer, multiple infra contracts in one transaction
if (transactionsForThrowawayDeployer.length > 0) {
console.log("-- Deploying Infra");
await deployWithThrowawayDeployer(signer, transactionsForThrowawayDeployer, {});
}

const resolvedImplementationAddress = await resolveAddress(implementationAddress);

console.log(`-- Deploying ${publishedContractName} at ${resolvedImplementationAddress}`);
// send each transaction directly to Create2 factory
await Promise.all(
transactionsforDirectDeploy.map(tx => {
return deployContractDeterministic(signer, tx, {});
}),
);
console.log();
} catch (e) {
console.log("Error while deploying: ", e);
console.log();
continue;
}
} else {
console.log("No previous release found");
return;
}
}

console.log("Deployments done.");
console.log();

console.log("---------- Verification ---------");
console.log();
for (const publishedContractName of contractsToDeploy) {
try {
await sdk.verifier.verifyThirdwebContract(
publishedContractName,
apiMap[parseInt(chainId)],
chainIdApiKey[parseInt(chainId)] as string,
);
console.log();
} catch (error) {
console.log(error);
console.log();
}
}

console.log("All done.");
}

main()
.then(() => process.exit(0))
.catch(e => {
console.error(e);
process.exit(1);
});
35 changes: 35 additions & 0 deletions scripts/deploy-prebuilt-deterministic/bootstrap-verify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ThirdwebSDK } from "@thirdweb-dev/sdk";

import { apiMap, chainIdApiKey, contractsToDeploy } from "./constants";

////// To run this script: `npx ts-node scripts/deploy-prebuilt-deterministic/bootstrap-verify.ts` //////
const chainId = "84531"; // update here

async function main() {
console.log("---------- Verification ---------");
console.log();

const sdk = new ThirdwebSDK(chainId);
for (const publishedContractName of contractsToDeploy) {
try {
await sdk.verifier.verifyThirdwebContract(
publishedContractName,
apiMap[parseInt(chainId)],
chainIdApiKey[parseInt(chainId)] as string,
);
console.log();
} catch (error) {
console.log(error);
console.log();
}
}

console.log("All done.");
}

main()
.then(() => process.exit(0))
.catch(e => {
console.error(e);
process.exit(1);
});
21 changes: 21 additions & 0 deletions scripts/deploy-prebuilt-deterministic/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const chainIdToName: Record<number, string> = {
[ChainId.AvalancheFujiTestnet]: "avalanche-testnet",
[ChainId.BinanceSmartChainMainnet]: "binance",
[ChainId.BinanceSmartChainTestnet]: "binance-testnet",
[84531]: "base-goerli",
[8453]: "base",
};

export const chainIdApiKey: Record<number, string | undefined> = {
Expand All @@ -35,6 +37,8 @@ export const chainIdApiKey: Record<number, string | undefined> = {
[ChainId.AvalancheFujiTestnet]: process.env.SNOWTRACE_API_KEY || process.env.SCAN_API_KEY,
[ChainId.BinanceSmartChainMainnet]: process.env.BINANCE_SCAN_API_KEY || process.env.SCAN_API_KEY,
[ChainId.BinanceSmartChainTestnet]: process.env.BINANCE_SCAN_API_KEY || process.env.SCAN_API_KEY,
[84531]: "" as string,
[8453]: "" as string,
};

export const apiMap: Record<number, string> = {
Expand All @@ -52,4 +56,21 @@ export const apiMap: Record<number, string> = {
43114: "https://api.snowtrace.io/api",
421613: "https://api-goerli.arbiscan.io/api",
80001: "https://api-testnet.polygonscan.com/api",
84531: "https://api-goerli.basescan.org/api",
8453: "https://api.basescan.org/api",
};

export const contractsToDeploy = [
"DropERC721",
"DropERC1155",
"DropERC20",
"TokenERC20",
"TokenERC721",
"TokenERC1155",
"MarketplaceV3",
"Split",
"VoteERC20",
"NFTStake",
"TokenStake",
"EditionStake",
];
2 changes: 1 addition & 1 deletion scripts/deploy-prebuilt-deterministic/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ThirdwebSDK } from "@thirdweb-dev/sdk";

import { apiMap, chainIdApiKey, chainIdToName } from "./constants";

////// To run this script: `npx ts-node scripts/deploy-new-flow/verify.ts` //////
////// To run this script: `npx ts-node scripts/deploy-prebuilt-deterministic/verify.ts` //////
const deployedContractName = "VoteERC20";

async function main() {
Expand Down
Loading

0 comments on commit 0549e98

Please sign in to comment.