Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diamond cut auto propose to safe #764

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updates script to automatically propose diamondCut to safe when deplo…
…ying to PROD
  • Loading branch information
0xDEnYO committed Aug 15, 2024
commit f06be97026f7cb711951382b899aef39d07ab69a
14 changes: 8 additions & 6 deletions script/deploy/deploySingleContract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ deploySingleContract() {

# read function arguments into variables
local CONTRACT="$1"
NETWORK="$2"
ENVIRONMENT="$3"
VERSION="$4"
EXIT_ON_ERROR="$5"
DIAMOND_TYPE="$6" # optional parameter (only used by CelerIMFacet)
local NETWORK="$2"
local ENVIRONMENT="$3"
local VERSION="$4"
local EXIT_ON_ERROR="$5"
local DIAMOND_TYPE="$6" # optional parameter (only used by CelerIMFacet)

# load env variables
source .env
Expand Down Expand Up @@ -93,7 +93,7 @@ deploySingleContract() {
if [[ -z "$CONTRACT" ]]; then
# get user-selected deploy script and contract from list
SCRIPT=$(ls -1 "$DEPLOY_SCRIPT_DIRECTORY" | sed -e 's/\.s.sol$//' | grep 'Deploy' | gum filter --placeholder "Deploy Script")
CONTRACT=$(echo $SCRIPT | sed -e 's/Deploy//')
local CONTRACT=$(echo $SCRIPT | sed -e 's/Deploy//')
else
SCRIPT="Deploy"$CONTRACT
fi
Expand Down Expand Up @@ -250,8 +250,10 @@ deploySingleContract() {

# end this script according to flag
if [[ -z "$EXIT_ON_ERROR" ]]; then
echo "return 1"
return 1
else
echo "exit 1"
exit 1
fi
fi
Expand Down
17 changes: 6 additions & 11 deletions script/deploy/safe/propose-to-safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import {
type SafeTransactionDataPartial,
} from '@safe-global/safe-core-sdk-types'
import * as chains from 'viem/chains'
import {
chainNameMappings,
getSafeUtilityContracts,
safeAddresses,
safeApiUrls,
} from './config'
import { getViemChainForNetworkName } from '../../../utils/network'
import { getSafeUtilityContracts, safeAddresses, safeApiUrls } from './config'

const chainMap: Record<string, Chain> = {}
for (const [k, v] of Object.entries(chains)) {
Expand Down Expand Up @@ -54,17 +50,16 @@ const main = defineCommand({
},
},
async run({ args }) {
const chainName = chainNameMappings[args.network] || args.network
const chain: Chain = chainMap[chainName]
const chain = getViemChainForNetworkName(args.network)

const config: SafeApiKitConfig = {
chainId: BigInt(chain.id),
txServiceUrl: safeApiUrls[chainName.toLowerCase()],
txServiceUrl: safeApiUrls[args.network.toLowerCase()],
}

const safeService = new SafeApiKit(config)

const safeAddress = safeAddresses[chainName.toLowerCase()]
const safeAddress = safeAddresses[args.network.toLowerCase()]

const rpcUrl = args.rpcUrl || chain.rpcUrls.default.http[0]
const provider = new ethers.JsonRpcProvider(rpcUrl)
Expand Down Expand Up @@ -100,7 +95,7 @@ const main = defineCommand({

console.info('Signer Address', senderAddress)
console.info('Safe Address', safeAddress)
console.info('Network', chainName)
console.info('Network', chain.name)
console.info('Proosing transaction to', args.to)

// Propose transaction to the service
Expand Down
51 changes: 43 additions & 8 deletions script/tasks/diamondUpdateFacet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,47 @@ diamondUpdateFacet() {
attempts=1
while [ $attempts -le "$MAX_ATTEMPTS_PER_SCRIPT_EXECUTION" ]; do
echo "[info] trying to execute $SCRIPT on $DIAMOND_CONTRACT_NAME now - attempt ${attempts} (max attempts:$MAX_ATTEMPTS_PER_SCRIPT_EXECUTION)"
# try to execute call
# check if command output should be printed to console
if [[ "$DEBUG" == *"true"* ]]; then
# print output to console
RAW_RETURN_DATA=$(NETWORK=$NETWORK FILE_SUFFIX=$FILE_SUFFIX USE_DEF_DIAMOND=$USE_MUTABLE_DIAMOND NO_BROADCAST=false PRIVATE_KEY=$(getPrivateKey "$NETWORK" "$ENVIRONMENT") forge script "$SCRIPT_PATH" -f $NETWORK -vvvv --json --silent --broadcast --skip-simulation --legacy)
# check if we are deploying to PROD
if [[ "$ENVIRONMENT" == "production" ]]; then
# PROD: suggest diamondCut transaction to SAFE
UPDATE_SCRIPT=$(echo "$DEPLOY_SCRIPT_DIRECTORY""$SCRIPT".s.sol)
PRIVATE_KEY=$(getPrivateKey $NETWORK $ENVIRONMENT)
echoDebug "Calculating facet cuts for $SCRIPT..."
RAW_RETURN_DATA=$(NO_BROADCAST=true NETWORK=$NETWORK FILE_SUFFIX=$FILE_SUFFIX USE_DEF_DIAMOND=$USE_MUTABLE_DIAMOND PRIVATE_KEY=$PRIVATE_KEY forge script "$UPDATE_SCRIPT" -f $NETWORK -vvvv --json --silent --skip-simulation --legacy)
CLEAN_RETURN_DATA=$(echo $RAW_RETURN_DATA | sed 's/^.*{\"logs/{\"logs/')
FACET_CUT=$(echo $CLEAN_RETURN_DATA | jq -r '.returns.cutData.value')

if [ "$FACET_CUT" != "0x" ]; then
echoDebug "Proposing facet cut for $script..."
DIAMOND_ADDRESS=$(getContractAddressFromDeploymentLogs "$NETWORK" "$ENVIRONMENT" "$DIAMOND_CONTRACT_NAME")
ts-node script/deploy/safe/propose-to-safe.ts --to "$DIAMOND_ADDRESS" --calldata "$FACET_CUT" --network "$NETWORK" --rpcUrl $(getRPCUrl $NETWORK) --privateKey "$SAFE_SIGNER_PRIVATE_KEY"
fi
else
# STAGING: just deploy normally without further checks
RAW_RETURN_DATA=$(NETWORK=$NETWORK FILE_SUFFIX=$FILE_SUFFIX USE_DEF_DIAMOND=$USE_MUTABLE_DIAMOND NO_BROADCAST=false PRIVATE_KEY=$(getPrivateKey "$NETWORK" "$ENVIRONMENT") forge script "$SCRIPT_PATH" -f $NETWORK -vvvv --json --silent --broadcast --skip-simulation --legacy)
fi
else
# do not print output to console
RAW_RETURN_DATA=$(NETWORK=$NETWORK FILE_SUFFIX=$FILE_SUFFIX USE_DEF_DIAMOND=$USE_MUTABLE_DIAMOND NO_BROADCAST=false PRIVATE_KEY=$(getPrivateKey "$NETWORK" "$ENVIRONMENT") forge script "$SCRIPT_PATH" -f $NETWORK -vvvv --json --silent --broadcast --skip-simulation --legacy) 2>/dev/null
fi
# check if we are deploying to PROD
if [[ "$ENVIRONMENT" == "production" ]]; then
# PROD: suggest diamondCut transaction to SAFE
UPDATE_SCRIPT=$(echo "$DEPLOY_SCRIPT_DIRECTORY"Update"$SCRIPT".s.sol)
PRIVATE_KEY=$(getPrivateKey $NETWORK $ENVIRONMENT)
echoDebug "Calculating facet cuts for $script..."
RAW_RETURN_DATA=$(NO_BROADCAST=true NETWORK=$NETWORK FILE_SUFFIX=$FILE_SUFFIX USE_DEF_DIAMOND=$USE_MUTABLE_DIAMOND PRIVATE_KEY=$PRIVATE_KEY forge script "$UPDATE_SCRIPT" -f $NETWORK -vvvv --json --silent --skip-simulation --legacy)
CLEAN_RETURN_DATA=$(echo $RAW_RETURN_DATA | sed 's/^.*{\"logs/{\"logs/')
FACET_CUT=$(echo $CLEAN_RETURN_DATA | jq -r '.returns.cutData.value')
if [ "$FACET_CUT" != "0x" ]; then
echo "Proposing facet cut for $script on network $NETWROK..."
DIAMOND_ADDRESS=$(getContractAddressFromDeploymentLogs "$NETWORK" "$ENVIRONMENT" "$DIAMOND_CONTRACT_NAME")
ts-node script/deploy/safe/propose-to-safe.ts --to "$DIAMOND_ADDRESS" --calldata "$FACET_CUT" --network "$NETWORK" --rpcUrl $(getRPCUrl $NETWORK) --privateKey "$SAFE_SIGNER_PRIVATE_KEY"
fi
else
# STAGING: just deploy normally without further checks
RAW_RETURN_DATA=$(NETWORK=$NETWORK FILE_SUFFIX=$FILE_SUFFIX USE_DEF_DIAMOND=$USE_MUTABLE_DIAMOND NO_BROADCAST=false PRIVATE_KEY=$(getPrivateKey "$NETWORK" "$ENVIRONMENT") forge script "$SCRIPT_PATH" -f $NETWORK -vvvv --json --silent --broadcast --skip-simulation --legacy)
fi
fi
RETURN_CODE=$?
echoDebug "RAW_RETURN_DATA: $RAW_RETURN_DATA"

Expand Down Expand Up @@ -142,8 +175,10 @@ diamondUpdateFacet() {
return 1
fi

# save facet addresses
saveDiamondFacets "$NETWORK" "$ENVIRONMENT" "$USE_MUTABLE_DIAMOND" "$FACETS"
# save facet addresses (only if deploying to PROD, otherwise we update the logs before the diamondCut tx gets signed in the SAFE)
if [[ "$ENVIRONMENT" != "production" ]]; then
saveDiamondFacets "$NETWORK" "$ENVIRONMENT" "$USE_MUTABLE_DIAMOND" "$FACETS"
fi

echo "[info] $SCRIPT successfully executed on network $NETWORK in $ENVIRONMENT environment"
return 0
Expand Down
Loading