Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow string callback id #315

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
44 changes: 44 additions & 0 deletions x/ibc-hooks/move-hooks/message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package move_hooks

import (
"encoding/json"

movetypes "github.com/initia-labs/initia/x/move/types"
)

Expand Down Expand Up @@ -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 aStr asyncCallbackStringID
err := json.Unmarshal(bz, &aStr)
if err != nil {
return err
}

a.Id = aStr.Id
a.ModuleAddress = aStr.ModuleAddress
a.ModuleName = aStr.ModuleName
return nil
}

a.Id = ac.Id
a.ModuleAddress = ac.ModuleAddress
a.ModuleName = ac.ModuleName
return nil
}
33 changes: 33 additions & 0 deletions x/ibc-hooks/move-hooks/message_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading