Skip to content

Commit 15bc233

Browse files
authored
chore(scripts): add contract upgrade automation scripts (#996)
* chore: add upgrade scripts * chore: update commands
1 parent 03a623a commit 15bc233

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

tests/Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,29 @@ deploy-v1:
124124
@echo "$(GREEN)Deploying v1 implementations to $(ENV) environment...$(NC)"
125125
@$(MAKE) -f scripts/deploy.mk deploy-gnoswap-impl-v1 ENV=$(ENV)
126126

127+
.PHONY: deploy-version
128+
deploy-version:
129+
ifndef CONTRACT
130+
$(error CONTRACT is not set. Usage: make deploy-version CONTRACT=pool VERSION=v2)
131+
endif
132+
ifndef VERSION
133+
$(error VERSION is not set. Usage: make deploy-version CONTRACT=pool VERSION=v2)
134+
endif
135+
@scripts/patch-upgrade.sh contract/r/gnoswap/$(CONTRACT) $(VERSION)
136+
@echo "$(GREEN)Deploying $(CONTRACT) with version $(VERSION) to $(ENV) environment...$(NC)"
137+
@$(MAKE) -f scripts/deploy.mk deploy-contract-version CONTRACT=$(CONTRACT) VERSION=$(VERSION) ENV=$(ENV)
138+
139+
.PHONY: upgrade-version
140+
upgrade-version:
141+
ifndef CONTRACT
142+
$(error CONTRACT is not set. Usage: make upgrade-version CONTRACT=pool VERSION=v2)
143+
endif
144+
ifndef VERSION
145+
$(error VERSION is not set. Usage: make upgrade-version CONTRACT=pool VERSION=v2)
146+
endif
147+
@echo "$(GREEN)Upgrading $(CONTRACT) to version $(VERSION) on $(ENV) environment...$(NC)"
148+
@$(MAKE) -f scripts/deploy.mk upgrade-contract-version CONTRACT=$(CONTRACT) VERSION=$(VERSION) ENV=$(ENV)
149+
127150
# Testing targets
128151
.PHONY: test-pool
129152
test-pool:

tests/scripts/deploy.mk

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,27 @@ deploy-staker-v1:
213213
$(info ************ deploy staker-v1 ************)
214214
@echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/contract/r/gnoswap/staker/v1 -pkgpath gno.land/r/gnoswap/staker/v1 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 100000000ugnot -gas-wanted 100000000 -memo "" gnoswap_admin
215215
@echo
216+
217+
# Deploy contracts with specific version
218+
# Usage: make deploy-contract-version CONTRACT=staker VERSION=v2
219+
deploy-contract-version:
220+
ifndef CONTRACT
221+
$(error CONTRACT is not set. Usage: make deploy-contract-version CONTRACT=staker VERSION=v2)
222+
endif
223+
ifndef VERSION
224+
$(error VERSION is not set. Usage: make deploy-contract-version CONTRACT=staker VERSION=v2)
225+
endif
226+
$(info ************ deploy $(CONTRACT)-$(VERSION) ************)
227+
@echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/contract/r/gnoswap/$(CONTRACT)/$(VERSION) -pkgpath gno.land/r/gnoswap/$(CONTRACT)/$(VERSION) -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 10000000ugnot -gas-wanted 100000000 -memo "" gnoswap_admin
228+
@echo
229+
230+
upgrade-contract-version:
231+
ifndef CONTRACT
232+
$(error CONTRACT is not set. Usage: make upgrade-version CONTRACT=staker VERSION=v2)
233+
endif
234+
ifndef VERSION
235+
$(error VERSION is not set. Usage: make upgrade-version CONTRACT=staker VERSION=v2)
236+
endif
237+
$(info ************ upgrade implementation of $(CONTRACT)-$(VERSION) ************)
238+
@echo "" | gnokey maketx call -pkgpath gno.land/r/gnoswap/$(CONTRACT) -func UpgradeImpl -args "gno.land/r/gnoswap/$(CONTRACT)/$(VERSION)" -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 10000000ugnot -gas-wanted 1000000000 -memo "" gnoswap_admin
239+
@echo

tests/scripts/patch-upgrade.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/bin/bash
2+
3+
# Script to create an upgradeable contract by copying v1 to a new version
4+
# Usage: ./patch-upgrade.sh <contract_path> <version>
5+
# Example: ./patch-upgrade.sh contract/r/gnoswap/launchpad v2
6+
7+
set -e
8+
9+
# Check if required arguments are provided
10+
if [ $# -ne 2 ]; then
11+
echo "Usage: $0 <contract_path> <version>"
12+
echo "Example: $0 contract/r/gnoswap/launchpad v2"
13+
exit 1
14+
fi
15+
16+
# Get the script directory and project root
17+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
18+
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
19+
20+
# Get contract path relative to project root
21+
CONTRACT_PATH_REL=$1
22+
VERSION=$2
23+
24+
# Convert to absolute path based on project root
25+
CONTRACT_PATH="${PROJECT_ROOT}/${CONTRACT_PATH_REL}"
26+
27+
# Validate that VERSION starts with 'v' followed by a number
28+
if [[ ! $VERSION =~ ^v[0-9]+$ ]]; then
29+
echo "Error: Version must be in format 'v<number>' (e.g., v2, v3)"
30+
exit 1
31+
fi
32+
33+
# Check if source directory exists
34+
SOURCE_DIR="${CONTRACT_PATH}/v1"
35+
if [ ! -d "$SOURCE_DIR" ]; then
36+
echo "Error: Source directory '$SOURCE_DIR' does not exist"
37+
exit 1
38+
fi
39+
40+
# Set target directory
41+
TARGET_DIR="${CONTRACT_PATH}/${VERSION}"
42+
43+
# Check if target directory already exists
44+
if [ -d "$TARGET_DIR" ]; then
45+
echo "Error: Target directory '$TARGET_DIR' already exists"
46+
echo "Please remove it first or choose a different version"
47+
exit 1
48+
fi
49+
50+
echo "Creating upgradeable contract..."
51+
echo " Project Root: $PROJECT_ROOT"
52+
echo " Contract Path: $CONTRACT_PATH_REL"
53+
echo " Source: $SOURCE_DIR"
54+
echo " Target: $TARGET_DIR"
55+
echo " Version: $VERSION"
56+
echo ""
57+
58+
# Step 1: Copy v1 directory to new version directory (excluding *test.gno files)
59+
echo "[1/3] Copying v1 directory to ${VERSION} (excluding *test.gno files)..."
60+
rsync -a --exclude='*test.gno' "$SOURCE_DIR/" "$TARGET_DIR/"
61+
echo " ✓ Directory copied successfully"
62+
63+
# Step 2: Update gnomod.toml module path
64+
GNOMOD_FILE="${TARGET_DIR}/gnomod.toml"
65+
if [ -f "$GNOMOD_FILE" ]; then
66+
echo "[2/3] Updating gnomod.toml module path..."
67+
# Replace /v1 with /${VERSION} in the module line
68+
if [[ "$OSTYPE" == "darwin"* ]]; then
69+
# macOS sed syntax
70+
sed -i '' "s|/v1\"|/${VERSION}\"|g" "$GNOMOD_FILE"
71+
else
72+
# Linux sed syntax
73+
sed -i "s|/v1\"|/${VERSION}\"|g" "$GNOMOD_FILE"
74+
fi
75+
echo " ✓ gnomod.toml updated successfully"
76+
else
77+
echo " ⚠ Warning: gnomod.toml not found in target directory"
78+
fi
79+
80+
# Step 3: Update package declaration in all .gno files
81+
echo "[3/3] Updating package declarations in *.gno files..."
82+
GNO_FILES=$(find "$TARGET_DIR" -name "*.gno" -type f)
83+
FILE_COUNT=0
84+
85+
for file in $GNO_FILES; do
86+
if [[ "$OSTYPE" == "darwin"* ]]; then
87+
# macOS sed syntax
88+
sed -i '' "s/^package v1$/package ${VERSION}/g" "$file"
89+
else
90+
# Linux sed syntax
91+
sed -i "s/^package v1$/package ${VERSION}/g" "$file"
92+
fi
93+
FILE_COUNT=$((FILE_COUNT + 1))
94+
done
95+
96+
echo " ✓ Updated $FILE_COUNT .gno files"
97+
echo ""
98+
echo "✅ Upgradeable contract created successfully!"
99+
echo "New contract location: $TARGET_DIR"

0 commit comments

Comments
 (0)