Skip to content

Commit

Permalink
fix(ibc): fix and add sanity check for GetDecTwapFromBytes (#152)
Browse files Browse the repository at this point in the history
* fix(ibc): fix and add sanity check for GetDecTwapFromBytes

* comment out debug log

* remove comment

* add err handler
  • Loading branch information
tuantran1702 authored Mar 6, 2024
1 parent b12eca5 commit 9d8dc6f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
13 changes: 8 additions & 5 deletions x/feeabs/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ func (k Keeper) OnAcknowledgementPacket(ctx sdk.Context, ack channeltypes.Acknow
}
continue
}

twapRate, err := k.GetDecTWAPFromBytes(icqRes.Value)
// k.Logger(ctx).Info(fmt.Sprintf("ICQ response %+v", icqRes))
// Not sure why, but the value is unmarshalled to icqRes.Key instead of icqRes.Value
// 10:36AM INF ICQ response {Code:0 Log: Info: Index:0 Key:[10 19 50 49 52 50 56 53 55 49 52 48 48 48 48 48 48 48 48 48 48] Value:[] ProofOps:<nil> Height:0 Codespace:}
twapRate, err := k.GetDecTWAPFromBytes(icqRes.Key)
if err != nil {
k.Logger(ctx).Error("Failed to get twap")
continue
Expand Down Expand Up @@ -202,14 +204,15 @@ func (k Keeper) GetChannelID(ctx sdk.Context) string {
return string(store.Get(types.KeyChannelID))
}

// TODO: add testing
func (k Keeper) GetDecTWAPFromBytes(bz []byte) (sdk.Dec, error) {
if bz == nil {
return sdk.Dec{}, sdkerrors.New("GetDecTWAPFromBytes: err ", 1, "nil bytes")
}
var ibcTokenTwap types.QueryArithmeticTwapToNowResponse
err := k.cdc.Unmarshal(bz, &ibcTokenTwap)
if err != nil {
if err != nil || ibcTokenTwap.ArithmeticTwap.IsNil() {
return sdk.Dec{}, sdkerrors.New("arithmeticTwap data umarshal", 1, err.Error())
}

return ibcTokenTwap.ArithmeticTwap, nil
}

Expand Down
16 changes: 16 additions & 0 deletions x/feeabs/keeper/ibc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package keeper_test

import (
"github.com/stretchr/testify/require"

sdkmath "cosmossdk.io/math"
)

func (s *KeeperTestSuite) TestGetDecTWAPFromBytes() {
s.SetupTest()

data := []byte{10, 19, 50, 49, 52, 50, 56, 53, 55, 49, 52, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48}
twap, err := s.feeAbsKeeper.GetDecTWAPFromBytes(data)
require.NoError(s.T(), err)
require.Equal(s.T(), sdkmath.LegacyMustNewDecFromStr("2.142857140000000000"), twap)
}

0 comments on commit 9d8dc6f

Please sign in to comment.