|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 6 | +CONTRACTS_DIR="$ROOT_DIR/contracts" |
| 7 | +MIGRATIONS_DIR="$CONTRACTS_DIR/migrations" |
| 8 | +HISTORY_DIR="$MIGRATIONS_DIR/history" |
| 9 | +SNAPSHOTS_DIR="$MIGRATIONS_DIR/snapshots" |
| 10 | + |
| 11 | +# shellcheck source=../../scripts/utils.sh |
| 12 | +source "$ROOT_DIR/scripts/utils.sh" |
| 13 | + |
| 14 | +migration::require_command() { |
| 15 | + check_command "$1" |
| 16 | +} |
| 17 | + |
| 18 | +migration::require_env() { |
| 19 | + validate_env "$1" |
| 20 | +} |
| 21 | + |
| 22 | +migration::ensure_workspace() { |
| 23 | + mkdir -p "$HISTORY_DIR" "$SNAPSHOTS_DIR" |
| 24 | +} |
| 25 | + |
| 26 | +migration::timestamp() { |
| 27 | + date -u +"%Y%m%dT%H%M%SZ" |
| 28 | +} |
| 29 | + |
| 30 | +migration::read_active_contract_id() { |
| 31 | + local network="$1" |
| 32 | + local env_file="$CONTRACTS_DIR/.env.$network" |
| 33 | + |
| 34 | + if [ ! -f "$env_file" ]; then |
| 35 | + return 0 |
| 36 | + fi |
| 37 | + |
| 38 | + grep '^CONTRACT_ID=' "$env_file" | tail -n1 | cut -d'=' -f2- |
| 39 | +} |
| 40 | + |
| 41 | +migration::write_active_contract_id() { |
| 42 | + local network="$1" |
| 43 | + local contract_id="$2" |
| 44 | + local env_file="$CONTRACTS_DIR/.env.$network" |
| 45 | + |
| 46 | + printf 'CONTRACT_ID=%s\n' "$contract_id" > "$env_file" |
| 47 | +} |
| 48 | + |
| 49 | +migration::invoke_read() { |
| 50 | + local contract_id="$1" |
| 51 | + local network="$2" |
| 52 | + shift 2 |
| 53 | + |
| 54 | + soroban contract invoke \ |
| 55 | + --id "$contract_id" \ |
| 56 | + --network "$network" \ |
| 57 | + -- "$@" |
| 58 | +} |
| 59 | + |
| 60 | +migration::export_snapshot() { |
| 61 | + local contract_id="$1" |
| 62 | + local network="$2" |
| 63 | + local output_dir="$3" |
| 64 | + |
| 65 | + mkdir -p "$output_dir/plans" "$output_dir/subscriptions" |
| 66 | + |
| 67 | + local plan_count |
| 68 | + local subscription_count |
| 69 | + |
| 70 | + plan_count="$(migration::invoke_read "$contract_id" "$network" get_plan_count)" |
| 71 | + subscription_count="$(migration::invoke_read "$contract_id" "$network" get_subscription_count)" |
| 72 | + |
| 73 | + printf 'contract_id=%s\nnetwork=%s\nplan_count=%s\nsubscription_count=%s\n' \ |
| 74 | + "$contract_id" "$network" "$plan_count" "$subscription_count" > "$output_dir/summary.env" |
| 75 | + |
| 76 | + local i |
| 77 | + for ((i = 1; i <= plan_count; i++)); do |
| 78 | + migration::invoke_read "$contract_id" "$network" get_plan --plan_id "$i" > "$output_dir/plans/$i.json" |
| 79 | + done |
| 80 | + |
| 81 | + for ((i = 1; i <= subscription_count; i++)); do |
| 82 | + migration::invoke_read "$contract_id" "$network" get_subscription --subscription_id "$i" > "$output_dir/subscriptions/$i.json" |
| 83 | + done |
| 84 | +} |
| 85 | + |
| 86 | +migration::build_contract() { |
| 87 | + ( |
| 88 | + cd "$CONTRACTS_DIR" |
| 89 | + cargo build --target wasm32-unknown-unknown --release |
| 90 | + soroban contract optimize --wasm target/wasm32-unknown-unknown/release/subtrackr.wasm |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +migration::default_wasm_path() { |
| 95 | + printf '%s\n' "$CONTRACTS_DIR/target/wasm32-unknown-unknown/release/subtrackr.optimized.wasm" |
| 96 | +} |
| 97 | + |
| 98 | +migration::deploy_contract() { |
| 99 | + local wasm_path="$1" |
| 100 | + local source_account="$2" |
| 101 | + local network="$3" |
| 102 | + |
| 103 | + soroban contract deploy \ |
| 104 | + --wasm "$wasm_path" \ |
| 105 | + --source "$source_account" \ |
| 106 | + --network "$network" |
| 107 | +} |
| 108 | + |
| 109 | +migration::initialize_contract() { |
| 110 | + local contract_id="$1" |
| 111 | + local source_account="$2" |
| 112 | + local network="$3" |
| 113 | + local admin_address="$4" |
| 114 | + |
| 115 | + soroban contract invoke \ |
| 116 | + --id "$contract_id" \ |
| 117 | + --source "$source_account" \ |
| 118 | + --network "$network" \ |
| 119 | + -- initialize \ |
| 120 | + --admin "$admin_address" |
| 121 | +} |
| 122 | + |
| 123 | +migration::validate_contract_access() { |
| 124 | + local contract_id="$1" |
| 125 | + local network="$2" |
| 126 | + |
| 127 | + migration::invoke_read "$contract_id" "$network" get_plan_count > /dev/null |
| 128 | + migration::invoke_read "$contract_id" "$network" get_subscription_count > /dev/null |
| 129 | +} |
| 130 | + |
| 131 | +migration::write_history() { |
| 132 | + local output_file="$1" |
| 133 | + shift |
| 134 | + printf '%s\n' "$@" > "$output_file" |
| 135 | +} |
0 commit comments