Skip to content

Commit

Permalink
Wait for SNS lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
dskloetd committed Oct 11, 2024
1 parent eeea702 commit 7bc702d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
34 changes: 34 additions & 0 deletions bin/dfx-sns-get-lifecycle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
set -euo pipefail
SOURCE_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"

print_help() {
cat <<-EOF
Get the current lifecycle of the SNS as a descriptive string rather than a
number.
EOF
}

declare -A LIFECYCLE_MAP=(
[1]="pending"
[2]="open"
[3]="committed"
[4]="aborted"
[5]="adopted"
)

# Source the clap.bash file ---------------------------------------------------
source "$SOURCE_DIR/clap.bash"
# Define options
clap.define short=n long=network desc="The dfx network to use" variable=DFX_NETWORK default="local"
clap.define short=i long=identity desc="The dfx identity to use" variable=DFX_IDENTITY default="$(dfx identity whoami)"
# Source the output file ----------------------------------------------------------
source "$(clap.build)"

export DFX_NETWORK
export DFX_IDENTITY

LIFECYCLE_ID="$(dfx canister call sns_swap get_lifecycle '(record{})' | idl2json | jq -r '.lifecycle[0]')"

echo "${LIFECYCLE_MAP[$LIFECYCLE_ID]}"
3 changes: 3 additions & 0 deletions bin/dfx-sns-sale-buy
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ source "$(clap.build)"

export DFX_NETWORK

# The swap must be open before we can buy in.
dfx-sns-wait-for-lifecycle --lifecycle "open" --network "$DFX_NETWORK" --identity "$DFX_IDENTITY"

# Set the purchased amout to the maximum allowed, if requested.
if [[ "${AMOUNT:-}" = "max" ]]; then
SWAP_STATE="$(dfx canister call sns_swap get_state '(record {})' --network "$DFX_NETWORK")"
Expand Down
3 changes: 3 additions & 0 deletions bin/dfx-sns-sale-finalize
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ clap.define short=b long=bin desc="Local directory for executables" variable=USE
# Source the output file ----------------------------------------------------------
source "$(clap.build)"

# The swap must be committed before it can be finalized.
dfx-sns-wait-for-lifecycle --lifecycle "committed" --network "$DFX_NETWORK" --identity "$DFX_IDENTITY"

dfx canister --network "$DFX_NETWORK" call sns_swap finalize_swap '(record {})' | tee ,sns-sale-finalize.idl

ERROR_MESSAGE="$(idl2json <,sns-sale-finalize.idl | jq -r '.error_message')"
Expand Down
54 changes: 54 additions & 0 deletions bin/dfx-sns-wait-for-lifecycle
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
set -euo pipefail
SOURCE_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
PATH="$SOURCE_DIR:$PATH"

print_help() {
cat <<-EOF
Wait up to 2 minutes for the current SNS to reach the given lifecycle.
EOF
}

LIFECYCLES=(
"pending"
"open"
"committed"
"aborted"
"adopted"
)

LIFECYCLE_LIST=$(printf " %s," "${LIFECYCLES[@]}")
LIFECYCLE_LIST=${LIFECYCLE_LIST%,*}

# Source the clap.bash file ---------------------------------------------------
source "$SOURCE_DIR/clap.bash"
# Define options
clap.define short=l long=lifecycle desc="One of $LIFECYCLE_LIST" variable=LIFECYCLE default="open"
clap.define short=n long=network desc="The dfx network to use" variable=DFX_NETWORK default="local"
clap.define short=i long=identity desc="The dfx identity to use" variable=DFX_IDENTITY default="$(dfx identity whoami)"
# Source the output file ----------------------------------------------------------
source "$(clap.build)"

if [[ ! "${LIFECYCLE_LIST}" =~ " ${LIFECYCLE:-}," ]]; then
echo "ERROR: Unknown lifecycle '$LIFECYCLE'"
echo "Please choose one of: $LIFECYCLE_LIST"
exit 1
fi

current_lifecycle=$(dfx-sns-get-lifecycle --network "$DFX_NETWORK" --identity "$DFX_IDENTITY")

echo "Current lifecycle: $current_lifecycle"
echo "Waiting for lifecycle: $LIFECYCLE"

for ((try = 60; try > 0; try--)); do
if [[ "$current_lifecycle" == "$LIFECYCLE" ]]; then
echo "Lifecycle reached."
exit 0
fi
sleep 2
current_lifecycle=$(dfx-sns-get-lifecycle --network "$DFX_NETWORK" --identity "$DFX_IDENTITY")
done

echo "ERROR: Lifecycle not reached after 2 minutes."
exit 1

0 comments on commit 7bc702d

Please sign in to comment.