Skip to content

Commit

Permalink
handle receiver and coin type
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed May 23, 2023
1 parent bcc0b2e commit 12b0ce6
Show file tree
Hide file tree
Showing 2 changed files with 326 additions and 5 deletions.
237 changes: 237 additions & 0 deletions x/cronos/keeper/precompiles/lib/cosmos_types.abigen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 89 additions & 5 deletions x/cronos/keeper/precompiles/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package precompiles
import (
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"strings"

sdkmath "cosmossdk.io/math"

"github.com/crypto-org-chain/cronos/v2/x/cronos/keeper/precompiles/lib"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
Expand All @@ -15,6 +20,7 @@ import (

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v5/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v5/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
Expand Down Expand Up @@ -94,6 +100,52 @@ func AllLogsFromEvents(events []abci.Event) ([][]*ethtypes.Log, error) {
return allLogs, nil
}

func convertAddress(addrString string) (*common.Address, error) {
cfg := sdk.GetConfig()
var addr []byte
// try hex, then bech32
switch {
case common.IsHexAddress(addrString):
addr = common.HexToAddress(addrString).Bytes()
case strings.HasPrefix(addrString, cfg.GetBech32ValidatorAddrPrefix()):
addr, _ = sdk.ValAddressFromBech32(addrString)
case strings.HasPrefix(addrString, cfg.GetBech32AccountAddrPrefix()):
addr, _ = sdk.AccAddressFromBech32(addrString)
default:
return nil, fmt.Errorf("expected a valid hex or bech32 address (acc prefix %s), got '%s'", cfg.GetBech32AccountAddrPrefix(), addrString)
}
to := common.BytesToAddress(addr)
return &to, nil
}

var coinType, _ = abi.NewType("tuple[]", "structCosmos.Coin[]", []abi.ArgumentMarshaling{
{
Name: "amount",
Type: "uint256",
InternalType: "uint256",
Components: nil,
Indexed: false,
},
{
Name: "denom",
Type: "string",
InternalType: "string",
Components: nil,
Indexed: false,
},
})

func sdkCoinsToEvmCoins(sdkCoins sdk.Coins) []lib.CosmosCoin {
evmCoins := make([]lib.CosmosCoin, len(sdkCoins))
for i, coin := range sdkCoins {
evmCoins[i] = lib.CosmosCoin{
Amount: coin.Amount.BigInt(),
Denom: coin.Denom,
}
}
return evmCoins
}

// ParseTxLogsFromEvent parse tx logs from one event
func ParseTxLogsFromEvent(event abci.Event) ([]*ethtypes.Log, error) {
var ethLogs []*ethtypes.Log
Expand Down Expand Up @@ -144,20 +196,52 @@ func ParseTxLogsFromEvent(event abci.Event) ([]*ethtypes.Log, error) {
}

arg := []interface{}{method}
attrVals := make([]any, 0)
nonIndexedInputs := abi.Arguments{}
for _, attr := range event.Attributes {
arg = append(arg, string(attr.Value))
if string(attr.Key) == "packet_data" {
var tokenPacketData types.FungibleTokenPacketData
err := json.Unmarshal(attr.Value, &tokenPacketData)
if err != nil {
return nil, err
}
receiver, err := convertAddress(tokenPacketData.Receiver)
if err != nil {
return nil, err
}
arg = append(arg, receiver.String())
amt, ok := sdkmath.NewIntFromString(tokenPacketData.Amount)
if !ok {
return nil, err
}
coin := sdk.NewCoin(tokenPacketData.Denom, amt)
nonIndexedInputs = append(nonIndexedInputs, abi.Argument{
Name: "amount",
Type: coinType,
Indexed: false,
})
attrVals = append(attrVals, sdkCoinsToEvmCoins(sdk.NewCoins(coin)))
} else {
arg = append(arg, string(attr.Value))
}
}

ethLog := new(ethtypes.Log)
params := args{[][]interface{}{arg}}
topics, err := abi.MakeTopics(params.query...)
if err != nil {
return nil, err
}
if len(topics) > 0 {
ethLogs = append(ethLogs, &ethtypes.Log{
Topics: topics[0],
})
ethLog.Topics = topics[0]
}
if len(nonIndexedInputs) > 0 {
ethLog.Data, err = nonIndexedInputs.PackValues(attrVals)
if err != nil {
return nil, err
}
}
return ethLogs, nil
return append(ethLogs, ethLog), nil
}

func unmarshalAndExec[T MsgType, U any](
Expand Down

0 comments on commit 12b0ce6

Please sign in to comment.