static void ExportChunk(uint chunkID, uint maxBlock, CronAPI api) { var fileName = "chain/chunk" + chunkID; if (File.Exists(fileName)) { var blocks = LoadChunk(fileName); if (blocks.Count == chunkSize) { return; } } var lines = new List <string>(); uint startBlock = chunkID * chunkSize; uint endBlock = startBlock + (chunkSize - 1); for (uint i = startBlock; i <= endBlock; i++) { if (i > maxBlock) { break; } var response = CronRPC.ForMainNet().QueryRPC("getblock", new object[] { i }); var blockData = response.GetString("result"); lines.Add(blockData); } ExportBlocks(chunkID, startBlock, lines); }
public override Dictionary <string, List <UnspentEntry> > GetUnspent(UInt160 hash) { var result = new Dictionary <string, List <UnspentEntry> >(); var account = Chain.GetAccount(hash); foreach (var entry in account.unspent) { var tx = Chain.GetTransaction(entry.prevHash); var output = tx.outputs[entry.prevIndex]; var unspent = new UnspentEntry() { index = entry.prevIndex, hash = entry.prevHash, value = output.value }; var symbol = CronAPI.SymbolFromAssetID(output.assetID); List <UnspentEntry> list; if (result.ContainsKey(symbol)) { list = result[symbol]; } else { list = new List <UnspentEntry>(); result[symbol] = list; } list.Add(unspent); } return(result); }
private void button1_Click(object sender, EventArgs e) { if (privateKeyInput.Text.Length == 52) { keyPair = KeyPair.FromWIF(privateKeyInput.Text); } else if (privateKeyInput.Text.Length == 64) { var keyBytes = privateKeyInput.Text.HexToBytes(); keyPair = new KeyPair(keyBytes); } else { MessageBox.Show("Invalid key input, must be 104 or 64 hexdecimal characters."); return; } var net = netComboBox.SelectedItem.ToString(); switch (net) { case "Test": api = CronRPC.ForMainNet(); break; case "Main": api = CronRPC.ForMainNet(); break; default: { MessageBox.Show("Invalid net."); return; } } tabs.TabPages.Remove(loginPage); tabs.TabPages.Add(loadingPage); timer1.Enabled = true; var task = new Task(() => OpenWallet()); task.Start(); }
static void Main() { string fileName; do { Console.Write("Enter whitelist file name or CRON address: "); fileName = Console.ReadLine(); if (!fileName.Contains(".")) { break; } if (File.Exists(fileName)) { break; } } while (true); List <string> lines; if (fileName.Contains(".")) { lines = File.ReadAllLines(fileName).ToList(); } else { lines = new List <string>() { fileName }; } if (File.Exists(result_fileName)) { var finishedLines = File.ReadAllLines(result_fileName); var finishedAddresses = new HashSet <string>(); foreach (var entry in finishedLines) { var temp = entry.Split(','); finishedAddresses.Add(temp[0]); } var previousTotal = lines.Count; lines = lines.Where(x => !finishedAddresses.Contains(x)).ToList(); var skippedTotal = previousTotal - lines.Count; Console.WriteLine($"Skipping {skippedTotal} addresses..."); } throw new Exception("TODO:// specify port and url!!!"); var api = new RemoteRPCNode(0, null, CronNodesKind.CRON_GLOBAL); var bi = new BlockIterator(api); api.SetLogger(x => { ColorPrint(ConsoleColor.DarkGray, x); }); string privateKey; byte[] scriptHash = null; do { Console.Write("Enter WIF private key: "); privateKey = Console.ReadLine(); if (privateKey.Length == 52) { break; } } while (true); var keys = KeyPair.FromWIF(privateKey); Console.WriteLine("Public address: " + keys.address); do { Console.Write("Enter contract script hash or token symbol: "); var temp = Console.ReadLine(); scriptHash = CronAPI.GetScriptHashFromSymbol(temp); if (scriptHash == null && temp.Length == 40) { scriptHash = CronAPI.GetScriptHashFromString(temp); } } while (scriptHash == null); var token = new NEP5(api, scriptHash); decimal amount; Console.WriteLine($"Write amount of {token.Symbol} to distribute to each address:"); do { if (decimal.TryParse(Console.ReadLine(), out amount) && amount > 0) { break; } } while (true); int skip = 0; int done = 0; Console.WriteLine($"Initializing {token.Name} airdrop..."); var srcBalance = token.BalanceOf(keys); Console.WriteLine($"Balance of {keys.address} is {srcBalance} {token.Symbol}"); var minimum = lines.Count * amount; if (srcBalance < minimum) { ColorPrint(ConsoleColor.Red, $"Error: For this Airdrop you need at least {minimum} {token.Symbol} at {keys.address}"); Console.ReadLine(); return; } foreach (var temp in lines) { var address = temp.Trim(); if (!address.IsValidAddress()) { skip++; ColorPrint(ConsoleColor.Yellow, "Invalid address: " + address); continue; } var hash = address.GetScriptHashFromAddress(); var balance = token.BalanceOf(hash); Console.WriteLine($"Found {address}: {balance} {token.Symbol}"); Console.WriteLine($"Sending {token.Symbol} to {address}"); Transaction tx = null; int failCount = 0; int failLimit = 20; do { int tryCount = 0; int tryLimit = 3; do { tx = token.Transfer(keys, address, amount); Thread.Sleep(1000); if (tx != null) { break; } Console.WriteLine("Tx failed, retrying..."); tryCount++; } while (tryCount < tryLimit); if (tx != null) { break; } else { Console.WriteLine("Changing RPC server..."); Thread.Sleep(2000); api.rpcEndpoint = null; failCount++; } } while (failCount < failLimit); if (failCount >= failLimit || tx == null) { ColorPrint(ConsoleColor.Red, "Try limit reached, internal problem maybe?"); break; } Console.WriteLine("Unconfirmed transaction: " + tx.Hash); api.WaitForTransaction(bi, keys, tx); ColorPrint(ConsoleColor.Green, "Confirmed transaction: " + tx.Hash); File.AppendAllText(result_fileName, $"{address},{tx.Hash}\n"); done++; } Console.WriteLine($"Skipped {skip} invalid addresses."); Console.WriteLine($"Airdropped {amount} {token.Symbol} to {done} addresses."); Console.WriteLine("Finished."); Console.ReadLine(); }
public void Init() { api = CronRPC.ForMainNet(); token = api.GetToken("RPX"); }
static void Main(string[] args) { string address; do { Console.WriteLine("Write an CRON address to listen:"); address = Console.ReadLine(); } while (!address.IsValidAddress()); int cronPort = 0; string cronURL = null; var api = new RemoteRPCNode(cronPort, cronURL, CronNodesKind.CRON_GLOBAL); var oldBlockCount = api.GetBlockHeight(); var targetScriptHash = new UInt160(address.AddressToScriptHash()); Console.WriteLine("Now listening for transactions..."); do { // wait for block generation Thread.Sleep(10000); var newBlockCount = api.GetBlockHeight(); if (newBlockCount != oldBlockCount) { Console.WriteLine($"Fetching block {newBlockCount}"); // retrieve latest block var block = api.GetBlock(newBlockCount); if (block == null) { Console.WriteLine($"Failed..."); continue; } oldBlockCount = newBlockCount; // inspect each tx in the block for inputs sent to the target address foreach (var tx in block.transactions) { var amounts = new Dictionary <string, decimal>(); foreach (var output in tx.outputs) { if (output.scriptHash.Equals(targetScriptHash)) { var asset = CronAPI.SymbolFromAssetID(output.assetID); if (amounts.ContainsKey(asset)) { amounts[asset] += output.value; } else { amounts[asset] = output.value; } } } if (amounts.Count > 0) { foreach (var entry in amounts) { Console.WriteLine($"{entry.Value} {entry.Key} was sent to {address}, in tx {tx.Hash}"); } } } } } while (true); }
static void Main() { string fileName = null; do { Console.Write("Enter whitelist file name or NEO address: "); fileName = Console.ReadLine(); if (!fileName.Contains(".")) { break; } if (File.Exists(fileName)) { break; } } while (true); List <string> lines; if (fileName.Contains(".")) { lines = File.ReadAllLines(fileName).ToList(); } else { lines = new List <string>() { fileName }; } var ext = Path.GetExtension(fileName); var result_filename = fileName.Replace(ext, "_result" + ext); if (File.Exists(result_filename)) { var finishedLines = File.ReadAllLines(result_filename); var finishedAddresses = new HashSet <string>(); foreach (var entry in finishedLines) { var temp = entry.Split(','); for (int i = 1; i < temp.Length; i++) { finishedAddresses.Add(temp[i]); } } var previousTotal = lines.Count; lines = lines.Where(x => !finishedAddresses.Contains(x)).ToList(); var skippedTotal = previousTotal - lines.Count; Console.WriteLine($"Skipping {skippedTotal} addresses..."); } int done = 0; //var api = NeoDB.ForMainNet(); var api = new RemoteRPCNode(10332, "http://neoscan.io", CronNodesKind.CRON_GLOBAL); //var api = new CustomRPCNode(); var bi = new BlockIterator(api); api.SetLogger(x => { ColorPrint(ConsoleColor.DarkGray, x); }); string privateKey; byte[] scriptHash = null; do { Console.Write("Enter WIF private key: "); privateKey = Console.ReadLine(); if (privateKey.Length == 52) { break; } } while (true); var keys = KeyPair.FromWIF(privateKey); Console.WriteLine("Public address: " + keys.address); do { Console.Write("Enter contract script hash or token symbol: "); var temp = Console.ReadLine(); scriptHash = CronAPI.GetScriptHashFromSymbol(temp); if (scriptHash == null && temp.Length == 40) { scriptHash = CronAPI.GetScriptHashFromString(temp); } } while (scriptHash == null); var token = new NEP5(api, scriptHash); Console.WriteLine($"Starting whitelisting of {token.Name} addresses..."); var batch = new List <string>(); foreach (var temp in lines) { var address = temp.Trim(); if (!address.IsValidAddress()) { ColorPrint(ConsoleColor.Yellow, "Invalid address: " + address); continue; } batch.Add(address); if (batch.Count < 5) { continue; } Console.WriteLine($"New address batch..."); var scripts = new List <object>(); var batchContent = ""; foreach (var entry in batch) { Console.WriteLine($"\t{entry}"); if (batchContent.Length > 0) { batchContent += ","; } batchContent += entry; var hash = entry.GetScriptHashFromAddress(); scripts.Add(hash); } Console.WriteLine($"Sending batch to contract..."); Transaction tx = null; int failCount = 0; int failLimit = 20; do { int tryCount = 0; int tryLimit = 3; do { tx = api.CallContract(keys, token.ScriptHash, "whitelistAdd", scripts.ToArray()); Thread.Sleep(1000); if (tx != null) { break; } Console.WriteLine("Tx failed, retrying..."); tryCount++; } while (tryCount < tryLimit); if (tx != null) { break; } else { Console.WriteLine("Changing RPC server..."); Thread.Sleep(2000); api.rpcEndpoint = null; failCount++; } } while (failCount < failLimit); if (failCount >= failLimit || tx == null) { ColorPrint(ConsoleColor.Red, "Try limit reached, internal problem maybe?"); break; } Console.WriteLine("Unconfirmed transaction: " + tx.Hash); api.WaitForTransaction(bi, keys, tx); ColorPrint(ConsoleColor.Green, "Confirmed transaction: " + tx.Hash); File.AppendAllText(result_filename, $"{tx.Hash},{batchContent}\n"); done += batch.Count; batch.Clear(); } Console.WriteLine($"Activated {done} addresses."); Console.WriteLine("Finished."); Console.ReadLine(); }