Skip to content

Commit

Permalink
feat: erc20 additional data (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Sep 3, 2024
1 parent 25c41f5 commit 74f3c5b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy_testnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,4 @@ jobs:
fields: repo,message,commit,author,action,job,eventName,ref,workflow # selectable (default: repo,message)
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required
if: always()
if: always()
13 changes: 11 additions & 2 deletions chains/evm/executor/message-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package executor
import (
"bytes"
"errors"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -88,8 +89,8 @@ func PermissionlessGenericMessageHandler(msg *transfer.TransferMessage) (*propos
}

func ERC20MessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, error) {
if len(msg.Data.Payload) != 2 {
return nil, errors.New("malformed payload. Len of payload should be 2")
if len(msg.Data.Payload) != 2 && len(msg.Data.Payload) != 3 {
return nil, fmt.Errorf("wrong payload length %d", len(msg.Data.Payload))
}
amount, ok := msg.Data.Payload[0].([]byte)
if !ok {
Expand All @@ -104,6 +105,14 @@ func ERC20MessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, err
recipientLen := big.NewInt(int64(len(recipient))).Bytes()
data = append(data, common.LeftPadBytes(recipientLen, 32)...) // length of recipient (uint256)
data = append(data, recipient...) // recipient ([]byte)
if len(msg.Data.Payload) == 3 {
optionalMessage, ok := msg.Data.Payload[2].([]byte)
if !ok {
return nil, errors.New("wrong optional message format")
}

data = append(data, optionalMessage...)
}

return proposal.NewProposal(msg.Source, msg.Destination, transfer.TransferProposalData{
DepositNonce: msg.Data.DepositNonce,
Expand Down
37 changes: 35 additions & 2 deletions chains/evm/executor/message-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/stretchr/testify/suite"
)

var errIncorrectERC20PayloadLen = errors.New("malformed payload. Len of payload should be 2")
var errIncorrectERC20PayloadLen = errors.New("wrong payload length 1")
var errIncorrectERC721PayloadLen = errors.New("malformed payload. Len of payload should be 3")
var errIncorrectGenericPayloadLen = errors.New("malformed payload. Len of payload should be 1")
var errIncorrectERC1155PayloadLen = errors.New("malformed payload. Len of payload should be 4")
Expand All @@ -47,7 +47,6 @@ func (s *ERC20HandlerTestSuite) SetupTest() {}
func (s *ERC20HandlerTestSuite) TearDownTest() {}

func (s *ERC20HandlerTestSuite) TestERC20HandleMessage() {

message := &message.Message{
Source: 1,
Destination: 0,
Expand All @@ -70,6 +69,40 @@ func (s *ERC20HandlerTestSuite) TestERC20HandleMessage() {
s.NotNil(prop)
}

func (s *ERC20HandlerTestSuite) TestERC20HandleMessage_WithOptionalMessage() {
amount := []byte{2}
recipient := []byte{241, 229, 143, 177, 119, 4, 194, 218, 132, 121, 165, 51, 249, 250, 212, 173, 9, 147, 202, 107}
optionalMessage := []byte("optionalMessage")

expectedData := common.LeftPadBytes(amount, 32)
expectedData = append(expectedData, common.LeftPadBytes(big.NewInt(int64(len(recipient))).Bytes(), 32)...)
expectedData = append(expectedData, recipient...)
expectedData = append(expectedData, optionalMessage...)

message := &message.Message{
Source: 1,
Destination: 0,
Data: transfer.TransferMessageData{
DepositNonce: 1,
ResourceId: [32]byte{0},
Payload: []interface{}{
amount,
recipient,
optionalMessage,
},
Type: transfer.FungibleTransfer,
},
Type: transfer.TransferMessageType,
}

mh := executor.TransferMessageHandler{}
prop, err := mh.HandleMessage(message)

s.Nil(err)
s.NotNil(prop)
s.Equal(prop.Data.(transfer.TransferProposalData).Data, expectedData)
}

func (s *ERC20HandlerTestSuite) TestERC20HandleMessageIncorrectDataLen() {
message := &message.Message{
Source: 1,
Expand Down
10 changes: 9 additions & 1 deletion chains/evm/listener/depositHandlers/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,21 @@ func (dh *Erc20DepositHandler) HandleDeposit(
amount,
recipientAddress,
}

metadata := make(map[string]interface{})
// append optional message if it exists
if len(calldata) > int(96+recipientAddressLength.Int64()) {
metadata["gasLimit"] = new(big.Int).SetBytes(calldata[64+recipientAddressLength.Int64() : 96+recipientAddressLength.Int64()]).Uint64()
payload = append(payload, calldata[64+recipientAddressLength.Int64():])
}

return message.NewMessage(
sourceID,
destID,
transfer.TransferMessageData{
DepositNonce: nonce,
ResourceId: resourceID,
Metadata: nil,
Metadata: metadata,
Payload: payload,
Type: transfer.FungibleTransfer,
},
Expand Down
13 changes: 11 additions & 2 deletions chains/evm/listener/depositHandlers/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ func TestRunErc20HandlerTestSuite(t *testing.T) {
func (s *Erc20HandlerTestSuite) TestErc20HandleEvent() {
// 0xf1e58fb17704c2da8479a533f9fad4ad0993ca6b
recipientByteSlice := []byte{241, 229, 143, 177, 119, 4, 194, 218, 132, 121, 165, 51, 249, 250, 212, 173, 9, 147, 202, 107}
maxFee := big.NewInt(200000)
optionalMessage := common.LeftPadBytes(maxFee.Bytes(), 32)
optionalMessage = append(optionalMessage, []byte("optionalMessage")...)

metadata := make(map[string]interface{})
metadata["gasLimit"] = uint64(200000)

calldata := evm.ConstructErc20DepositData(recipientByteSlice, big.NewInt(2))
calldata = append(calldata, optionalMessage...)
depositLog := &events.Deposit{
DestinationDomainID: 0,
ResourceID: [32]byte{0},
Expand All @@ -45,7 +52,7 @@ func (s *Erc20HandlerTestSuite) TestErc20HandleEvent() {

sourceID := uint8(1)
amountParsed := calldata[:32]
recipientAddressParsed := calldata[64:]
recipientAddressParsed := calldata[64 : 64+len(recipientByteSlice)]

expected := &message.Message{
Source: sourceID,
Expand All @@ -56,8 +63,10 @@ func (s *Erc20HandlerTestSuite) TestErc20HandleEvent() {
Payload: []interface{}{
amountParsed,
recipientAddressParsed,
optionalMessage,
},
Type: transfer.FungibleTransfer,
Type: transfer.FungibleTransfer,
Metadata: metadata,
},
Type: transfer.TransferMessageType,
ID: "messageID",
Expand Down

0 comments on commit 74f3c5b

Please sign in to comment.