public ApiPaymentsController(ILoggerFactory loggerFactory,
                              ApplicationDbContext db,
                              IRateCache rateCache,
                              IExchangeService exchangeService,
                              IPaymentService paymentService)
 {
     _logger          = loggerFactory.CreateLogger(nameof(ApiPaymentsController));
     _db              = db;
     _rateCache       = rateCache;
     _exchangeService = exchangeService;
     _paymentService  = paymentService;
 }
        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);
        }
예제 #3
0
        public ExchangeToStableService(ILoggerFactory loggerFactory,
                                       ApplicationDbContext db,
                                       IConfiguration configuration,
                                       IRateCache rateCache,
                                       IMemoryCache cache,
                                       ICryptoProviderService cryptoProviderService,
                                       GraftService graft)
        {
            _settings = configuration
                        .GetSection("ExchangeService")
                        .Get <ExchangeServiceConfiguration>();

            _logger                = loggerFactory.CreateLogger(nameof(ExchangeService));
            _db                    = db;
            _rateCache             = rateCache;
            _cache                 = cache;
            _cryptoProviderService = cryptoProviderService;
            _graft                 = graft;
        }
예제 #4
0
        public ExchangeService(ILoggerFactory loggerFactory,
                               ApplicationDbContext db,
                               IConfiguration configuration,
                               IRateCache rateCache,
                               IMemoryCache cache,
                               ICryptoProviderService cryptoProviderService,
                               IGraftWalletService wallet)
        {
            _settings = configuration
                        .GetSection("PaymentService")
                        .Get <PaymentServiceConfiguration>();

            _logger                = loggerFactory.CreateLogger(nameof(ExchangeService));
            _db                    = db;
            _rateCache             = rateCache;
            _cache                 = cache;
            _cryptoProviderService = cryptoProviderService;
            _wallet                = wallet;
        }
예제 #5
0
        public PaymentService(ILoggerFactory loggerFactory,
                              IConfiguration configuration,
                              ApplicationDbContext db,
                              IRateCache rateCache,
                              IMemoryCache cache,
                              IExchangeBroker broker,
                              IHttpContextAccessor context,
                              GraftService graft)
        {
            _settings = configuration
                        .GetSection("PaymentService")
                        .Get <PaymentsConfiguration>();

            _logger    = loggerFactory.CreateLogger(nameof(PaymentService));
            _db        = db;
            _rateCache = rateCache;
            _cache     = cache;
            _broker    = broker;
            _context   = context;
            _graft     = graft;
        }
예제 #6
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ApplicationDbContext context, WatcherService watcher, IRateCache rateCache,
                              IEmailSender emailService, IExchangeBroker broker)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Error/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseAuthentication();
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "GRAFT Payment Terminal Gateway API V1");
            });

            app.UseMiddleware();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            watcher.Add(rateCache);
            watcher.Add(emailService);
            watcher.Add(broker);

            // comment this line if you want to apply the migrations as a separate process
            context.Database.Migrate();
        }
예제 #7
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext context,
                              ILoggerFactory loggerFactory, WatcherService watcher, IEmailSender emailSender, IRateCache rateCache,
                              IBitcoinService bitcoinService, IGraftWalletService graftWalletService)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Error/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseAuthentication();
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "GRAFT Exchange Broker API V1");
            });

            app.UseMiddleware();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            watcher.Add(rateCache);
            watcher.Add(emailSender);
            watcher.Add(bitcoinService);
            watcher.Add(graftWalletService);

            // comment this line if you want to apply the migrations as a separate process
            context.Database.Migrate();

            BitcoinService.Init(options =>
            {
                options.IsTestNetwork          = Configuration["BitcoinService:NetworkType"] != "MainNet";
                options.TryCount               = 2;
                options.DelayMs                = 2000;
                options.LoggerFactory          = loggerFactory;
                options.DbContext              = context;
                options.BitcoinExtPubKeyString = Configuration["BitcoinService:BitcoinExtPubKeyString"];
            });

            EthereumService.Init(
                ConnectionString,
                loggerFactory,
                Configuration["EthereumService:NetworkType"] != "MainNet",
                Configuration["EthereumService:EthereumPoolWalletPassword"],
                Configuration["EthereumService:EthereumGethNodeUrl"],
                Configuration["EthereumService:EthereumBrokerWallet"],
                Convert.ToDecimal(Configuration["EthereumService:EthereumPoolDrainLimit"]));

            bitcoinService.Ping();

            GraftWalletService.Init(context,
                                    Configuration["GraftWalletService:RpcUrl"],
                                    ConnectionString,
                                    loggerFactory);
        }
예제 #8
0
        internal static async Task <Exchange> Create(BrokerExchangeParams model,
                                                     IRateCache rateCache, PaymentServiceConfiguration settings)
        {
            var log = new List <EventItem>
            {
                new EventItem($"Started exchange calculation")
            };

            log.Add(new EventItem($"Requesting {model.SellCurrency} rate..."));
            decimal sellRate = await rateCache.GetRateToUsd(model.SellCurrency);

            log.Add(new EventItem($"Received {model.SellCurrency} rate: {sellRate} USD"));

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

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

            decimal sellAmount  = 0;
            decimal graftAmount = 0;
            decimal buyerAmount = 0;
            decimal feeAmount   = 0;
            decimal fee         = settings.ExchangeBrokerFee;

            if (!string.IsNullOrWhiteSpace(model.FiatCurrency))
            {
                if (model.SellFiatAmount > 0)
                {
                    sellAmount  = model.SellFiatAmount / sellRate;
                    graftAmount = model.SellFiatAmount / graftRate;
                    feeAmount   = graftAmount * fee;
                    buyerAmount = graftAmount - feeAmount;
                }
                else
                {
                    buyerAmount = model.BuyFiatAmount / graftRate;
                    graftAmount = buyerAmount / (1 - fee);
                    feeAmount   = graftAmount - buyerAmount;
                    sellAmount  = graftAmount * graftRate / sellRate;
                }
            }
            else
            {
                if (model.SellAmount > 0)
                {
                    sellAmount  = model.SellAmount;
                    graftAmount = sellAmount * sellRate / graftRate;
                    feeAmount   = graftAmount * fee;
                    buyerAmount = graftAmount - feeAmount;
                }
                else
                {
                    buyerAmount = model.BuyAmount;
                    graftAmount = buyerAmount / (1 - fee);
                    feeAmount   = graftAmount - buyerAmount;
                    sellAmount  = graftAmount * graftRate / sellRate;
                }
            }

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

                SellAmount   = sellAmount,
                SellCurrency = model.SellCurrency,

                BuyAmount   = buyerAmount,
                BuyCurrency = model.BuyCurrency,

                SellToUsdRate  = sellRate,
                GraftToUsdRate = graftRate,

                ExchangeBrokerFee = feeAmount,

                BuyerWallet = model.WalletAddress,

                BuyerTransactionStatus = GraftTransactionStatus.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);
        }