|
|
|
|
@@ -24,11 +24,11 @@ func NewEstimateTransfer(deps Deps) *estimateTransferFeeCommand {
|
|
|
|
|
|
|
|
|
|
func (c *estimateTransferFeeCommand) Execute(ctx context.Context, req *chainv1.EstimateTransferFeeRequest) gsresponse.Responder[chainv1.EstimateTransferFeeResponse] {
|
|
|
|
|
if err := c.deps.EnsureRepository(ctx); err != nil {
|
|
|
|
|
c.deps.Logger.Warn("repository unavailable", zap.Error(err))
|
|
|
|
|
c.deps.Logger.Warn("Repository unavailable", zap.Error(err))
|
|
|
|
|
return gsresponse.Unavailable[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, err)
|
|
|
|
|
}
|
|
|
|
|
if req == nil {
|
|
|
|
|
c.deps.Logger.Warn("nil request")
|
|
|
|
|
c.deps.Logger.Warn("Empty request received")
|
|
|
|
|
return gsresponse.InvalidArgument[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("request is required"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -46,42 +46,42 @@ func (c *estimateTransferFeeCommand) Execute(ctx context.Context, req *chainv1.E
|
|
|
|
|
sourceWallet, err := c.deps.Storage.Wallets().Get(ctx, sourceWalletRef)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, merrors.ErrNoData) {
|
|
|
|
|
c.deps.Logger.Warn("source wallet not found", zap.String("source_wallet_ref", sourceWalletRef))
|
|
|
|
|
c.deps.Logger.Warn("Source wallet not found", zap.String("source_wallet_ref", sourceWalletRef))
|
|
|
|
|
return gsresponse.NotFound[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, err)
|
|
|
|
|
}
|
|
|
|
|
c.deps.Logger.Warn("storage get wallet failed", zap.Error(err), zap.String("source_wallet_ref", sourceWalletRef))
|
|
|
|
|
c.deps.Logger.Warn("Storage get wallet failed", zap.Error(err), zap.String("source_wallet_ref", sourceWalletRef))
|
|
|
|
|
return gsresponse.Auto[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
networkKey := strings.ToLower(strings.TrimSpace(sourceWallet.Network))
|
|
|
|
|
networkCfg, ok := c.deps.Networks.Network(networkKey)
|
|
|
|
|
if !ok {
|
|
|
|
|
c.deps.Logger.Warn("unsupported chain", zap.String("network", networkKey))
|
|
|
|
|
c.deps.Logger.Warn("Unsupported chain", zap.String("network", networkKey))
|
|
|
|
|
return gsresponse.InvalidArgument[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("unsupported chain for wallet"))
|
|
|
|
|
}
|
|
|
|
|
if c.deps.Drivers == nil {
|
|
|
|
|
c.deps.Logger.Warn("chain drivers missing", zap.String("network", networkKey))
|
|
|
|
|
c.deps.Logger.Warn("Chain drivers missing", zap.String("network", networkKey))
|
|
|
|
|
return gsresponse.Internal[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, merrors.Internal("chain drivers not configured"))
|
|
|
|
|
}
|
|
|
|
|
chainDriver, err := c.deps.Drivers.Driver(networkKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.deps.Logger.Warn("unsupported chain driver", zap.String("network", networkKey), zap.Error(err))
|
|
|
|
|
c.deps.Logger.Warn("Unsupported chain driver", zap.String("network", networkKey), zap.Error(err))
|
|
|
|
|
return gsresponse.InvalidArgument[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, merrors.InvalidArgument("unsupported chain for wallet"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dest, err := resolveDestination(ctx, c.deps, req.GetDestination(), sourceWallet)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, merrors.ErrNoData) {
|
|
|
|
|
c.deps.Logger.Warn("destination not found", zap.String("destination_wallet_ref", req.GetDestination().GetManagedWalletRef()))
|
|
|
|
|
c.deps.Logger.Warn("Destination not found", zap.String("destination_wallet_ref", req.GetDestination().GetManagedWalletRef()))
|
|
|
|
|
return gsresponse.NotFound[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, err)
|
|
|
|
|
}
|
|
|
|
|
c.deps.Logger.Warn("invalid destination", zap.Error(err))
|
|
|
|
|
c.deps.Logger.Warn("Invalid destination", zap.Error(err))
|
|
|
|
|
return gsresponse.InvalidArgument[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destinationAddress, err := destinationAddress(ctx, c.deps, chainDriver, dest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.deps.Logger.Warn("failed to resolve destination address", zap.Error(err))
|
|
|
|
|
c.deps.Logger.Warn("Failed to resolve destination address", zap.Error(err))
|
|
|
|
|
return gsresponse.InvalidArgument[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -101,7 +101,7 @@ func (c *estimateTransferFeeCommand) Execute(ctx context.Context, req *chainv1.E
|
|
|
|
|
}
|
|
|
|
|
feeMoney, err := chainDriver.EstimateFee(ctx, driverDeps, networkCfg, walletForFee, destinationAddress, amount)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.deps.Logger.Warn("fee estimation failed", zap.Error(err))
|
|
|
|
|
c.deps.Logger.Warn("Fee estimation failed", zap.Error(err))
|
|
|
|
|
return gsresponse.Auto[chainv1.EstimateTransferFeeResponse](c.deps.Logger, mservice.ChainGateway, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|