From 4d2d20cff4606fa08e70f00def26449365ddd13f Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Tue, 23 Jul 2024 12:15:29 +0200 Subject: [PATCH] fix_: allow sending 0 value transactions --- .../pathprocessor/processor_bridge_hop.go | 2 +- services/wallet/router/router_send_type.go | 18 +++++++ services/wallet/router/router_v2.go | 13 +++++ services/wallet/router/router_v2_test_data.go | 48 +++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/services/wallet/router/pathprocessor/processor_bridge_hop.go b/services/wallet/router/pathprocessor/processor_bridge_hop.go index e8d79047e..59cf666c1 100644 --- a/services/wallet/router/pathprocessor/processor_bridge_hop.go +++ b/services/wallet/router/pathprocessor/processor_bridge_hop.go @@ -438,7 +438,7 @@ func (h *HopBridgeProcessor) CalculateFees(params ProcessorInputParams) (*big.In // Remove token fee from bonder fee as said here: // https://docs.hop.exchange/v/developer-docs/api/api#get-v1-quote // `bonderFee` - The suggested bonder fee for the amount in. The bonder fee also includes the cost of the destination transaction fee. - tokenFee := ZeroBigIntValue //new(big.Int).Sub(h.bonderFee.AmountIn.Int, h.bonderFee.EstimatedRecieved.Int) + tokenFee := new(big.Int).Sub(bonderFee.AmountIn.Int, bonderFee.EstimatedRecieved.Int) return bonderFee.BonderFee.Int, tokenFee, nil } diff --git a/services/wallet/router/router_send_type.go b/services/wallet/router/router_send_type.go index e5c670ac5..4a4f1fc85 100644 --- a/services/wallet/router/router_send_type.go +++ b/services/wallet/router/router_send_type.go @@ -133,6 +133,24 @@ func (s SendType) canUseProcessor(p pathprocessor.PathProcessor) bool { } } +func (s SendType) processZeroAmountInProcessor(amountIn *big.Int, amountOut *big.Int, processorName string) bool { + if amountIn.Cmp(pathprocessor.ZeroBigIntValue) == 0 { + if s == Transfer { + if processorName != pathprocessor.ProcessorTransferName { + return false + } + } else if s == Swap { + if amountOut.Cmp(pathprocessor.ZeroBigIntValue) == 0 { + return false + } + } else { + return false + } + } + + return true +} + func (s SendType) isAvailableBetween(from, to *params.Network) bool { if s.IsCollectiblesTransfer() || s.IsEnsTransfer() || diff --git a/services/wallet/router/router_v2.go b/services/wallet/router/router_v2.go index c4c5e7f63..1a80a59f4 100644 --- a/services/wallet/router/router_v2.go +++ b/services/wallet/router/router_v2.go @@ -638,6 +638,15 @@ func (r *Router) getCrossChainsOptionsForSendingAmount(input *RouteInputParams, amountLocked := false amountToSend := input.AmountIn.ToInt() + + if amountToSend.Cmp(pathprocessor.ZeroBigIntValue) == 0 { + finalCrossChainAmountOptions[selectedFromChain.ChainID] = append(finalCrossChainAmountOptions[selectedFromChain.ChainID], amountOption{ + amount: amountToSend, + locked: false, + }) + continue + } + lockedAmount, fromChainLocked := input.FromLockedAmount[selectedFromChain.ChainID] if fromChainLocked { amountToSend = lockedAmount.ToInt() @@ -826,6 +835,10 @@ func (r *Router) resolveCandidates(ctx context.Context, input *RouteInputParams, continue } + if !input.SendType.processZeroAmountInProcessor(amountOption.amount, input.AmountOut.ToInt(), pProcessor.Name()) { + continue + } + for _, dest := range selectedTohains { if !input.SendType.isAvailableFor(network) { diff --git a/services/wallet/router/router_v2_test_data.go b/services/wallet/router/router_v2_test_data.go index c2541bad0..75e5780c3 100644 --- a/services/wallet/router/router_v2_test_data.go +++ b/services/wallet/router/router_v2_test_data.go @@ -207,6 +207,54 @@ type normalTestParams struct { func getNormalTestParamsList() []normalTestParams { return []normalTestParams{ + { + name: "ETH transfer - No Specific FromChain - No Specific ToChain - 0 AmountIn", + input: &RouteInputParams{ + testnetMode: false, + Uuid: uuid.NewString(), + SendType: Transfer, + AddrFrom: common.HexToAddress("0x1"), + AddrTo: common.HexToAddress("0x2"), + AmountIn: (*hexutil.Big)(big.NewInt(0)), + TokenID: pathprocessor.EthSymbol, + + testsMode: true, + testParams: &routerTestParams{ + tokenFrom: &token.Token{ + ChainID: 1, + Symbol: pathprocessor.EthSymbol, + Decimals: 18, + }, + tokenPrices: testTokenPrices, + suggestedFees: testSuggestedFees, + balanceMap: testBalanceMapPerChain, + estimationMap: testEstimationMap, + bonderFeeMap: testBbonderFeeMap, + approvalGasEstimation: testApprovalGasEstimation, + approvalL1Fee: testApprovalL1Fee, + }, + }, + expectedCandidates: []*PathV2{ + { + ProcessorName: pathprocessor.ProcessorTransferName, + FromChain: &mainnet, + ToChain: &mainnet, + ApprovalRequired: false, + }, + { + ProcessorName: pathprocessor.ProcessorTransferName, + FromChain: &optimism, + ToChain: &optimism, + ApprovalRequired: false, + }, + { + ProcessorName: pathprocessor.ProcessorTransferName, + FromChain: &arbitrum, + ToChain: &arbitrum, + ApprovalRequired: false, + }, + }, + }, { name: "ETH transfer - No Specific FromChain - No Specific ToChain", input: &RouteInputParams{