Skip to content

Commit 3fdb5b5

Browse files
committed
feat: function to generate bridge URL, make swap url params optional
1 parent 6fa9ce7 commit 3fdb5b5

File tree

4 files changed

+81
-8
lines changed

4 files changed

+81
-8
lines changed

src/Packages/Marketplace/Runtime/Bridge.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Immutable.Marketplace.Bridge
6+
{
7+
/// <summary>
8+
/// Provides functionality for generating a link to the bridge flow,
9+
/// simplifying the process of moving tokens from and to the Immutable zkEVM network.
10+
/// </summary>
11+
public class Bridge
12+
{
13+
private readonly string _environment;
14+
private static readonly Dictionary<string, string> BaseUrls = new()
15+
{
16+
{ "sandbox", "https://checkout-playground.sandbox.immutable.com/checkout/squid" },
17+
{ "production", "https://toolkit.immutable.com/checkout/squid" }
18+
};
19+
20+
/// <summary>
21+
/// Initialises a new instance of the <see cref="Bridge"/> class.
22+
/// </summary>
23+
/// <param name="environment">Specifies the environment (<c>sandbox</c> or <c>production</c>).</param>
24+
public Bridge(string environment)
25+
{
26+
_environment = environment;
27+
}
28+
29+
/// <summary>
30+
/// Generates a link for the bridge flow.
31+
/// </summary>
32+
/// <param name="fromTokenAddress">The address of the token being moved from (default is null).</param>
33+
/// <param name="fromChain">The ID of the source blockchain (default is null).</param>
34+
/// <param name="toTokenAddress">The address of the token being moved to (default is null).</param>
35+
/// <param name="toChain">The ID of the destination blockchain (default is null).</param>
36+
/// <returns>A bridge URL.</returns>
37+
public string GetLink(string? fromTokenAddress, string? fromChain, string? toTokenAddress, string? toChain)
38+
{
39+
var baseUrl = BaseUrls[_environment];
40+
41+
var queryParams = new Dictionary<string, string>();
42+
43+
if (!string.IsNullOrEmpty(fromTokenAddress))
44+
queryParams["fromToken"] = fromTokenAddress;
45+
46+
if (!string.IsNullOrEmpty(fromChain))
47+
queryParams["fromChain"] = fromChain;
48+
49+
if (!string.IsNullOrEmpty(toTokenAddress))
50+
queryParams["toTokenAddress"] = toTokenAddress;
51+
52+
if (!string.IsNullOrEmpty(toChain))
53+
queryParams["toChain"] = toChain;
54+
55+
var queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray());
56+
return $"{baseUrl}?{queryString}";
57+
}
58+
}
59+
}

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

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Packages/Marketplace/Runtime/Swap/Swap.cs

+16-8
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,28 @@ public Swap(string environment)
3535
/// <summary>
3636
/// Generates a link for the swap flow.
3737
/// </summary>
38-
/// <param name="fromTokenAddress">The address of the token being swapped from.</param>
39-
/// <param name="toTokenAddress">The address of the token being swapped to.</param>
38+
/// <param name="fromTokenAddress">The address of the token being swapped from (default is null).</param>
39+
/// <param name="toTokenAddress">The address of the token being swapped to (default is null).</param>
4040
/// <returns>A swap URL</returns>
41-
public string GetLink(string fromTokenAddress, string toTokenAddress)
41+
public string GetLink(string? fromTokenAddress = null, string? toTokenAddress = null)
4242
{
4343
var baseUrl = BaseUrls[_environment];
4444
var apiKey = ApiKeys[_environment];
4545

4646
var queryParams = new Dictionary<string, string>
47-
{
48-
{"publishableKey", apiKey},
49-
{"fromTokenAddress", fromTokenAddress},
50-
{"toTokenAddress", toTokenAddress}
51-
};
47+
{
48+
{"publishableKey", apiKey}
49+
};
50+
51+
if (!string.IsNullOrEmpty(fromTokenAddress))
52+
{
53+
queryParams["fromTokenAddress"] = fromTokenAddress;
54+
}
55+
56+
if (!string.IsNullOrEmpty(toTokenAddress))
57+
{
58+
queryParams["toTokenAddress"] = toTokenAddress;
59+
}
5260

5361
var queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray());
5462
return $"{baseUrl}?{queryString}";

0 commit comments

Comments
 (0)