diff --git a/sample/Assets/Scripts/Marketplace/BridgeScript.cs b/sample/Assets/Scripts/Marketplace/BridgeScript.cs index fe6bdc470..1d75b310b 100644 --- a/sample/Assets/Scripts/Marketplace/BridgeScript.cs +++ b/sample/Assets/Scripts/Marketplace/BridgeScript.cs @@ -27,10 +27,13 @@ public void OpenWidget() var link = LinkFactory.GenerateBridgeLink( environment: environment, - fromTokenAddress: FromTokenAddress.text.IsNullOrEmpty() ? null : FromTokenAddress.text, - fromChainID: FromChain.text.IsNullOrEmpty() ? null : FromChain.text, - toTokenAddress: ToTokenAddress.text.IsNullOrEmpty() ? null : ToTokenAddress.text, - toChainID: ToChain.text.IsNullOrEmpty() ? null : ToChain.text + queryParams: new BridgeQueryParams + { + FromTokenAddress = FromTokenAddress.text.IsNullOrEmpty() ? null : FromTokenAddress.text, + FromChainID = FromChain.text.IsNullOrEmpty() ? null : FromChain.text, + ToTokenAddress = ToTokenAddress.text.IsNullOrEmpty() ? null : ToTokenAddress.text, + ToChainID = ToChain.text.IsNullOrEmpty() ? null : ToChain.text + } ); Application.OpenURL(link); diff --git a/sample/Assets/Scripts/Marketplace/OnRampScript.cs b/sample/Assets/Scripts/Marketplace/OnRampScript.cs index cb6e24435..54886cccb 100644 --- a/sample/Assets/Scripts/Marketplace/OnRampScript.cs +++ b/sample/Assets/Scripts/Marketplace/OnRampScript.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using AltWebSocketSharp; using Immutable.Marketplace; using UnityEngine; @@ -49,11 +50,17 @@ public void OpenWidget() var link = LinkFactory.GenerateOnRampLink( environment: environment, email: email, - address: walletAddress, - fiatCurrency: FiatCurrencyInput.text.IsNullOrEmpty() ? "USD" : FiatCurrencyInput.text, - fiatAmount: FiatAmountInput.text.IsNullOrEmpty() ? "50" : FiatAmountInput.text, - cryptoCurrency: CryptoCurrency.text.IsNullOrEmpty() ? "IMX" : CryptoCurrency.text, - cryptoCurrencyList: CryptoCurrencyList.text.IsNullOrEmpty() ? "imx,eth,usdc" : CryptoCurrencyList.text + walletAddress: walletAddress, + queryParams: new OnRampQueryParams + { + DefaultFiatCurrency = FiatCurrencyInput.text.IsNullOrEmpty() ? "USD" : FiatCurrencyInput.text, + DefaultFiatAmount = FiatAmountInput.text.IsNullOrEmpty() ? "50" : FiatAmountInput.text, + DefaultCryptoCurrency = CryptoCurrency.text.IsNullOrEmpty() ? "IMX" : CryptoCurrency.text, + CryptoCurrencyList = CryptoCurrencyList.text.IsNullOrEmpty() ? "imx,eth,usdc" : CryptoCurrencyList.text + }, + extraQueryParams: new Dictionary { + {"themeColor", "000000"} + } ); Application.OpenURL(link); @@ -66,5 +73,4 @@ public void Cancel() { SceneManager.LoadScene("MarketplaceScene"); } - -} +} \ No newline at end of file diff --git a/sample/Assets/Scripts/Marketplace/SwapScript.cs b/sample/Assets/Scripts/Marketplace/SwapScript.cs index 5fc6f3ed0..321f53375 100644 --- a/sample/Assets/Scripts/Marketplace/SwapScript.cs +++ b/sample/Assets/Scripts/Marketplace/SwapScript.cs @@ -44,8 +44,11 @@ public void OpenWidget() var link = LinkFactory.GenerateSwapLink( environment: environment, publishableKey: publishableKey, - fromTokenAddress: FromTokenAddress.text.IsNullOrEmpty() ? null : FromTokenAddress.text, - toTokenAddress: ToTokenAddress.text.IsNullOrEmpty() ? null : ToTokenAddress.text + queryParams: new SwapQueryParams + { + FromTokenAddress = FromTokenAddress.text.IsNullOrEmpty() ? null : FromTokenAddress.text, + ToTokenAddress = ToTokenAddress.text.IsNullOrEmpty() ? null : ToTokenAddress.text + } ); Application.OpenURL(link); diff --git a/src/Packages/Marketplace/Runtime/LinkFactory.cs b/src/Packages/Marketplace/Runtime/LinkFactory.cs index b6629e171..e65ae34df 100644 --- a/src/Packages/Marketplace/Runtime/LinkFactory.cs +++ b/src/Packages/Marketplace/Runtime/LinkFactory.cs @@ -11,45 +11,97 @@ public class LinkFactory /// /// Specifies the environment (Sandbox or Production). /// The user's email address, pre-filled in the on-ramp flow. - /// The user's wallet address, where tokens will be sent. - /// The fiat currency to use (default: "USD"). - /// The amount of fiat currency to spend when purchasing cryptocurrency (default: "50"). - /// The cryptocurrency to purchase (default: "IMX"). - /// A comma-separated list of available cryptocurrencies for purchase (default: "imx,eth,usdc"). + /// The user's wallet address, where tokens will be sent. + /// The query parameters for the on-ramp flow. Uses default values if not specified. + /// Optional additional query parameters. See Transak docs for possible fields. /// The generated on-ramp URL. + /// + /// If includes any fields that are already defined in , + /// the values in will take precedence. + /// For example, if contains "defaultFiatAmount", it will be ignored and the value + /// from will be used instead. + /// public static string GenerateOnRampLink( Environment environment, string email, - string address, - string fiatCurrency = "USD", - string fiatAmount = "50", - string cryptoCurrency = "IMX", - string cryptoCurrencyList = "imx,eth,usdc" + string walletAddress, + OnRampQueryParams queryParams = default, + Dictionary? extraQueryParams = null ) { var baseUrl = LinkConfig.GetBaseUrl(environment, Flow.OnRamp); var apiKey = LinkConfig.GetApiKey(environment, Flow.OnRamp); - var queryParams = new Dictionary + var queryParamsDictionary = new Dictionary { - {"apiKey", apiKey}, - {"network", "immutablezkevm"}, - {"defaultPaymentMethod", "credit_debit_card"}, - {"disablePaymentMethods", ""}, - {"productsAvailed", "buy"}, - {"exchangeScreenTitle", "Buy"}, - {"themeColor", "0D0D0D"}, - {"defaultCryptoCurrency", cryptoCurrency}, - {"email", Uri.EscapeDataString(email)}, - {"isAutoFillUserData", "true"}, - {"disableWalletAddressForm", "true"}, - {"defaultFiatAmount", fiatAmount}, - {"defaultFiatCurrency", fiatCurrency}, - {"walletAddress", address}, - {"cryptoCurrencyList", cryptoCurrencyList} + { "apiKey", apiKey }, + { "cryptoCurrencyList", queryParams.CryptoCurrencyList }, + { "defaultCryptoCurrency", queryParams.DefaultCryptoCurrency }, + { "defaultFiatAmount", queryParams.DefaultFiatAmount }, + { "defaultFiatCurrency", queryParams.DefaultFiatCurrency }, + { + "defaultPaymentMethod", + extraQueryParams != null && + extraQueryParams.TryGetValue("defaultPaymentMethod", out var defaultPaymentMethod) + ? defaultPaymentMethod + : "credit_debit_card" + }, + { + "disablePaymentMethods", + extraQueryParams != null && + extraQueryParams.TryGetValue("disablePaymentMethods", out var disablePaymentMethods) + ? disablePaymentMethods + : "" + }, + { + "disableWalletAddressForm", + extraQueryParams != null && + extraQueryParams.TryGetValue("disableWalletAddressForm", out var disableWalletAddressForm) + ? disableWalletAddressForm + : "true" + }, + { "email", Uri.EscapeDataString(email) }, + { + "exchangeScreenTitle", + extraQueryParams != null && + extraQueryParams.TryGetValue("exchangeScreenTitle", out var exchangeScreenTitle) + ? exchangeScreenTitle + : "Buy" + }, + { + "isAutoFillUserData", + extraQueryParams != null && + extraQueryParams.TryGetValue("isAutoFillUserData", out var isAutoFillUserData) + ? isAutoFillUserData + : "true" + }, + { "network", "immutablezkevm" }, + { "productsAvailed", "buy" }, + { + "themeColor", + extraQueryParams != null && extraQueryParams.TryGetValue("themeColor", out var themeColor) + ? themeColor + : "0D0D0D" + }, + { "walletAddress", walletAddress } }; - var queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray()); + + // Add any extra parameters that are not already in the queryParamsDictionary + if (extraQueryParams != null) + { + foreach (var kvp in extraQueryParams) + { + // Add to dictionary only if the key is not already in the dictionary + if (!queryParamsDictionary.ContainsKey(kvp.Key)) + { + queryParamsDictionary[kvp.Key] = kvp.Value; + } + } + } + + var queryString = string.Join("&", + queryParamsDictionary.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray()); return $"{baseUrl}?{queryString}"; } @@ -58,34 +110,29 @@ public static string GenerateOnRampLink( /// /// Specifies the environment (Sandbox or Production). /// The publishable key obtained from Immutable Hub. See API keys for more details. - /// The address of the token being swapped from (default is null). - /// The address of the token being swapped to (default is null). - /// A swap URL + /// The query parameters for the swap flow. Uses default values if not specified. + /// The generated swap URL. public static string GenerateSwapLink( Environment environment, string publishableKey, - string? fromTokenAddress = null, - string? toTokenAddress = null + SwapQueryParams queryParams = default ) { var baseUrl = LinkConfig.GetBaseUrl(environment, Flow.Swap); - var queryParams = new Dictionary + var queryParamsDictionary = new Dictionary { - {"publishableKey", publishableKey} + { "publishableKey", publishableKey } }; - if (!string.IsNullOrEmpty(fromTokenAddress)) - { - queryParams["fromTokenAddress"] = fromTokenAddress; - } + if (!string.IsNullOrEmpty(queryParams.FromTokenAddress)) + queryParamsDictionary["fromTokenAddress"] = queryParams.FromTokenAddress; - if (!string.IsNullOrEmpty(toTokenAddress)) - { - queryParams["toTokenAddress"] = toTokenAddress; - } + if (!string.IsNullOrEmpty(queryParams.ToTokenAddress)) + queryParamsDictionary["toTokenAddress"] = queryParams.ToTokenAddress; - var queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray()); + var queryString = string.Join("&", + queryParamsDictionary.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray()); return $"{baseUrl}?{queryString}"; } @@ -93,36 +140,31 @@ public static string GenerateSwapLink( /// Generates a link for the bridge flow. /// /// Specifies the environment (Sandbox or Production). - /// The address of the token being moved from (default is null). - /// The ID of the source blockchain (default is null). - /// The address of the token being moved to (default is null). - /// The ID of the destination blockchain (default is null). - /// A bridge URL. + /// The query parameters for the bridge flow. Uses default values if not specified. + /// The generated bridge URL. public static string GenerateBridgeLink( Environment environment, - string? fromTokenAddress = null, - string? fromChainID = null, - string? toTokenAddress = null, - string? toChainID = null + BridgeQueryParams queryParams = default ) { var baseUrl = LinkConfig.GetBaseUrl(environment, Flow.Bridge); - var queryParams = new Dictionary(); + var queryParamsDictionary = new Dictionary(); - if (!string.IsNullOrEmpty(fromTokenAddress)) - queryParams["fromToken"] = fromTokenAddress.ToLower(); + if (!string.IsNullOrEmpty(queryParams.FromChainID)) + queryParamsDictionary["fromChain"] = queryParams.FromChainID; - if (!string.IsNullOrEmpty(fromChainID)) - queryParams["fromChain"] = fromChainID; + if (!string.IsNullOrEmpty(queryParams.FromTokenAddress)) + queryParamsDictionary["fromToken"] = queryParams.FromTokenAddress.ToLower(); - if (!string.IsNullOrEmpty(toTokenAddress)) - queryParams["toToken"] = toTokenAddress.ToLower(); + if (!string.IsNullOrEmpty(queryParams.ToChainID)) + queryParamsDictionary["toChain"] = queryParams.ToChainID; - if (!string.IsNullOrEmpty(toChainID)) - queryParams["toChain"] = toChainID; + if (!string.IsNullOrEmpty(queryParams.ToTokenAddress)) + queryParamsDictionary["toToken"] = queryParams.ToTokenAddress.ToLower(); - var queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray()); + var queryString = string.Join("&", + queryParamsDictionary.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray()); return $"{baseUrl}?{queryString}"; } } diff --git a/src/Packages/Marketplace/Runtime/LinkQueryParams.cs b/src/Packages/Marketplace/Runtime/LinkQueryParams.cs new file mode 100644 index 000000000..04217aa8f --- /dev/null +++ b/src/Packages/Marketplace/Runtime/LinkQueryParams.cs @@ -0,0 +1,70 @@ +namespace Immutable.Marketplace +{ + /// + /// Represents the query parameters for generating an on-ramp URL. + /// + public struct OnRampQueryParams + { + /// + /// The cryptocurrency to purchase (default: "IMX"). + /// + public string DefaultCryptoCurrency { get; set; } + + /// + /// The amount of fiat currency to spend when purchasing cryptocurrency (default: "50"). + /// + public string DefaultFiatAmount { get; set; } + + /// + /// The fiat currency to use (default: "USD"). + /// + public string DefaultFiatCurrency { get; set; } + + /// + /// A comma-separated list of available cryptocurrencies for purchase (default: "imx,eth,usdc"). + /// + public string CryptoCurrencyList { get; set; } + } + + /// + /// Represents the query parameters for generating a swap URL. + /// + public struct SwapQueryParams + { + /// + /// The address of the token being swapped from (default is null). + /// + public string? FromTokenAddress { get; set; } + + /// + /// The address of the token being swapped to (default is null). + /// + public string? ToTokenAddress { get; set; } + } + + /// + /// Represents the query parameters for generating a bridge URL. + /// + public struct BridgeQueryParams + { + /// + /// The ID of the source blockchain (default is null). + /// + public string? FromChainID { get; set; } + + /// + /// The address of the token being moved from (default is null). + /// + public string? FromTokenAddress { get; set; } + + /// + /// The ID of the destination blockchain (default is null). + /// + public string? ToChainID { get; set; } + + /// + /// The address of the token being moved to (default is null). + /// + public string? ToTokenAddress { get; set; } + } +} \ No newline at end of file diff --git a/src/Packages/Marketplace/Runtime/LinkQueryParams.cs.meta b/src/Packages/Marketplace/Runtime/LinkQueryParams.cs.meta new file mode 100644 index 000000000..31621b30b --- /dev/null +++ b/src/Packages/Marketplace/Runtime/LinkQueryParams.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a3d4658a7ab84ad8b8b6d9cfa89b6205 +timeCreated: 1738042715 \ No newline at end of file