private JObject ProcessGetUnclaimed(JArray parameters) { UInt160 scriptHash = GetScriptHashFromParam(parameters[0].AsString()); JObject json = new JObject(); Fixed8 available = Fixed8.Zero; Fixed8 unavailable = Fixed8.Zero; var spentsCache = new DbCache <UserSystemAssetCoinOutputsKey, UserSystemAssetCoinOutputs>( _db, null, null, SystemAssetSpentUnclaimedCoinsPrefix); var unspentsCache = new DbCache <UserSystemAssetCoinOutputsKey, UserSystemAssetCoinOutputs>( _db, null, null, SystemAssetUnspentCoinsPrefix); using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot()) { var storeSpentCoins = snapshot.SpentCoins; byte[] prefix = new [] { (byte)1 }.Concat(scriptHash.ToArray()).ToArray(); foreach (var claimableInTx in spentsCache.Find(prefix)) { var spentCoinState = storeSpentCoins.TryGet(claimableInTx.Key.TxHash); foreach (var claimTxIndex in claimableInTx.Value.AmountByTxIndex) { var startHeight = spentCoinState.TransactionHeight; var endHeight = spentCoinState.Items[claimTxIndex.Key]; CalculateClaimable(snapshot, claimTxIndex.Value, startHeight, endHeight, out var generated, out var sysFee); available += generated + sysFee; } } var transactionsCache = snapshot.Transactions; foreach (var claimableInTx in unspentsCache.Find(prefix)) { var transaction = transactionsCache.TryGet(claimableInTx.Key.TxHash); foreach (var claimTxIndex in claimableInTx.Value.AmountByTxIndex) { var startHeight = transaction.BlockIndex; var endHeight = Blockchain.Singleton.Height; CalculateClaimable(snapshot, claimTxIndex.Value, startHeight, endHeight, out var generated, out var sysFee); unavailable += generated + sysFee; } } } json["available"] = (double)(decimal)available; json["unavailable"] = (double)(decimal)unavailable; json["unclaimed"] = (double)(decimal)(available + unavailable); return(json); }
private JObject GetNep5Balances(JArray _params) { UInt160 userScriptHash = GetScriptHashFromParam(_params[0].AsString()); JObject json = new JObject(); JArray balances = new JArray(); json["balance"] = balances; json["address"] = userScriptHash.ToAddress(); var dbCache = new DbCache <Nep5BalanceKey, Nep5Balance>(_db, null, null, Nep5BalancePrefix); byte[] prefix = userScriptHash.ToArray(); foreach (var storageKeyValuePair in dbCache.Find(prefix)) { byte[] script; using (ScriptBuilder sb = new ScriptBuilder()) { sb.EmitAppCall(storageKeyValuePair.Key.AssetScriptHash, "decimals"); sb.EmitAppCall(storageKeyValuePair.Key.AssetScriptHash, "symbol"); sb.EmitAppCall(storageKeyValuePair.Key.AssetScriptHash, "name"); script = sb.ToArray(); } ApplicationEngine engine = ApplicationEngine.Run(script); JObject balance = new JObject(); balance["asset_hash"] = storageKeyValuePair.Key.AssetScriptHash.ToArray().Reverse().ToHexString(); if (!engine.State.HasFlag(VMState.FAULT)) { balance["asset_name"] = engine.ResultStack.Pop().GetString(); balance["asset_symbol"] = engine.ResultStack.Pop().GetString(); balance["asset_decimals"] = (byte)engine.ResultStack.Pop().GetBigInteger(); } balance["amount"] = storageKeyValuePair.Value.Balance.ToString(); balance["last_updated_block"] = storageKeyValuePair.Value.LastUpdatedBlock; balances.Add(balance); } return(json); }
private JObject GetNep5Balances(JArray _params) { UInt160 userScriptHash = GetScriptHashFromParam(_params[0].AsString()); JObject json = new JObject(); JArray balances = new JArray(); json["balance"] = balances; json["address"] = userScriptHash.ToAddress(); var dbCache = new DbCache <Nep5BalanceKey, Nep5Balance>(_db, null, null, Nep5BalancePrefix); byte[] prefix = userScriptHash.ToArray(); foreach (var storageKeyValuePair in dbCache.Find(prefix)) { JObject balance = new JObject(); balance["asset_hash"] = storageKeyValuePair.Key.AssetScriptHash.ToArray().Reverse().ToHexString(); balance["amount"] = storageKeyValuePair.Value.Balance.ToString(); balance["last_updated_block"] = storageKeyValuePair.Value.LastUpdatedBlock; balances.Add(balance); } return(json); }