diff --git a/app/upgrade.go b/app/upgrade.go index 21308195..9279a0db 100644 --- a/app/upgrade.go +++ b/app/upgrade.go @@ -7,7 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" ) -const upgradeName = "0.6.2" +const upgradeName = "0.6.4" // RegisterUpgradeHandlers returns upgrade handlers func (app *InitiaApp) RegisterUpgradeHandlers(cfg module.Configurator) { diff --git a/x/ibc-hooks/move-hooks/message.go b/x/ibc-hooks/move-hooks/message.go index 1eb161ef..5c16b27c 100644 --- a/x/ibc-hooks/move-hooks/message.go +++ b/x/ibc-hooks/move-hooks/message.go @@ -1,6 +1,8 @@ package move_hooks import ( + "encoding/json" + movetypes "github.com/initia-labs/initia/x/move/types" ) @@ -50,3 +52,45 @@ type HookData struct { // sender chain. AsyncCallback *AsyncCallback `json:"async_callback,omitempty"` } + +// asyncCallback is same as AsyncCallback. +type asyncCallback struct { + // callback id should be issued form the executor contract + Id uint64 `json:"id"` + ModuleAddress string `json:"module_address"` + ModuleName string `json:"module_name"` +} + +// asyncCallbackStringID is same as AsyncCallback but +// it has Id as string. +type asyncCallbackStringID struct { + // callback id should be issued form the executor contract + Id uint64 `json:"id,string"` + ModuleAddress string `json:"module_address"` + ModuleName string `json:"module_name"` +} + +// UnmarshalJSON implements the json unmarshaler interface. +// custom unmarshaler is required because we have to handle +// id as string and uint64. +func (a *AsyncCallback) UnmarshalJSON(bz []byte) error { + var ac asyncCallback + err := json.Unmarshal(bz, &ac) + if err != nil { + var acStr asyncCallbackStringID + err := json.Unmarshal(bz, &acStr) + if err != nil { + return err + } + + a.Id = acStr.Id + a.ModuleAddress = acStr.ModuleAddress + a.ModuleName = acStr.ModuleName + return nil + } + + a.Id = ac.Id + a.ModuleAddress = ac.ModuleAddress + a.ModuleName = ac.ModuleName + return nil +} diff --git a/x/ibc-hooks/move-hooks/message_test.go b/x/ibc-hooks/move-hooks/message_test.go new file mode 100644 index 00000000..199918d3 --- /dev/null +++ b/x/ibc-hooks/move-hooks/message_test.go @@ -0,0 +1,33 @@ +package move_hooks_test + +import ( + "encoding/json" + "testing" + + movehooks "github.com/initia-labs/initia/x/ibc-hooks/move-hooks" + "github.com/stretchr/testify/require" +) + +func Test_Unmarshal_AsyncCallback(t *testing.T) { + var callback movehooks.AsyncCallback + err := json.Unmarshal([]byte(`{ + "id": 99, + "module_address": "0x1", + "module_name": "Counter" + }`), &callback) + require.NoError(t, err) + require.Equal(t, movehooks.AsyncCallback{ + Id: 99, + ModuleAddress: "0x1", + ModuleName: "Counter", + }, callback) + + var callbackStringID movehooks.AsyncCallback + err = json.Unmarshal([]byte(`{ + "id": "99", + "module_address": "0x1", + "module_name": "Counter" + }`), &callbackStringID) + require.NoError(t, err) + require.Equal(t, callback, callbackStringID) +}