Exemple #1
0
        public JObject ExpressEnumNotifications(JArray @params)
        {
            var contracts = ((JArray)@params[0]).Select(j => UInt160.Parse(j.AsString())).ToHashSet();
            var events    = ((JArray)@params[1]).Select(j => j.AsString()).ToHashSet(StringComparer.OrdinalIgnoreCase);
            int skip      = @params.Count >= 3 ? (int)@params[2].AsNumber() : 0;
            int take      = @params.Count >= 4 ? (int)@params[3].AsNumber() : MAX_NOTIFICATIONS;

            if (take > MAX_NOTIFICATIONS)
            {
                take = MAX_NOTIFICATIONS;
            }

            var notifications = PersistencePlugin
                                .GetNotifications(
                storageProvider,
                SeekDirection.Backward,
                contracts.Count > 0 ? contracts : null,
                events.Count > 0 ? events : null)
                                .Skip(skip);

            var count             = 0;
            var jsonNotifications = new JArray();
            var truncated         = false;

            foreach (var(blockIndex, _, notification) in notifications)
            {
                if (count++ > take)
                {
                    truncated = true;
                    break;
                }

                var jNotification = new JObject
                {
                    ["block-index"]    = blockIndex,
                    ["script-hash"]    = notification.ScriptHash.ToString(),
                    ["event-name"]     = notification.EventName,
                    ["inventory-type"] = (byte)notification.InventoryType,
                    ["inventory-hash"] = notification.InventoryHash.ToString(),
                    ["state"]          = Neo.VM.Helper.ToJson(notification.State),
                };
                jsonNotifications.Add(jNotification);
            }

            return(new JObject
            {
                ["truncated"] = truncated,
                ["notifications"] = jsonNotifications,
            });
        }
Exemple #2
0
        public JObject GetNep17Balances(JArray @params)
        {
            var address = AsScriptHash(@params[0]);

            using var snapshot = neoSystem.GetSnapshot();

            // collect the non-zero balances of all the deployed Nep17 contracts for the specified account
            var addressBalances = TokenContract.Enumerate(snapshot)
                                  .Where(c => c.standard == TokenStandard.Nep17)
                                  .Select(c => (
                                              scriptHash: c.scriptHash,
                                              balance: snapshot.GetNep17Balance(c.scriptHash, address, neoSystem.Settings)))
                                  .Where(t => !t.balance.IsZero)
                                  .ToList();

            // collect the last block index a transfer occurred for all account balances
            var updateIndexes = new Dictionary <UInt160, uint>();

            if (addressBalances.Count > 0)
            {
                var notifications = PersistencePlugin.GetNotifications(
                    storageProvider,
                    SeekDirection.Backward,
                    addressBalances.Select(b => b.scriptHash).ToHashSet(),
                    TRANSFER);

                foreach (var(blockIndex, _, notification) in notifications)
                {
                    // iterate backwards thru the notifications looking for all the Transfer events from a contract
                    // in assets where a Transfer event hasn't already been recorded
                    if (!updateIndexes.ContainsKey(notification.ScriptHash))
                    {
                        var transfer = TransferNotificationRecord.Create(notification);
                        if (transfer is not null &&
                            (transfer.From == address || transfer.To == address))
                        {
                            // if the specified account was the sender or receiver of the current transfer,
                            // record the update index. Stop the iteration if indexes for all the assets are
                            // have been recorded
                            updateIndexes.Add(notification.ScriptHash, blockIndex);
                            if (updateIndexes.Count == addressBalances.Count)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            var balances = new JArray();

            for (int i = 0; i < addressBalances.Count; i++)
            {
                var(scriptHash, balance) = addressBalances[i];
                var lastUpdatedBlock = updateIndexes.TryGetValue(scriptHash, out var _index) ? _index : 0;

                balances.Add(new JObject
                {
                    ["assethash"]        = scriptHash.ToString(),
                    ["amount"]           = balance.ToString(),
                    ["lastupdatedblock"] = lastUpdatedBlock,
                });
            }

            return(new JObject
            {
                ["address"] = address.ToAddress(neoSystem.Settings.AddressVersion),
                ["balance"] = balances,
            });
        }