Beispiel #1
0
        private void RefreshAccount(string account)
        {
            var balance = EthereumService.GetBalance(account, BlockTag.Latest);
            var pending = EthereumService.GetBalance(account, BlockTag.Pending);
            var txCount = EthereumService.GetTransactionCount(account, BlockTag.Latest);
            var changes = EthereumService.GetFilterChanges(GetCurrentAccount().FilterId);

            BeginInvoke((MethodInvoker) delegate
            {
                lblBalance.Text = string.Format("{0} Eth", balance.WeiToEther());
                lblPending.Text = string.Format("{0} Eth", pending.WeiToEther());
                lblTxCount.Text = txCount.ToString();
            });
        }
Beispiel #2
0
        private void btnShowBalance_Click(object sender, EventArgs e)
        {
            string account = lstboxAccountList.SelectedItem.ToString();
            bool   result  = EthereumService.UnlockAccount(account, "Pass@123");

            if (result)
            {
                var balance = EthereumService.GetBalance(account, BlockTag.Latest);

                MessageBox.Show(account + " - " + string.Format("{0} Eth", balance.WeiToEther()));
            }
            else
            {
                MessageBox.Show("No Balance in this account");
            }
        }
Beispiel #3
0
        private List <EthAccount> LoadAccounts()
        {
            var privateConnection = new ConnectionOptions()
            {
                Port = "8545",
                Url  = "http://localhost"
            };

            ethereumService = new EthereumService(privateConnection);
            Accounts        = new List <EthAccount>();

            var accounts = ethereumService.GetAccounts();



            foreach (var account in accounts)
            {
                var a = new EthAccount();
                a.Address = account;

                var filter = new Filter(fromBlock: "0x01")
                {
                    Address = a.Address
                };

                a.FilterId = ethereumService.NewFilter(filter);
                Accounts.Add(a);
                var sign = ethereumService.Sign(a.Address, "Pass@123");
                a.Unlocked = sign != null;

                ethereumService.UnlockAccount(a.Address, "Pass@123");

                bool result = ethereumService.UnlockAccount(account, "Pass@123");
                if (result)
                {
                    var balance = ethereumService.GetBalance(account, BlockTag.Latest);
                    //var specifier = "0,0.000";
                    a.Balance = balance.WeiToEther();//string.Format("{0} Eth", balance.WeiToEther().ToString("0,0.000"));
                }
            }
            return(Accounts);
        }
Beispiel #4
0
        public static void Run(string file)
        {
            if (string.IsNullOrEmpty(file) || !File.Exists(file))
            {
                Console.WriteLine("File required");
                return;
            }

            object            console           = new object();
            ConnectionOptions connectionOptions = new ConnectionOptions()
            {
                Port = "8545",
                Url  = "http://127.0.0.1"
            };

            long            count = 0, have = 0;
            EthereumService ethereumService = new EthereumService(connectionOptions);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Start checking accounts");
            Console.WriteLine("");

            Parallel.ForEach(File.ReadAllLines(file), (address) =>
                             //foreach(string address in File.ReadAllLines(file))
            {
                string endAddress = address;

                if (!address.StartsWith("0x"))
                {
                    endAddress = "0x" + address;
                }

                Interlocked.Increment(ref count);

                string sammout;
                try
                {
                    long tx = ethereumService.GetTransactionCount(endAddress, BlockTag.Latest);
                    if (tx == 0)
                    {
                        return;
                    }

                    BigInteger ammout = ethereumService.GetBalance(endAddress, BlockTag.Latest);

                    sammout = ammout.ToString();
                    Interlocked.Increment(ref have);
                }
                catch (Exception ex)
                {
                    sammout = "ERROR " + ex.Message;
                }

                lock (console)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("[" + endAddress + "] ");
                    Console.ForegroundColor = sammout.StartsWith("ERROR ") ? ConsoleColor.Red : ConsoleColor.Yellow;
                    Console.WriteLine(sammout);
                }
            });

            // END

            lock (console)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine();
                Console.WriteLine("*****************************************");
                Console.WriteLine("Checked accounts [" + count + "] ");
                Console.WriteLine("Accounts with balance [" + have + "] ");
            }
        }
        public async Task <IActionResult> CheckIfCanSweepNow(SweepFundsViewModel viewModel, string command,
                                                             bool leaveActiveInvoice = false)
        {
            var web3 = _ethereumService.GetWeb3(viewModel.ChainId);

            if (web3 == null)
            {
                TempData.SetStatusMessageModel(new StatusMessageModel()
                {
                    Message = $"Web3 not available", Severity = StatusMessageModel.StatusSeverity.Error
                });

                return(RedirectToAction("GetStoreEthereumLikePaymentMethods", new { storeId = StoreData.Id }));
            }

            viewModel.GasPrice ??= (ulong)(await web3.Eth.GasPrice.SendRequestAsync()).Value;
            viewModel.SweepRequests = viewModel.SweepRequests
                                      .Where(request =>
                                             leaveActiveInvoice ||
                                             string.IsNullOrEmpty(request.UnableToSweepBecauseOfActiveInvoiceWithNativeCurrency))
                                      .OrderByDescending(request => request.Sufficient(viewModel.GasPrice.Value, out var excess)
                                                         ).ToList();

            var networks = _btcPayNetworkProvider.GetAll().OfType <EthereumBTCPayNetwork>()
                           .Where(network => network.ChainId == viewModel.ChainId);
            var mainNetwork          = networks.SingleOrDefault(network => !(network is ERC20BTCPayNetwork));
            var etherTransferService = web3.Eth.GetEtherTransferService();
            var transferHandler      = web3.Eth.GetContractTransactionHandler <TransferFunction>();

            foreach (var sweepRequest in viewModel.SweepRequests)
            {
                sweepRequest.Native ??= new SweepRequestItem()
                {
                    Sweep = true, CryptoCode = mainNetwork.CryptoCode,
                };

                var amt = (await _ethereumService.GetBalance(mainNetwork, sweepRequest.Address)).Value;
                sweepRequest.Native.Amount  = amt;
                sweepRequest.Native.GasCost = amt == 0
                    ? 0
                    : (ulong)await etherTransferService.EstimateGasAsync(viewModel.DestinationAddress,
                                                                         EthereumLikePaymentData.GetValue(mainNetwork, amt));

                foreach (SweepRequestItem sweepRequestItem in sweepRequest.Tokens)
                {
                    var network = _btcPayNetworkProvider.GetNetwork <ERC20BTCPayNetwork>(sweepRequestItem.CryptoCode);

                    sweepRequestItem.Amount = (await _ethereumService.GetBalance(network, sweepRequest.Address)).Value;
                    if (sweepRequestItem.Amount == 0 && string.IsNullOrEmpty(sweepRequestItem.TransactionHash))
                    {
                        sweepRequestItem.TransactionHash = "external sweep detected";
                        var i = await _invoiceRepository.GetInvoice(sweepRequestItem.InvoiceId);

                        var pD    = i.GetPayments(network.CryptoCode).Last(entity => entity.Accounted);
                        var ethpd = pD.GetCryptoPaymentData() as EthereumLikePaymentData;

                        ethpd.SweepingTransaction = sweepRequestItem.TransactionHash;
                        pD.SetCryptoPaymentData(ethpd);
                        await _invoiceRepository.UpdatePayments(new List <PaymentEntity>() { pD });
                    }

                    var transfer = new TransferFunction()
                    {
                        To          = viewModel.DestinationAddress,
                        Value       = sweepRequestItem.Amount,
                        FromAddress = sweepRequest.Address
                    };
                    sweepRequestItem.GasCost =
                        (ulong)(await transferHandler.EstimateGasAsync(network.SmartContractAddress,
                                                                       transfer))
                        .Value;
                }

                sweepRequest.Tokens = sweepRequest.Tokens.Where(item => item.Amount > 0).ToList();
            }

            if (command == "sweep")
            {
                if (await DoSweepAction(viewModel, web3))
                {
                    return(await CheckIfCanSweepNow(viewModel, null));
                }
            }

            return(View("SweepFundsSpecifyInfo", viewModel));
        }
        public static void Run()
        {
            Console.WriteLine("Press Control+C for cancel");
            Console.CancelKeyPress += Console_CancelKeyPress;

            object            console           = new object();
            ConnectionOptions connectionOptions = new ConnectionOptions()
            {
                Port = "8545",
                Url  = "http://127.0.0.1"
            };

            long            have            = 0;
            Random          r               = new Random();
            EthereumService ethereumService = new EthereumService(connectionOptions);

            while (!Cancel)
            {
                Parallel.For(0, 1000, (i) =>
                {
                    if (Cancel)
                    {
                        return;
                    }

                    byte[] privKey = new byte[32];
                    r.NextBytes(privKey);

                    EthAddress e      = new EthAddress(privKey);
                    string endAddress = e.Address;

                    if (!endAddress.StartsWith("0x"))
                    {
                        endAddress = "0x" + endAddress;
                    }

                    string sammout;
                    try
                    {
                        //Thread.Sleep(5);
                        Interlocked.Increment(ref have);

                        long tx = ethereumService.GetTransactionCount(endAddress, BlockTag.Latest);
                        if (tx == 0)
                        {
                            return;
                        }

                        BigInteger ammout = ethereumService.GetBalance(endAddress, BlockTag.Latest);
                        sammout           = ammout.ToString();
                    }
                    catch (Exception ex)
                    {
                        sammout = "ERROR " + ex.Message;
                    }
                    finally
                    {
                        lock (console) Console.Title = have.ToString();
                    }

                    lock (console)
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write("[" + privKey.ToHex() + "] ");
                        Console.ForegroundColor = sammout.StartsWith("ERROR ") ? ConsoleColor.Red : ConsoleColor.Yellow;
                        Console.WriteLine(sammout);

                        File.AppendAllText("./Lotery.txt", privKey.ToHex() + " " + sammout + Environment.NewLine);
                    }
                });
            }
        }
        static void Main1(string[] args)
        {
            return;

            var ipc = @"\\.\pipe\geth.ipc";

            IpcChannel ipcCh = new IpcChannel(ipc);

            var liveConnection = new ConnectionOptions()
            {
                Port = "8545",
                Url  = "http://localhost"
            };

            var privateConnection = new ConnectionOptions()
            {
                Port = "8545",
                Url  = "http://localhost"
            };

            var ethereumService = new EthereumService(privateConnection);

            var exampleTxHash      = "0x79b636e7a28e74b6d1db3be815e2658c759dd3213605ca7916b3a19402d0ba42";
            var exampleBlockHash   = "0xca3130089dca52a645b1545a0f04930257dea601b54011aececd616d04fec12f";
            var exampleBlockNumber = 514064;
            var exampleAddress     = "0x63a9975ba31b0b9626b34300f7f627147df1f526";


            var privateAddress1 = "0xcab3e4e71d5f5578c62eb7da9014220761952148";
            var privateAddress2 = "0xa8c98594d716fed32366411457cc0ff803fcd845";

            //Console.WriteLine(ethereumService.GetBalance("0xcab3e4e71d5f5578c62eb7da9014220761952148", BlockTag.Latest));


            //var t = new Transaction() {From = privateAddress1, To = privateAddress2, Value = "0x9184e72a" };
            //Console.WriteLine(ethereumService.SendTransaction(t));
            //Console.WriteLine(ethereumService.SendTransaction(privateAddress1, privateAddress2, -1, null,-1,350000)); //return nothing

            //Console.WriteLine(ethereumService.GetWeb3ClientVersion());
            //Console.WriteLine(ethereumService.GetWeb3Sha3("Hello world"));
            //Console.WriteLine(ethereumService.GetNetVersion());
            //Console.WriteLine(ethereumService.GetNetListening());
            //Console.WriteLine(ethereumService.GetNetPeerCount());
            //Console.WriteLine(ethereumService.GetProtocolVersion());
            //Console.WriteLine(ethereumService.GetSyncing());
            //Console.WriteLine(ethereumService.GetCoinbase().Value);
            //Console.WriteLine(ethereumService.GetMining());
            //Console.WriteLine(ethereumService.GetHashrate());
            //Console.WriteLine(ethereumService.GetGasPrice());
            //ethereumService.GetAccounts().ToList().ForEach(i => Console.Write(@"[{0}] ", i));

            var accounts = ethereumService.GetAccounts();

            foreach (var account in accounts)
            {
                var balance = ethereumService.GetBalance(account, BlockTag.Latest);
                Console.WriteLine("account :" + EtherCurrencyConverter.Convert(balance));

                var sign = ethereumService.Sign(account, "School bus");

                Console.WriteLine("sign : " + sign);
            }

            //Console.WriteLine(ethereumService.GetBlockNumber());
            //Console.WriteLine(ethereumService.GetBalance(exampleAddress, BlockTag.Latest));
            //Console.WriteLine(ethereumService.GetStorageAt(exampleAddress, 100, BlockTag.Latest));
            //Console.WriteLine(ethereumService.GetTransactionCount(exampleAddress, BlockTag.Latest));
            //Console.WriteLine(ethereumService.GetBlockTransactionCountByHash(exampleBlockHash));
            //Console.WriteLine(ethereumService.GetBlockTransactionCountByNumber(BlockTag.Latest));
            //Console.WriteLine(ethereumService.GetUncleCountByBlockHash(exampleBlockHash));
            //Console.WriteLine(ethereumService.GetUncleCountByBlockNumber(10));
            //Console.WriteLine(ethereumService.GetCode(exampleAddress, BlockTag.Latest));

            //Console.WriteLine(ethereumService.Sign(exampleAddress, "School bus")); // return nothing
            //Console.WriteLine(ethereumService.SendTransaction("0xb60e8dd61c5d32be8058bb8eb970870f07233155", "0xd46e8dd67c5d32be8058bb8eb970870f072445675", 30400, "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675")); //return nothing
            //Console.WriteLine(ethereumService.SendRawTransaction("0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675")); // returns nothing
            //Console.WriteLine(ethereumService.Call("0xb60e8dd61c5d32be8058bb8eb970870f07233155", "0xd46e8dd67c5d32be8058bb8eb970870f072445675", 30400, "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675")); //return nothing

            //Console.WriteLine(ethereumService.EstimateGas()); // not yet implemented
            //Console.WriteLine(ethereumService.GetBlockByHash(exampleBlockHash, true)); // works but need to fix partial value return (fullblock:false)
            //Console.WriteLine(ethereumService.GetBlockByNumber(exampleBlockNumber, BlockTag.Quantity,true));
            //Console.WriteLine(ethereumService.GetTransactionByHash(exampleTxHash));


            //Console.WriteLine(ethereumService.GetTransactionByBlockHashAndIndex(exampleBlockHash,0));
            //Console.WriteLine(ethereumService.GetTransactionByBlockNumberAndIndex(exampleBlockNumber, 0));
            //Console.WriteLine(ethereumService.GetTransactionReceipt(exampleTxHash));
            //Console.WriteLine(ethereumService.GetUncleByBlockHashAndIndex(exampleBlockHash,0)); // doesnt seem to return the uncle
            //Console.WriteLine(ethereumService.GetUncleByBlockNumberAndIndex(exampleBlockNumber, 0)); // doesnt seem to return the uncle

            //ethereumService.GetCompilers().ToList().ForEach(i => Console.Write(@"[{0}] ", i));

            //var contract = @"contract test { function multiply(uint a) returns(uint d) {   return a * 7;   } }";


            //Console.WriteLine(ethereumService.CompileSolidity(contract)); //not working
            //Console.WriteLine(ethereumService.CompileLLL()); //not yet implemented
            //Console.WriteLine(ethereumService.CompileSerpent()); //not yet implemented

            //var filter = ethereumService.NewFilter(exampleAddress);
            //Console.WriteLine(filter);
            //var blockFilter = ethereumService.NewBlockFilter();
            //Console.WriteLine(blockFilter);
            //var newPendingTransactionFilter = ethereumService.NewPendingTransactionFilter();
            //Console.WriteLine(newPendingTransactionFilter);

            //ethereumService.GetFilterChanges(blockFilter).ToList().ForEach(i => Console.Write(@"[{0}] ", i)); // return the right objects
            //ethereumService.GetFilterLogs(blockFilter).ToList().ForEach(i => Console.Write(@"[{0}] ", i)); // return the right objects

            //var uninstallFilter = ethereumService.UninstallFilter(filter);
            //Console.WriteLine(uninstallFilter);

            // Console.WriteLine(ethereumService.GetLogs(new Log())); // not working

            //Console.WriteLine(ethereumService.GetWork());
            //Console.WriteLine(ethereumService.SubmitWork("0x0000000000000001","0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", "0xD1FE5700000000000000000000000000D1FE5700000000000000000000000000")); // errors probably due to parameters passed
            //Console.WriteLine(ethereumService.SubmitHashrate("0x0000000000000000000000000000000000000000000000000000000000500000","0x59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c")); // return null. probably bad parameters

            //Console.WriteLine(ethereumService.ShhVersion());

            //Console.WriteLine(ethereumService.ShhPost(null, null, null, null, null, null));

            Console.ReadLine();
        }