private static Hash SendTransfer(JSONRPC_Client rpc, Logger logger, string nexusName, string host, PhantasmaKeys from, Address to, BigInteger amount) { Throw.IfNull(rpc, nameof(rpc)); Throw.IfNull(logger, nameof(logger)); var script = ScriptUtils.BeginScript().AllowGas(from.Address, Address.Null, 1, 9999).TransferTokens("SOUL", from.Address, to, amount).SpendGas(from.Address).EndScript(); var tx = new Transaction(nexusName, "main", script, Timestamp.Now + TimeSpan.FromMinutes(30)); tx.Sign(from); var bytes = tx.ToByteArray(true); //log.Debug("RAW: " + Base16.Encode(bytes)); var response = rpc.SendRequest(logger, host, "sendRawTransaction", Base16.Encode(bytes)); if (response == null) { logger.Error($"Error sending {amount} {DomainSettings.FuelTokenSymbol} from {from.Address} to {to}..."); return(Hash.Null); } if (response.HasNode("error")) { var error = response.GetString("error"); logger.Error("Error: " + error); return(Hash.Null); } var hash = response.Value; return(Hash.Parse(hash)); }
private static bool ConfirmTransaction(JSONRPC_Client rpc, Logger logger, string host, Hash hash, int maxTries = 99999) { var hashStr = hash.ToString(); int tryCount = 0; int delay = 250; do { var response = rpc.SendRequest(logger, host, "getConfirmations", hashStr); if (response == null) { logger.Error("Transfer request failed"); return(false); } var confirmations = response.GetInt32("confirmations"); if (confirmations > 0) { logger.Success("Confirmations: " + confirmations); return(true); } tryCount--; if (tryCount >= maxTries) { return(false); } Thread.Sleep(delay); delay *= 2; } while (true); }
private static BigInteger FetchBalance(JSONRPC_Client rpc, Logger logger, string host, Address address) { var response = rpc.SendRequest(logger, host, "getAccount", address.ToString()); if (response == null) { logger.Error($"Error fetching balance of {address}..."); return(0); } var balances = response["balances"]; if (balances == null) { logger.Error($"Error fetching balance of {address}..."); return(0); } BigInteger total = 0; foreach (var entry in balances.Children) { var chain = entry.GetString("chain"); var symbol = entry.GetString("symbol"); if (symbol == DomainSettings.FuelTokenSymbol) { total += BigInteger.Parse(entry.GetString("amount")); } } return(total); }