Skip to content

[DX-3527] feat: function to generate bridge URL #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Packages/Marketplace/Runtime/Bridge.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions src/Packages/Marketplace/Runtime/Bridge/Bridge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Immutable.Marketplace.Bridge
{
/// <summary>
/// Provides functionality for generating a link to the bridge flow,
/// simplifying the process of moving tokens from and to the Immutable zkEVM network.
/// </summary>
public class Bridge
{
private readonly string _environment;
private static readonly Dictionary<string, string> BaseUrls = new()
{
{ "sandbox", "https://checkout-playground.sandbox.immutable.com/checkout/squid" },
{ "production", "https://toolkit.immutable.com/checkout/squid" }
};

/// <summary>
/// Initialises a new instance of the <see cref="Bridge"/> class.
/// </summary>
/// <param name="environment">Specifies the environment (<c>sandbox</c> or <c>production</c>).</param>
public Bridge(string environment)
{
_environment = environment;
}

/// <summary>
/// Generates a link for the bridge flow.
/// </summary>
/// <param name="fromTokenAddress">The address of the token being moved from (default is null).</param>
/// <param name="fromChain">The ID of the source blockchain (default is null).</param>
/// <param name="toTokenAddress">The address of the token being moved to (default is null).</param>
/// <param name="toChain">The ID of the destination blockchain (default is null).</param>
/// <returns>A bridge URL.</returns>
public string GetLink(string? fromTokenAddress, string? fromChain, string? toTokenAddress, string? toChain)
{
var baseUrl = BaseUrls[_environment];

var queryParams = new Dictionary<string, string>();

if (!string.IsNullOrEmpty(fromTokenAddress))
queryParams["fromToken"] = fromTokenAddress;

if (!string.IsNullOrEmpty(fromChain))
queryParams["fromChain"] = fromChain;

if (!string.IsNullOrEmpty(toTokenAddress))
queryParams["toTokenAddress"] = toTokenAddress;

if (!string.IsNullOrEmpty(toChain))
queryParams["toChain"] = toChain;

var queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}").ToArray());
return $"{baseUrl}?{queryString}";
}
}
}
3 changes: 3 additions & 0 deletions src/Packages/Marketplace/Runtime/Bridge/Bridge.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions src/Packages/Marketplace/Runtime/Swap/Swap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,28 @@ public Swap(string environment)
/// <summary>
/// Generates a link for the swap flow.
/// </summary>
/// <param name="fromTokenAddress">The address of the token being swapped from.</param>
/// <param name="toTokenAddress">The address of the token being swapped to.</param>
/// <param name="fromTokenAddress">The address of the token being swapped from (default is null).</param>
/// <param name="toTokenAddress">The address of the token being swapped to (default is null).</param>
/// <returns>A swap URL</returns>
public string GetLink(string fromTokenAddress, string toTokenAddress)
public string GetLink(string? fromTokenAddress = null, string? toTokenAddress = null)
{
var baseUrl = BaseUrls[_environment];
var apiKey = ApiKeys[_environment];

var queryParams = new Dictionary<string, string>
{
{"publishableKey", apiKey},
{"fromTokenAddress", fromTokenAddress},
{"toTokenAddress", toTokenAddress}
};
{
{"publishableKey", apiKey}
};

if (!string.IsNullOrEmpty(fromTokenAddress))
{
queryParams["fromTokenAddress"] = fromTokenAddress;
}

if (!string.IsNullOrEmpty(toTokenAddress))
{
queryParams["toTokenAddress"] = toTokenAddress;
}

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