示例#1
0
        public async Task HandleTransactions(WebWalletTransactionsAction action, IDispatcher dispatcher)
        {
            var result = await action.wallet.Sync(null);

            List <string> txs = new List <string>();

            if (result == Lyra.Core.Blocks.APIResultCodes.Success)
            {
                var accHeight = await client.GetAccountHeight(action.wallet.AccountId);

                Dictionary <string, long> oldBalance = null;
                var start = accHeight.Height - 100;
                if (start < 1)
                {
                    start = 1;                                          // only show the last 100 tx
                }
                for (long i = start; i <= accHeight.Height; i++)
                {
                    var blockResult = await client.GetBlockByIndex(action.wallet.AccountId, i);

                    var block = blockResult.GetBlock() as TransactionBlock;
                    if (block == null)
                    {
                        txs.Add("Null");
                    }
                    else
                    {
                        var str = $"No. {block.Height} {block.TimeStamp}, ";
                        if (block is SendTransferBlock sb)
                        {
                            str += $"Send to {sb.DestinationAccountId}";
                        }
                        else if (block is ReceiveTransferBlock rb)
                        {
                            if (rb.SourceHash == null)
                            {
                                str += $"Genesis";
                            }
                            else
                            {
                                var srcBlockResult = await client.GetBlock(rb.SourceHash);

                                var srcBlock = srcBlockResult.GetBlock() as TransactionBlock;
                                str += $"Receive from {srcBlock.AccountID}";
                            }
                        }
                        str += BalanceDifference(oldBalance, block.Balances);
                        str += $" Balance: {string.Join(", ", block.Balances.Select(m => $"{m.Key}: {m.Value.ToBalanceDecimal()}"))}";

                        txs.Add(str);

                        oldBalance = block.Balances;
                    }
                }
            }
            txs.Reverse();
            dispatcher.Dispatch(new WebWalletTransactionsResultAction {
                wallet = action.wallet, transactions = txs
            });
        }
        protected override async Task HandleAsync(BlockSearchAction action, IDispatcher dispatcher)
        {
            var    hashToSearch = action.hash;
            Block  blockResult  = null;
            Block  prevBlock    = null;
            long   maxHeight    = 0;
            string key          = null;

            if (string.IsNullOrWhiteSpace(hashToSearch))
            {
                var genSvcRet = await client.GetLastConsolidationBlock();

                if (genSvcRet.ResultCode == APIResultCodes.Success)
                {
                    blockResult = genSvcRet.GetBlock();
                }
            }
            else
            {
                BlockAPIResult ret = null;
                if (hashToSearch.Length < 40)
                {
                    ret = await client.GetServiceBlockByIndex(action.hash, action.height);
                }
                else if (hashToSearch.Length == 44 || hashToSearch.Length == 43)                // hash
                {
                    ret = await client.GetBlock(action.hash);
                }
                else
                {
                    var exists = await client.GetAccountHeight(action.hash);

                    if (exists.ResultCode == APIResultCodes.Success)
                    {
                        maxHeight = exists.Height;
                        key       = action.hash;
                        ret       = await client.GetBlockByIndex(action.hash, action.height == 0?exists.Height : action.height);
                    }
                }

                if (ret != null && ret.ResultCode == APIResultCodes.Success)
                {
                    blockResult = ret.GetBlock();
                }
            }

            (key, maxHeight) = await GetMaxHeightAsync(blockResult);

            try
            {
                if (blockResult != null)
                {
                    prevBlock = blockResult.PreviousHash == null ? null : (await client.GetBlock(blockResult.PreviousHash)).GetBlock();
                }
            }
            catch (Exception) { }

            dispatcher.Dispatch(new BlockSearchResultAction(blockResult, prevBlock, key, maxHeight));
        }