-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis-output
executable file
·90 lines (73 loc) · 3.33 KB
/
genesis-output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env zsh
# Get the absolute path of the script's directory
SCRIPT_DIR="${0:A:h}"
ALL_ALLOCATIONS_FILE="${SCRIPT_DIR}/all_allocations.json"
rm -Rf "${ALL_ALLOCATIONS_FILE}"
# List of contract names and their target addresses
typeset -A CONTRACT_ADDRESSES
################################################################
# NEVER EVER EVER EVER CHANGE THE ADDRESSES IN THIS LIST!!!!!!
################################################################
CONTRACT_ADDRESSES=(
"StarterKitERC20Registry" "0x5e771e1417100000000000000000000000000001"
"PresetStarterKitERC20Factory" "0x5e771e1417100000000000000000000000000002"
"StarterKitERC20DexFactory" "0x5e771e1417100000000000000000000000000003"
)
# Initialize an empty JSON object for all allocations
echo "{}" > "${ALL_ALLOCATIONS_FILE}"
# Function to process a single Solidity file
process_sol_file() {
local sol_file="$1"
local contract_name="${sol_file:t:r}"
local target_address="${CONTRACT_ADDRESSES[$contract_name]}"
# Skip if the contract is not in the CONTRACT_ADDRESSES list
if [[ -z "$target_address" ]]; then
echo "Skipping $contract_name: Not in CONTRACT_ADDRESSES list"
return
fi
# Deploy the contract to a temporary blockchain
local DEPLOYED_ADDRESS=$(forge create "${sol_file}:${contract_name}" --unlocked --from "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" --json | jq -r .deployedTo)
if [[ -z "$DEPLOYED_ADDRESS" ]]; then
echo "Error: Unable to deploy $contract_name"
return
fi
# Get storage layout
local STORAGE_LAYOUT=$(forge inspect "${sol_file}:${contract_name}" storage-layout)
if [[ -z "$STORAGE_LAYOUT" ]]; then
echo "Error: Unable to get storage layout for $contract_name"
return
fi
# Initialize an empty JSON object for storage
local STORAGE_JSON="{}"
# Read storage slots
echo "$STORAGE_LAYOUT" | jq -c '.storage[]' | while read -r slot; do
local SLOT_NUMBER=$(echo "$slot" | jq -r .slot)
local SLOT_VALUE=$(cast storage "$DEPLOYED_ADDRESS" "$SLOT_NUMBER")
STORAGE_JSON=$(echo "$STORAGE_JSON" | jq --arg slot "0x000000000000000000000000000000000000000000000000000000000000000$SLOT_NUMBER" --arg value "$SLOT_VALUE" '. + {($slot): $value}')
done
# Get bytecode from the deployed contract
local BYTECODE=$(cast code "$DEPLOYED_ADDRESS" | sed 's/^0x//')
if [[ -z "$BYTECODE" ]]; then
echo "Error: Unable to get bytecode for deployed $contract_name"
return
fi
# Use jq to add the contract allocation to the all_allocations.json file
if ! jq --arg address "$target_address" \
--arg bytecode "$BYTECODE" \
--argjson storage "$STORAGE_JSON" \
'. + {($address): {
balance: "0x0",
code: ("0x" + $bytecode),
storage: $storage
}}' "${ALL_ALLOCATIONS_FILE}" > "${SCRIPT_DIR}/temp.json"; then
echo "Error: jq command failed for $contract_name"
return
fi
mv "${SCRIPT_DIR}/temp.json" "${ALL_ALLOCATIONS_FILE}"
echo "Added genesis configuration for $contract_name to all_allocations.json"
}
# Find all .sol files in the contracts subfolder of solidity-* top level folders and process them
for sol_file in contracts/*.sol(.); do
process_sol_file "$sol_file"
done
echo "Complete genesis allocation has been written to ${ALL_ALLOCATIONS_FILE}"