Skip to content

Commit

Permalink
test: assert forward memo structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Dec 17, 2024
1 parent 0c0fd1d commit 05f431f
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions tests/ibc-hooks/ibc_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,22 @@ func (suite *HooksTestSuite) TestCrosschainSwapsFinalMemo() {
suite.Require().NoError(err)
suite.Require().NotNil(res)

fungibleTokenPacketEvent := findFungibleTokenPacketEvent(res.GetEvents())
suite.Require().NotNil(fungibleTokenPacketEvent)

memo, err := parseFungibleTokenPacketEventMemo(fungibleTokenPacketEvent)
suite.Require().NotNil(memo)
suite.Require().NoError(err)

_, finalMemo := extractNextAndFinalMemosFromSwapMsg(memo)
suite.Require().NotNil(finalMemo)
suite.assertForwardMemoStructure(
finalMemo,
suite.GetSenderChannel(ChainB, ChainC),
"transfer",
accountC.String(),
)

// Declare the expected amounts on C
expectedPreToken0BalanceOnC2 := postToken0BalanceOnC.Amount
expectedPostToken0BalanceOnC2 := postToken0BalanceOnC.Amount.Add(osmomath.NewInt(receiveAmount))
Expand Down Expand Up @@ -2230,3 +2246,73 @@ func (suite *HooksTestSuite) TestCrosschainSwapsFinalMemo() {
postToken0BalanceOnC2 := suite.chainC.GetOsmosisApp().BankKeeper.GetBalance(suite.chainC.GetContext(), accountC, "token0")
suite.Require().Equal(expectedPostToken0BalanceOnC2, postToken0BalanceOnC2.Amount)
}

func findFungibleTokenPacketEvent(events []abci.Event) *abci.Event {
for i := 0; i < len(events); i++ {
if events[i].Type == "fungible_token_packet" {
return &events[i]
}
}
return nil
}

func parseFungibleTokenPacketEventMemo(event *abci.Event) (map[string]any, error) {
for i := 0; i < len(event.Attributes); i++ {
if event.Attributes[i].Key == "memo" {
var memo map[string]any
decoder := json.NewDecoder(strings.NewReader(event.Attributes[i].Value))
err := decoder.Decode(&memo)
if err != nil {
return nil, err
}
return memo, nil
}
}
return nil, fmt.Errorf("could not find memo in event")
}

func extractNextAndFinalMemosFromSwapMsg(memo map[string]any) (nextMemo map[string]any, finalMemo map[string]any) {
var ok bool

wasm, ok := memo["wasm"].(map[string]any)
if !ok {
return nil, nil
}

msg, ok := wasm["msg"].(map[string]any)
if !ok {
return nil, nil
}

osmosisSwap, ok := msg["osmosis_swap"].(map[string]any)
if !ok {
return nil, nil
}

nextMemoAny := osmosisSwap["next_memo"]
if nextMemoAny != nil {
nextMemo, ok = nextMemoAny.(map[string]any)
if !ok {
return nil, nil
}
}

finalMemoAny := osmosisSwap["final_memo"]
if finalMemoAny != nil {
finalMemo, ok = finalMemoAny.(map[string]any)
if !ok {
return nil, nil
}
}

return
}

func (suite *HooksTestSuite) assertForwardMemoStructure(memo map[string]any, channel, port, receiver string) {
forward, ok := memo["forward"].(map[string]any)
suite.Require().True(ok)

suite.Require().Equal(channel, forward["channel"])
suite.Require().Equal(port, forward["port"])
suite.Require().Equal(receiver, forward["receiver"])
}

0 comments on commit 05f431f

Please sign in to comment.