-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.go
97 lines (80 loc) · 2.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"fmt"
"time"
"github.com/ilkamo/jupiter-go/jupiter"
"github.com/ilkamo/jupiter-go/solana"
)
func main() {
jupClient, err := jupiter.NewClientWithResponses(jupiter.DefaultAPIURL)
if err != nil {
panic(err)
}
ctx := context.TODO()
slippageBps := 250
// Get the current quote for a swap.
// Ensure that the input and output mints are valid.
// The amount is the smallest unit of the input token.
quoteResponse, err := jupClient.GetQuoteWithResponse(ctx, &jupiter.GetQuoteParams{
InputMint: "So11111111111111111111111111111111111111112",
OutputMint: "WENWENvqqNya429ubCdR81ZmD69brwQaaBYY6p3LCpk",
Amount: 100000,
SlippageBps: &slippageBps,
})
if err != nil {
panic(err)
}
if quoteResponse.JSON200 == nil {
panic("invalid GetQuoteWithResponse response")
}
quote := quoteResponse.JSON200
// More info: https://station.jup.ag/docs/apis/troubleshooting
prioritizationFeeLamports := jupiter.SwapRequest_PrioritizationFeeLamports{}
if err = prioritizationFeeLamports.UnmarshalJSON([]byte(`"auto"`)); err != nil {
panic(err)
}
dynamicComputeUnitLimit := true
// Get instructions for a swap.
// Ensure your public key is valid.
swapResponse, err := jupClient.PostSwapWithResponse(ctx, jupiter.PostSwapJSONRequestBody{
PrioritizationFeeLamports: &prioritizationFeeLamports,
QuoteResponse: *quote,
UserPublicKey: "{YOUR_PUBLIC_KEY}",
DynamicComputeUnitLimit: &dynamicComputeUnitLimit,
})
if err != nil {
panic(err)
}
if swapResponse.JSON200 == nil {
panic("invalid PostSwapWithResponse response")
}
swap := swapResponse.JSON200
fmt.Printf("%+v", swap)
// Create a wallet from private key.
walletPrivateKey := "{YOUR_PRIVATE_KEY}"
wallet, err := solana.NewWalletFromPrivateKeyBase58(walletPrivateKey)
if err != nil {
panic(err)
}
// Create a Solana client. Change the URL to the desired Solana node.
solanaClient, err := solana.NewClient(wallet, "https://api.mainnet-beta.solana.com")
if err != nil {
panic(err)
}
// Sign and send the transaction.
signedTx, err := solanaClient.SendTransactionOnChain(ctx, swap.SwapTransaction)
if err != nil {
panic(err)
}
// Wait a bit to let the transaction propagate to the network.
// This is just an example and not a best practice.
// You could use a ticker or wait until we implement the WebSocket monitoring ;)
time.Sleep(20 * time.Second)
// Get the status of the transaction (pull the status from the blockchain at intervals
// until the transaction is confirmed)
_, err = solanaClient.CheckSignature(ctx, signedTx)
if err != nil {
panic(err)
}
}