Skip to content

Commit

Permalink
allow string ibc callback id (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 authored Dec 3, 2024
1 parent 8898144 commit 46ca792
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
40 changes: 40 additions & 0 deletions app/ibc-hooks/message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package evm_hooks

import (
"encoding/json"

evmtypes "github.com/initia-labs/minievm/x/evm/types"
)

Expand Down Expand Up @@ -43,3 +45,41 @@ type HookData struct {
// AsyncCallback is a contract address
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"`
ContractAddress string `json:"contract_address"`
}

// 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"`
ContractAddress string `json:"contract_address"`
}

// 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.ContractAddress = acStr.ContractAddress
return nil
}

a.Id = ac.Id
a.ContractAddress = ac.ContractAddress
return nil
}
30 changes: 30 additions & 0 deletions app/ibc-hooks/message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package evm_hooks_test

import (
"encoding/json"
"testing"

evmhooks "github.com/initia-labs/minievm/app/ibc-hooks"
"github.com/stretchr/testify/require"
)

func Test_Unmarshal_AsyncCallback(t *testing.T) {
var callback evmhooks.AsyncCallback
err := json.Unmarshal([]byte(`{
"id": 99,
"contract_address": "0x1"
}`), &callback)
require.NoError(t, err)
require.Equal(t, evmhooks.AsyncCallback{
Id: 99,
ContractAddress: "0x1",
}, callback)

var callbackStringID evmhooks.AsyncCallback
err = json.Unmarshal([]byte(`{
"id": "99",
"contract_address": "0x1"
}`), &callbackStringID)
require.NoError(t, err)
require.Equal(t, callback, callbackStringID)
}

0 comments on commit 46ca792

Please sign in to comment.