Skip to content

Commit

Permalink
fix refactor and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
toteki committed Jun 21, 2023
1 parent e32f0e9 commit 9935dbf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
63 changes: 63 additions & 0 deletions x/leverage/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,69 @@ func (s *IntegrationTestSuite) TestQuerier_AccountSummary() {
require.Equal(expected, *resp)
}

func (s *IntegrationTestSuite) TestQuerier_Inspect() {
ctx, require := s.ctx, s.Require()

// creates accounts
addr1 := s.newAccount(coin.New(umeeDenom, 1000_000000))
s.supply(addr1, coin.New(umeeDenom, 1000_000000))
s.collateralize(addr1, coin.New("u/"+umeeDenom, 1000_000000))
s.borrow(addr1, coin.New(umeeDenom, 10_500000))
addr2 := s.newAccount(coin.New(umeeDenom, 1000_000000))
s.supply(addr2, coin.New(umeeDenom, 1000_000000))
s.collateralize(addr2, coin.New("u/"+umeeDenom, 60_000000))
s.borrow(addr2, coin.New(umeeDenom, 1_500000))
addr3 := s.newAccount(coin.New(umeeDenom, 1000_000000))
s.supply(addr3, coin.New(umeeDenom, 1000_000000))
s.collateralize(addr3, coin.New("u/"+umeeDenom, 600_000000))
s.borrow(addr3, coin.New(umeeDenom, 15_000000))

resp, err := s.queryClient.Inspect(ctx.Context(), &types.QueryInspect{})
require.NoError(err)

expected := types.QueryInspectResponse{
Borrowers: []types.InspectAccount{
{
Address: addr3.String(),
Analysis: &types.RiskInfo{
Borrowed: 63.15,
Liquidation: 656,
Value: 2526,
},
Position: &types.DecBalances{
Collateral: sdk.NewDecCoins(coin.Dec("UMEE", "600")),
Borrowed: sdk.NewDecCoins(coin.Dec("UMEE", "15")),
},
},
{
Address: addr1.String(),
Analysis: &types.RiskInfo{
Borrowed: 44.2,
Liquidation: 1094,
Value: 4210,
},
Position: &types.DecBalances{
Collateral: sdk.NewDecCoins(coin.Dec("UMEE", "1000")),
Borrowed: sdk.NewDecCoins(coin.Dec("UMEE", "10.5")),
},
},
{
Address: addr2.String(),
Analysis: &types.RiskInfo{
Borrowed: 6.31,
Liquidation: 65.67,
Value: 252,
},
Position: &types.DecBalances{
Collateral: sdk.NewDecCoins(coin.Dec("UMEE", "60")),
Borrowed: sdk.NewDecCoins(coin.Dec("UMEE", "1.5")),
},
},
},
}
require.Equal(expected, *resp)
}

func (s *IntegrationTestSuite) TestQuerier_LiquidationTargets() {
ctx, require := s.ctx, s.Require()

Expand Down
10 changes: 5 additions & 5 deletions x/leverage/keeper/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ func symbolDecCoins(

for _, c := range coins {
exchangeRate := sdk.OneDec()
if types.HasUTokenPrefix(c.Denom) {
// uTokens will be converted to base tokens
c.Denom = types.ToTokenDenom(c.Denom)
exchangeRate = tokens[c.Denom].exchangeRate
}
t, ok := tokens[c.Denom]
if !ok {
// unregistered tokens cannot be converted, but can be returned as base denom
symbolCoins = symbolCoins.Add(sdk.NewDecCoinFromDec(c.Denom, sdk.NewDecFromInt(c.Amount)))
continue
}
if types.HasUTokenPrefix(c.Denom) {
// uTokens will be converted to base tokens
c.Denom = types.ToTokenDenom(c.Denom)
exchangeRate = t.exchangeRate
}
exponentMultiplier := ten.Power(uint64(t.exponent))
amount := exchangeRate.MulInt(c.Amount).Quo(exponentMultiplier)
symbolCoins = symbolCoins.Add(sdk.NewDecCoinFromDec(t.symbol, amount))
Expand Down

0 comments on commit 9935dbf

Please sign in to comment.