public JObject GetNep17Balances(JArray _params)
        {
            using SnapshotView snapshot = Blockchain.Singleton.GetSnapshot();
            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 <Nep17BalanceKey, Nep17Balance>(_db, null, null, Nep17BalancePrefix);

            byte[] prefix = userScriptHash.ToArray();
            foreach (var(key, value) in dbCache.Find(prefix))
            {
                JObject balance = new JObject();
                if (NativeContract.ContractManagement.GetContract(snapshot, key.AssetScriptHash) is null)
                {
                    continue;
                }
                balance["assethash"]        = key.AssetScriptHash.ToString();
                balance["amount"]           = value.Balance.ToString();
                balance["lastupdatedblock"] = value.LastUpdatedBlock;
                balances.Add(balance);
            }
            return(json);
        }
        public 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(key, value) in dbCache.Find(prefix))
            {
                JObject balance = new JObject();
                if (Blockchain.Singleton.View.Contracts.TryGet(key.AssetScriptHash) is null)
                {
                    continue;
                }
                balance["asset_hash"]         = key.AssetScriptHash.ToString();
                balance["amount"]             = value.Balance.ToString();
                balance["last_updated_block"] = value.LastUpdatedBlock;
                balances.Add(balance);
            }
            return(json);
        }
Exemple #3
0
        private JObject ProcessGetClaimableSpents(JArray parameters)
        {
            UInt160 scriptHash = GetScriptHashFromParam(parameters[0].AsString());
            var     dbCache    = new DbCache <UserSystemAssetCoinOutputsKey, UserSystemAssetCoinOutputs>(
                _db, null, null, SystemAssetSpentUnclaimedCoinsPrefix);

            JObject json      = new JObject();
            JArray  claimable = new JArray();

            json["claimable"] = claimable;
            json["address"]   = scriptHash.ToAddress();

            Fixed8 totalUnclaimed = Fixed8.Zero;

            using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot())
            {
                var    storeSpentCoins = snapshot.SpentCoins;
                byte[] prefix = new [] { (byte)1 }.Concat(scriptHash.ToArray()).ToArray();
                foreach (var claimableInTx in dbCache.Find(prefix))
                {
                    if (!AddClaims(claimable, ref totalUnclaimed, _rpcMaxUnspents, snapshot, storeSpentCoins,
                                   claimableInTx))
                    {
                        break;
                    }
                }
            }
            json["unclaimed"] = (double)(decimal)totalUnclaimed;
            return(json);
        }
Exemple #4
0
        private JObject ProcessGetUnspents(JArray _params)
        {
            UInt160 scriptHash    = GetScriptHashFromParam(_params[0].AsString());
            byte    startingToken = 0; // 0 = Utility Token (GAS), 1 = Governing Token (NEO)
            int     maxIterations = 2;

            if (_params.Count > 1)
            {
                maxIterations = 1;
                bool isGoverningToken = _params[1].AsBoolean();
                if (isGoverningToken)
                {
                    startingToken = 1;
                }
            }

            var unspentsCache = new DbCache <UserSystemAssetCoinOutputsKey, UserSystemAssetCoinOutputs>(
                _db, null, null, SystemAssetUnspentCoinsPrefix);

            string[]  nativeAssetNames = { "GAS", "NEO" };
            UInt256[] nativeAssetIds   = { Blockchain.UtilityToken.Hash, Blockchain.GoverningToken.Hash };

            JObject json     = new JObject();
            JArray  balances = new JArray();

            json["balance"] = balances;
            json["address"] = scriptHash.ToAddress();
            for (byte tokenIndex = startingToken; maxIterations-- > 0; tokenIndex++)
            {
                byte[] prefix = new [] { tokenIndex }.Concat(scriptHash.ToArray()).ToArray();

                var    unspents = new JArray();
                Fixed8 total    = new Fixed8(0);

                foreach (var unspentInTx in unspentsCache.Find(prefix))
                {
                    if (!AddUnspents(unspents, ref total, unspentInTx))
                    {
                        break;
                    }
                }

                if (unspents.Count <= 0)
                {
                    continue;
                }

                var balance = new JObject();
                balance["unspent"]      = unspents;
                balance["asset_hash"]   = nativeAssetIds[tokenIndex].ToString().Substring(2);
                balance["asset_symbol"] = balance["asset"] = nativeAssetNames[tokenIndex];
                balance["amount"]       = new JNumber((double)(decimal)total);;
                balances.Add(balance);
            }

            return(json);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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);
        }