diff --git a/api/gateway/chain/internal/service/gateway/commands/transfer/fee.go b/api/gateway/chain/internal/service/gateway/commands/transfer/fee.go index 4dfa741..e662e07 100644 --- a/api/gateway/chain/internal/service/gateway/commands/transfer/fee.go +++ b/api/gateway/chain/internal/service/gateway/commands/transfer/fee.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/tech/sendico/gateway/chain/internal/service/gateway/driver" + "github.com/tech/sendico/gateway/chain/internal/service/gateway/shared" "github.com/tech/sendico/pkg/api/routers/gsresponse" "github.com/tech/sendico/pkg/merrors" "github.com/tech/sendico/pkg/mservice" @@ -84,19 +85,28 @@ func (c *estimateTransferFeeCommand) Execute(ctx context.Context, req *chainv1.E return gsresponse.InvalidArgument[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, err) } + walletForFee := sourceWallet + nativeCurrency := shared.NativeCurrency(networkCfg) + if nativeCurrency != "" && strings.EqualFold(nativeCurrency, amount.GetCurrency()) { + copyWallet := *sourceWallet + copyWallet.ContractAddress = "" + copyWallet.TokenSymbol = nativeCurrency + walletForFee = ©Wallet + } + driverDeps := driver.Deps{ Logger: c.deps.Logger, Registry: c.deps.Networks, RPCTimeout: c.deps.RPCTimeout, } - feeMoney, err := chainDriver.EstimateFee(ctx, driverDeps, networkCfg, sourceWallet, destinationAddress, amount) + feeMoney, err := chainDriver.EstimateFee(ctx, driverDeps, networkCfg, walletForFee, destinationAddress, amount) if err != nil { c.deps.Logger.Warn("fee estimation failed", zap.Error(err)) return gsresponse.Auto[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, err) } contextLabel := "erc20_transfer" - if strings.TrimSpace(sourceWallet.ContractAddress) == "" { + if strings.TrimSpace(walletForFee.ContractAddress) == "" { contextLabel = "native_transfer" } resp := &chainv1.EstimateTransferFeeResponse{ diff --git a/api/gateway/chain/internal/service/gateway/commands/transfer/submit.go b/api/gateway/chain/internal/service/gateway/commands/transfer/submit.go index 52cbb9b..7729e72 100644 --- a/api/gateway/chain/internal/service/gateway/commands/transfer/submit.go +++ b/api/gateway/chain/internal/service/gateway/commands/transfer/submit.go @@ -113,6 +113,14 @@ func (c *submitTransferCommand) Execute(ctx context.Context, req *chainv1.Submit netAmount := shared.CloneMoney(amount) netAmount.Amount = netDec.String() + effectiveTokenSymbol := sourceWallet.TokenSymbol + effectiveContractAddress := sourceWallet.ContractAddress + nativeCurrency := shared.NativeCurrency(networkCfg) + if nativeCurrency != "" && strings.EqualFold(nativeCurrency, amountCurrency) { + effectiveTokenSymbol = nativeCurrency + effectiveContractAddress = "" + } + transfer := &model.Transfer{ IdempotencyKey: idempotencyKey, TransferRef: shared.GenerateTransferRef(), @@ -120,8 +128,8 @@ func (c *submitTransferCommand) Execute(ctx context.Context, req *chainv1.Submit SourceWalletRef: sourceWalletRef, Destination: destination, Network: sourceWallet.Network, - TokenSymbol: sourceWallet.TokenSymbol, - ContractAddress: sourceWallet.ContractAddress, + TokenSymbol: effectiveTokenSymbol, + ContractAddress: effectiveContractAddress, RequestedAmount: shared.CloneMoney(amount), NetAmount: netAmount, Fees: fees, diff --git a/api/gateway/chain/internal/service/gateway/shared/helpers.go b/api/gateway/chain/internal/service/gateway/shared/helpers.go index c40c5b7..7ff63be 100644 --- a/api/gateway/chain/internal/service/gateway/shared/helpers.go +++ b/api/gateway/chain/internal/service/gateway/shared/helpers.go @@ -119,6 +119,15 @@ func TransferStatusToProto(status model.TransferStatus) chainv1.TransferStatus { } } +// NativeCurrency returns the canonical native token symbol for a network. +func NativeCurrency(network Network) string { + currency := strings.ToUpper(strings.TrimSpace(network.NativeToken)) + if currency == "" { + currency = strings.ToUpper(strings.TrimSpace(network.Name)) + } + return currency +} + // Network describes a supported blockchain network and known token contracts. type Network struct { Name string