Skip to content

Commit

Permalink
zero debt check
Browse files Browse the repository at this point in the history
  • Loading branch information
vuong177 committed Nov 27, 2024
1 parent 5340d20 commit 62c33c0
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
.DS_Store
node_modules
node_modules
.venv
129 changes: 129 additions & 0 deletions x/oracle/oracle/data_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env python3
from unittest import result
import requests
import json
import sys
import time

CONSTANTS = {
"GATE_IO": {
"SYMBOLS": {
"NOM":"nom",
},
"URL": "https://api.gateio.ws/api/v4/spot/tickers"
},

"MEXC" : {
"SYMBOLS": {
"NOM":"nom",
},
"URL": "https://api.mexc.com/api/v3/ticker/price"

}
}



def get_gate_price(symbols):
if not symbols:
return []

# Set a header for api
HEADER = {
"Accept": "application/json",
}

PARAMETERS = {
"currency_pair": "NOM_USDT",
}

# URL to retrieve the price of all tokens
url = CONSTANTS["GATE_IO"]["URL"]
result = []
# Set request parameters


try:
# Make api call
response = requests.get(url, params=PARAMETERS, headers=HEADER, timeout=3)
# Retrieve prices from response
for idx, symbol in enumerate(symbols):
if response.status_code == 200:
result.append(float(response.json()[idx]["last"]))
else:
result.append(0)
# Return prices
return result
except Exception as e:
return [0 for i in range(len(symbols))]

def get_price_mecx(symbols):
if not symbols:
return []

# Set a header for api
HEADER = {
"Accept": "application/json",
}

# URL to retrieve the price of all tokens
url = CONSTANTS["MEXC"]["URL"]

result = []
# Set request parameters
PARAMETERS = {
"symbol": "NOMUSDT",
}


try:
# Make api call
response = requests.get(url, params=PARAMETERS, headers=HEADER, timeout=3)

# Retrieve prices from response
for idx, symbol in enumerate(symbols):
if response.status_code == 200:
result.append(float(response.json()["price"]) )
else:
result.append(0)
# Return prices
return result
except Exception as e:
return [0 for i in range(len(symbols))]




def main(symbols):
if len(symbols) == 0:
return ""
try:
# Get price from bingx
result_mecx = get_price_mecx(symbols)
# Get price from gate
result_gate = get_gate_price(symbols)

# if lenghth of the results from all the sources is not same, then return 0
if not len(result_gate) == len(result_mecx):
return ",".join("0" for i in range(len(symbols)))

result = []
for item in zip(result_gate, result_mecx):
different_sources_price = list(item)
non_zero_sources = [price for price in different_sources_price if price != 0]
if len(non_zero_sources) != 0:
mean = sum(non_zero_sources) / len(non_zero_sources)
result.append(mean)
else:
result.append(0)

return ",".join(str(price) for price in result)
except Exception as e:
return ",".join("0" for i in range(len(symbols)))

if __name__ == "__main__":
try:
print(main(sys.argv[1:]))
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
11 changes: 8 additions & 3 deletions x/vaults/keeper/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,13 @@ func (k *Keeper) WithdrawFromVault(
}

newLockValue := math.LegacyNewDecFromInt(newLock.Amount).Mul(*price)
ratio := newLockValue.Quo(math.LegacyNewDecFromInt(vault.Debt.Amount))

if ratio.LT(vm.Params.MinCollateralRatio) {
return fmt.Errorf("ratio less than min ratio. Got: %d, min: %d", ratio, vm.Params.MinCollateralRatio)
// collateral ratio check
if !vault.Debt.Amount.IsZero() {
collateralRatio := newLockValue.Quo(math.LegacyNewDecFromInt(vault.Debt.Amount))
if collateralRatio.LT(vm.Params.MinCollateralRatio) {
return fmt.Errorf("ratio less than min ratio. Got: %d, min: %d", collateralRatio, vm.Params.MinCollateralRatio)
}
}

err = k.BankKeeper.SendCoins(ctx, sdk.MustAccAddressFromBech32(vault.Address), sender, sdk.NewCoins(collateral))
Expand Down Expand Up @@ -479,6 +482,7 @@ func (k *Keeper) shouldLiquidate(
if math.LegacyNewDecFromInt(vault.Debt.Amount).Equal(math.LegacyZeroDec()) {
return false, nil
}

ratio := collateralValue.Quo(math.LegacyNewDecFromInt(vault.Debt.Amount))

if ratio.LTE(liquidationRatio) {
Expand Down Expand Up @@ -728,6 +732,7 @@ func (k *Keeper) Liquidate(
vault.CollateralLocked.Amount = vault.CollateralLocked.Amount.Sub(penaltyAmount)
totalCollateralRemain.Amount = totalCollateralRemain.Amount.Sub(penaltyAmount)

// vault with zero debt never be liquidated so we don't need to check zero value here
ratio := math.LegacyNewDecFromInt(vault.CollateralLocked.Amount).Mul(vault.LiquidationPrice).Quo(math.LegacyNewDecFromInt(vault.Debt.Amount))
ratios = append(ratios, ratio)
}
Expand Down

0 comments on commit 62c33c0

Please sign in to comment.