Skip to content

Commit

Permalink
[Pay] Allow passing purchaseData with quotes (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper authored Dec 4, 2024
1 parent f3b8791 commit b75f909
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 63 deletions.
42 changes: 14 additions & 28 deletions Thirdweb/Thirdweb.Pay/ThirdwebPay.GetBuyWithCryptoQuote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,18 @@ public partial class ThirdwebPay
/// <exception cref="Exception">Thrown if the HTTP response is not successful.</exception>
public static async Task<BuyWithCryptoQuoteResult> GetBuyWithCryptoQuote(ThirdwebClient client, BuyWithCryptoQuoteParams buyWithCryptoParams)
{
var queryString = new Dictionary<string, string>
{
{ "fromAddress", buyWithCryptoParams.FromAddress },
{ "fromChainId", buyWithCryptoParams.FromChainId?.ToString() },
{ "fromTokenAddress", buyWithCryptoParams.FromTokenAddress },
{ "fromAmount", buyWithCryptoParams.FromAmount },
{ "fromAmountWei", buyWithCryptoParams.FromAmountWei },
{ "toChainId", buyWithCryptoParams.ToChainId?.ToString() },
{ "toTokenAddress", buyWithCryptoParams.ToTokenAddress },
{ "toAmount", buyWithCryptoParams.ToAmount },
{ "toAmountWei", buyWithCryptoParams.ToAmountWei },
{ "toAddress", buyWithCryptoParams.ToAddress },
{ "maxSlippageBPS", buyWithCryptoParams.MaxSlippageBPS?.ToString() },
{ "intentId", buyWithCryptoParams.IntentId }
};

var queryStringFormatted = string.Join("&", queryString.Where(kv => kv.Value != null).Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}"));
var url = $"{THIRDWEB_PAY_CRYPTO_QUOTE_ENDPOINT}?{queryStringFormatted}";

var getResponse = await client.HttpClient.GetAsync(url);

var content = await getResponse.Content.ReadAsStringAsync();

if (!getResponse.IsSuccessStatusCode)
var response = await client.HttpClient.PostAsync(
THIRDWEB_PAY_CRYPTO_QUOTE_ENDPOINT,
new StringContent(
JsonConvert.SerializeObject(buyWithCryptoParams, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
System.Text.Encoding.UTF8,
"application/json"
)
);

var content = await response.Content.ReadAsStringAsync();

if (!response.IsSuccessStatusCode)
{
ErrorResponse error;
try
Expand All @@ -56,14 +44,12 @@ public static async Task<BuyWithCryptoQuoteResult> GetBuyWithCryptoQuote(Thirdwe
Reason = "Unknown",
Code = "Unknown",
Stack = "Unknown",
StatusCode = (int)getResponse.StatusCode
StatusCode = (int)response.StatusCode
}
};
}

throw new Exception(
$"HTTP error! Code: {error.Error.Code} Message: {error.Error.Message} Reason: {error.Error.Reason} StatusCode: {error.Error.StatusCode} Stack: {error.Error.Stack}"
);
throw new Exception($"HTTP error! Code: {error.Error.Code} Message: {error.Error.Message} Reason: {error.Error.Reason} StatusCode: {error.Error.StatusCode} Stack: {error.Error.Stack}");
}

var data = JsonConvert.DeserializeObject<GetSwapQuoteResponse>(content);
Expand Down
41 changes: 14 additions & 27 deletions Thirdweb/Thirdweb.Pay/ThirdwebPay.GetBuyWithFiatQuote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,18 @@ public partial class ThirdwebPay
/// <exception cref="Exception">Thrown if the HTTP response is not successful.</exception>
public static async Task<BuyWithFiatQuoteResult> GetBuyWithFiatQuote(ThirdwebClient client, BuyWithFiatQuoteParams buyWithFiatParams)
{
var queryString = new Dictionary<string, string>
{
{ "fromCurrencySymbol", buyWithFiatParams.FromCurrencySymbol },
{ "fromAmount", buyWithFiatParams.FromAmount },
{ "fromAmountUnits", buyWithFiatParams.FromAmountUnits },
{ "toAddress", buyWithFiatParams.ToAddress },
{ "toChainId", buyWithFiatParams.ToChainId },
{ "toTokenAddress", buyWithFiatParams.ToTokenAddress },
{ "toAmount", buyWithFiatParams.ToAmount },
{ "toAmountWei", buyWithFiatParams.ToAmountWei },
{ "preferredProvider", buyWithFiatParams.PreferredProvider },
{ "maxSlippageBPS", buyWithFiatParams.MaxSlippageBPS?.ToString() }
};

var queryStringFormatted = string.Join("&", queryString.Where(kv => kv.Value != null).Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}"));
var url = $"{THIRDWEB_PAY_FIAT_QUOTE_ENDPOINT}?{queryStringFormatted}";
url += buyWithFiatParams.IsTestMode ? "&isTestMode=true" : "&isTestMode=false";

var getResponse = await client.HttpClient.GetAsync(url);

var content = await getResponse.Content.ReadAsStringAsync();

if (!getResponse.IsSuccessStatusCode)
var response = await client.HttpClient.PostAsync(
THIRDWEB_PAY_FIAT_QUOTE_ENDPOINT,
new StringContent(
JsonConvert.SerializeObject(buyWithFiatParams, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
System.Text.Encoding.UTF8,
"application/json"
)
);

var content = await response.Content.ReadAsStringAsync();

if (!response.IsSuccessStatusCode)
{
ErrorResponse error;
try
Expand All @@ -55,14 +44,12 @@ public static async Task<BuyWithFiatQuoteResult> GetBuyWithFiatQuote(ThirdwebCli
Reason = "Unknown",
Code = "Unknown",
Stack = "Unknown",
StatusCode = (int)getResponse.StatusCode
StatusCode = (int)response.StatusCode
}
};
}

throw new Exception(
$"HTTP error! Code: {error.Error.Code} Message: {error.Error.Message} Reason: {error.Error.Reason} StatusCode: {error.Error.StatusCode} Stack: {error.Error.Stack}"
);
throw new Exception($"HTTP error! Code: {error.Error.Code} Message: {error.Error.Message} Reason: {error.Error.Reason} StatusCode: {error.Error.StatusCode} Stack: {error.Error.Stack}");
}

var data = JsonConvert.DeserializeObject<GetFiatQuoteResponse>(content);
Expand Down
9 changes: 8 additions & 1 deletion Thirdweb/Thirdweb.Pay/Types.GetBuyWithCryptoQuote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class BuyWithCryptoQuoteParams(
string toAmountWei = null,
string toAddress = null,
double? maxSlippageBPS = null,
string intentId = null
string intentId = null,
object purchaseData = null
)
{
/// <summary>
Expand Down Expand Up @@ -95,6 +96,12 @@ public class BuyWithCryptoQuoteParams(
/// </summary>
[JsonProperty("intentId")]
public string IntentId { get; set; } = intentId;

/// <summary>
/// Additional data for the purchase. Useful with direct transfer flow.
/// </summary>
[JsonProperty("purchaseData")]
public object PurchaseData { get; set; } = purchaseData;
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions Thirdweb/Thirdweb.Pay/Types.GetBuyWithCryptoStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public class BuyWithCryptoStatusResult
/// </summary>
[JsonProperty("bridge")]
public string Bridge { get; set; }

/// <summary>
/// Additional data for the purchase. Useful with direct transfer flow.
/// </summary>
[JsonProperty("purchaseData")]
public object PurchaseData { get; set; }
}

/// <summary>
Expand Down
20 changes: 14 additions & 6 deletions Thirdweb/Thirdweb.Pay/Types.GetBuyWithFiatQuote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class BuyWithFiatQuoteParams(
string toAmountWei = null,
double? maxSlippageBPS = null,
bool isTestMode = false,
string preferredProvider = null
string preferredProvider = null,
object purchaseData = null
)
{
/// <summary>
Expand All @@ -40,11 +41,6 @@ public class BuyWithFiatQuoteParams(
[JsonProperty("fromAmountUnits")]
public string FromAmountUnits { get; set; } = fromAmountUnits;

/// <summary>
/// The provider to use on the application for thirdweb pay
/// </summary>
[JsonProperty("preferredProvider")]
public string PreferredProvider { get; set; } = preferredProvider;
/// <summary>
/// The address to receive the purchased tokens.
/// </summary>
Expand Down Expand Up @@ -86,6 +82,18 @@ public class BuyWithFiatQuoteParams(
/// </summary>
[JsonProperty("isTestMode")]
public bool IsTestMode { get; set; } = isTestMode;

/// <summary>
/// The provider to use on the application for thirdweb pay
/// </summary>
[JsonProperty("preferredProvider")]
public string PreferredProvider { get; set; } = preferredProvider;

/// <summary>
/// Additional data for the purchase. Useful with direct transfer flow.
/// </summary>
[JsonProperty("purchaseData")]
public object PurchaseData { get; set; } = purchaseData;
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions Thirdweb/Thirdweb.Pay/Types.GetBuyWithFiatStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public class BuyWithFiatStatusResult
/// </summary>
[JsonProperty("failureMessage")]
public string FailureMessage { get; set; }

/// <summary>
/// Additional data for the purchase. Useful with direct transfer flow.
/// </summary>
[JsonProperty("purchaseData")]
public object PurchaseData { get; set; }
}

/// <summary>
Expand Down
19 changes: 18 additions & 1 deletion Thirdweb/Thirdweb.Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,24 @@ public static string GenerateSIWE(LoginPayloadData loginPayloadData)
/// <returns>True if it is a zkSync chain ID, otherwise false.</returns>
public static async Task<bool> IsZkSync(ThirdwebClient client, BigInteger chainId)
{
if (chainId.Equals(324) || chainId.Equals(300) || chainId.Equals(302) || chainId.Equals(11124) || chainId.Equals(4654) || chainId.Equals(333271) || chainId.Equals(37111))
if (
chainId.Equals(324)
|| chainId.Equals(300)
|| chainId.Equals(302)
|| chainId.Equals(11124)
|| chainId.Equals(282)
|| chainId.Equals(388)
|| chainId.Equals(4654)
|| chainId.Equals(333271)
|| chainId.Equals(37111)
|| chainId.Equals(978658)
|| chainId.Equals(531050104)
|| chainId.Equals(4457845)
|| chainId.Equals(2741)
|| chainId.Equals(240)
|| chainId.Equals(61166)
|| chainId.Equals(555271)
)
{
return true;
}
Expand Down

0 comments on commit b75f909

Please sign in to comment.