From 918ed525aa9d317399087192e7e42dce396f3c02 Mon Sep 17 00:00:00 2001 From: Sai Kumar Date: Wed, 7 Feb 2024 15:48:20 +0530 Subject: [PATCH] add inflows storing when we receive native token --- x/uibc/quota/intest/quota_test.go | 42 ++++++++++++++++++++----------- x/uibc/quota/quota.go | 21 +++++++++++++--- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/x/uibc/quota/intest/quota_test.go b/x/uibc/quota/intest/quota_test.go index fb20c821e4..93bf4fb352 100644 --- a/x/uibc/quota/intest/quota_test.go +++ b/x/uibc/quota/intest/quota_test.go @@ -10,6 +10,7 @@ import ( "github.com/golang/mock/gomock" "gotest.tools/v3/assert" + appparams "github.com/umee-network/umee/v6/app/params" lfixtures "github.com/umee-network/umee/v6/x/leverage/fixtures" ltypes "github.com/umee-network/umee/v6/x/leverage/types" "github.com/umee-network/umee/v6/x/oracle/types" @@ -149,13 +150,14 @@ func TestKeeper_UndoUpdateQuota(t *testing.T) { } func TestKeeper_RecordIBCInflow(t *testing.T) { - atomAmount := sdkmath.NewInt(100_000000) - atomPrice := sdk.MustNewDecFromStr("0.37") + tokenAmount := sdkmath.NewInt(100_000000) + tokenPrice := sdk.MustNewDecFromStr("0.37") + tokenExponent := 6 // ibc denom = base_denom when sender chain is source chain - // ibc denom = ibc/XXX when sender chain is receiver chain - atomToken := sdk.NewCoin("uatom", atomAmount) - atomExponent := 6 - inflowBaseDenom := "ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9" + atomToken := sdk.NewCoin("uatom", tokenAmount) + // ibc denom = (port/channel/base_denom) when we receive token back which is send from UMEE + umeeNativeToken := sdk.NewCoin("transfer/channel-10/uumee", tokenAmount) + atomIBCDenom := "ibc/C4CFF46FD6DE35CA4CF4CE031E643C8FDC9BA4B99AE598E9B0ED98FE3A2319F9" ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -168,11 +170,16 @@ func TestKeeper_RecordIBCInflow(t *testing.T) { err := k.ResetAllQuotas() assert.NilError(t, err) - // ATOM, returns token settings and prices from mock leverage and oracle keepers, no errors expected - leverageMock.EXPECT().GetTokenSettings(ctx, inflowBaseDenom).Return( - lfixtures.Token(inflowBaseDenom, "ATOM", uint32(atomExponent)), nil, + // ATOM returns token settings and prices from mock leverage and oracle keepers, no errors expected + leverageMock.EXPECT().GetTokenSettings(ctx, atomIBCDenom).Return( + lfixtures.Token(atomIBCDenom, "ATOM", uint32(tokenExponent)), nil, ).AnyTimes() - oracleMock.EXPECT().Price(ctx, "ATOM").Return(atomPrice, nil).AnyTimes() + oracleMock.EXPECT().Price(ctx, "ATOM").Return(tokenPrice, nil).AnyTimes() + // UMEE + leverageMock.EXPECT().GetTokenSettings(ctx, appparams.BondDenom).Return( + lfixtures.Token(appparams.BondDenom, appparams.DisplayDenom, uint32(tokenExponent)), nil, + ).AnyTimes() + oracleMock.EXPECT().Price(ctx, appparams.DisplayDenom).Return(tokenPrice, nil).AnyTimes() packet := channeltypes.Packet{ Sequence: 10, @@ -183,10 +190,14 @@ func TestKeeper_RecordIBCInflow(t *testing.T) { Data: nil, } + // A -> B (record if B is receiver chain) ackErr := k.RecordIBCInflow(packet, atomToken.Denom, atomToken.Amount.String()) assert.Assert(t, nil, ackErr) + // A -> B (record if B is source chain, we are receiving token back which is send from A chain) + ackErr = k.RecordIBCInflow(packet, umeeNativeToken.Denom, umeeNativeToken.Amount.String()) + assert.Assert(t, nil, ackErr) - o := k.GetTokenInflow(inflowBaseDenom) + o := k.GetTokenInflow(atomIBCDenom) // expected inflow amount 37 =( atomPrice * atomAmount) / atomExponent inflowAmount := sdkmath.LegacyMustNewDecFromStr("37") assert.DeepEqual(t, o.Amount, inflowAmount) @@ -194,16 +205,19 @@ func TestKeeper_RecordIBCInflow(t *testing.T) { ackErr = k.RecordIBCInflow(packet, atomToken.Denom, atomToken.Amount.String()) assert.Assert(t, nil, ackErr) - o = k.GetTokenInflow(inflowBaseDenom) + o = k.GetTokenInflow(atomIBCDenom) assert.DeepEqual(t, o.Amount, inflowAmount.Add(inflowAmount)) + p := k.GetTokenInflow(appparams.BondDenom) + assert.DeepEqual(t, p.Amount, inflowAmount) + allInflows, err := k.GetAllInflows() assert.NilError(t, err) - assert.DeepEqual(t, allInflows, sdk.DecCoins{o}) + assert.DeepEqual(t, allInflows, sdk.DecCoins{o, p}) err = k.ResetAllQuotas() assert.NilError(t, err) - o = k.GetTokenInflow(inflowBaseDenom) + o = k.GetTokenInflow(atomIBCDenom) assert.DeepEqual(t, o.Amount, sdk.ZeroDec()) } diff --git a/x/uibc/quota/quota.go b/x/uibc/quota/quota.go index 5a67b7c5f4..0922ffc106 100644 --- a/x/uibc/quota/quota.go +++ b/x/uibc/quota/quota.go @@ -10,7 +10,6 @@ import ( channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" "github.com/cosmos/ibc-go/v7/modules/core/exported" - appparams "github.com/umee-network/umee/v6/app/params" "github.com/umee-network/umee/v6/util" "github.com/umee-network/umee/v6/util/coin" "github.com/umee-network/umee/v6/util/store" @@ -233,14 +232,28 @@ func (k Keeper) UndoUpdateQuota(denom string, amount sdkmath.Int) error { func (k Keeper) RecordIBCInflow(packet channeltypes.Packet, denom, amount string, ) exported.Acknowledgement { // if chain is recevier and sender chain is source then we need create ibc_denom (ibc/hash(channel,denom)) to - // check ibc_denom is exists in leverage token registry. - if ics20types.SenderChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), denom) && - denom != appparams.BondDenom { + // check ibc_denom to if it exists in the leverage token registry. + if ics20types.SenderChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), denom) { // SendPacket did not prefix the denom, so we must prefix denom here // NOTE: sourcePrefix already contains the trailing "/" sourcePrefix := ics20types.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel()) prefixedDenom := sourcePrefix + denom denom = ics20types.ParseDenomTrace(prefixedDenom).IBCDenom() + } else { + // if we receive back a token, that was originally sent from UMEE, then we need to fetch the native denom + // receive denom(port/channel/base_denom) to base_denom + + // remove prefix added by sender chain + voucherPrefix := ics20types.GetDenomPrefix(packet.GetSourcePort(), packet.GetSourceChannel()) + unprefixedDenom := denom[len(voucherPrefix):] + // coin denomination used in sending from the escrow address + denom = unprefixedDenom + // The denomination used to send the coins is either the native denom or the hash of the path + // if the denomination is not native. + denomTrace := ics20types.ParseDenomTrace(unprefixedDenom) + if !denomTrace.IsNativeDenom() { + denom = denomTrace.IBCDenom() + } } ts, err := k.leverage.GetTokenSettings(*k.ctx, denom)