Skip to content

Commit 0686453

Browse files
committed
refactor: Update IBC callback handling to include transfer details
1 parent 0b303e4 commit 0686453

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

tests/e2e/ibc_callbacks_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ func TestIBCCallbacks(t *testing.T) {
145145
assert.Equal(t, []byte("{\"result\":\"AQ==\"}"), response.IBCDestinationCallbacks[0].Ack.Data)
146146
assert.Equal(t,
147147
wasmvmtypes.Array[wasmvmtypes.Coin]{wasmvmtypes.NewCoin(1, ibcDenom)},
148-
response.IBCDestinationCallbacks[0].Funds,
148+
response.IBCDestinationCallbacks[0].Transfer.Funds,
149149
)
150+
assert.Equal(t, contractAddrA.String(), response.IBCDestinationCallbacks[0].Transfer.Receiver)
150151

151152
balances := chainB.GetWasmApp().BankKeeper.GetAllBalances(chainB.GetContext(), contractAddrB)
152153
// sanity check that the balance of the contract is correct
@@ -184,13 +185,13 @@ func TestIBCCallbacks(t *testing.T) {
184185
}
185186
}
186187

187-
func TestIBCDestinationCallbackFunds(t *testing.T) {
188+
func TestIBCDestinationCallbackTransfer(t *testing.T) {
188189
// scenario:
189190
// given two chains
190191
// with an ics-20 channel established
191192
// and an ibc-callbacks contract deployed on chain A
192193
// when someone sends an ibc transfer to chain B and back to the contract on A
193-
// then the contract on A should receive a destination chain callback with correct funds
194+
// then the contract on A should receive a destination chain callback with correct transfer info
194195

195196
coord := wasmibctesting.NewCoordinator(t, 2)
196197
chainA := wasmibctesting.NewWasmTestChain(coord.GetChain(ibctesting.GetChainID(1)))
@@ -226,7 +227,7 @@ func TestIBCDestinationCallbackFunds(t *testing.T) {
226227
contractAddr := chainA.InstantiateContract(codeID, []byte(`{}`))
227228
require.NotEmpty(t, contractAddr)
228229

229-
// when someone sends an ibc transfer to chain B and back to the contract on A
230+
// when someone sends an ibc transfer to chain B
230231
chainA.SendMsgs(
231232
ibctransfertypes.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID,
232233
oneToken[0], actorChainA.String(), actorChainB.String(), chainA.GetTimeoutHeight(), 0, ""),
@@ -252,8 +253,10 @@ func TestIBCDestinationCallbackFunds(t *testing.T) {
252253
// the denom should be reversed back correctly to the original denom
253254
assert.Equal(t,
254255
wasmvmtypes.Array[wasmvmtypes.Coin]{wasmvmtypes.NewCoin(1, sdk.DefaultBondDenom)},
255-
response.IBCDestinationCallbacks[0].Funds,
256+
response.IBCDestinationCallbacks[0].Transfer.Funds,
256257
)
258+
assert.Equal(t, contractAddr.String(), response.IBCDestinationCallbacks[0].Transfer.Receiver)
259+
assert.Equal(t, actorChainB.String(), response.IBCDestinationCallbacks[0].Transfer.Sender)
257260
}
258261

259262
func TestIBCCallbacksWithoutEntrypoints(t *testing.T) {

tests/e2e/testdata/ibc_callbacks.wasm

3.12 KB
Binary file not shown.

x/wasm/ibc.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ func (i IBCHandler) IBCReceivePacketCallback(
416416
return err
417417
}
418418

419-
var funds wasmvmtypes.Array[wasmvmtypes.Coin]
419+
var transfer *wasmvmtypes.IBCTransferCallback
420420

421421
var parsedAck channeltypes.Acknowledgement_Result
422422
err = json.Unmarshal(ack.Acknowledgement(), &parsedAck)
@@ -432,37 +432,40 @@ func (i IBCHandler) IBCReceivePacketCallback(
432432
return errorsmod.Wrap(err, "unmarshal transfer packet data")
433433
}
434434

435+
// just making sure we have a valid address
435436
receiverAddr, err := sdk.AccAddressFromBech32(transferData.Receiver)
436437
if err != nil {
437438
return err
438439
}
439-
// we only want to tell the contract about the funds if the transfer was sent to the contract
440-
if receiverAddr.Equals(contractAddr) {
441-
// fill funds with the transfer amount
442-
443-
// For a more in-depth explanation of the logic here, see the transfer module implementation:
444-
// https://github.com/cosmos/ibc-go/blob/a6217ab02a4d57c52a938eeaff8aeb383e523d12/modules/apps/transfer/keeper/relay.go#L147-L175
445-
if transferData.Token.Denom.HasPrefix(packet.GetSourcePort(), packet.GetSourceChannel()) {
446-
// this is a denom coming from this chain, being sent back again
447-
// remove prefix
448-
transferData.Token.Denom.Trace = transferData.Token.Denom.Trace[1:]
449-
} else {
450-
// prefixing happens on the receiving end, so we need to do that here
451-
trace := []transfertypes.Hop{transfertypes.NewHop(packet.GetDestPort(), packet.GetDestChannel())}
452-
transferData.Token.Denom.Trace = append(trace, transferData.Token.Denom.Trace...)
453-
}
454-
455-
funds = append(funds, wasmvmtypes.Coin{
456-
Denom: transferData.Token.GetDenom().IBCDenom(),
457-
Amount: transferData.Token.GetAmount(),
458-
})
440+
441+
// For a more in-depth explanation of the logic here, see the transfer module implementation:
442+
// https://github.com/cosmos/ibc-go/blob/a6217ab02a4d57c52a938eeaff8aeb383e523d12/modules/apps/transfer/keeper/relay.go#L147-L175
443+
if transferData.Token.Denom.HasPrefix(packet.GetSourcePort(), packet.GetSourceChannel()) {
444+
// this is a denom coming from this chain, being sent back again
445+
// remove prefix
446+
transferData.Token.Denom.Trace = transferData.Token.Denom.Trace[1:]
447+
} else {
448+
// prefixing happens on the receiving end, so we need to do that here
449+
trace := []transfertypes.Hop{transfertypes.NewHop(packet.GetDestPort(), packet.GetDestChannel())}
450+
transferData.Token.Denom.Trace = append(trace, transferData.Token.Denom.Trace...)
451+
}
452+
453+
transfer = &wasmvmtypes.IBCTransferCallback{
454+
Receiver: receiverAddr.String(),
455+
Sender: transferData.Sender,
456+
Funds: wasmvmtypes.Array[wasmvmtypes.Coin]{
457+
{
458+
Denom: transferData.Token.GetDenom().IBCDenom(),
459+
Amount: transferData.Token.GetAmount(),
460+
},
461+
},
459462
}
460463
}
461464

462465
msg := wasmvmtypes.IBCDestinationCallbackMsg{
463-
Ack: wasmvmtypes.IBCAcknowledgement{Data: ack.Acknowledgement()},
464-
Packet: newIBCPacket(packet),
465-
Funds: funds,
466+
Ack: wasmvmtypes.IBCAcknowledgement{Data: ack.Acknowledgement()},
467+
Packet: newIBCPacket(packet),
468+
Transfer: transfer,
466469
}
467470

468471
err = i.keeper.IBCDestinationCallback(cachedCtx, contractAddr, msg)

0 commit comments

Comments
 (0)