Skip to content

Commit 31e1cc1

Browse files
committed
fix: allow string callback id
1 parent c79d331 commit 31e1cc1

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

app/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/cosmos/cosmos-sdk/types/module"
88
)
99

10-
const upgradeName = "0.6.2"
10+
const upgradeName = "0.6.4"
1111

1212
// RegisterUpgradeHandlers returns upgrade handlers
1313
func (app *InitiaApp) RegisterUpgradeHandlers(cfg module.Configurator) {

x/ibc-hooks/move-hooks/message.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package move_hooks
22

33
import (
4+
"encoding/json"
5+
46
movetypes "github.com/initia-labs/initia/x/move/types"
57
)
68

@@ -50,3 +52,45 @@ type HookData struct {
5052
// sender chain.
5153
AsyncCallback *AsyncCallback `json:"async_callback,omitempty"`
5254
}
55+
56+
// asyncCallback is same as AsyncCallback.
57+
type asyncCallback struct {
58+
// callback id should be issued form the executor contract
59+
Id uint64 `json:"id"`
60+
ModuleAddress string `json:"module_address"`
61+
ModuleName string `json:"module_name"`
62+
}
63+
64+
// asyncCallbackStringID is same as AsyncCallback but
65+
// it has Id as string.
66+
type asyncCallbackStringID struct {
67+
// callback id should be issued form the executor contract
68+
Id uint64 `json:"id,string"`
69+
ModuleAddress string `json:"module_address"`
70+
ModuleName string `json:"module_name"`
71+
}
72+
73+
// UnmarshalJSON implements the json unmarshaler interface.
74+
// custom unmarshaler is required because we have to handle
75+
// id as string and uint64.
76+
func (a *AsyncCallback) UnmarshalJSON(bz []byte) error {
77+
var ac asyncCallback
78+
err := json.Unmarshal(bz, &ac)
79+
if err != nil {
80+
var aStr asyncCallbackStringID
81+
err := json.Unmarshal(bz, &aStr)
82+
if err != nil {
83+
return err
84+
}
85+
86+
a.Id = aStr.Id
87+
a.ModuleAddress = aStr.ModuleAddress
88+
a.ModuleName = aStr.ModuleName
89+
return nil
90+
}
91+
92+
a.Id = ac.Id
93+
a.ModuleAddress = ac.ModuleAddress
94+
a.ModuleName = ac.ModuleName
95+
return nil
96+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package move_hooks_test
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
movehooks "github.com/initia-labs/initia/x/ibc-hooks/move-hooks"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func Test_Unmarshal_AsyncCallback(t *testing.T) {
12+
var callback movehooks.AsyncCallback
13+
err := json.Unmarshal([]byte(`{
14+
"id": 99,
15+
"module_address": "0x1",
16+
"module_name": "Counter"
17+
}`), &callback)
18+
require.NoError(t, err)
19+
require.Equal(t, movehooks.AsyncCallback{
20+
Id: 99,
21+
ModuleAddress: "0x1",
22+
ModuleName: "Counter",
23+
}, callback)
24+
25+
var callbackStringID movehooks.AsyncCallback
26+
err = json.Unmarshal([]byte(`{
27+
"id": "99",
28+
"module_address": "0x1",
29+
"module_name": "Counter"
30+
}`), &callbackStringID)
31+
require.NoError(t, err)
32+
require.Equal(t, callback, callbackStringID)
33+
}

0 commit comments

Comments
 (0)