forked from ScribeSavant/raydium-swap-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
70 lines (53 loc) · 1.63 KB
/
run.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
package main
import (
"context"
"fmt"
"os"
"github.com/davecgh/go-spew/spew"
"github.com/gagliardetto/solana-go/rpc"
"github.com/itherunder/raydium-swap-go/raydium"
"github.com/itherunder/raydium-swap-go/raydium/trade"
"github.com/itherunder/raydium-swap-go/raydium/utils"
)
func main() {
executeTransaction := false
connection := rpc.New(os.Getenv("RPC_URL"))
raydium := raydium.New(connection, os.Getenv("WALLET_PRIVATE_KEY"))
inputToken := utils.NewToken("SOL", "So11111111111111111111111111111111111111112", 9)
outputToken := utils.NewToken("RAY", "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R", 6)
slippage := utils.NewPercent(1, 100) // 1% slippage (for 0.5 set second parameter to "1000" example: utils.NewPercent(5, 1000) )
amount := utils.NewTokenAmount(inputToken, 0.1) // 0.1 sol
poolKeys, err := raydium.Pool.GetPoolKeys(inputToken.Mint, outputToken.Mint)
if err != nil {
panic(err)
}
amountsOut, err := raydium.Liquidity.GetAmountsOut(poolKeys, amount, slippage)
if err != nil {
panic(err)
}
tx, err := raydium.Trade.MakeSwapTransaction(
poolKeys,
amountsOut.AmountIn,
amountsOut.MinAmountOut,
trade.FeeConfig{
MicroLamports: 25000, // fee 0.000025 sol
},
)
if err != nil {
panic(err)
}
if !executeTransaction {
simRes, err := connection.SimulateTransaction(context.Background(), tx)
if err != nil {
spew.Dump(err.Error())
return
}
spew.Dump(simRes)
} else {
signature, err := connection.SendTransactionWithOpts(context.Background(), tx, rpc.TransactionOpts{SkipPreflight: true})
if err != nil {
panic(err)
}
fmt.Println("Transaction successfully sent: ", signature)
}
}