Beispiel #1
0
        public async Task <SwapOffchainContextData> FillTradeContext(SwapOffchainContextData context, TradeQueueItem.MarketOrder order, List <TradeQueueItem.TradeInfo> trades, string clientId)
        {
            context.Order        = order;
            context.Trades       = trades;
            context.ClientTrades = await _clientTradesFactory.Create(order, trades, clientId);

            context.IsTrustedClient = (await _clientAccountClient.IsTrustedAsync(clientId)).Value;
            context.Status          = OperationStatus.Matched;

            if (!context.IsTrustedClient)
            {
                var aggregatedTrades = await AggregateSwaps(trades);

                context.SellTransfer = aggregatedTrades.sellTransfer;
                context.BuyTransfer  = aggregatedTrades.buyTransfer;

                var operations = new[] { aggregatedTrades.sellTransfer, aggregatedTrades.buyTransfer };

                foreach (var operation in operations)
                {
                    var trade = context.ClientTrades.FirstOrDefault(x =>
                                                                    x.ClientId == clientId && x.AssetId == operation.Asset.Id &&
                                                                    Math.Abs(x.Amount - (double)operation.Amount) < LykkeConstants.Eps);

                    // find existed operation (which was inserted in LW after guarantee transfer)
                    var existed = context.Operations.FirstOrDefault(x => x.ClientId == clientId && x.AssetId == operation.Asset.Id);

                    if (existed != null)
                    {
                        existed.ClientTradeId = trade?.Id;
                        continue;
                    }

                    context.Operations.Add(new SwapOffchainContextData.Operation()
                    {
                        TransactionId = operation.TransferId,
                        Amount        = operation.Amount,
                        ClientId      = clientId,
                        AssetId       = operation.Asset.Id,
                        ClientTradeId = trade?.Id
                    });
                }
            }
            return(context);
        }
Beispiel #2
0
        private async Task CreateTransaction(string orderId, List <AggregatedTransfer> operations, IClientTrade[] trades)
        {
            var contextData = new SwapOffchainContextData();

            foreach (var operation in operations)
            {
                var trade = trades.FirstOrDefault(x => x.ClientId == operation.ClientId && x.AssetId == operation.AssetId && Math.Abs(x.Amount - (double)operation.Amount) < 0.00000001);

                contextData.Operations.Add(new SwapOffchainContextData.Operation()
                {
                    TransactionId = operation.TransferId,
                    Amount        = operation.Amount,
                    ClientId      = operation.ClientId,
                    AssetId       = operation.AssetId,
                    ClientTradeId = trade?.Id
                });
            }

            await _bitcoinTransactionsRepository.CreateAsync(orderId, BitCoinCommands.SwapOffchain, "", null, "");

            await _bitcoinTransactionService.SetTransactionContext(orderId, contextData);
        }