Exemplo n.º 1
0
        public async Task <BrokerExchangeResult> Exchange(BrokerExchangeToStableParams model)
        {
            _logger.LogInformation("Exchange: {@params}", model);

            //ValidateExchangeParams(model);

            // calculate exchange
            Exchange exchange = await ExchangeToStableCalculator.Create(model, _rateCache, _settings);

            exchange.InTxId        = Guid.NewGuid().ToString();
            exchange.InBlockNumber = await _graft.Sale(exchange.InTxId, exchange.SellAmount);

            exchange.PayWalletAddress = _settings.GraftWalletAddress;
            exchange.Log($"{model.SellCurrency} address: {exchange.PayWalletAddress}");

            _cache.Set(exchange.ExchangeId, exchange, DateTimeOffset.Now.AddMinutes(_settings.PaymentTimeoutMinutes));

            _db.Exchange.Add(exchange);
            await _db.SaveChangesAsync();

            var res = GetExchangeResult(exchange);

            _logger.LogInformation("Exchange Result: {@params}", res);
            return(res);
        }
        internal static async Task <Exchange> Create(BrokerExchangeToStableParams model,
                                                     IRateCache rateCache, ExchangeServiceConfiguration settings)
        {
            var log = new List <EventItem>
            {
                new EventItem($"Started exchange calculation")
            };

            log.Add(new EventItem($"Requesting GRFT rate..."));
            decimal graftRate = await rateCache.GetRateToUsd("GRFT");

            log.Add(new EventItem($"Received GRFT rate: {graftRate} USD"));

            decimal fee         = settings.ExchangeBrokerFee;
            decimal usdAmount   = graftRate * model.SellAmount;
            decimal feeAmount   = usdAmount * fee;
            decimal buyerAmount = usdAmount - feeAmount;

            var exchange = new Exchange
            {
                ExchangeId = model.ExchangeId ?? Guid.NewGuid().ToString(),
                CreatedAt  = DateTime.UtcNow,
                //Status = PaymentStatus.Waiting,

                SellAmount   = model.SellAmount,
                SellCurrency = model.SellCurrency,

                BuyAmount   = buyerAmount,
                BuyCurrency = "USDT",

                SellToUsdRate  = 1M,
                GraftToUsdRate = graftRate,

                ExchangeBrokerFee = feeAmount,

                BuyerWallet = model.WalletAddress,

                InTxStatus  = PaymentStatus.Waiting,
                OutTxStatus = PaymentStatus.New
            };

            exchange.ProcessingEvents = log;

            log.Add(new EventItem($"Created exchange: {exchange.SellAmount} {exchange.SellCurrency} to {exchange.BuyAmount} {exchange.BuyCurrency}"));
            log.Add(new EventItem($"Exchange Broker fee of {fee*100}% is {exchange.ExchangeBrokerFee} GRFT"));

            return(exchange);
        }
Exemplo n.º 3
0
        async Task ExchangeToStable(Payment payment)
        {
            try
            {
                var terminal = _db.Terminal
                               .Where(t => t.Id == payment.TerminalId)
                               .Include(t => t.ServiceProvider)
                               .Include(t => t.Store).ThenInclude(t => t.Merchant)
                               .FirstOrDefault();

                if (terminal == null)
                {
                    throw new ApiException(ErrorCode.InvalidApiKey);
                }

                var prm = new BrokerExchangeToStableParams()
                {
                    SellCurrency  = "GRFT",
                    SellAmount    = payment.MerchantAmount,
                    WalletAddress = terminal.ServiceProvider.WalletAddress
                };

                var exchangeToStableRes = await _broker.ExchangeToStable(prm);

                payment.BrokerGraftWallet          = exchangeToStableRes.PayWalletAddress;
                payment.ConvertToStableTxId        = exchangeToStableRes.GraftPaymentId;
                payment.ConvertToStableBlockNumber = exchangeToStableRes.GraftBlockNumber;

                // pay GRFT to the broker
                await _graft.Pay(payment.ConvertToStableTxId, payment.ConvertToStableBlockNumber,
                                 payment.BrokerGraftWallet, payment.MerchantAmount);

                //await PayToBroker(payment);

                // check status
                await _broker.ExchangeToStableStatus(new BrokerExchangeStatusParams { ExchangeId = exchangeToStableRes.ExchangeId });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> ExchangeToStable([FromBody] BrokerExchangeToStableParams model)
        {
            var res = await _exchangeToStableService.Exchange(model);

            return(Ok(res));
        }