From 31ea44a2b66f9410db66662a6a12e4e568c5ab03 Mon Sep 17 00:00:00 2001 From: David de Kloet Date: Thu, 28 Nov 2024 16:05:24 +0100 Subject: [PATCH] Provide fake ICP Swap data in test environment --- bin/dfx-mock-icp-swap-install | 94 +++++++++++++++++++++++++++++++++++ bin/dfx-snapshot-stock-make | 4 -- bin/dfx-stock-deploy | 6 +++ bin/dfx-stock-test | 10 ++++ 4 files changed, 110 insertions(+), 4 deletions(-) create mode 100755 bin/dfx-mock-icp-swap-install diff --git a/bin/dfx-mock-icp-swap-install b/bin/dfx-mock-icp-swap-install new file mode 100755 index 00000000..6ad44fe0 --- /dev/null +++ b/bin/dfx-mock-icp-swap-install @@ -0,0 +1,94 @@ +#!/usr/bin/env bash +set -euo pipefail +SOURCE_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +PATH="$SOURCE_DIR:$PATH" + +print_help() { + cat <<-EOF + + Installs an asset canister which serves a JSON file at /tickers with fake + price data for some SNS tokens as well as ckBTC, ckETH, ckUSDC and ckRED. + The price data is intended to be in the same format as provided by + ICP Swap at https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/tickers + EOF +} + +# 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" +# Source the output file ---------------------------------------------------------- +source "$(clap.build)" + +export DFX_NETWORK + +if ! [[ -e dfx.json ]]; then + echo "ERROR: No dfx.json found. Please run this from inside a dfx project." >&2 + exit 1 +fi + +get_random_price() { + # $RANDOM generates a random number between 0 and 32767. + # So we end up with a random number between 1 and ~17, with 4 digits behind + # the floating point. + echo "scale=4; $RANDOM / 2000 + 1" | bc +} + +generate_ticker_data() { + for ledger_canister in "$@"; do + if ! dfx canister id "$ledger_canister" >/dev/null 2>&1; then + echo "Skipping canister $ledger_canister as it does not exist." >&2 + continue + fi + symbol="$(dfx canister call "$ledger_canister" icrc1_metadata | idl2json | jq -r '[ .[] | {key: .["0"], value: .["1"]} ] | from_entries | .["icrc1:symbol"].Text')" + canister_id="$(dfx canister id "$ledger_canister")" + price="$(get_random_price)" + echo "Adding mock ticker data for $symbol with price $price" >&2 + symbol=$symbol canister_id=$canister_id price=$price jq -n '{ + ticker_name: (env.symbol + "_ICP"), + base_id: env.canister_id, + base_currency: env.symbol, + last_price: env.price, + ticker_id: "mohjv-bqaaa-aaaag-qjyia-cai", + target_id: "ryjl3-tyaaa-aaaaa-aaaba-cai", + target_currency: "ICP", + base_volume: "21176081.947944", + target_volume: "21036115.232038", + base_volume_24H: "396573.802868", + target_volume_24H: "35434.014785", + total_volume_usd: "37747146.085648", + volume_usd_24H: "396974.748666", + fee_usd: "736.687374", + liquidity_in_usd: "653394.543845" + }' + done +} + +# We only use the first page of the aggregator. So some SNS tokens will not +# have price data, which is realistic anyway. +aggregator_url="$(dfx-canister-url sns_aggregator)/v1/sns/list/page/0/slow.json" +mapfile -t sns_ledger_canister_ids < <(curl -LsSf "$aggregator_url" | jq -r '.[] | .canister_ids.ledger_canister_id') + +ICP_SWAP_DATA_DIR="icp-swap" +ICP_SWAP_DATA_FILE="icp-swap/tickers" + +TOP_DIR="$(git rev-parse --show-toplevel)" +mkdir -p "$TOP_DIR/$ICP_SWAP_DATA_DIR" + +generate_ticker_data ckusdc_ledger ckbtc_ledger cketh_ledger ckred_ledger "${sns_ledger_canister_ids[@]}" | jq -s . >"$ICP_SWAP_DATA_FILE" + +ICP_SWAP_DATA_DIR="$ICP_SWAP_DATA_DIR" jq -s '{ + canisters: { + "icp-swap": { + "source": [ + env.ICP_SWAP_DATA_DIR + ], + "build": "true", + "type": "assets", + } + } +} * .[0]' dfx.json | sponge dfx.json + +# This deploys an assets canister with assets from directory 'icp-swap' as +# defined in dfx.json. +dfx deploy icp-swap diff --git a/bin/dfx-snapshot-stock-make b/bin/dfx-snapshot-stock-make index 7e67ff30..2b2ac6d7 100755 --- a/bin/dfx-snapshot-stock-make +++ b/bin/dfx-snapshot-stock-make @@ -43,10 +43,6 @@ dfxvm default "$(jq -r .dfx dfx.json)" : Create stock state dfx-stock-deploy --ic_commit "$DFX_IC_COMMIT" --ic_dir "$IC_REPO_DIR" --parallel_sns_count "$PARALLEL_SNS_COUNT" --unique_logo "$UNIQUE_LOGO" -: "Wait for aggregator to get all SNSs" -# 2 SNSes were created by dfx-sns-demo. -dfx-sns-aggregator-wait - : "Wait for a checkpoint" dfx-network-wait-for-checkpoint --timeout 600 diff --git a/bin/dfx-stock-deploy b/bin/dfx-stock-deploy index 5a099f27..bf9dfced 100755 --- a/bin/dfx-stock-deploy +++ b/bin/dfx-stock-deploy @@ -109,3 +109,9 @@ NNS_ROOT="$(dfx canister id nns-root --network "$DFX_NETWORK")" for canister in nns-dapp internet_identity; do dfx canister --network "$DFX_NETWORK" update-settings "$canister" --add-controller "$NNS_ROOT" done + +: "Wait for aggregator to get all SNSs" +# 2 SNSes were created by dfx-sns-demo. +dfx-sns-aggregator-wait + +dfx-mock-icp-swap-install --network "$DFX_NETWORK" diff --git a/bin/dfx-stock-test b/bin/dfx-stock-test index 3fd38a9f..2d8018b9 100755 --- a/bin/dfx-stock-test +++ b/bin/dfx-stock-test @@ -57,4 +57,14 @@ export DFX_NETWORK fi ) +( + # We expect 4 ck tokens + 10 SNSes (from the aggregator first page) + print_title "icp-swap should have 14 tickers" + icp_swap_url="http://$(dfx canister id icp-swap).localhost:8080/tickers" + if [ "$(curl -fsS "$icp_swap_url" | jq length)" != "14" ]; then + echo "ERROR: icp-swap should have 14 tickers." >&2 + exit 1 + fi +) + echo "$(basename "$0") PASSED"