Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dex): fix calculation of stableswap swaps and add tests #1151

Merged
merged 12 commits into from
Jan 25, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* [#1126](https://github.com/NibiruChain/nibiru/pull/1126) - test(oracle): stop the tyrannical behavior of TestFuzz_PickReferencePair
* [#1131](https://github.com/NibiruChain/nibiru/pull/1131) - fix(oracle): use correct distribution module account
* [#1151](https://github.com/NibiruChain/nibiru/pull/1151) - fix(dex): fix swap calculation for stableswap pools

## [v0.16.3](https://github.com/NibiruChain/nibiru/releases/tag/v0.16.3)

Expand Down
34 changes: 20 additions & 14 deletions contrib/scripts/e2e/localnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ console_log_text_color() {
reset=$(tput sgr0)
}

if [ console_log_text_color ]; then echo "succesfully toggled console coloring"
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_info() {
echo "${blue}"
echo "$1"
echo "${reset}"
}

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

echo_success () {
echo_success() {
echo "${green}"
echo "$1"
echo "${reset}"
Expand All @@ -48,9 +49,9 @@ if [[ "$OSTYPE" == "darwin"* ]]; then
fi

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

# Remove previous data
Expand All @@ -71,7 +72,6 @@ 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
Expand All @@ -80,7 +80,6 @@ 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
Expand Down Expand Up @@ -162,7 +161,7 @@ fi
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
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
}
Expand All @@ -174,13 +173,13 @@ add_genesis_vpools_with_coingecko_prices() {
curl -X 'GET' \
'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cethereum&vs_currencies=usd' \
-H 'accept: application/json' \
> $temp_json_fname
>$temp_json_fname

local M=1000000

local num_users=24000
local faucet_nusd_amt=100
local quote_amt=$(($num_users * $faucet_nusd_amt * $M))
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%.*}
Expand All @@ -205,7 +204,7 @@ add_genesis_vpools_default() {
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 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
}

Expand All @@ -231,6 +230,13 @@ add_genesis_param '.app_state.perp.pair_metadata[0].latest_cumulative_premium_fr
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"'

add_genesis_param '.app_state.oracle.params.twap_lookback_window = "900s"'
add_genesis_param '.app_state.oracle.params.vote_period = "10"'
add_genesis_param '.app_state.oracle.exchange_rates[0].pair = "ubtc:unusd"'
add_genesis_param '.app_state.oracle.exchange_rates[0].exchange_rate = "20000"'
add_genesis_param '.app_state.oracle.exchange_rates[1].pair = "ueth:unusd"'
add_genesis_param '.app_state.oracle.exchange_rates[1].exchange_rate = "2000"'

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

Expand Down
2 changes: 1 addition & 1 deletion contrib/scripts/localnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ 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="10000000000000unibi,10000000000000unusd,10000000000000uusdt"
GENESIS_COINS="10000000000000unibi,10000000000000unusd,10000000000000uusdt,10000000000000uusdc"
CHAIN_DIR="$HOME/.nibid"
echo "CHAIN_DIR: $CHAIN_DIR"
echo "CHAIN_ID: $CHAIN_ID"
Expand Down
5 changes: 4 additions & 1 deletion x/dex/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,10 @@ func (k Keeper) ExitPool(

if poolSharesOut.Amount.GT(pool.TotalShares.Amount) ||
poolSharesOut.Amount.LTE(sdk.ZeroInt()) {
return sdk.Coins{}, errors.New("invalid number of pool shares")
return sdk.Coins{}, fmt.Errorf(
"invalid number of pool shares %s must be between 0 and %s",
poolSharesOut.Amount, pool.TotalShares.Amount,
)
}

// calculate withdrawn liquidity
Expand Down
Loading