Beispiel #1
0
        /// <summary>
        /// Creates the <see cref="Request{T}"/> object and gets account transactions.
        /// </summary>
        /// <param name="options">The options for this request.</param>
        /// <returns>A <see cref="Request{T}"/> object.</returns>
        public Request <AccountTxResponse> RequestAccountTx(AccountTxOptions options)
        {
            var request = new Request <AccountTxResponse>(this, "account_tx", (data) =>
            {
                var response = JsonConvert.DeserializeObject <ResponseData <AccountTxResponse> >(data as string);
                if (response.Result.RawTransactions != null)
                {
                    response.Result.Transactions = response.Result.RawTransactions
                                                   .Select(tx => ResponseParser.ProcessTx(tx.Tx, tx.Meta, options.Account))
                                                   .ToArray();
                }
                return(response.Result);
            });

            if (!Utils.IsValidAddress(options.Account))
            {
                request.Message.Error = new InvalidAddressException(options.Account, "Account", "Invalid account address.");
                return(request);
            }

            request.Message.account = options.Account;
            SetLedgerOptions(options.Ledger, request);
            request.Message.ledger_index_min = options.LedgerMin == null ? 0 : options.LedgerMin.Value;
            request.Message.ledger_index_max = options.LedgerMax == null ? -1 : options.LedgerMax;

            if (options.Limit != null)
            {
                request.Message.limit = options.Limit.Value;
            }

            if (options.Offset != null)
            {
                request.Message.offset = options.Offset.Value;
            }

            if (options.Marker != null && options.Marker.IsValid())
            {
                request.Message.marker = options.Marker;
            }

            if (options.Forward != null)
            {
                request.Message.forward = options.Forward;
            }

            return(request);
        }
Beispiel #2
0
        /// <summary>
        /// Creates the <see cref="Request{T}"/> object and gets one transaction information.
        /// </summary>
        /// <param name="options">The options for this request.</param>
        /// <returns>A <see cref="Request{T}"/> object.</returns>
        public Request <TxResponse> RequestTx(TxOptions options)
        {
            var request = new Request <TxResponse>(this, "tx", (data) =>
            {
                var response             = JsonConvert.DeserializeObject <ResponseData <TxResponse> >(data as string);
                response.Result.TxResult = ResponseParser.ProcessTx(response.Result, response.Result.Meta, response.Result.Account);
                return(response.Result);
            });

            if (!Utils.IsValidHash(options.Hash))
            {
                request.Message.Error = new InvalidHashException(options.Hash, "Hash", "Invalid tx hash.");
                return(request);
            }

            request.Message.transaction = options.Hash;
            return(request);
        }
Beispiel #3
0
        /// <summary>
        /// Creates the <see cref="Request{T}"/> object and gets order book info.
        /// </summary>
        /// <param name="options">The options for this request.</param>
        /// <returns>A <see cref="Request{T}"/> object.</returns>
        public Request <OrderBookResponse> RequestOrderBook(OrderBookOptions options)
        {
            var request = new Request <OrderBookResponse>(this, "book_offers").SetFilter(data =>
            {
                var response = JsonConvert.DeserializeObject <ResponseData <OrderBookResponse> >(data as string);
                var result   = response.Result;

                if (result?.Offers != null)
                {
                    foreach (var offer in result.Offers)
                    {
                        offer.Price = ResponseParser.GetPrice(offer.TakerGets, offer.TakerPays, offer.IsSell);
                    }
                }
                return(result);
            });

            var takerGets = options.Pays;

            if (!Utils.IsValidAmount0(takerGets))
            {
                request.Message.Error = new InvalidAmountException(takerGets, "Pays", "Invalid pays (taker gets) amount.");
                return(request);
            }

            var takerPays = options.Gets;

            if (!Utils.IsValidAmount0(takerPays))
            {
                request.Message.Error = new InvalidAmountException(takerPays, "Gets", "Invalid gets (taker pays) amount.");
                return(request);
            }

            request.Message.taker_gets = takerGets;
            request.Message.taker_pays = takerPays;
            request.Message.taker      = options.Taker ?? Config.AccountOne;

            if (options.Limit != null)
            {
                request.Message.limit = options.Limit;
            }

            return(request);
        }
Beispiel #4
0
        private void _remote_Transactions(object sender, TransactionsEventArgs e)
        {
            if (_accounts.Count == 0)
            {
                return;
            }

            var response = e.Result;
            var accounts = ResponseParser.AffectedAccounts(response.Transaction, response.Meta);

            foreach (var account in accounts)
            {
                if (_accounts.ContainsKey(account))
                {
                    var result = ResponseParser.ProcessTx(response.Transaction, response.Meta, account);
                    _accounts[account].Invoke(new MessageResult <TxResult>(e.Message, null, result));
                }
            }
        }
Beispiel #5
0
        private void HandleTransaction(string data)
        {
            if (_transactions == null)
            {
                return;
            }

            var response = JsonConvert.DeserializeObject <TransactionResponse>(data);
            var hash     = response.Transaction.Hash;

            if (_txCache.Contains(hash))
            {
                return;
            }

            _txCache.Add(hash, true);
            response.TxResult = ResponseParser.ProcessTx(response.Transaction, response.Meta, response.Transaction.Account);
            var args = new TransactionsEventArgs {
                Message = data, Result = response
            };

            OnTransactions(args);
        }
Beispiel #6
0
        /// <summary>
        /// Registers the listener for given order book pair.
        /// </summary>
        /// <param name="gets">The amount of gets.</param>
        /// <param name="pays">The amount of pays.</param>
        /// <param name="callback">The callback.</param>
        public void RegisterListener(Amount gets, Amount pays, MessageCallback <TxResult> callback)
        {
            var key = ResponseParser.GetAmoutPairKey(gets, pays);

            _books[key] = callback;
        }
Beispiel #7
0
        /// <summary>
        /// Creates the <see cref="Request{T}"/> object and gets the ledger in system.
        /// </summary>
        /// <param name="options">The options for this request.</param>
        /// <returns>A <see cref="Request{T}"/> object.</returns>
        public Request <LedgerResponse> RequestLedger(LedgerOptions options = null)
        {
            var request = new Request <LedgerResponse>(this, "ledger", (data) =>
            {
                var response = JsonConvert.DeserializeObject <ResponseData <LedgerResponse> >(data as string);
                var result   = response.Result;

                var transactions = result?.Ledger?.Transactions;
                if (transactions != null)
                {
                    foreach (var tx in transactions)
                    {
                        if (tx.IsExpanded)
                        {
                            tx.TxResult = ResponseParser.ProcessTx(tx, tx.Meta, tx.Account);
                        }
                    }
                }
                return(result);
            });

            if (options != null)
            {
                if (options.LedgerIndex != null)
                {
                    request.Message.ledger_index = options.LedgerIndex.Value;
                }

                if (options.LedgerHash != null)
                {
                    if (!Utils.IsValidHash(options.LedgerHash))
                    {
                        request.Message.Error = new InvalidHashException(options.LedgerHash, "LedgerHash", "Invalid ledger hash.");
                        return(request);
                    }
                    request.Message.ledger_hash = options.LedgerHash;
                }

                if (options.Full != null)
                {
                    request.Message.full = options.Full.Value;
                }

                if (options.Expand != null)
                {
                    request.Message.expand = options.Expand.Value;
                }

                if (options.Transactions != null)
                {
                    request.Message.transactions = options.Transactions.Value;
                }

                if (options.Accounts != null)
                {
                    request.Message.accounts = options.Accounts.Value;
                }
            }

            return(request);
        }