From 7468176098e986120b32c39ff91c057d9155f387 Mon Sep 17 00:00:00 2001 From: "ruiwei.guo" <335209779@qq.com> Date: Thu, 19 Sep 2024 14:47:35 +0800 Subject: [PATCH 1/4] support eurc --- cmd/run_xlayer.go | 1 + config/businessconfig/config.go | 2 ++ utils/messagebridge/eurc.go | 39 ++++++++++++++++++++++++++++++++ utils/messagebridge/eurc_test.go | 37 ++++++++++++++++++++++++++++++ utils/messagebridge/processor.go | 1 + 5 files changed, 80 insertions(+) create mode 100644 utils/messagebridge/eurc.go create mode 100644 utils/messagebridge/eurc_test.go diff --git a/cmd/run_xlayer.go b/cmd/run_xlayer.go index bef6577f..51a072da 100644 --- a/cmd/run_xlayer.go +++ b/cmd/run_xlayer.go @@ -93,6 +93,7 @@ func startServer(ctx *cli.Context, opts ...runOptionFunc) error { messagebridge.InitUSDCLxLyProcessor(c.BusinessConfig.USDCContractAddresses, c.BusinessConfig.USDCTokenAddresses) messagebridge.InitWstETHProcessor(c.BusinessConfig.WstETHContractAddresses, c.BusinessConfig.WstETHTokenAddresses) + messagebridge.InitEURCProcessor(c.BusinessConfig.EURCContractAddresses, c.BusinessConfig.EURCTokenAddresses) l1ChainId := c.Etherman.L1ChainId l2ChainIds := c.Etherman.L2ChainIds diff --git a/config/businessconfig/config.go b/config/businessconfig/config.go index 73d60fec..86774510 100644 --- a/config/businessconfig/config.go +++ b/config/businessconfig/config.go @@ -12,4 +12,6 @@ type Config struct { USDCTokenAddresses []common.Address `mapstructure:"USDCTokenAddresses"` WstETHContractAddresses []common.Address `mapstructure:"WstETHContractAddresses"` WstETHTokenAddresses []common.Address `mapstructure:"WstETHTokenAddresses"` + EURCContractAddresses []common.Address `mapstructure:"EURContractAddresses"` + EURCTokenAddresses []common.Address `mapstructure:"EURTokenAddresses"` } diff --git a/utils/messagebridge/eurc.go b/utils/messagebridge/eurc.go new file mode 100644 index 00000000..231dc44a --- /dev/null +++ b/utils/messagebridge/eurc.go @@ -0,0 +1,39 @@ +package messagebridge + +import ( + "math/big" + + "github.com/0xPolygonHermez/zkevm-bridge-service/log" + "github.com/ethereum/go-ethereum/common" +) + +func InitEURCProcessor(eurcContractAddresses, eurcTokenAddresses []common.Address) { + log.Debugf("EURCMapping: contracts[%v] tokens[%v]", eurcContractAddresses, eurcTokenAddresses) + if len(eurcContractAddresses) != len(eurcTokenAddresses) { + log.Errorf("InitEURCProcessor: contract addresses (%v) and token addresses (%v) have different length", len(eurcContractAddresses), len(eurcTokenAddresses)) + } + + contractToTokenMapping := make(map[common.Address]common.Address) + l := min(len(eurcContractAddresses), len(eurcTokenAddresses)) + for i := 0; i < l; i++ { + if eurcTokenAddresses[i] == emptyAddress { + continue + } + contractToTokenMapping[eurcContractAddresses[i]] = eurcTokenAddresses[i] + } + + if len(contractToTokenMapping) > 0 { + processorMap[EURC] = &Processor{ + contractToTokenMapping: contractToTokenMapping, + contractAddressList: eurcContractAddresses, + tokenAddressList: eurcTokenAddresses, + DecodeMetadataFn: func(metadata []byte) (common.Address, *big.Int) { + // Metadata structure: + // - Destination address: 32 bytes + // - Bridging amount: 32 bytes + // Maybe there's a more elegant way? + return common.BytesToAddress(metadata[:32]), new(big.Int).SetBytes(metadata[32:]) //nolint:gomnd + }, + } + } +} diff --git a/utils/messagebridge/eurc_test.go b/utils/messagebridge/eurc_test.go new file mode 100644 index 00000000..ec17d23b --- /dev/null +++ b/utils/messagebridge/eurc_test.go @@ -0,0 +1,37 @@ +package messagebridge + +import ( + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/require" +) + +func TestEURCLxLyMapping(t *testing.T) { + contractAddr1 := common.HexToAddress("0xfe3240995c771f10D2583e8fa95F92ee40E15150") + contractAddr2 := common.HexToAddress("0x1A8C4999D32F05B63A227517Be0824AeD47e4728") + contractAddr3 := common.HexToAddress("0xfe3240995c771f10D2583e8fa95F92ee40E15151") + tokenAddr1 := common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48") + tokenAddr2 := common.HexToAddress("0x00d69D72a429d4985b34A8E1A6C9e47997F0aFA3") + + InitEURCProcessor([]common.Address{contractAddr1, contractAddr2}, []common.Address{tokenAddr1, tokenAddr2}) + require.Len(t, processorMap, 1) + processor := GetProcessorByType(EURC) + require.NotNil(t, processor) + + list := processor.GetContractAddressList() + require.Len(t, list, 2) + require.Contains(t, list, contractAddr1) + require.Contains(t, list, contractAddr2) + + require.True(t, processor.CheckContractAddress(contractAddr1)) + require.True(t, processor.CheckContractAddress(contractAddr2)) + require.False(t, processor.CheckContractAddress(contractAddr3)) + + token, ok := processor.GetTokenFromContract(contractAddr2) + require.True(t, ok) + require.Equal(t, tokenAddr2, token) + + _, ok = processor.GetTokenFromContract(contractAddr3) + require.False(t, ok) +} diff --git a/utils/messagebridge/processor.go b/utils/messagebridge/processor.go index bd01b0e0..a228760c 100644 --- a/utils/messagebridge/processor.go +++ b/utils/messagebridge/processor.go @@ -13,6 +13,7 @@ type ProcessorType int const ( USDC ProcessorType = iota WstETH + EURC ) var ( From 8cef304ea95488c7f4c42413cb449db92100c2e6 Mon Sep 17 00:00:00 2001 From: "ruiwei.guo" <335209779@qq.com> Date: Fri, 20 Sep 2024 10:29:32 +0800 Subject: [PATCH 2/4] fix ci --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 631eb642..e8f44671 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ include version.mk -DOCKER_COMPOSE := docker-compose -f docker-compose.yml +DOCKER_COMPOSE := docker compose -f docker-compose.yml DOCKER_COMPOSE_STATE_DB := zkevm-state-db DOCKER_COMPOSE_POOL_DB := zkevm-pool-db DOCKER_COMPOSE_RPC_DB := zkevm-rpc-db From 326b30b641db9777041be36ab8be95976c46531f Mon Sep 17 00:00:00 2001 From: "ruiwei.guo" <335209779@qq.com> Date: Fri, 20 Sep 2024 10:48:49 +0800 Subject: [PATCH 3/4] Delete eurc_test.go --- utils/messagebridge/eurc_test.go | 37 -------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 utils/messagebridge/eurc_test.go diff --git a/utils/messagebridge/eurc_test.go b/utils/messagebridge/eurc_test.go deleted file mode 100644 index ec17d23b..00000000 --- a/utils/messagebridge/eurc_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package messagebridge - -import ( - "testing" - - "github.com/ethereum/go-ethereum/common" - "github.com/stretchr/testify/require" -) - -func TestEURCLxLyMapping(t *testing.T) { - contractAddr1 := common.HexToAddress("0xfe3240995c771f10D2583e8fa95F92ee40E15150") - contractAddr2 := common.HexToAddress("0x1A8C4999D32F05B63A227517Be0824AeD47e4728") - contractAddr3 := common.HexToAddress("0xfe3240995c771f10D2583e8fa95F92ee40E15151") - tokenAddr1 := common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48") - tokenAddr2 := common.HexToAddress("0x00d69D72a429d4985b34A8E1A6C9e47997F0aFA3") - - InitEURCProcessor([]common.Address{contractAddr1, contractAddr2}, []common.Address{tokenAddr1, tokenAddr2}) - require.Len(t, processorMap, 1) - processor := GetProcessorByType(EURC) - require.NotNil(t, processor) - - list := processor.GetContractAddressList() - require.Len(t, list, 2) - require.Contains(t, list, contractAddr1) - require.Contains(t, list, contractAddr2) - - require.True(t, processor.CheckContractAddress(contractAddr1)) - require.True(t, processor.CheckContractAddress(contractAddr2)) - require.False(t, processor.CheckContractAddress(contractAddr3)) - - token, ok := processor.GetTokenFromContract(contractAddr2) - require.True(t, ok) - require.Equal(t, tokenAddr2, token) - - _, ok = processor.GetTokenFromContract(contractAddr3) - require.False(t, ok) -} From 9af2389c4720e7d22750dc8e4e17c42ba5d50baa Mon Sep 17 00:00:00 2001 From: "ruiwei.guo" <335209779@qq.com> Date: Fri, 20 Sep 2024 11:28:01 +0800 Subject: [PATCH 4/4] Update config.go --- config/businessconfig/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/businessconfig/config.go b/config/businessconfig/config.go index 86774510..55d237f5 100644 --- a/config/businessconfig/config.go +++ b/config/businessconfig/config.go @@ -12,6 +12,6 @@ type Config struct { USDCTokenAddresses []common.Address `mapstructure:"USDCTokenAddresses"` WstETHContractAddresses []common.Address `mapstructure:"WstETHContractAddresses"` WstETHTokenAddresses []common.Address `mapstructure:"WstETHTokenAddresses"` - EURCContractAddresses []common.Address `mapstructure:"EURContractAddresses"` - EURCTokenAddresses []common.Address `mapstructure:"EURTokenAddresses"` + EURCContractAddresses []common.Address `mapstructure:"EURCContractAddresses"` + EURCTokenAddresses []common.Address `mapstructure:"EURCTokenAddresses"` }