コード例 #1
0
        public async Task <SmartBitResult> PushTx(Transaction transaction)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }

            var post = $"{BaseUrl}blockchain/pushtx";

            var content = new StringContent(new JObject(new JProperty("hex", transaction.ToHex())).ToString(), Encoding.UTF8,
                                            "application/json");
            HttpResponseMessage smartBitResponse = null;
            await Sem.WaitAsync().ConfigureAwait(false);

            try
            {
                smartBitResponse = await HttpClient.PostAsync(post, content).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                return(new SmartBitResult
                {
                    State = SmartBitResultState.ConnectionError,
                    AdditionalInformation = ex.ToString()
                });
            }
            finally
            {
                Sem.SafeRelease();
            }
            if (smartBitResponse == null)
            {
                return(new SmartBitResult
                {
                    State = SmartBitResultState.ConnectionError,
                    AdditionalInformation = $"{nameof(smartBitResponse)} is null"
                });
            }
            if (smartBitResponse.StatusCode != System.Net.HttpStatusCode.OK)
            {
                return(new SmartBitResult
                {
                    State = SmartBitResultState.ConnectionError,
                    AdditionalInformation = $"Server answered with {smartBitResponse.StatusCode}"
                });
            }

            try
            {
                string response = await smartBitResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                var json = JObject.Parse(response);

                var result = new SmartBitResult
                {
                    AdditionalInformation = response
                };
                if (json.Value <bool>("success"))
                {
                    result.State = SmartBitResultState.Success;
                }
                else
                {
                    result.State = SmartBitResultState.Failure;
                }
                return(result);
            }
            catch (Exception ex)
            {
                return(new SmartBitResult
                {
                    State = SmartBitResultState.UnexpectedResponse,
                    AdditionalInformation = ex.ToString()
                });
            }
        }
コード例 #2
0
        public async Task <SmartBitResult> GetTransaction(string txId)
        {
            if (txId == null)
            {
                throw new ArgumentNullException(nameof(txId));
            }

            var get = $"{BaseUrl}blockchain/tx/" + txId + "/hex";

            HttpResponseMessage smartBitResponse = null;
            await Sem.WaitAsync().ConfigureAwait(false);

            try
            {
                smartBitResponse = await HttpClient.GetAsync(get).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                return(new SmartBitResult
                {
                    State = SmartBitResultState.ConnectionError,
                    AdditionalInformation = ex.ToString()
                });
            }
            finally
            {
                Sem.SafeRelease();
            }
            if (smartBitResponse == null)
            {
                return(new SmartBitResult
                {
                    State = SmartBitResultState.ConnectionError,
                    AdditionalInformation = $"{nameof(smartBitResponse)} is null"
                });
            }
            if (smartBitResponse.StatusCode == System.Net.HttpStatusCode.BadRequest)
            {
                return(new SmartBitResult
                {
                    State = SmartBitResultState.Failure,
                    TransactionHex = null
                });
            }
            if (smartBitResponse.StatusCode != System.Net.HttpStatusCode.OK)
            {
                return(new SmartBitResult
                {
                    State = SmartBitResultState.ConnectionError,
                    AdditionalInformation = $"Server answered with {smartBitResponse.StatusCode}"
                });
            }

            try
            {
                string response = await smartBitResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                var txResponse = JsonConvert.DeserializeObject <TransactionHex>(response);

                var result = new SmartBitResult
                {
                    AdditionalInformation = response
                };
                if (txResponse.success)
                {
                    result.State          = SmartBitResultState.Success;
                    result.TransactionHex = txResponse.hex.First().hex;
                }
                else
                {
                    result.State          = SmartBitResultState.Failure;
                    result.TransactionHex = null;
                }

                return(result);
            }
            catch (Exception ex)
            {
                return(new SmartBitResult
                {
                    State = SmartBitResultState.UnexpectedResponse,
                    AdditionalInformation = ex.ToString()
                });
            }
        }