Skip to content

Commit

Permalink
fix: allow string callback id (#315)
Browse files Browse the repository at this point in the history
* fix: allow string callback id

* align variable name
  • Loading branch information
beer-1 authored Dec 3, 2024
1 parent c79d331 commit 103a58e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
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 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
}
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)
}

0 comments on commit 103a58e

Please sign in to comment.