Beispiel #1
0
        /// <summary>
        /// Create a new Bitcoin or Gluwacoin transaction.
        /// </summary>
        /// <param name="currency">Currency type</param>
        /// <param name="address">Your public Address.</param>
        /// <param name="privateKey">Your Private Key.</param>
        /// <param name="amount">Transaction amount, not including the fee.</param>
        /// <param name="target">The address that the transaction will be sent to.</param>
        /// <param name="merchantOrderID">Identifier for the transaction that was provided by the merchant user. Optional.</param>
        /// <param name="note">Additional information about the transaction that a user can provide. Optional.</param>
        /// <param name="nonce">Nonce for the transaction. For Gluwacoin currencies only.</param>
        /// <param name="idem">Idempotent key for the transaction to prevent duplicate transactions.</param>
        /// <param name="paymentID">ID for the QR code payment.</param>
        /// <param name="paymentSig">Signature of the QR code payment.Required if PaymentID is not null.</param>
        /// <response code="202">Newly accepted transaction.</response>
        /// <response code="400">Invalid request. or Validation error. See inner errors for more details. or (BTC only) Signed BTC transaction could not be verified.</response>
        /// <response code="403">For payments, payment signature could not be verified.</response>
        /// <response code="409">A transaction with the same transaction hash, payment ID, or idem already exists.</response>
        /// <response code="500">Server error.</response>
        /// <response code="503">Service unavailable.</response>
        public async Task <Result <bool, ErrorResponse> > CreateTransactionAsync(
            ECurrency currency,
            string address,
            string privateKey,
            string amount,
            string target,
            string merchantOrderID = null,
            string note            = null,
            string nonce           = null,
            Guid?idem         = null,
            Guid?paymentID    = null,
            string paymentSig = null)
        {
            if (string.IsNullOrWhiteSpace(address))
            {
                throw new ArgumentNullException(nameof(address));
            }
            else if (string.IsNullOrWhiteSpace(privateKey))
            {
                throw new ArgumentNullException(nameof(privateKey));
            }
            else if (string.IsNullOrWhiteSpace(amount))
            {
                throw new ArgumentNullException(nameof(amount));
            }
            else if (string.IsNullOrWhiteSpace(target))
            {
                throw new ArgumentNullException(nameof(target));
            }
            else if (paymentID != null)
            {
                if (string.IsNullOrWhiteSpace(paymentSig))
                {
                    throw new ArgumentException(nameof(paymentSig));
                }
            }

            var result     = new Result <bool, ErrorResponse>();
            var requestUri = $"{mEnv.BaseUrl}/v1/Transactions";

            Result <FeeResponse, ErrorResponse> getFee = await getFeeAsync(currency);

            if (getFee.IsFailure)
            {
                result.Error = getFee.Error;

                return(result);
            }

            string fee       = getFee.Data.MinimumFee;
            string signature = null;

            if (currency == ECurrency.BTC)
            {
                signature = await getBtcTransactionSignatureAsync(currency, address, amount, fee, target, privateKey);
            }
            else
            {
                if (nonce == null)
                {
                    nonce = GluwaService.GetNonceString();
                }

                signature = getGluwacoinTransactionSignature(currency, amount, fee, nonce, address, target, privateKey);
            }

            TransactionRequest bodyParams = new TransactionRequest
            {
                Signature       = signature,
                Currency        = currency,
                Target          = target,
                Amount          = amount,
                Fee             = getFee.Data.MinimumFee,
                Source          = address,
                Nonce           = nonce,
                MerchantOrderID = merchantOrderID,
                Note            = note,
                Idem            = idem,
                PaymentID       = paymentID,
                PaymentSig      = paymentSig
            };

            string        json    = bodyParams.ToJson();
            StringContent content = new StringContent(json, Encoding.UTF8, "application/json");

            try
            {
                using (HttpClient httpClient = new HttpClient())
                    using (var response = await httpClient.PostAsync(requestUri, content))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            result.IsSuccess = true;
                            result.Data      = true;

                            return(result);
                        }

                        string contentString = await response.Content.ReadAsStringAsync();

                        result.Error = ResponseHandler.GetError(response.StatusCode, requestUri, contentString);
                    }
            }
            catch (HttpRequestException)
            {
                result.IsSuccess = false;
                result.Error     = ResponseHandler.GetExceptionError();
            }
            return(result);
        }