Skip to content

Commit 62c33c0

Browse files
committed
zero debt check
1 parent 5340d20 commit 62c33c0

File tree

3 files changed

+139
-4
lines changed

3 files changed

+139
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor
22
.DS_Store
3-
node_modules
3+
node_modules
4+
.venv

x/oracle/oracle/data_source.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env python3
2+
from unittest import result
3+
import requests
4+
import json
5+
import sys
6+
import time
7+
8+
CONSTANTS = {
9+
"GATE_IO": {
10+
"SYMBOLS": {
11+
"NOM":"nom",
12+
},
13+
"URL": "https://api.gateio.ws/api/v4/spot/tickers"
14+
},
15+
16+
"MEXC" : {
17+
"SYMBOLS": {
18+
"NOM":"nom",
19+
},
20+
"URL": "https://api.mexc.com/api/v3/ticker/price"
21+
22+
}
23+
}
24+
25+
26+
27+
def get_gate_price(symbols):
28+
if not symbols:
29+
return []
30+
31+
# Set a header for api
32+
HEADER = {
33+
"Accept": "application/json",
34+
}
35+
36+
PARAMETERS = {
37+
"currency_pair": "NOM_USDT",
38+
}
39+
40+
# URL to retrieve the price of all tokens
41+
url = CONSTANTS["GATE_IO"]["URL"]
42+
result = []
43+
# Set request parameters
44+
45+
46+
try:
47+
# Make api call
48+
response = requests.get(url, params=PARAMETERS, headers=HEADER, timeout=3)
49+
# Retrieve prices from response
50+
for idx, symbol in enumerate(symbols):
51+
if response.status_code == 200:
52+
result.append(float(response.json()[idx]["last"]))
53+
else:
54+
result.append(0)
55+
# Return prices
56+
return result
57+
except Exception as e:
58+
return [0 for i in range(len(symbols))]
59+
60+
def get_price_mecx(symbols):
61+
if not symbols:
62+
return []
63+
64+
# Set a header for api
65+
HEADER = {
66+
"Accept": "application/json",
67+
}
68+
69+
# URL to retrieve the price of all tokens
70+
url = CONSTANTS["MEXC"]["URL"]
71+
72+
result = []
73+
# Set request parameters
74+
PARAMETERS = {
75+
"symbol": "NOMUSDT",
76+
}
77+
78+
79+
try:
80+
# Make api call
81+
response = requests.get(url, params=PARAMETERS, headers=HEADER, timeout=3)
82+
83+
# Retrieve prices from response
84+
for idx, symbol in enumerate(symbols):
85+
if response.status_code == 200:
86+
result.append(float(response.json()["price"]) )
87+
else:
88+
result.append(0)
89+
# Return prices
90+
return result
91+
except Exception as e:
92+
return [0 for i in range(len(symbols))]
93+
94+
95+
96+
97+
def main(symbols):
98+
if len(symbols) == 0:
99+
return ""
100+
try:
101+
# Get price from bingx
102+
result_mecx = get_price_mecx(symbols)
103+
# Get price from gate
104+
result_gate = get_gate_price(symbols)
105+
106+
# if lenghth of the results from all the sources is not same, then return 0
107+
if not len(result_gate) == len(result_mecx):
108+
return ",".join("0" for i in range(len(symbols)))
109+
110+
result = []
111+
for item in zip(result_gate, result_mecx):
112+
different_sources_price = list(item)
113+
non_zero_sources = [price for price in different_sources_price if price != 0]
114+
if len(non_zero_sources) != 0:
115+
mean = sum(non_zero_sources) / len(non_zero_sources)
116+
result.append(mean)
117+
else:
118+
result.append(0)
119+
120+
return ",".join(str(price) for price in result)
121+
except Exception as e:
122+
return ",".join("0" for i in range(len(symbols)))
123+
124+
if __name__ == "__main__":
125+
try:
126+
print(main(sys.argv[1:]))
127+
except Exception as e:
128+
print(e, file=sys.stderr)
129+
sys.exit(1)

x/vaults/keeper/vault.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,13 @@ func (k *Keeper) WithdrawFromVault(
398398
}
399399

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

403-
if ratio.LT(vm.Params.MinCollateralRatio) {
404-
return fmt.Errorf("ratio less than min ratio. Got: %d, min: %d", ratio, vm.Params.MinCollateralRatio)
402+
// collateral ratio check
403+
if !vault.Debt.Amount.IsZero() {
404+
collateralRatio := newLockValue.Quo(math.LegacyNewDecFromInt(vault.Debt.Amount))
405+
if collateralRatio.LT(vm.Params.MinCollateralRatio) {
406+
return fmt.Errorf("ratio less than min ratio. Got: %d, min: %d", collateralRatio, vm.Params.MinCollateralRatio)
407+
}
405408
}
406409

407410
err = k.BankKeeper.SendCoins(ctx, sdk.MustAccAddressFromBech32(vault.Address), sender, sdk.NewCoins(collateral))
@@ -479,6 +482,7 @@ func (k *Keeper) shouldLiquidate(
479482
if math.LegacyNewDecFromInt(vault.Debt.Amount).Equal(math.LegacyZeroDec()) {
480483
return false, nil
481484
}
485+
482486
ratio := collateralValue.Quo(math.LegacyNewDecFromInt(vault.Debt.Amount))
483487

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

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

0 commit comments

Comments
 (0)