|
| 1 | +import { ethers } from 'hardhat'; |
| 2 | +import SafeApiKit from '@safe-global/api-kit'; |
| 3 | +import Safe from '@safe-global/protocol-kit'; |
| 4 | +import { OperationType } from '@safe-global/types-kit'; |
| 5 | +import { deployViaCreateX } from './utils/deployViaCreateX'; |
| 6 | +import { getTransactionReceipt, stripAddressPadding } from './utils/helpers'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Deployment script |
| 10 | + */ |
| 11 | +async function deploy() { |
| 12 | + const [signer] = await ethers.getSigners(); |
| 13 | + |
| 14 | + const CHAIN_ID = BigInt(process.env.CHAIN_ID as string); |
| 15 | + const CHAIN_RPC_URL = process.env.CHAIN_RPC_URL as string; |
| 16 | + const PRIVATE_KEY = process.env.PRIVATE_KEY as string; |
| 17 | + const SAFE_ADDRESS = process.env.SAFE_ADDRESS as string; |
| 18 | + const SAFE_API_KEY = process.env.SAFE_API_KEY as string; |
| 19 | + |
| 20 | + const protocolKit = await Safe.init({ |
| 21 | + provider: CHAIN_RPC_URL, |
| 22 | + signer: PRIVATE_KEY, |
| 23 | + safeAddress: SAFE_ADDRESS |
| 24 | + }); |
| 25 | + const apiKit = new SafeApiKit({ |
| 26 | + chainId: CHAIN_ID, |
| 27 | + apiKey: SAFE_API_KEY |
| 28 | + }); |
| 29 | + |
| 30 | + const trx = await deployViaCreateX(); |
| 31 | + const safeTransaction = await protocolKit.createTransaction({ |
| 32 | + transactions: [{ ...trx, value: '0', operation: OperationType.Call }] |
| 33 | + }); |
| 34 | + const safeTxHash = await protocolKit.getTransactionHash(safeTransaction); |
| 35 | + const signature = await protocolKit.signHash(safeTxHash); |
| 36 | + |
| 37 | + await apiKit.proposeTransaction({ |
| 38 | + safeAddress: SAFE_ADDRESS, |
| 39 | + safeTransactionData: safeTransaction.data, |
| 40 | + safeTxHash, |
| 41 | + senderAddress: signer.address, |
| 42 | + senderSignature: signature.data |
| 43 | + }); |
| 44 | + |
| 45 | + const threshold = await protocolKit.getThreshold(); |
| 46 | + const confirmations = await apiKit.getTransactionConfirmations(safeTxHash); |
| 47 | + console.log(`Current signatures: ${confirmations.count}, Required: ${threshold}`); |
| 48 | + |
| 49 | + if (confirmations.count >= threshold) { |
| 50 | + const safeTransaction = await apiKit.getTransaction(safeTxHash); |
| 51 | + const { hash } = await protocolKit.executeTransaction(safeTransaction); |
| 52 | + const receipt = await getTransactionReceipt(hash); |
| 53 | + const createX = receipt?.logs.find((log: any) => log.address.toLowerCase() == trx.to.toLowerCase()); |
| 54 | + const deployedAt = createX?.topics[1]; |
| 55 | + if (deployedAt) { |
| 56 | + console.log('Transaction hash: ', hash); |
| 57 | + console.log('Contract successfully deployed at: ', stripAddressPadding(deployedAt)); |
| 58 | + } |
| 59 | + } else { |
| 60 | + console.log(`Waiting for atleast ${threshold - confirmations.count} more signature(s) before execution`); |
| 61 | + console.log(`Share this transaction hash with other Safe owners: ${safeTxHash}`); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +deploy() |
| 66 | + .then(() => process.exit(0)) |
| 67 | + .catch((error) => { |
| 68 | + console.error('Deployment failed:'); |
| 69 | + console.error(error); |
| 70 | + process.exit(1); |
| 71 | + }); |
0 commit comments