Skip to content

Commit 3c3f6dc

Browse files
committed
refactor: Replace if-else with case statement for operation handling in transaction preparation
1 parent f0b098a commit 3c3f6dc

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

.github/workflows/manage-contract-roles-safe.yml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,20 @@ jobs:
7979
ROLE_HASH=$(cast keccak "$ROLE_NAME")
8080
8181
# Determine the function selector and encode calldata
82-
if [ "${{ inputs.operation }}" = "grant" ]; then
83-
TRANSACTION_DATA=$(cast calldata "grantRole(bytes32,address)" "$ROLE_HASH" "${{ inputs.target_address }}")
84-
FUNCTION_NAME="grantRole(bytes32,address)"
85-
else
86-
TRANSACTION_DATA=$(cast calldata "revokeRole(bytes32,address)" "$ROLE_HASH" "${{ inputs.target_address }}")
87-
FUNCTION_NAME="revokeRole(bytes32,address)"
88-
fi
82+
case "${{ inputs.operation }}" in
83+
grant)
84+
TRANSACTION_DATA=$(cast calldata "grantRole(bytes32,address)" "$ROLE_HASH" "${{ inputs.target_address }}")
85+
FUNCTION_NAME="grantRole(bytes32,address)"
86+
;;
87+
revoke)
88+
TRANSACTION_DATA=$(cast calldata "revokeRole(bytes32,address)" "$ROLE_HASH" "${{ inputs.target_address }}")
89+
FUNCTION_NAME="revokeRole(bytes32,address)"
90+
;;
91+
*)
92+
echo "❌ Error: Unknown operation ${{ inputs.operation }}"
93+
exit 1
94+
;;
95+
esac
8996
9097
# Prepare transactions array based on role type
9198
TRANSACTIONS='[]'

.github/workflows/transfer-admin-role-safe.yml

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,38 @@ jobs:
7373
7474
# Prepare transactions array for both contracts
7575
TRANSACTIONS='[]'
76-
77-
if [ "${{ inputs.operation }}" = "begin-transfer" ]; then
78-
# beginDefaultAdminTransfer(address newAdmin)
79-
TRANSACTION_DATA=$(cast calldata "beginDefaultAdminTransfer(address)" "${{ inputs.new_admin_address }}")
80-
FUNCTION_NAME="beginDefaultAdminTransfer(address)"
81-
82-
# Create transaction for token contract
83-
TRANSACTIONS=$(echo "$TRANSACTIONS" | jq -c --arg to "$TOKEN_CONTRACT" --arg data "$TRANSACTION_DATA" \
84-
'. += [{"to": $to, "data": $data}]')
85-
86-
# Create transaction for bridge contract
87-
TRANSACTIONS=$(echo "$TRANSACTIONS" | jq -c --arg to "$BRIDGE_ADDRESS" --arg data "$TRANSACTION_DATA" \
88-
'. += [{"to": $to, "data": $data}]')
89-
else
90-
# acceptDefaultAdminTransfer()
91-
TRANSACTION_DATA=$(cast calldata "acceptDefaultAdminTransfer()")
92-
FUNCTION_NAME="acceptDefaultAdminTransfer()"
93-
94-
# Create transaction for token contract
95-
TRANSACTIONS=$(echo "$TRANSACTIONS" | jq -c --arg to "$TOKEN_CONTRACT" --arg data "$TRANSACTION_DATA" \
96-
'. += [{"to": $to, "data": $data}]')
97-
98-
# Create transaction for bridge contract
99-
TRANSACTIONS=$(echo "$TRANSACTIONS" | jq -c --arg to "$BRIDGE_ADDRESS" --arg data "$TRANSACTION_DATA" \
100-
'. += [{"to": $to, "data": $data}]')
101-
fi
76+
case "${{ inputs.operation }}" in
77+
begin-transfer)
78+
# beginDefaultAdminTransfer(address newAdmin)
79+
TRANSACTION_DATA=$(cast calldata "beginDefaultAdminTransfer(address)" "${{ inputs.new_admin_address }}")
80+
FUNCTION_NAME="beginDefaultAdminTransfer(address)"
81+
82+
# Create transaction for token contract
83+
TRANSACTIONS=$(echo "$TRANSACTIONS" | jq -c --arg to "$TOKEN_CONTRACT" --arg data "$TRANSACTION_DATA" \
84+
'. += [{"to": $to, "data": $data}]')
85+
86+
# Create transaction for bridge contract
87+
TRANSACTIONS=$(echo "$TRANSACTIONS" | jq -c --arg to "$BRIDGE_ADDRESS" --arg data "$TRANSACTION_DATA" \
88+
'. += [{"to": $to, "data": $data}]')
89+
;;
90+
accept-transfer)
91+
# acceptDefaultAdminTransfer()
92+
TRANSACTION_DATA=$(cast calldata "acceptDefaultAdminTransfer()")
93+
FUNCTION_NAME="acceptDefaultAdminTransfer()"
94+
95+
# Create transaction for token contract
96+
TRANSACTIONS=$(echo "$TRANSACTIONS" | jq -c --arg to "$TOKEN_CONTRACT" --arg data "$TRANSACTION_DATA" \
97+
'. += [{"to": $to, "data": $data}]')
98+
99+
# Create transaction for bridge contract
100+
TRANSACTIONS=$(echo "$TRANSACTIONS" | jq -c --arg to "$BRIDGE_ADDRESS" --arg data "$TRANSACTION_DATA" \
101+
'. += [{"to": $to, "data": $data}]')
102+
;;
103+
*)
104+
echo "❌ Error: Unknown operation ${{ inputs.operation }}"
105+
exit 1
106+
;;
107+
esac
102108
103109
echo "transactions=$(echo $TRANSACTIONS | jq -c .)" >> $GITHUB_OUTPUT
104110
echo "safe-address=${{ secrets.SAFE_ADDRESS }}" >> $GITHUB_OUTPUT

0 commit comments

Comments
 (0)