private static async void WithdrawListener(Miner miner, CryptoConnector connector, string btcSecret, string ethSecret, NodeConfig conf) { var withdrwawalDictionary = new Dictionary <string, Withdrawal>(); var ethConnector = new EthereumOracleConnector(ethSecret, conf.ETHEndpoint, conf.OracleETHAddres); var btcConnector = new BitcoinOracleConnector(conf.Network, conf.BTCEndpoint, btcSecret); while (await reader.WaitToReadAsync()) { var chain = await reader.ReadAsync(); chain = chain.Get(5); if (chain != null) { foreach (var wd in chain.CurrentBlock.Withdrawals) { try { if (!withdrwawalDictionary.ContainsKey(wd.HashStr) && !(await repository.Contains(wd.HashStr))) { WithdrawInfo result = null; if (wd.Asset == "BTC") { result = await btcConnector.WithdrawBtc(wd.HashStr, wd.TargetWallet, wd.Size); } else { result = await ethConnector.WithdrawEth(wd.HashStr, wd.TargetWallet, wd.Size, wd.Asset); } if (result != null) { withdrwawalDictionary.Add(wd.HashStr, wd); result.HashStr = wd.HashStr; await repository.Save(result); } } } catch (Exception e) { Log.Error("Failed to withdrawal... \n" + e.ToString()); } } } } }
async static Task Main(string[] args) { Console.WriteLine("Starting Oracle"); var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile("pkcscfg.json", optional: false, reloadOnChange: true) .AddCommandLine(args); IConfigurationRoot configuration = builder.Build(); Log.Debug("Building Config"); var conf = new NodeConfig(); configuration.GetSection("Node").Bind(conf); var pkcsConf = new PKCS(); configuration.GetSection("PKCS").Bind(pkcsConf); Log.Debug("NodeConfig created"); repository = new WithdrawInfoRepository(); var wdRepository = new WithdrawalRepository(); var connector = new CryptoConnector(conf.OracleETHAddres, conf.OracleBTCAddres, conf.ETHEndpoint, conf.BTCEndpoint, conf.Network); var miner = new Miner(new PeerListener(conf.ListenPort), new WalletRepository(), new BlockRepository(), new BalanceRepository(), new BookRepository(), new DepositRepository(), new OfferRepository(), new TransactionRepository(), wdRepository, new TradeRepository(), repository, connector, conf.ListenPort, true, 1000, true); ethConnector = new EthereumOracleConnector(conf.ETHEndpoint, conf.OracleETHAddres, pkcsConf.User, pkcsConf.EthKey); btcConnector = new BitcoinOracleConnector(conf.Network, conf.BTCEndpoint, pkcsConf.User, pkcsConf.BtcKey); if (conf.Reprocess) { Log.Information("Reprocessing withdraws: "); foreach (var wdid in await repository.ListToReprocess()) { Log.Information("Reprocessing withdraw: " + wdid); var wd = await wdRepository.Get(wdid); await ProcessWithdraw(wd); } } reader = miner.GetChainStream(); await miner.Start(true); WithdrawListener(miner, connector, conf); if (!string.IsNullOrEmpty(conf.SeedAddress) && conf.SeedPort > 0) { try { var seednode = new Peer(new TcpClient(conf.SeedAddress, conf.SeedPort)); miner.Connect(seednode); } catch (Exception e) { Log.Error(e.ToString()); throw; } } await Task.Delay(Timeout.Infinite); }