Skip to content

Commit

Permalink
ci: Create e2e wasm unit test (#1107)
Browse files Browse the repository at this point in the history
* update changelog

* update changelog

* build flag for linux

* build flag for linux

* temp commit

* update main

* add dist

* add cgo enabled

* Update go release.

* add flags

* other test

* add rm dist flag

* add flag amd64

* add go releaser action

* add flags

* remove ledger flag

* add tm flag

* add lib

* try sudo

* add link statically

* change back ldflags

* add muslc as build tag

* some test

* remove file

* last try build flags

* add ldflag linkmode

* add another ldflags

* add colon

* Comment all related to muslc

* add external ld flags

* add netgo

* add install musl dev

* add lm linker flag

* add math lib

* update changelog

* add lost wasm line

* move entry to breaking changes

* fix: localnet script

* create e2e wasm

* add tag for version

* use self repository

* ls release folder

* echo release folder

* echo outputs

* echo outputs

* download release

* try release

* try release output

* try var

* use wget

* use another action

* fix action

* use filter

* unpack release

* change name

* show release

* add nibid version

* execute nibid

* add nibid

* run network

* run network

* add runs on

* insert code

* ls test

* launch localnet

* remove build localnet

* refactor binary

* try query

* add deploy wasm test

* add validator address

* temp commit

* deploy wasm

* add folder

* add chain id

* add node

* add nameservice code

* add register service

* remove nibid

* add end to end teset

* add changelog

* add header

Co-authored-by: NibiruHeisenberg <[email protected]>
  • Loading branch information
jgimeno and NibiruHeisenberg authored Jan 15, 2023
1 parent 29f6561 commit 97bbdfe
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 0 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/e2e-wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
name: CosmWasm e2e contract tests

on:
# On normal PRs or when workflow goreleaser finishes, as it gets the last release tag.
pull_request:
paths: ["**.go", "**.proto", "go.mod", "go.sum"]
workflow_run:
workflows: ["goreleaser"]
types:
- completed
workflow_dispatch:

jobs:
get-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Download release
id: latest_release
uses: pozetroninc/[email protected]
with:
repository: ${{ github.repository }}

- name: download release
uses: robinraju/[email protected]
with:
tag: ${{ steps.latest_release.outputs.release }}
fileName: "*linux_amd64.tar.gz"

- name: unpack release
run: |
tar -xzf *linux_amd64.tar.gz
- name: launch localnet
run: |
sh ./scripts/e2e/localnet.sh
- name: run e2e tests
run: |
sh ./scripts/e2e/deploy-wasm.sh
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### CI

* [#1032](https://github.com/NibiruChain/nibiru/pull/1107) - ci: Create e2e wasm contract test
### Improvements

* [#1134](https://github.com/NibiruChain/nibiru/pull/1134) - refactor: remove panics from vpool and spillovers from the perp module. It's now impossible to call functions in x/perp that would panic in vpool.
Expand Down Expand Up @@ -83,6 +86,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1097](https://github.com/NibiruChain/nibiru/pull/1097) - feat(perp): Track and expose the net size of a pair with a query
* [#1105](https://github.com/NibiruChain/nibiru/pull/1105) - feat(perp): Add (notional) volume to metrics state

### API Breaking
* [#1074](https://github.com/NibiruChain/nibiru/pull/1074) - feat(vpool): Add gov proposal for editing the vpool config without changing the reserves.

### State Machine Breaking

* [#1102](https://github.com/NibiruChain/nibiru/pull/1102) - refactor(perp)!: replace CumulativePremiumFractions array with single value
Expand Down
16 changes: 16 additions & 0 deletions Dockerfile-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.19-alpine

WORKDIR /build

ARG ARCH=x86_64

RUN set -eux; apk add --no-cache ca-certificates build-base;
RUN apk add git


ADD https://github.com/CosmWasm/wasmvm/releases/download/v1.1.1/libwasmvm_muslc.aarch64.a /lib/libwasmvm_muslc.aarch64.a
ADD https://github.com/CosmWasm/wasmvm/releases/download/v1.1.1/libwasmvm_muslc.x86_64.a /lib/libwasmvm_muslc.x86_64.a
RUN sha256sum /lib/libwasmvm_muslc.aarch64.a | grep 9ecb037336bd56076573dc18c26631a9d2099a7f2b40dc04b6cae31ffb4c8f9a
RUN sha256sum /lib/libwasmvm_muslc.x86_64.a | grep 6e4de7ba9bad4ae9679c7f9ecf7e283dd0160e71567c6a7be6ae47c81ebe7f32

RUN cp /lib/libwasmvm_muslc.${ARCH}.a /lib/libwasmvm_muslc.a
Binary file added scripts/e2e/contracts/cw_nameservice.wasm
Binary file not shown.
42 changes: 42 additions & 0 deletions scripts/e2e/deploy-wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
BINARY="./nibid"
DENOM="unibi"
CHAIN_ID="nibiru-localnet-0"
TXFLAG="--gas-prices 0.1$DENOM --gas auto --gas-adjustment 1.3 -y -b block --chain-id $CHAIN_ID"

# validator addr
VALIDATOR_ADDR=$($BINARY keys show validator --address)
echo "Validator address:"
echo "$VALIDATOR_ADDR"

BALANCE_1=$($BINARY q bank balances $VALIDATOR_ADDR)
echo "Pre-store balance:"
echo "$BALANCE_1"
echo "TX Flags: $TXFLAG"

CONTRACT_CODE=$($BINARY tx wasm store "./scripts/e2e/contracts/cw_nameservice.wasm" --from validator $TXFLAG --output json | jq -r '.logs[0].events[-1].attributes[-1].value')
echo "Stored: $CONTRACT_CODE"

BALANCE_2=$($BINARY q bank balances $VALIDATOR_ADDR)
echo "Post-store balance:"
echo "$BALANCE_2"

INIT='{"purchase_price":{"amount":"100","denom":"unibi"},"transfer_price":{"amount":"999","denom":"unibi"}}'
$BINARY tx wasm instantiate $CONTRACT_CODE "$INIT" --from validator $TXFLAG --label "awesome name service" --no-admin

CONTRACT_ADDRESS=$($BINARY query wasm list-contract-by-code $CONTRACT_CODE --output json | jq -r '.contracts[-1]')
echo "Contract Address: $CONTRACT_ADDRESS"

$BINARY query wasm contract $CONTRACT_ADDRESS

# purchase a domain name
$BINARY tx wasm execute $CONTRACT_ADDRESS '{"register":{"name":"uniques-domain"}}' --amount 100$DENOM --from validator $TXFLAG -y

# query registered name
NAME_QUERY='{"resolve_record": {"name": "uniques-domain"}}'
DOMAIN_OWNER=$($BINARY query wasm contract-state smart $CONTRACT_ADDRESS "$NAME_QUERY" --output json | jq -r '.data.address')
echo "Owner: $DOMAIN_OWNER"

if [ $DOMAIN_OWNER != $VALIDATOR_ADDR ]; then
echo "Domain owner is not the validator address"
exit 1
fi
242 changes: 242 additions & 0 deletions scripts/e2e/localnet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
#!/bin/sh
set -e

# Console log text colour
console_log_text_color() {
red=$(tput setaf 9)
green=$(tput setaf 10)
blue=$(tput setaf 12)
reset=$(tput sgr0)
}

if [ console_log_text_color ]; then echo "succesfully toggled console coloring"
else
# For Ubuntu and Debian. MacOS has tput by default.
apt-get install libncurses5-dbg -y
fi

echo_info () {
echo "${blue}"
echo "$1"
echo "${reset}"
}

echo_error () {
echo "${red}"
echo "$1"
echo "${reset}"
}

echo_success () {
echo "${green}"
echo "$1"
echo "${reset}"
}

# Set localnet settings
BINARY="./nibid"
CHAIN_ID="nibiru-localnet-0"
RPC_PORT="26657"
GRPC_PORT="9090"
MNEMONIC="guard cream sadness conduct invite crumble clock pudding hole grit liar hotel maid produce squeeze return argue turtle know drive eight casino maze host"
GENESIS_COINS="1000000000unibi,10000000000000unusd"
CHAIN_DIR="$HOME/.nibid"

SEDOPTION=""
if [[ "$OSTYPE" == "darwin"* ]]; then
SEDOPTION="''"
fi

# Stop nibid if it is already running
if pgrep -x "$BINARY" > /dev/null; then
echo_error "Terminating $BINARY..."
killall nibid
fi

# Remove previous data
echo_info "Removing previous chain data from $CHAIN_DIR..."
rm -rf $CHAIN_DIR

# Add directory for chain, exit if error
if ! mkdir -p $CHAIN_DIR 2>/dev/null; then
echo_error "Failed to create chain folder. Aborting..."
exit 1
fi

# Initialize nibid with "localnet" chain id
echo_info "Initializing $CHAIN_ID..."
if $BINARY init nibiru-localnet-0 --chain-id $CHAIN_ID --overwrite; then
echo_success "Successfully initialized $CHAIN_ID"
else
echo_error "Failed to initialize $CHAIN_ID"
fi


# Configure keyring-backend to "test"
echo_info "Configuring keyring-backend..."
if $BINARY config keyring-backend test; then
echo_success "Successfully configured keyring-backend"
else
echo_error "Failed to configure keyring-backend"
fi


# Configure chain-id
echo_info "Configuring chain-id..."
if $BINARY config chain-id $CHAIN_ID; then
echo_success "Successfully configured chain-id"
else
echo_error "Failed to configure chain-id"
fi

# Configure broadcast mode
echo_info "Configuring broadcast mode..."
if $BINARY config broadcast-mode block; then
echo_success "Successfully configured broadcast-mode"
else
echo_error "Failed to configure broadcast mode"
fi

# Configure output mode
echo_info "Configuring output mode..."
if $BINARY config output json; then
echo_success "Successfully configured output mode"
else
echo_error "Failed to configure output mode"
fi

# Enable API Server
echo_info "Enabling API server"
if sed -i $SEDOPTION '/\[api\]/,+3 s/enable = false/enable = true/' $CHAIN_DIR/config/app.toml; then
echo_success "Successfully enabled API server"
else
echo_error "Failed to enable API server"
fi

# Enable Swagger Docs
echo_info "Enabling Swagger Docs"
if sed -i $SEDOPTION 's/swagger = false/swagger = true/' $CHAIN_DIR/config/app.toml; then
echo_success "Successfully enabled Swagger Docs"
else
echo_error "Failed to enable Swagger Docs"
fi

# Enable CORS for localnet
echo_info "Enabling CORS"
if sed -i $SEDOPTION 's/enabled-unsafe-cors = false/enabled-unsafe-cors = true/' $CHAIN_DIR/config/app.toml; then
echo_success "Successfully enabled CORS"
else
echo_error "Failed to enable CORS"
fi

echo_info "Adding genesis accounts..."
echo "$MNEMONIC" | $BINARY keys add validator --recover
if $BINARY add-genesis-account $($BINARY keys show validator -a) $GENESIS_COINS; then
echo_success "Successfully added genesis accounts"
else
echo_error "Failed to add genesis accounts"
fi

echo_info "Adding gentx validator..."
if $BINARY gentx validator 900000000unibi --chain-id $CHAIN_ID; then
echo_success "Successfully added gentx"
else
echo_error "Failed to add gentx"
fi

echo_info "Collecting gentx..."
if $BINARY collect-gentxs; then
echo_success "Successfully collected genesis txs into genesis.json"
else
echo_error "Failed to collect genesis txs"
fi

# ------------------------------------------------------------------------
# Configure genesis params
# ------------------------------------------------------------------------

# add_genesis_params runs a jq command to edit fields of the genesis.json .
#
# Args:
# $1 : the jq input that gets mapped to the json.
add_genesis_param() {
echo "jq input $1"
# copy param ($1) to tmp_genesis.json
cat $CHAIN_DIR/config/genesis.json | jq "$1" > $CHAIN_DIR/config/tmp_genesis.json
# rewrite genesis.json with the contents of tmp_genesis.json
mv $CHAIN_DIR/config/tmp_genesis.json $CHAIN_DIR/config/genesis.json
}

echo_info "Configuring genesis params"

add_genesis_vpools_with_coingecko_prices() {
local temp_json_fname="tmp_vpool_prices.json"
curl -X 'GET' \
'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cethereum&vs_currencies=usd' \
-H 'accept: application/json' \
> $temp_json_fname

local M=1000000

local num_users=24000
local faucet_nusd_amt=100
local quote_amt=$(($num_users * $faucet_nusd_amt * $M))

price_btc=$(cat tmp_vpool_prices.json | jq -r '.bitcoin.usd')
price_btc=${price_btc%.*}
base_amt_btc=$(($quote_amt / $price_btc))

price_eth=$(cat tmp_vpool_prices.json | jq -r '.ethereum.usd')
price_eth=${price_eth%.*}
base_amt_eth=$(($quote_amt / $price_eth))

$BINARY add-genesis-vpool ubtc:unusd $base_amt_btc $quote_amt 0.1 0.1 0.1 0.0625 12
$BINARY add-genesis-vpool ueth:unusd $base_amt_eth $quote_amt 0.1 0.1 0.1 0.04 20

echo 'tmp_vpool_prices: '
cat $temp_json_fname | jq .
rm -f $temp_json_fname
}

add_genesis_vpools_default() {
# nibid add-genesis-vpool [pair] [base-asset-reserve] [quote-asset-reserve] [trade-limit-ratio] [fluctuation-limit-ratio] [maxOracle-spread-ratio] [maintenance-margin-ratio] [max-leverage]
local KILO="000"
local MEGA="000000"
local quote_amt=10$KILO$MEGA
local base_amt_btc=$(($quote_amt / 16500))
local base_amt_eth=$(($quote_amt / 1200))
$BINARY add-genesis-vpool ubtc:unusd $base_amt_btc $quote_amt 0.1 0.1 0.1 0.0625 12
$BINARY add-genesis-vpool ueth:unusd $base_amt_eth $quote_amt 0.1 0.1 0.1 0.0625 10
}

# x/vpool
if add_genesis_vpools_with_coingecko_prices; then
echo_success "set vpools with coingecko prices"
elif add_genesis_vpools_default; then
echo_success "set vpools with defaults"
else
echo_error "failed to set genesis vpools"
fi

# x/perp
add_genesis_param '.app_state.perp.params.stopped = false'
add_genesis_param '.app_state.perp.params.fee_pool_fee_ratio = "0.001"'
add_genesis_param '.app_state.perp.params.ecosystem_fund_fee_ratio = "0.001"'
add_genesis_param '.app_state.perp.params.liquidation_fee_ratio = "0.025"'
add_genesis_param '.app_state.perp.params.partial_liquidation_ratio = "0.25"'
add_genesis_param '.app_state.perp.params.funding_rate_interval = "30 min"'
add_genesis_param '.app_state.perp.params.twap_lookback_window = "900s"'
add_genesis_param '.app_state.perp.pair_metadata[0].pair = {token0:"ubtc",token1:"unusd"}'
add_genesis_param '.app_state.perp.pair_metadata[0].latest_cumulative_premium_fraction = "0"'
add_genesis_param '.app_state.perp.pair_metadata[1].pair = {token0:"ueth",token1:"unusd"}'
add_genesis_param '.app_state.perp.pair_metadata[1].latest_cumulative_premium_fraction = "0"'

# x/pricefeed
$BINARY add-genesis-oracle nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl

cat $HOME/.nibid/config/genesis.json | jq '.app_state.pricefeed.params.twap_lookback_window = "900s"' > $HOME/.nibid/config/tmp_genesis.json && mv $HOME/.nibid/config/tmp_genesis.json $HOME/.nibid/config/genesis.json

# Start the network
echo_info "Starting $CHAIN_ID in $CHAIN_DIR..."

$BINARY start &

0 comments on commit 97bbdfe

Please sign in to comment.