-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JUP-setup jupiter program to swap solana tokens and added functionali…
…ty to deserialize transactions
- Loading branch information
Chintan Patel
authored and
Chintan Patel
committed
Aug 13, 2024
1 parent
a5173cb
commit f196779
Showing
12 changed files
with
480 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/main/java/org/p2p/solanaj/programs/JupiterSwapProgram.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package org.p2p.solanaj.programs; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.Getter; | ||
import org.apache.hc.client5.http.classic.methods.HttpGet; | ||
import org.apache.hc.client5.http.classic.methods.HttpPost; | ||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
import org.apache.hc.client5.http.impl.classic.HttpClients; | ||
import org.apache.hc.core5.http.HttpEntity; | ||
import org.apache.hc.core5.http.io.HttpClientResponseHandler; | ||
import org.apache.hc.core5.http.io.entity.EntityUtils; | ||
import org.apache.hc.core5.http.io.entity.StringEntity; | ||
import org.apache.hc.core5.net.URIBuilder; | ||
import org.p2p.solanaj.core.Account; | ||
import org.p2p.solanaj.core.Transaction; | ||
import org.p2p.solanaj.rpc.RpcClient; | ||
import org.p2p.solanaj.rpc.RpcException; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Base64; | ||
|
||
public final class JupiterSwapProgram { | ||
|
||
public static final String SOL_QUOTE_TOKEN = "So11111111111111111111111111111111111111112"; | ||
|
||
private static final CloseableHttpClient httpClient = HttpClients.createDefault(); | ||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
private static final HttpClientResponseHandler<JsonNode> handler = response -> { | ||
int status = response.getCode(); | ||
if (status >= 200 && status < 300) { | ||
HttpEntity entity = response.getEntity(); | ||
return entity != null ? objectMapper.readTree(EntityUtils.toString(entity)) : null; | ||
} else { | ||
throw new IOException("Unexpected response status: " + status); | ||
} | ||
}; | ||
|
||
public static URI createQuoteUri(String inputToken, String outputToken, String amount, String slippage) { | ||
try { | ||
return new URIBuilder("https://quote-api.jup.ag/v6/quote") | ||
.addParameter("inputMint", inputToken) | ||
.addParameter("outputMint", outputToken) | ||
.addParameter("amount", amount) | ||
.addParameter("slippageBps", slippage) | ||
.build(); | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException("Failed to create quote URI: ", e); | ||
} | ||
} | ||
|
||
public static JsonNode getJupiterQuote(URI quoteUri) { | ||
HttpGet quoteRequest = new HttpGet(quoteUri); | ||
|
||
JsonNode quote; | ||
try { | ||
quote = httpClient.execute(quoteRequest, handler); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to retrieve quote from jupiter for " + quoteUri, e); | ||
} | ||
return quote; | ||
} | ||
|
||
public static String swapToken(RpcClient rpcClient, Account account, JsonNode quote) { | ||
SwapRequest swapRequestBody = new SwapRequest(quote, account.getPublicKey().toString()); | ||
String jsonSwapRequestBody = null; | ||
try { | ||
jsonSwapRequestBody = objectMapper.writeValueAsString(swapRequestBody); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
HttpPost swapRequest = new HttpPost("https://quote-api.jup.ag/v6/swap"); | ||
swapRequest.setHeader("Content-Type", "application/json"); | ||
swapRequest.setEntity(new StringEntity(jsonSwapRequestBody)); | ||
|
||
JsonNode swapRes = null; | ||
try { | ||
swapRes = httpClient.execute(swapRequest, handler); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
String swapTransaction = swapRes.get("swapTransaction").asText(); | ||
|
||
byte[] base64Decoded = Base64.getDecoder().decode(swapTransaction); | ||
Transaction transaction = Transaction.deserialize(base64Decoded); | ||
|
||
try { | ||
return rpcClient.getApi().sendTransaction(transaction, account); | ||
} catch (RpcException e) { | ||
throw new RuntimeException("Failed to send swap transaction", e); | ||
} | ||
} | ||
|
||
public static String swapToken(RpcClient rpcClient, Account account, String inputToken, String outputToken, String amount, String slippage) { | ||
URI quoteUri = createQuoteUri(inputToken, outputToken, amount, slippage); | ||
return swapToken(rpcClient, account, quoteUri); | ||
} | ||
|
||
public static String swapToken(RpcClient rpcClient, Account account, URI quoteUri) { | ||
JsonNode quote = getJupiterQuote(quoteUri); | ||
return swapToken(rpcClient, account, quote); | ||
} | ||
|
||
@Getter | ||
public record SwapRequest(Object quoteResponse, String userPublicKey) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/org/p2p/solanaj/utils/GuardedArrayUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.p2p.solanaj.utils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class GuardedArrayUtils { | ||
|
||
public static Byte guardedShift(List<Byte> byteArray) { | ||
if (byteArray.isEmpty()) { | ||
throw new IllegalArgumentException("Byte array length is 0"); | ||
} | ||
return byteArray.remove(0); | ||
} | ||
|
||
public static byte[] guardedSplice( | ||
List<Byte> byteList, | ||
Integer start, | ||
Integer deleteCount, | ||
byte... items) { | ||
List<Byte> removedItems; | ||
if (deleteCount != null) { | ||
if (start + deleteCount > byteList.size()) { | ||
throw new Error("Reached end of bytes"); | ||
} | ||
removedItems = new ArrayList<>(byteList.subList(start, start + deleteCount)); | ||
byteList.subList(start, start + deleteCount).clear(); | ||
} else { | ||
if (start > byteList.size()) { | ||
throw new Error("Reached end of bytes"); | ||
} | ||
removedItems = Collections.emptyList(); | ||
} | ||
List<Byte> itemsToAdd = ByteUtils.toByteList(items); | ||
byteList.addAll(start, itemsToAdd); | ||
|
||
if (!removedItems.isEmpty()) { | ||
return ByteUtils.toByteArray(removedItems); | ||
} | ||
return ByteUtils.emptyByteArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.