Skip to content

Commit 9d8dc6f

Browse files
authored
fix(ibc): fix and add sanity check for GetDecTwapFromBytes (#152)
* fix(ibc): fix and add sanity check for GetDecTwapFromBytes * comment out debug log * remove comment * add err handler
1 parent b12eca5 commit 9d8dc6f

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

x/feeabs/keeper/ibc.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ func (k Keeper) OnAcknowledgementPacket(ctx sdk.Context, ack channeltypes.Acknow
146146
}
147147
continue
148148
}
149-
150-
twapRate, err := k.GetDecTWAPFromBytes(icqRes.Value)
149+
// k.Logger(ctx).Info(fmt.Sprintf("ICQ response %+v", icqRes))
150+
// Not sure why, but the value is unmarshalled to icqRes.Key instead of icqRes.Value
151+
// 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:}
152+
twapRate, err := k.GetDecTWAPFromBytes(icqRes.Key)
151153
if err != nil {
152154
k.Logger(ctx).Error("Failed to get twap")
153155
continue
@@ -202,14 +204,15 @@ func (k Keeper) GetChannelID(ctx sdk.Context) string {
202204
return string(store.Get(types.KeyChannelID))
203205
}
204206

205-
// TODO: add testing
206207
func (k Keeper) GetDecTWAPFromBytes(bz []byte) (sdk.Dec, error) {
208+
if bz == nil {
209+
return sdk.Dec{}, sdkerrors.New("GetDecTWAPFromBytes: err ", 1, "nil bytes")
210+
}
207211
var ibcTokenTwap types.QueryArithmeticTwapToNowResponse
208212
err := k.cdc.Unmarshal(bz, &ibcTokenTwap)
209-
if err != nil {
213+
if err != nil || ibcTokenTwap.ArithmeticTwap.IsNil() {
210214
return sdk.Dec{}, sdkerrors.New("arithmeticTwap data umarshal", 1, err.Error())
211215
}
212-
213216
return ibcTokenTwap.ArithmeticTwap, nil
214217
}
215218

x/feeabs/keeper/ibc_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package keeper_test
2+
3+
import (
4+
"github.com/stretchr/testify/require"
5+
6+
sdkmath "cosmossdk.io/math"
7+
)
8+
9+
func (s *KeeperTestSuite) TestGetDecTWAPFromBytes() {
10+
s.SetupTest()
11+
12+
data := []byte{10, 19, 50, 49, 52, 50, 56, 53, 55, 49, 52, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48}
13+
twap, err := s.feeAbsKeeper.GetDecTWAPFromBytes(data)
14+
require.NoError(s.T(), err)
15+
require.Equal(s.T(), sdkmath.LegacyMustNewDecFromStr("2.142857140000000000"), twap)
16+
}

0 commit comments

Comments
 (0)