Created
September 4, 2022 16:58
-
-
Save pythonpete32/91e98d812110a564d29a863503218e24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---------------------------------------------------------------------------- // | |
// GNOSIS SAFE DEPLOYMENT SCRIPT // | |
// ---------------------------------------------------------------------------- // | |
import { ethers } from 'ethers' | |
import EthersAdapter from '@gnosis.pm/safe-ethers-lib' | |
import SafeServiceClient from '@gnosis.pm/safe-service-client' | |
import Safe, { SafeFactory, EthSignSignature } from '@gnosis.pm/safe-core-sdk' | |
import * as dotenv from 'dotenv' | |
dotenv.config() | |
// 0. variables | |
const txServiceUrl = 'https://safe-transaction.goerli.gnosis.io/' | |
const NFT_ADDRESS = '0xc33Cfd8A8323b5b3Be74723A3E776EA9B7C05Ba1' | |
const mintFunction = 'function mintTo(address) public payable returns (uint256)' | |
const { log } = console | |
// 1. create the Wallet that will deploy the safe | |
const provider = new ethers.providers.JsonRpcProvider(process.env.GOERLI_RPC_URL) | |
const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider) | |
// 2. create an ethers adapter for the SafeServiceClient | |
const ethAdapter = new EthersAdapter['default']({ | |
ethers, | |
signer | |
}) | |
// 3. create a SafeFactory | |
const safeFactory = await SafeFactory.create({ ethAdapter }) | |
// 4. create a Safe, returns a SDK object | |
const safeAccountConfig = { | |
owners: [signer.address], | |
threshold: 1, | |
} | |
const safeSdk = await safeFactory.deploySafe({ safeAccountConfig }) | |
log('Safe address:', safeSdk.getAddress()) | |
// 5. create a SafeServiceClient | |
const safeService = new SafeServiceClient['default']({ txServiceUrl, ethAdapter }) | |
// 6. create a SafeTransaction | |
const nft = new ethers.Contract(NFT_ADDRESS, [mintFunction], signer) | |
const mintData = nft.interface.encodeFunctionData('mintTo', [signer.address]) | |
const safeTransactionData = { | |
to: NFT_ADDRESS, | |
value: 0, | |
data: mintData, | |
} | |
const safeTransaction = await safeSdk.createTransaction({ safeTransactionData }) | |
const safeTransactionHash = await safeSdk.getTransactionHash(safeTransaction) | |
log('Safe transaction:', safeTransaction) | |
log('Safe transaction hash:', safeTransactionHash) | |
// 7. propose a transaction to the relayer | |
const senderSignature = await safeSdk.signTransactionHash(safeTransactionHash) | |
await safeService.proposeTransaction({ | |
safeAddress: safeSdk.getAddress(), | |
safeTransactionData: safeTransaction.data, | |
safeTxHash: safeTransactionHash, | |
senderAddress: signer.address, | |
senderSignature: senderSignature.data, | |
}) | |
// 8. get pending transactions from the relayer | |
// const pendingTxs = await safeService.getPendingTransactions(safeSdk.getAddress()) | |
const tx = await safeService.getTransaction(safeTransactionHash) | |
log('Pending transaction:', tx) | |
// 9. confirm the transaction | |
await safeService.confirmTransaction(safeTransactionHash, senderSignature.data) | |
// 10. send the transaction status | |
// if more than one owner | |
// const executeTransactionData = { | |
// to: tx.to, | |
// value: tx.value, | |
// data: tx.data, | |
// operation: tx.operation, | |
// safeTxGas: tx.safeTxGas, | |
// baseGas: gas, | |
// gasPrice: feeData.gasPrice, | |
// gasToken: tx.gasToken, | |
// refundReceiver: tx.refundReceiver, | |
// nonce: tx.nonce | |
// } | |
// const executeSafeTransaction = await safeSdk.createTransaction({ executeTransactionData }) | |
// tx.confirmations.forEach(confirmation => { | |
// const signature = new EthSignSignature(confirmation.owner, confirmation.signature) | |
// safeTransaction.addSignature(signature) | |
// }) | |
const executeTxResponse = await safeSdk.executeTransaction(safeTransaction) | |
const receipt = executeTxResponse.transactionResponse && (await executeTxResponse.transactionResponse.wait()) | |
log("receipt", receipt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment