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

feat: add erc1155 listener/handler #254

Merged
merged 16 commits into from
Mar 21, 2024
5 changes: 5 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ func Run() error {
depositHandler.RegisterDepositHandler(handler.Address, coreListener.Erc721DepositHandler)
mh.RegisterMessageHandler(handler.Address, coreExecutor.ERC721MessageHandler)
}
case "erc1155":
{
depositHandler.RegisterDepositHandler(handler.Address, listener.Erc1155DepositHandler)
mh.RegisterMessageHandler(handler.Address, executor.Erc1155MessageHandler)
}
}
}
depositListener := coreEvents.NewListener(client)
Expand Down
54 changes: 54 additions & 0 deletions chains/evm/executor/message-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package executor

import (
"bytes"
"encoding/gob"
"errors"
"math/big"

Expand Down Expand Up @@ -52,3 +53,56 @@ func PermissionlessGenericMessageHandler(msg *message.Message, handlerAddr, brid

return proposal.NewProposal(msg.Source, msg.Destination, msg.DepositNonce, msg.ResourceId, data.Bytes(), handlerAddr, bridgeAddress, msg.Metadata), nil
}

func Erc1155MessageHandler(msg *message.Message, handlerAddr, bridgeAddress common.Address) (*proposal.Proposal, error) {

if len(msg.Payload) != 4 {
return nil, errors.New("malformed payload. Len of payload should be 4")
}
tokenIDs, ok := msg.Payload[0].([]*big.Int)
if !ok {
return nil, errors.New("wrong payload tokenIDs format")
}
amounts, ok := msg.Payload[1].([]*big.Int)
if !ok {
return nil, errors.New("wrong payload amounts format")
}
recipient, ok := msg.Payload[2].([]byte)
if !ok {
return nil, errors.New("wrong payload recipient format")
}
transferData, ok := msg.Payload[3].([]byte)
if !ok {
return nil, errors.New("wrong payload transferData format")
}

// Convert []*big.Int slices to []byte
tokenIDsBytes, err := encodeBigIntSlice(tokenIDs)
if err != nil {
return nil, err
}

amountsBytes, err := encodeBigIntSlice(amounts)
if err != nil {
return nil, err
}

// Concatenate the byte slices
data := bytes.Buffer{}
data.Write(tokenIDsBytes)
data.Write(amountsBytes)
data.Write(recipient)
data.Write(transferData)

return proposal.NewProposal(msg.Source, msg.Destination, msg.DepositNonce, msg.ResourceId, data.Bytes(), handlerAddr, bridgeAddress, msg.Metadata), nil
}

func encodeBigIntSlice(ints []*big.Int) ([]byte, error) {
var buffer bytes.Buffer
encoder := gob.NewEncoder(&buffer)
err := encoder.Encode(ints)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
43 changes: 43 additions & 0 deletions chains/evm/executor/message-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,46 @@ func (s *PermissionlessGenericHandlerTestSuite) Test_HandleMessage() {
s.Nil(err)
s.Equal(expected, prop)
}

// Erc1155
type Erc1155HandlerTestSuite struct {
suite.Suite
}

func TestRunErc1155HandlerTestSuite(t *testing.T) {
suite.Run(t, new(Erc1155HandlerTestSuite))
}

func (s *Erc1155HandlerTestSuite) SetupSuite() {}
func (s *Erc1155HandlerTestSuite) TearDownSuite() {}
func (s *Erc1155HandlerTestSuite) SetupTest() {}
func (s *Erc1155HandlerTestSuite) TearDownTest() {}

func (s *Erc1155HandlerTestSuite) TestErc1155MessageHandler() {
message := &message.Message{
Source: 1,
Destination: 0,
DepositNonce: 1,
ResourceId: [32]byte{0},
Type: message.FungibleTransfer,
Payload: []interface{}{
[]*big.Int{
big.NewInt(1),
big.NewInt(2),
big.NewInt(3),
},
[]*big.Int{
big.NewInt(1),
big.NewInt(2),
big.NewInt(3),
},
[]byte{241, 229, 143, 177, 119, 4, 194, 218, 132, 121, 165, 51, 249, 250, 212, 173, 9, 147, 202, 107},
[]byte{},
},
}

prop, err := executor.Erc1155MessageHandler(message, common.HexToAddress("0x4CEEf6139f00F9F4535Ad19640Ff7A0137708485"), common.HexToAddress("0xf1e58fb17704c2da8479a533f9fad4ad0993ca6b"))

s.Nil(err)
s.NotNil(prop)
}
29 changes: 29 additions & 0 deletions chains/evm/listener/deposit-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (

"github.com/ChainSafe/chainbridge-core/relayer/message"
"github.com/ChainSafe/chainbridge-core/types"
"github.com/umbracle/ethgo/abi"
)

const (
PermissionlessGenericTransfer message.TransferType = "PermissionlessGenericTransfer"
ERC1155Transfer message.TransferType = "Erc1155"
)

// GenericDepositHandler converts data pulled from generic deposit event logs into message
Expand Down Expand Up @@ -79,3 +81,30 @@ func Erc20DepositHandler(sourceID, destId uint8, nonce uint64, resourceID types.

return message.NewMessage(sourceID, destId, nonce, resourceID, message.FungibleTransfer, payload, message.Metadata{}), nil
}

type CallData struct {
Tokenids []*big.Int
Amounts []*big.Int
Recipient []byte
Transferdata []byte
}

func Erc1155DepositHandler(sourceID, destId uint8, nonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte) (*message.Message, error) {

typ := abi.MustNewType("tuple(uint256[] tokenIDs, uint256[] amounts, bytes transferData, bytes transferData)")
var decodedCallData CallData

err := typ.DecodeStruct(calldata, &decodedCallData)
if err != nil {
return nil, err
}

payload := []interface{}{
decodedCallData.Tokenids,
decodedCallData.Amounts,
decodedCallData.Recipient[:20],
decodedCallData.Transferdata[20:],
}

return message.NewMessage(sourceID, destId, nonce, resourceID, ERC1155Transfer, payload, message.Metadata{}), nil
}
50 changes: 50 additions & 0 deletions chains/evm/listener/deposit-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,53 @@ func (s *Erc20HandlerTestSuite) TestErc20HandleEventAlternativeAmount() {
s.Equal(message, expected)
s.Equal(big.NewInt(200), new(big.Int).SetBytes(message.Payload[0].([]byte)))
}

type Erc1155HandlerTestSuite struct {
suite.Suite
}

func TestRunErc1155HandlerTestSuite(t *testing.T) {
suite.Run(t, new(Erc20HandlerTestSuite))
}

func (s *Erc1155HandlerTestSuite) TestErc1155HandleEvent() {
tcar121293 marked this conversation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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 241, 229, 143, 177, 119, 4, 194, 218, 132, 121, 165, 51, 249, 250, 212, 173, 9, 147, 202, 107, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

depositLog := &events.Deposit{
DestinationDomainID: 2,
ResourceID: [32]byte{0},
DepositNonce: 1,
SenderAddress: common.HexToAddress("0x4CEEf6139f00F9F4535Ad19640Ff7A0137708485"),
Data: callData,
HandlerResponse: []byte{},
}
sourceID := uint8(1)

expected := &message.Message{
Source: sourceID,
Destination: depositLog.DestinationDomainID,
DepositNonce: depositLog.DepositNonce,
ResourceId: depositLog.ResourceID,
Type: listener.ERC1155Transfer,
Payload: []interface{}{
[]*big.Int{
big.NewInt(1),
big.NewInt(2),
big.NewInt(3),
},
[]*big.Int{
big.NewInt(1),
big.NewInt(2),
big.NewInt(3),
},
[]byte{241, 229, 143, 177, 119, 4, 194, 218, 132, 121, 165, 51, 249, 250, 212, 173, 9, 147, 202, 107},
[]byte{},
},
}

message, err := listener.Erc1155DepositHandler(sourceID, depositLog.DestinationDomainID, depositLog.DepositNonce, depositLog.ResourceID, depositLog.Data, depositLog.HandlerResponse)

s.Nil(err)
s.NotNil(message)
s.Equal(message, expected)
}
5 changes: 5 additions & 0 deletions example/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ func Run() error {
depositHandler.RegisterDepositHandler(handler.Address, coreListener.Erc721DepositHandler)
mh.RegisterMessageHandler(handler.Address, coreExecutor.ERC721MessageHandler)
}
case "erc1155":
{
depositHandler.RegisterDepositHandler(handler.Address, listener.Erc1155DepositHandler)
mh.RegisterMessageHandler(handler.Address, executor.Erc1155MessageHandler)
}
}
}
depositListener := coreEvents.NewListener(client)
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/umbracle/ethgo v0.1.3 // indirect
github.com/umbracle/fastrlp v0.0.0-20220527094140-59d5dd30e722 // indirect
github.com/valyala/fastjson v1.4.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
)

Expand All @@ -39,7 +43,7 @@ require (
github.com/agl/ed25519 v0.0.0-20200305024217-f36fc4b53d43 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd v0.22.0-beta.0.20220201204404-81fbd9b67e54 // indirect
github.com/btcsuite/btcd v0.22.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/btcsuite/btcutil v1.0.3-0.20211129182920-9c4bbabe7acd // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBT
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btcd v0.22.0-beta.0.20220201204404-81fbd9b67e54 h1:khJx6kvXopB224O05cs6iwmX/zAh7RlkMf8MiPXVS1I=
github.com/btcsuite/btcd v0.22.0-beta.0.20220201204404-81fbd9b67e54/go.mod h1:vkwesBkYQtKXFYQYi9PyahtopbX53Tvk/O/qp2WI6Gk=
github.com/btcsuite/btcd v0.22.1/go.mod h1:wqgTSL29+50LRkmOVknEdmt8ZojIzhuWvgu/iptuN7Y=
github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k=
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
Expand Down Expand Up @@ -288,6 +289,8 @@ github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+u
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa h1:Q75Upo5UN4JbPFURXZ8nLKYUvF85dyFRop/vQ0Rv+64=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM=
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
Expand Down Expand Up @@ -728,10 +731,16 @@ github.com/tklauser/go-sysconf v0.3.5/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITn
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef h1:wHSqTBrZW24CsNJDfeh9Ex6Pm0Rcpc7qrgKBiL44vF4=
github.com/umbracle/ethgo v0.1.3 h1:s8D7Rmphnt71zuqrgsGTMS5gTNbueGO1zKLh7qsFzTM=
github.com/umbracle/ethgo v0.1.3/go.mod h1:g9zclCLixH8liBI27Py82klDkW7Oo33AxUOr+M9lzrU=
github.com/umbracle/fastrlp v0.0.0-20220527094140-59d5dd30e722 h1:10Nbw6cACsnQm7r34zlpJky+IzxVLRk6MKTS2d3Vp0E=
github.com/umbracle/fastrlp v0.0.0-20220527094140-59d5dd30e722/go.mod h1:c8J0h9aULj2i3umrfyestM6jCq0LK0U6ly6bWy96nd4=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.10.2 h1:x3p8awjp/2arX+Nl/G2040AZpOCHS/eMJJ1/a+mye4Y=
github.com/valyala/fastjson v1.4.1 h1:hrltpHpIpkaxll8QltMU8c3QZ5+qIiCL8yKqPFJI/yE=
github.com/valyala/fastjson v1.4.1/go.mod h1:nV6MsjxL2IMJQUoHDIrjEI7oLyeqK6aBD7EFWPsvP8o=
github.com/vedhavyas/go-subkey v1.0.3 h1:iKR33BB/akKmcR2PMlXPBeeODjWLM90EL98OrOGs8CA=
github.com/vedhavyas/go-subkey v1.0.3/go.mod h1:CloUaFQSSTdWnINfBRFjVMkWXZANW+nd8+TI5jYcl6Y=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
Expand Down Expand Up @@ -807,6 +816,7 @@ golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20200602180216-279210d13fed/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
Expand Down Expand Up @@ -1025,6 +1035,7 @@ golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
Expand Down
Loading