/// <summary>
        /// Get an array of all AccountBalances for an AccountIdentifier and the BlockIdentifier at which the balance lookup was performed.
        /// The BlockIdentifier must always be returned because some consumers of account balance data need to know specifically at which block
        /// the balance was calculated to compare balances they compute from operations with the balance returned by the node.
        /// It is important to note that making a balance request for an account without populating the SubAccountIdentifier should not
        /// result in the balance of all possible SubAccountIdentifiers being returned.
        /// Rather, it should result in the balance pertaining to no SubAccountIdentifiers being returned (sometimes called the liquid balance).
        /// To get all balances associated with an account, it may be necessary to perform multiple balance requests with unique AccountIdentifiers.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JObject AccountBalance(AccountBalanceRequest request)
        {
            DataCache       snapshot = system.StoreView;
            BlockIdentifier respBlockIdentifier;

            if (request.NetworkIdentifier?.Blockchain?.ToLower() != "neo n3")
            {
                return(Error.NETWORK_IDENTIFIER_INVALID.ToJson());
            }
            if (request.NetworkIdentifier?.Network?.ToLower() != network)
            {
                return(Error.NETWORK_IDENTIFIER_INVALID.ToJson());
            }
            if (request.AccountIdentifier is null)
            {
                return(Error.ACCOUNT_IDENTIFIER_INVALID.ToJson());
            }
            if (request.BlockIdentifier is null)
            {
                respBlockIdentifier = new(NativeContract.Ledger.CurrentIndex(system.StoreView), NativeContract.Ledger.CurrentHash(system.StoreView).ToString());
            }
            else if (request.BlockIdentifier.Index is null && request.BlockIdentifier.Hash is null)
            {
                return(Error.BLOCK_IDENTIFIER_INVALID.ToJson());
            }
Beispiel #2
0
        public JObject AccountBalance(AccountBalanceRequest request)
        {
            if (request.AccountIdentifier is null)
            {
                return(Error.ACCOUNT_IDENTIFIER_INVALID.ToJson());
            }

            UInt160 account;

            try
            {
                account = request.AccountIdentifier.Address.ToScriptHash();
            }
            catch (Exception)
            {
                return(Error.ACCOUNT_ADDRESS_INVALID.ToJson());
            }

            // can only get current balance
            Amount[] balances = GetUtxoBalance(account);
            if (balances is null)
            {
                return(Error.ACCOUNT_NOT_FOUND.ToJson());
            }

            if (request.AccountIdentifier.SubAccountIdentifier != null) // then need to get the nep5 balance
            {
                if (!UInt160.TryParse(request.AccountIdentifier.SubAccountIdentifier.Address, out UInt160 scriptHash))
                {
                    return(Error.CONTRACT_ADDRESS_INVALID.ToJson());
                }
                Amount[] nep5Balances = GetNep5Balance(scriptHash, account);
                if (nep5Balances is null)
                {
                    return(Error.VM_FAULT.ToJson());
                }
                balances = balances.Concat(nep5Balances).ToArray();
            }

            NeoBlock               currentBlock    = Blockchain.Singleton.GetBlock(Blockchain.Singleton.CurrentBlockHash);
            BlockIdentifier        blockIdentifier = new BlockIdentifier(currentBlock.Index, currentBlock.Hash.ToString());
            AccountBalanceResponse response        = new AccountBalanceResponse(blockIdentifier, balances);

            return(response.ToJson());
        }