-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from ethereum-optimism/nick/broadcast-artifacts
Generate Broadcast Artifacts
- Loading branch information
Showing
3 changed files
with
290 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Address, Hash, Transaction, TransactionReceipt } from "viem"; | ||
import { create } from "zustand"; | ||
|
||
export type CreateBroadcastParams = { | ||
contractName: string; | ||
contractAddress: Address; | ||
contractArguments?: any[]; | ||
foundryProjectRoot: string; | ||
} | ||
|
||
export type AddTransactionParams = { | ||
contractAddress: Address; | ||
chainId: number; | ||
hash: Hash; | ||
transaction: Transaction; | ||
receipt: TransactionReceipt; | ||
} | ||
|
||
export type MultichainBroadcast = { | ||
name: string; | ||
address: Address; | ||
timestamp: number; | ||
type: 'CREATE2'; | ||
contractArguments: any[]; | ||
foundryProjectRoot: string; | ||
transactions: Record<number, { | ||
chainId: number; | ||
hash: Hash; | ||
transaction: Transaction; | ||
receipt: TransactionReceipt; | ||
}> | ||
} | ||
|
||
export type BroadcastStore = { | ||
broadcasts: Record<Address, MultichainBroadcast>; | ||
createBroadcast: (params: CreateBroadcastParams) => void; | ||
addTransaction: (params: AddTransactionParams) => void; | ||
} | ||
|
||
export const useBroadcastStore = create<BroadcastStore>()( | ||
set => ({ | ||
broadcasts: {}, | ||
createBroadcast: (params: CreateBroadcastParams) => { | ||
const {contractName, contractAddress, contractArguments, foundryProjectRoot} = params; | ||
|
||
const timeInSecs = Math.floor(Date.now() / 1000); | ||
|
||
const broadcast = { | ||
name: contractName, | ||
address: contractAddress, | ||
timestamp: timeInSecs, | ||
transactions: {}, | ||
type: 'CREATE2', | ||
contractArguments, | ||
foundryProjectRoot, | ||
} | ||
|
||
set(state => ({ | ||
...state, | ||
broadcasts: { | ||
...state.broadcasts, | ||
[contractAddress]: broadcast, | ||
}, | ||
})); | ||
}, | ||
addTransaction: (params: AddTransactionParams) => { | ||
const { contractAddress, chainId, hash, transaction, receipt } = params; | ||
|
||
set(state => { | ||
const broadcast = state.broadcasts[contractAddress]; | ||
|
||
if (!broadcast) { | ||
return state; | ||
} | ||
|
||
broadcast.transactions[chainId] = { | ||
chainId, | ||
hash, | ||
transaction, | ||
receipt, | ||
} | ||
|
||
return state | ||
}) | ||
} | ||
}) | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { MultichainBroadcast } from "@/stores/broadcastStore"; | ||
import { Address, Hash, Hex, numberToHex, TransactionReceipt } from "viem"; | ||
|
||
export type TransactionBroadcast = { | ||
hash: Hash | ||
transactionType: string | ||
contractName: string | ||
contractAddress: Address | ||
function: string | null | ||
arguments: any[] | ||
transaction: { | ||
from: Address | ||
to: Address | ||
value: Hex; | ||
input: Hex; | ||
nonce: Hex; | ||
gas: Hex; | ||
chainId: Hex; | ||
}; | ||
additionalContracts: any[]; | ||
isFixedGasLimit: boolean; | ||
} | ||
|
||
export type Deployment = { | ||
transactions: TransactionBroadcast[]; | ||
receipts: TransactionReceipt[]; | ||
timestamp: number; | ||
chain: number; | ||
} | ||
|
||
export type RunArtifact = { | ||
deployments: Deployment[]; | ||
timestamp: number; | ||
} | ||
|
||
const DEFAULT_OUTPUT_DIR = path.join('broadcast', 'multi'); | ||
|
||
export function writeMultichainBroadcast(multichainBroadcast: MultichainBroadcast) { | ||
const { transactions, address, name, timestamp, type, contractArguments, foundryProjectRoot } = multichainBroadcast; | ||
|
||
if (!Object.keys(transactions).length) { | ||
return; | ||
} | ||
|
||
const outputDir = path.join(foundryProjectRoot, DEFAULT_OUTPUT_DIR); | ||
|
||
const runArtifact = { | ||
deployments: [], | ||
timestamp, | ||
} as RunArtifact | ||
|
||
Object.keys(transactions).forEach(chainIdStr => { | ||
const chainId = Number(chainIdStr); | ||
|
||
if (!transactions[chainId]) { | ||
return | ||
} | ||
|
||
const { hash, transaction, receipt } = transactions[chainId]; | ||
|
||
const transactionData = { | ||
hash, | ||
transactionType: type, | ||
contractName: name, | ||
contractAddress: address, | ||
function: null, | ||
arguments: contractArguments, | ||
transaction: { | ||
from: receipt.from, | ||
to: receipt.to, | ||
gas: transaction.gas, | ||
value: transaction.value, | ||
input: transaction.input, | ||
nonce: numberToHex(transaction.nonce), | ||
chainId: numberToHex(chainId), | ||
additionalContracts: [], | ||
isFixedGasLimit: false, | ||
} | ||
} as unknown as TransactionBroadcast | ||
|
||
const receiptData = { | ||
status: receipt.status === 'success' ? '0x1' : '0x0', | ||
cumulativeGasUsed: receipt.cumulativeGasUsed, | ||
logs: receipt.logs, | ||
logsBloom: receipt.logsBloom, | ||
type: transaction.typeHex, | ||
transactionHash: receipt.transactionHash, | ||
transactionIndex: numberToHex(receipt.transactionIndex), | ||
blockHash: receipt.blockHash, | ||
blockNumber: receipt.blockNumber, | ||
gasUsed: receipt.gasUsed, | ||
effectiveGasPrice: receipt.effectiveGasPrice, | ||
blobGasPrice: receipt.blobGasPrice, | ||
from: receipt.from, | ||
to: receipt.to, | ||
contractAddress: receipt.contractAddress, | ||
} as unknown as TransactionReceipt | ||
|
||
const deployment: Deployment = { | ||
transactions: [transactionData], | ||
receipts: [receiptData], | ||
timestamp, | ||
chain: chainId, | ||
} | ||
|
||
runArtifact.deployments.push(deployment); | ||
}) | ||
|
||
const latestDirname = path.join(outputDir, `${name}-latest`); | ||
const timestampDirname = path.join(outputDir, `${name}-${timestamp}`); | ||
fs.mkdirSync(latestDirname, { recursive: true }); | ||
fs.mkdirSync(timestampDirname, { recursive: true }); | ||
|
||
const fileContents = JSON.stringify( | ||
runArtifact, | ||
(_, val) => typeof val === 'bigint' ? numberToHex(val) : val, | ||
2, | ||
); | ||
|
||
fs.writeFileSync(path.join(latestDirname, 'run.json'), fileContents); | ||
fs.writeFileSync(path.join(timestampDirname, 'run.json'), fileContents); | ||
} |