Skip to content

Add implementation of GetLifiChains and GetLifiTokens endpoints - the… #285

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
87 changes: 87 additions & 0 deletions Assets/SequenceSDK/Marketplace/CurrencySwapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,92 @@ public async Task GetSwapQuoteTest_FailedToFetchPrice()
Assert.IsTrue(e.Message.Contains("Error fetching swap quote"));
}
}

[Test]
public async Task TestGetSupportedChains()
{
CurrencySwap currencySwap = new CurrencySwap(_chain);

try
{
Chain[] supportedChains = await currencySwap.GetSupportedChains();
Assert.IsNotNull(supportedChains);
Assert.Greater(supportedChains.Length, 0);
}
catch (Exception e)
{
Assert.Fail($"Exception encountered while fetching supported chains: {e.Message}");
}
}

[Test]
public async Task TestGetSupportedTokens()
{
CurrencySwap currencySwap = new CurrencySwap(_chain);

try
{
Token[] supportedTokens = await currencySwap.GetSupportedTokens(new[] { _chain });
Assert.IsNotNull(supportedTokens);
Assert.Greater(supportedTokens.Length, 0);
}
catch (Exception e)
{
Assert.Fail($"Exception encountered while fetching supported tokens: {e.Message}");
}
}

[Test]
public async Task TestGetSupportedTokens_EmptyChains()
{
CurrencySwap currencySwap = new CurrencySwap(_chain);

try
{
Token[] supportedTokens = await currencySwap.GetSupportedTokens(new Chain[0]);
Assert.Fail("Expected exception but none was thrown");
}
catch (Exception e)
{
Assert.IsTrue(e.Message.Contains("Error fetching supported tokens:"));
}
}

[Test]
public async Task TestGetSupportedTokens_NullChains()
{
CurrencySwap currencySwap = new CurrencySwap(_chain);

try
{
Token[] supportedTokens = await currencySwap.GetSupportedTokens(null);
Assert.Fail("Expected exception but none was thrown");
}
catch (Exception e)
{
Assert.IsTrue(e.Message.Contains("Error fetching supported tokens:"));
}
}

[Test]
public async Task TestGetSupportedTokens_AllSupportedChains()
{
CurrencySwap currencySwap = new CurrencySwap(_chain);

try
{
Chain[] supportedChains = await currencySwap.GetSupportedChains();
Assert.IsNotNull(supportedChains);
Assert.Greater(supportedChains.Length, 0);

Token[] supportedTokens = await currencySwap.GetSupportedTokens(supportedChains);
Assert.IsNotNull(supportedTokens);
Assert.Greater(supportedTokens.Length, 0);
}
catch (Exception e)
{
Assert.Fail($"Exception encountered while fetching supported tokens: {e.Message}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,15 @@ public Task<SwapQuote> GetSwapQuote(Address userWallet, Address buyCurrency, Add
{
throw new NotImplementedException();
}

public Task<Chain[]> GetSupportedChains()
{
throw new NotImplementedException();
}

public Task<Token[]> GetSupportedTokens(Chain[] chains)
{
throw new NotImplementedException();
}
}
}
63 changes: 63 additions & 0 deletions Packages/Sequence-Unity/Sequence/SequenceSDK/Ethereum/Token.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Numerics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine.Scripting;

namespace Sequence
{
[Serializable]
[JsonConverter(typeof(TokenConverter))]
public class Token
{
public Chain Chain;
public Address Contract;
public string TokenId;

public Token(Chain chain, Address contract, string tokenId = null)
{
Chain = chain;
Contract = contract;
TokenId = tokenId;
}

[Preserve]
[JsonConstructor]
public Token(BigInteger chainId, string contractAddress, string tokenId = null)
{
Chain = ChainDictionaries.ChainById[chainId.ToString()];
Contract = new Address(contractAddress);
TokenId = tokenId;
}
}

internal class TokenConverter : JsonConverter<Token>
{
public override void WriteJson(JsonWriter writer, Token value, JsonSerializer serializer)
{
var jsonObject = new JObject
{
["chainId"] = ulong.Parse(ChainDictionaries.ChainIdOf[value.Chain]),
["contractAddress"] = value.Contract.ToString(),
};
if (value.TokenId != null)
{
jsonObject["tokenId"] = value.TokenId;
}

jsonObject.WriteTo(writer);
}

public override Token ReadJson(JsonReader reader, Type objectType, Token existingValue,
bool hasExistingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);

BigInteger chainId = jsonObject["chainId"]?.Value<ulong>() ?? 0;
string contractAddress = jsonObject["contractAddress"]?.Value<string>();
string tokenId = jsonObject["tokenId"]?.Value<string>();

return new Token(chainId, contractAddress, tokenId);
}
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Sequence
[Serializable]
internal class GetTokenPricesArgs
{
public PriceFeed.Token[] tokens;
public Token[] tokens;

public GetTokenPricesArgs(PriceFeed.Token[] tokens)
public GetTokenPricesArgs(Token[] tokens)
{
this.tokens = tokens;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Sequence
[Serializable]
public class TokenPrice
{
public PriceFeed.Token token;
public Token token;
public Price price;
public Price price24hChange;
public Price floorPrice;
Expand All @@ -15,7 +15,7 @@ public class TokenPrice
public DateTime updatedAt;

[Preserve]
public TokenPrice(PriceFeed.Token token, Price price, Price price24hChange, Price floorPrice, Price buyPrice, Price sellPrice, DateTime updatedAt)
public TokenPrice(Token token, Price price, Price price24hChange, Price floorPrice, Price buyPrice, Price sellPrice, DateTime updatedAt)
{
this.token = token;
this.price = price;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,22 @@
using System;
using System.Numerics;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sequence.Config;
using Sequence.Utils;
using UnityEngine.Scripting;

namespace Sequence
{
public class PriceFeed
{
[Serializable]
[JsonConverter(typeof(TokenConverter))]
public class Token
[Obsolete("Use the Token class in the Sequence namespace instead.")]
public class Token : Sequence.Token
{
public Chain Chain;
public Address Contract;

public Token(Chain chain, Address contract)
{
Chain = chain;
Contract = contract;
}

[Preserve]
[JsonConstructor]
public Token(BigInteger chainId, string contractAddress)
public Token(Chain chain, Address contract, string tokenId = null) : base(chain, contract, tokenId)
{
Chain = ChainDictionaries.ChainById[chainId.ToString()];
Contract = new Address(contractAddress);
}
}

private class TokenConverter : JsonConverter<Token>
{
public override void WriteJson(JsonWriter writer, Token value, JsonSerializer serializer)
public Token(BigInteger chainId, string contractAddress, string tokenId = null) : base(chainId, contractAddress, tokenId)
{
var jsonObject = new JObject
{
["chainId"] = ulong.Parse(ChainDictionaries.ChainIdOf[value.Chain]),
["contractAddress"] = value.Contract.ToString(),
};
jsonObject.WriteTo(writer);
}

public override Token ReadJson(JsonReader reader, Type objectType, Token existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);

BigInteger chainId = jsonObject["chainId"]?.Value<ulong>() ?? 0;
string contractAddress = jsonObject["contractAddress"]?.Value<string>();

return new Token(chainId, contractAddress);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,37 @@ private async Task AssertWeHaveSufficientBalance(Address userWallet, Address buy
$"Insufficient balance of {sellCurrency} to buy {buyAmount} of {buyCurrency}, have {have}, need {required}");
}
}

public async Task<Chain[]> GetSupportedChains()
{
string url = BaseUrl.AppendTrailingSlashIfNeeded() + "GetLifiChains";
try
{
LifiSupportedChainsResponse response =
await _client.SendRequest<object, LifiSupportedChainsResponse>(url, null);
return response.GetChains();
}
catch (Exception e)
{
string error = $"Error fetching supported chains: {e.Message}";
throw new Exception(error);
}
}

public async Task<Token[]> GetSupportedTokens(Chain[] chains)
{
string url = BaseUrl.AppendTrailingSlashIfNeeded() + "GetLifiTokens";
try
{
GetLifiTokensResponse response =
await _client.SendRequest<GetLifiTokensRequest, GetLifiTokensResponse>(url, new GetLifiTokensRequest(chains));
return response.tokens;
}
catch (Exception e)
{
string error = $"Error fetching supported tokens: {e.Message}";
throw new Exception(error);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Numerics;
using Newtonsoft.Json;
using UnityEngine.Scripting;

namespace Sequence.Marketplace
{
[Serializable]
public class GetLifiTokensRequest
{
public BigInteger[] chainIds;

[JsonConstructor]
[Preserve]
public GetLifiTokensRequest(BigInteger[] chainIds)
{
this.chainIds = chainIds;
}

public GetLifiTokensRequest(Chain[] chains)
{
int length = chains.Length;
chainIds = new BigInteger[length];
for (int i = 0; i < length; i++)
{
chainIds[i] = BigInteger.Parse(ChainDictionaries.ChainIdOf[chains[i]]);
}
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using UnityEngine.Scripting;

namespace Sequence.Marketplace
{
[Serializable]
public class GetLifiTokensResponse
{
public Token[] tokens;

[Preserve]
public GetLifiTokensResponse(Token[] tokens)
{
this.tokens = tokens;
}
}
}

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

Loading