Skip to content

Commit 1a940b2

Browse files
authored
Merge pull request #390 from immutable/refactor/marketplace-query-params
[DX-3550] refactor: marketplace query params
2 parents 96d4c50 + 8a6817f commit 1a940b2

File tree

6 files changed

+202
-75
lines changed

6 files changed

+202
-75
lines changed

sample/Assets/Scripts/Marketplace/BridgeScript.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ public void OpenWidget()
2727

2828
var link = LinkFactory.GenerateBridgeLink(
2929
environment: environment,
30-
fromTokenAddress: FromTokenAddress.text.IsNullOrEmpty() ? null : FromTokenAddress.text,
31-
fromChainID: FromChain.text.IsNullOrEmpty() ? null : FromChain.text,
32-
toTokenAddress: ToTokenAddress.text.IsNullOrEmpty() ? null : ToTokenAddress.text,
33-
toChainID: ToChain.text.IsNullOrEmpty() ? null : ToChain.text
30+
queryParams: new BridgeQueryParams
31+
{
32+
FromTokenAddress = FromTokenAddress.text.IsNullOrEmpty() ? null : FromTokenAddress.text,
33+
FromChainID = FromChain.text.IsNullOrEmpty() ? null : FromChain.text,
34+
ToTokenAddress = ToTokenAddress.text.IsNullOrEmpty() ? null : ToTokenAddress.text,
35+
ToChainID = ToChain.text.IsNullOrEmpty() ? null : ToChain.text
36+
}
3437
);
3538

3639
Application.OpenURL(link);

sample/Assets/Scripts/Marketplace/OnRampScript.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using AltWebSocketSharp;
34
using Immutable.Marketplace;
45
using UnityEngine;
@@ -49,11 +50,17 @@ public void OpenWidget()
4950
var link = LinkFactory.GenerateOnRampLink(
5051
environment: environment,
5152
email: email,
52-
address: walletAddress,
53-
fiatCurrency: FiatCurrencyInput.text.IsNullOrEmpty() ? "USD" : FiatCurrencyInput.text,
54-
fiatAmount: FiatAmountInput.text.IsNullOrEmpty() ? "50" : FiatAmountInput.text,
55-
cryptoCurrency: CryptoCurrency.text.IsNullOrEmpty() ? "IMX" : CryptoCurrency.text,
56-
cryptoCurrencyList: CryptoCurrencyList.text.IsNullOrEmpty() ? "imx,eth,usdc" : CryptoCurrencyList.text
53+
walletAddress: walletAddress,
54+
queryParams: new OnRampQueryParams
55+
{
56+
DefaultFiatCurrency = FiatCurrencyInput.text.IsNullOrEmpty() ? "USD" : FiatCurrencyInput.text,
57+
DefaultFiatAmount = FiatAmountInput.text.IsNullOrEmpty() ? "50" : FiatAmountInput.text,
58+
DefaultCryptoCurrency = CryptoCurrency.text.IsNullOrEmpty() ? "IMX" : CryptoCurrency.text,
59+
CryptoCurrencyList = CryptoCurrencyList.text.IsNullOrEmpty() ? "imx,eth,usdc" : CryptoCurrencyList.text
60+
},
61+
extraQueryParams: new Dictionary<string, string> {
62+
{"themeColor", "000000"}
63+
}
5764
);
5865

5966
Application.OpenURL(link);
@@ -66,5 +73,4 @@ public void Cancel()
6673
{
6774
SceneManager.LoadScene("MarketplaceScene");
6875
}
69-
70-
}
76+
}

sample/Assets/Scripts/Marketplace/SwapScript.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ public void OpenWidget()
4444
var link = LinkFactory.GenerateSwapLink(
4545
environment: environment,
4646
publishableKey: publishableKey,
47-
fromTokenAddress: FromTokenAddress.text.IsNullOrEmpty() ? null : FromTokenAddress.text,
48-
toTokenAddress: ToTokenAddress.text.IsNullOrEmpty() ? null : ToTokenAddress.text
47+
queryParams: new SwapQueryParams
48+
{
49+
FromTokenAddress = FromTokenAddress.text.IsNullOrEmpty() ? null : FromTokenAddress.text,
50+
ToTokenAddress = ToTokenAddress.text.IsNullOrEmpty() ? null : ToTokenAddress.text
51+
}
4952
);
5053

5154
Application.OpenURL(link);

src/Packages/Marketplace/Runtime/LinkFactory.cs

Lines changed: 104 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,97 @@ public class LinkFactory
1111
/// </summary>
1212
/// <param name="environment">Specifies the environment (<c>Sandbox</c> or <c>Production</c>).</param>
1313
/// <param name="email">The user's email address, pre-filled in the on-ramp flow.</param>
14-
/// <param name="address">The user's wallet address, where tokens will be sent.</param>
15-
/// <param name="fiatCurrency">The fiat currency to use (default: "USD").</param>
16-
/// <param name="fiatAmount">The amount of fiat currency to spend when purchasing cryptocurrency (default: "50").</param>
17-
/// <param name="cryptoCurrency">The cryptocurrency to purchase (default: "IMX").</param>
18-
/// <param name="cryptoCurrencyList">A comma-separated list of available cryptocurrencies for purchase (default: "imx,eth,usdc").</param>
14+
/// <param name="walletAddress">The user's wallet address, where tokens will be sent.</param>
15+
/// <param name="queryParams">The query parameters for the on-ramp flow. Uses default values if not specified.</param>
16+
/// <param name="extraQueryParams">Optional additional query parameters. See <a href="https://docs.transak.com/docs/query-parameters">Transak docs</a> for possible fields.</param>
1917
/// <returns>The generated on-ramp URL.</returns>
18+
/// <remarks>
19+
/// If <paramref name="extraQueryParams"/> includes any fields that are already defined in <paramref name="queryParams"/>,
20+
/// the values in <paramref name="queryParams"/> will take precedence.
21+
/// For example, if <paramref name="extraQueryParams"/> contains "defaultFiatAmount", it will be ignored and the value
22+
/// from <paramref name="queryParams.DefaultFiatAmount"/> will be used instead.
23+
/// </remarks>
2024
public static string GenerateOnRampLink(
2125
Environment environment,
2226
string email,
23-
string address,
24-
string fiatCurrency = "USD",
25-
string fiatAmount = "50",
26-
string cryptoCurrency = "IMX",
27-
string cryptoCurrencyList = "imx,eth,usdc"
27+
string walletAddress,
28+
OnRampQueryParams queryParams = default,
29+
Dictionary<string, string>? extraQueryParams = null
2830
)
2931
{
3032
var baseUrl = LinkConfig.GetBaseUrl(environment, Flow.OnRamp);
3133
var apiKey = LinkConfig.GetApiKey(environment, Flow.OnRamp);
3234

33-
var queryParams = new Dictionary<string, string>
35+
var queryParamsDictionary = new Dictionary<string, string>
3436
{
35-
{"apiKey", apiKey},
36-
{"network", "immutablezkevm"},
37-
{"defaultPaymentMethod", "credit_debit_card"},
38-
{"disablePaymentMethods", ""},
39-
{"productsAvailed", "buy"},
40-
{"exchangeScreenTitle", "Buy"},
41-
{"themeColor", "0D0D0D"},
42-
{"defaultCryptoCurrency", cryptoCurrency},
43-
{"email", Uri.EscapeDataString(email)},
44-
{"isAutoFillUserData", "true"},
45-
{"disableWalletAddressForm", "true"},
46-
{"defaultFiatAmount", fiatAmount},
47-
{"defaultFiatCurrency", fiatCurrency},
48-
{"walletAddress", address},
49-
{"cryptoCurrencyList", cryptoCurrencyList}
37+
{ "apiKey", apiKey },
38+
{ "cryptoCurrencyList", queryParams.CryptoCurrencyList },
39+
{ "defaultCryptoCurrency", queryParams.DefaultCryptoCurrency },
40+
{ "defaultFiatAmount", queryParams.DefaultFiatAmount },
41+
{ "defaultFiatCurrency", queryParams.DefaultFiatCurrency },
42+
{
43+
"defaultPaymentMethod",
44+
extraQueryParams != null &&
45+
extraQueryParams.TryGetValue("defaultPaymentMethod", out var defaultPaymentMethod)
46+
? defaultPaymentMethod
47+
: "credit_debit_card"
48+
},
49+
{
50+
"disablePaymentMethods",
51+
extraQueryParams != null &&
52+
extraQueryParams.TryGetValue("disablePaymentMethods", out var disablePaymentMethods)
53+
? disablePaymentMethods
54+
: ""
55+
},
56+
{
57+
"disableWalletAddressForm",
58+
extraQueryParams != null &&
59+
extraQueryParams.TryGetValue("disableWalletAddressForm", out var disableWalletAddressForm)
60+
? disableWalletAddressForm
61+
: "true"
62+
},
63+
{ "email", Uri.EscapeDataString(email) },
64+
{
65+
"exchangeScreenTitle",
66+
extraQueryParams != null &&
67+
extraQueryParams.TryGetValue("exchangeScreenTitle", out var exchangeScreenTitle)
68+
? exchangeScreenTitle
69+
: "Buy"
70+
},
71+
{
72+
"isAutoFillUserData",
73+
extraQueryParams != null &&
74+
extraQueryParams.TryGetValue("isAutoFillUserData", out var isAutoFillUserData)
75+
? isAutoFillUserData
76+
: "true"
77+
},
78+
{ "network", "immutablezkevm" },
79+
{ "productsAvailed", "buy" },
80+
{
81+
"themeColor",
82+
extraQueryParams != null && extraQueryParams.TryGetValue("themeColor", out var themeColor)
83+
? themeColor
84+
: "0D0D0D"
85+
},
86+
{ "walletAddress", walletAddress }
5087
};
5188

52-
var queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray());
89+
90+
// Add any extra parameters that are not already in the queryParamsDictionary
91+
if (extraQueryParams != null)
92+
{
93+
foreach (var kvp in extraQueryParams)
94+
{
95+
// Add to dictionary only if the key is not already in the dictionary
96+
if (!queryParamsDictionary.ContainsKey(kvp.Key))
97+
{
98+
queryParamsDictionary[kvp.Key] = kvp.Value;
99+
}
100+
}
101+
}
102+
103+
var queryString = string.Join("&",
104+
queryParamsDictionary.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray());
53105
return $"{baseUrl}?{queryString}";
54106
}
55107

@@ -58,71 +110,61 @@ public static string GenerateOnRampLink(
58110
/// </summary>
59111
/// <param name="environment">Specifies the environment (<c>Sandbox</c> or <c>Production</c>).</param>
60112
/// <param name="publishableKey">The publishable key obtained from <a href="https://hub.immutable.com/">Immutable Hub</a>. See <a href="https://docs.immutable.com/api/zkEVM/apikeys">API keys</a> for more details.</param>
61-
/// <param name="fromTokenAddress">The address of the token being swapped from (default is null).</param>
62-
/// <param name="toTokenAddress">The address of the token being swapped to (default is null).</param>
63-
/// <returns>A swap URL</returns>
113+
/// <param name="queryParams">The query parameters for the swap flow. Uses default values if not specified.</param>
114+
/// <returns>The generated swap URL.</returns>
64115
public static string GenerateSwapLink(
65116
Environment environment,
66117
string publishableKey,
67-
string? fromTokenAddress = null,
68-
string? toTokenAddress = null
118+
SwapQueryParams queryParams = default
69119
)
70120
{
71121
var baseUrl = LinkConfig.GetBaseUrl(environment, Flow.Swap);
72122

73-
var queryParams = new Dictionary<string, string>
123+
var queryParamsDictionary = new Dictionary<string, string>
74124
{
75-
{"publishableKey", publishableKey}
125+
{ "publishableKey", publishableKey }
76126
};
77127

78-
if (!string.IsNullOrEmpty(fromTokenAddress))
79-
{
80-
queryParams["fromTokenAddress"] = fromTokenAddress;
81-
}
128+
if (!string.IsNullOrEmpty(queryParams.FromTokenAddress))
129+
queryParamsDictionary["fromTokenAddress"] = queryParams.FromTokenAddress;
82130

83-
if (!string.IsNullOrEmpty(toTokenAddress))
84-
{
85-
queryParams["toTokenAddress"] = toTokenAddress;
86-
}
131+
if (!string.IsNullOrEmpty(queryParams.ToTokenAddress))
132+
queryParamsDictionary["toTokenAddress"] = queryParams.ToTokenAddress;
87133

88-
var queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray());
134+
var queryString = string.Join("&",
135+
queryParamsDictionary.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray());
89136
return $"{baseUrl}?{queryString}";
90137
}
91138

92139
/// <summary>
93140
/// Generates a link for the bridge flow.
94141
/// </summary>
95142
/// <param name="environment">Specifies the environment (<c>Sandbox</c> or <c>Production</c>).</param>
96-
/// <param name="fromTokenAddress">The address of the token being moved from (default is null).</param>
97-
/// <param name="fromChainID">The ID of the source blockchain (default is null).</param>
98-
/// <param name="toTokenAddress">The address of the token being moved to (default is null).</param>
99-
/// <param name="toChainID">The ID of the destination blockchain (default is null).</param>
100-
/// <returns>A bridge URL.</returns>
143+
/// <param name="queryParams">The query parameters for the bridge flow. Uses default values if not specified.</param>
144+
/// <returns>The generated bridge URL.</returns>
101145
public static string GenerateBridgeLink(
102146
Environment environment,
103-
string? fromTokenAddress = null,
104-
string? fromChainID = null,
105-
string? toTokenAddress = null,
106-
string? toChainID = null
147+
BridgeQueryParams queryParams = default
107148
)
108149
{
109150
var baseUrl = LinkConfig.GetBaseUrl(environment, Flow.Bridge);
110151

111-
var queryParams = new Dictionary<string, string>();
152+
var queryParamsDictionary = new Dictionary<string, string>();
112153

113-
if (!string.IsNullOrEmpty(fromTokenAddress))
114-
queryParams["fromToken"] = fromTokenAddress.ToLower();
154+
if (!string.IsNullOrEmpty(queryParams.FromChainID))
155+
queryParamsDictionary["fromChain"] = queryParams.FromChainID;
115156

116-
if (!string.IsNullOrEmpty(fromChainID))
117-
queryParams["fromChain"] = fromChainID;
157+
if (!string.IsNullOrEmpty(queryParams.FromTokenAddress))
158+
queryParamsDictionary["fromToken"] = queryParams.FromTokenAddress.ToLower();
118159

119-
if (!string.IsNullOrEmpty(toTokenAddress))
120-
queryParams["toToken"] = toTokenAddress.ToLower();
160+
if (!string.IsNullOrEmpty(queryParams.ToChainID))
161+
queryParamsDictionary["toChain"] = queryParams.ToChainID;
121162

122-
if (!string.IsNullOrEmpty(toChainID))
123-
queryParams["toChain"] = toChainID;
163+
if (!string.IsNullOrEmpty(queryParams.ToTokenAddress))
164+
queryParamsDictionary["toToken"] = queryParams.ToTokenAddress.ToLower();
124165

125-
var queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray());
166+
var queryString = string.Join("&",
167+
queryParamsDictionary.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray());
126168
return $"{baseUrl}?{queryString}";
127169
}
128170
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace Immutable.Marketplace
2+
{
3+
/// <summary>
4+
/// Represents the query parameters for generating an on-ramp URL.
5+
/// </summary>
6+
public struct OnRampQueryParams
7+
{
8+
/// <summary>
9+
/// The cryptocurrency to purchase (default: "IMX").
10+
/// </summary>
11+
public string DefaultCryptoCurrency { get; set; }
12+
13+
/// <summary>
14+
/// The amount of fiat currency to spend when purchasing cryptocurrency (default: "50").
15+
/// </summary>
16+
public string DefaultFiatAmount { get; set; }
17+
18+
/// <summary>
19+
/// The fiat currency to use (default: "USD").
20+
/// </summary>
21+
public string DefaultFiatCurrency { get; set; }
22+
23+
/// <summary>
24+
/// A comma-separated list of available cryptocurrencies for purchase (default: "imx,eth,usdc").
25+
/// </summary>
26+
public string CryptoCurrencyList { get; set; }
27+
}
28+
29+
/// <summary>
30+
/// Represents the query parameters for generating a swap URL.
31+
/// </summary>
32+
public struct SwapQueryParams
33+
{
34+
/// <summary>
35+
/// The address of the token being swapped from (default is null).
36+
/// </summary>
37+
public string? FromTokenAddress { get; set; }
38+
39+
/// <summary>
40+
/// The address of the token being swapped to (default is null).
41+
/// </summary>
42+
public string? ToTokenAddress { get; set; }
43+
}
44+
45+
/// <summary>
46+
/// Represents the query parameters for generating a bridge URL.
47+
/// </summary>
48+
public struct BridgeQueryParams
49+
{
50+
/// <summary>
51+
/// The ID of the source blockchain (default is null).
52+
/// </summary>
53+
public string? FromChainID { get; set; }
54+
55+
/// <summary>
56+
/// The address of the token being moved from (default is null).
57+
/// </summary>
58+
public string? FromTokenAddress { get; set; }
59+
60+
/// <summary>
61+
/// The ID of the destination blockchain (default is null).
62+
/// </summary>
63+
public string? ToChainID { get; set; }
64+
65+
/// <summary>
66+
/// The address of the token being moved to (default is null).
67+
/// </summary>
68+
public string? ToTokenAddress { get; set; }
69+
}
70+
}

src/Packages/Marketplace/Runtime/LinkQueryParams.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)