Skip to content
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

✨ Add SwapMode to allow ExactOut mode with Jupiter #45

Merged
merged 1 commit into from
Sep 12, 2023
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
5 changes: 3 additions & 2 deletions src/Solana.Unity.Dex/IDexAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ namespace Solana.Unity.Dex
/// </remarks>
public interface IDexAggregator
{

/// <summary>
/// Creates a quote for a swap for a specified pair of input/output token mint.
/// </summary>
/// <param name="inputMint">The mint address of the input token (the token to swap).</param>
/// <param name="outputMint">The mint address of the output token (the token to swap for).</param>
/// <param name="amount">The amount to swap (could be of the input token or output token).</param>
/// <param name="amount">The amount to swap (could be of the input token or output token).</param>
/// <param name="swapMode">The swap mode. ExactIn or ExactOut</param>
/// <param name="slippageBps">The slippage % in BPS. If the output token amount exceeds the slippage then the swap transaction will fail.</param>
/// <param name="excludeDexes">Default is that all DEXes are included. You can pass in the DEXes that you want to exclude and separate them by ,. For example, Aldrin,Saber.</param>
/// <param name="onlyDirectRoutes">Default is false. Direct Routes limits Jupiter routing to single hop routes only.</param>
Expand All @@ -36,6 +36,7 @@ Task<SwapQuoteAg> GetSwapQuote(
PublicKey inputMint,
PublicKey outputMint,
BigInteger amount,
SwapMode swapMode = SwapMode.ExactIn,
ushort? slippageBps = null,
List<string> excludeDexes = null,
bool onlyDirectRoutes = false,
Expand Down
3 changes: 2 additions & 1 deletion src/Solana.Unity.Dex/Jupiter/JupiterDex.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Solana.Unity.Dex.Jupiter.Core.Types;
using Solana.Unity.Dex.Jupiter.Core.Types.Http;
using Solana.Unity.Dex.Models;
using Solana.Unity.Dex.Orca.Orca;
Expand Down Expand Up @@ -79,6 +78,7 @@ public async Task<SwapQuoteAg> GetSwapQuote(
PublicKey inputMint,
PublicKey outputMint,
BigInteger amount,
SwapMode swapMode = SwapMode.ExactIn,
ushort? slippageBps = null,
List<string> excludeDexes = null,
bool onlyDirectRoutes = false,
Expand All @@ -91,6 +91,7 @@ public async Task<SwapQuoteAg> GetSwapQuote(
new("inputMint", inputMint.ToString()),
new("outputMint", outputMint.ToString()),
new("amount", amount.ToString()),
new("swapMode", swapMode.ToString()),
new("asLegacyTransaction", "false")
};

Expand Down
2 changes: 1 addition & 1 deletion src/Solana.Unity.Dex/Solana.Unity.Dex.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@

<PropertyGroup>
<Product>Solana.Unity.Dex</Product>
<Version>1.2</Version>
<Version>1.3</Version>
</PropertyGroup>
</Project>
20 changes: 20 additions & 0 deletions test/Solana.Unity.Dex.Test/Jupiter/Integration/JupiterTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NUnit.Framework;
using Solana.Unity.Dex.Jupiter;
using Solana.Unity.Dex.Models;
using Solana.Unity.Dex.Quotes;
using System.Threading.Tasks;
using Solana.Unity.Dex.Test.Orca;
using Solana.Unity.Rpc;
Expand Down Expand Up @@ -37,6 +38,25 @@ public static async Task GetSwapQuote()
Assert.IsTrue(quote.RoutePlan.Count > 0);
}


[Test]
[Description("get a swap quote")]
public static async Task GetSwapQuoteWithExactOut()
{
IDexAggregator dexAg = new JupiterDexAg(TestConfiguration.TestWallet.Account);

PublicKey inputMint = new("So11111111111111111111111111111111111111112"); // SOL
PublicKey outputMint = new("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"); // USDC
BigInteger amount = 100;

var quote = await dexAg.GetSwapQuote(inputMint, outputMint, amount, SwapMode.ExactOut);

Assert.IsNotNull(quote);
Assert.IsTrue(quote.OutputAmount > 0);
Assert.IsTrue(quote.InputAmount > 0);
Assert.IsTrue(quote.RoutePlan.Count > 0);
}

[Test]
[Description("get a swap transaction")]
public static async Task GetSwap()
Expand Down