|
| 1 | +using Newtonsoft.Json; |
| 2 | + |
| 3 | +namespace Thirdweb.Pay |
| 4 | +{ |
| 5 | + public partial class ThirdwebPay |
| 6 | + { |
| 7 | + public static async Task<BuyWithCryptoQuoteResult> GetBuyWithCryptoQuote(ThirdwebClient client, BuyWithCryptoQuoteParams buyWithCryptoParams) |
| 8 | + { |
| 9 | + var queryString = new Dictionary<string, string> |
| 10 | + { |
| 11 | + { "fromAddress", buyWithCryptoParams.FromAddress }, |
| 12 | + { "fromChainId", buyWithCryptoParams.FromChainId?.ToString() }, |
| 13 | + { "fromTokenAddress", buyWithCryptoParams.FromTokenAddress }, |
| 14 | + { "fromAmount", buyWithCryptoParams.FromAmount }, |
| 15 | + { "fromAmountWei", buyWithCryptoParams.FromAmountWei }, |
| 16 | + { "toChainId", buyWithCryptoParams.ToChainId?.ToString() }, |
| 17 | + { "toTokenAddress", buyWithCryptoParams.ToTokenAddress }, |
| 18 | + { "toAmount", buyWithCryptoParams.ToAmount }, |
| 19 | + { "toAmountWei", buyWithCryptoParams.ToAmountWei }, |
| 20 | + { "maxSlippageBPS", buyWithCryptoParams.MaxSlippageBPS?.ToString() }, |
| 21 | + { "intentId", buyWithCryptoParams.IntentId } |
| 22 | + }; |
| 23 | + |
| 24 | + var queryStringFormatted = string.Join("&", queryString.Where(kv => kv.Value != null).Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}")); |
| 25 | + var url = $"{Constants.THIRDWEB_PAY_CRYPTO_QUOTE_ENDPOINT}?{queryStringFormatted}"; |
| 26 | + |
| 27 | + var getResponse = await client.HttpClient.GetAsync(url); |
| 28 | + |
| 29 | + var content = await getResponse.Content.ReadAsStringAsync(); |
| 30 | + |
| 31 | + if (!getResponse.IsSuccessStatusCode) |
| 32 | + { |
| 33 | + ErrorResponse error; |
| 34 | + try |
| 35 | + { |
| 36 | + error = JsonConvert.DeserializeObject<ErrorResponse>(content); |
| 37 | + } |
| 38 | + catch |
| 39 | + { |
| 40 | + error = new ErrorResponse |
| 41 | + { |
| 42 | + Error = new ErrorDetails |
| 43 | + { |
| 44 | + Message = "Unknown error", |
| 45 | + Reason = "Unknown", |
| 46 | + Code = "Unknown", |
| 47 | + Stack = "Unknown", |
| 48 | + StatusCode = (int)getResponse.StatusCode |
| 49 | + } |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + throw new Exception( |
| 54 | + $"HTTP error! Code: {error.Error.Code} Message: {error.Error.Message} Reason: {error.Error.Reason} StatusCode: {error.Error.StatusCode} Stack: {error.Error.Stack}" |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + var data = JsonConvert.DeserializeObject<GetSwapQuoteResponse>(content); |
| 59 | + return data.Result; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments