Skip to content

Commit 6a13eb7

Browse files
✨ Add IDexAggregator interface & Jupiter implementation (#42)
* ✨ Add IDexAggregator & Jupiter implementation * ⚡ Allow to set Mint on TokenData * ♻️ Refactor & Fix getting quote on WebGL
1 parent 43dd564 commit 6a13eb7

File tree

10 files changed

+719
-7
lines changed

10 files changed

+719
-7
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Solana.Unity.Dex.Models;
2+
using Solana.Unity.Dex.Quotes;
3+
using Solana.Unity.Rpc.Models;
4+
using System.Numerics;
5+
using Solana.Unity.Wallet;
6+
using System.Collections.Generic;
7+
using System.Threading.Tasks;
8+
9+
namespace Solana.Unity.Dex
10+
{
11+
/// <summary>
12+
/// A repository of functions that create and return full unsigned transactions to execute specified
13+
/// actions on the Whirlpool, e.g. creating a funded position or executing a swap.
14+
/// </summary>
15+
/// <remarks>
16+
/// The transactions are returned as Transaction instances, not signed. To complete the action each
17+
/// transaction must be signed, serialized, and sent to the RPC node. Each transaction may consist of
18+
/// a single instruction or multiple instructions.
19+
/// </remarks>
20+
public interface IDexAggregator
21+
{
22+
23+
/// <summary>
24+
/// Creates a quote for a swap for a specified pair of input/output token mint.
25+
/// </summary>
26+
/// <param name="inputMint">The mint address of the input token (the token to swap).</param>
27+
/// <param name="outputMint">The mint address of the output token (the token to swap for).</param>
28+
/// <param name="amount">The amount to swap (could be of the input token or output token).</param>
29+
/// <param name="slippageBps">The slippage % in BPS. If the output token amount exceeds the slippage then the swap transaction will fail.</param>
30+
/// <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>
31+
/// <param name="onlyDirectRoutes">Default is false. Direct Routes limits Jupiter routing to single hop routes only.</param>
32+
/// <param name="platformFeeBps">If you want to charge the user a fee, you can specify the fee in BPS. Fee % is taken out of the output token.</param>
33+
/// <param name="maxAccounts">Rough estimate of the max accounts to be used for the quote, so that you can compose with your own accounts</param>
34+
/// <returns>A SwapQuote instance containing information about the swap amounts.</returns>
35+
Task<SwapQuoteAg> GetSwapQuote(
36+
PublicKey inputMint,
37+
PublicKey outputMint,
38+
BigInteger amount,
39+
ushort? slippageBps = null,
40+
List<string> excludeDexes = null,
41+
bool onlyDirectRoutes = false,
42+
ushort? platformFeeBps = null,
43+
ushort? maxAccounts = null
44+
);
45+
46+
/// <summary>
47+
/// Creates a transaction to execute a swap for a specified pair of input/output token mint.
48+
/// </summary>
49+
/// <param name="quoteResponse"></param>
50+
/// <param name="userPublicKey"></param>
51+
/// <param name="destinationTokenAccount"></param>
52+
/// <param name="wrapAndUnwrapSol"></param>
53+
/// <param name="useSharedAccounts"></param>
54+
/// <param name="feeAccount"></param>
55+
/// <param name="computeUnitPriceMicroLamports"></param>
56+
/// <param name="useTokenLedger"></param>
57+
/// <returns></returns>
58+
Task<Transaction> Swap(
59+
SwapQuoteAg quoteResponse,
60+
PublicKey userPublicKey = null,
61+
PublicKey destinationTokenAccount = null,
62+
bool wrapAndUnwrapSol = true,
63+
bool useSharedAccounts = true,
64+
PublicKey feeAccount = null,
65+
BigInteger? computeUnitPriceMicroLamports = null,
66+
bool useTokenLedger = false
67+
);
68+
69+
/// <summary>
70+
/// Get the list of tokens that are supported by the dex
71+
/// </summary>
72+
/// <returns></returns>
73+
Task<IList<TokenData>> GetTokens(TokenListType tokenListType = TokenListType.Strict);
74+
75+
/// <summary>
76+
/// Get a token details given the symbol
77+
/// </summary>
78+
/// <param name="symbol">the token symbol</param>
79+
/// <returns></returns>
80+
Task<TokenData> GetTokenBySymbol(string symbol);
81+
82+
/// <summary>
83+
/// Get a token details given the mint address
84+
/// </summary>
85+
/// <param name="mint"></param>
86+
/// <returns></returns>
87+
Task<TokenData> GetTokenByMint(string mint);
88+
}
89+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Solana.Unity.Dex.Quotes;
2+
using Solana.Unity.Wallet;
3+
using BigInteger = System.Numerics.BigInteger;
4+
5+
namespace Solana.Unity.Dex.Jupiter.Core.Types.Http;
6+
7+
/// <summary>
8+
/// The request to get a swap quote from the Jupiter aggregator.
9+
/// </summary>
10+
public class SwapRequest
11+
{
12+
/// <summary>
13+
/// The swap quote
14+
/// </summary>
15+
public SwapQuoteAg QuoteResponse { get; set; }
16+
17+
/// <summary>
18+
/// The user's public key
19+
/// </summary>
20+
public string UserPublicKey { get; set; }
21+
22+
/// <summary>
23+
/// The user's token account
24+
/// </summary>
25+
public PublicKey DestinationTokenAccount { get; set; }
26+
27+
/// <summary>
28+
/// The user's token account
29+
/// </summary>
30+
public bool WrapAndUnwrapSol { get; set; }
31+
32+
/// <summary>
33+
/// The user's token account
34+
/// </summary>
35+
public bool UseSharedAccounts { get; set; }
36+
37+
/// <summary>
38+
/// The user's token account
39+
/// </summary>
40+
public PublicKey FeeAccount { get; set; }
41+
42+
/// <summary>
43+
/// The user's token account
44+
/// </summary>
45+
public BigInteger? ComputeUnitPriceMicroLamports { get; set; }
46+
47+
/// <summary>
48+
/// The user's token account
49+
/// </summary>
50+
public bool UseTokenLedger { get; set; }
51+
52+
/// <summary>
53+
/// Is this a legacy transaction
54+
/// </summary>
55+
public bool AsLegacyTransaction { get; set; }
56+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Newtonsoft.Json;
2+
using Org.BouncyCastle.Math;
3+
4+
namespace Solana.Unity.Dex.Jupiter.Core.Types.Http;
5+
6+
/// <summary>
7+
/// The response to get a swap quote from the Jupiter aggregator.
8+
/// </summary>
9+
public class SwapResponse
10+
{
11+
/// <summary>
12+
/// The swap quote
13+
/// </summary>
14+
public string SwapTransaction { get; set; }
15+
16+
[JsonProperty("lastValidBlockHeight")]
17+
private string _lastValidBlockHeight { get; set; }
18+
19+
/// <summary>
20+
/// The LastValidBlockHeight
21+
/// </summary>
22+
[JsonIgnore]
23+
public BigInteger LastValidBlockHeight => new(_lastValidBlockHeight);
24+
}

0 commit comments

Comments
 (0)