forked from 1fge/pump-fun-sniper-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsell-coin.go
134 lines (112 loc) · 3.73 KB
/
sell-coin.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/1fge/pump-fun-sniper-bot/pump"
"github.com/gagliardetto/solana-go"
associatedtokenaccount "github.com/gagliardetto/solana-go/programs/associated-token-account"
cb "github.com/gagliardetto/solana-go/programs/compute-budget"
"github.com/gagliardetto/solana-go/programs/token"
)
// SellCoinFast utilizes the fact that, unlike buying, we do not care if duplicate tx hit the chain
// if they do, we lose the priority fee, but ensure we are out of the position quickly. For this reason,
// we spam sell transactions every 400ms for a duration of 6 seconds, resulting in 15 sell tx
func (b *Bot) SellCoinFast(coin *Coin) {
fmt.Println("Preparing to sell coin", coin.mintAddr.String())
// send off sell requests separated by 400ms, wait for one to return
// valid transaction, otherwise repeat (for 45 seconds at most)
coin.isSellingCoin = true
defer coin.setExitedSellCoinTrue()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*6)
defer cancel()
ticker := time.NewTicker(400 * time.Millisecond)
defer ticker.Stop()
result := make(chan int, 1) // Buffered to ensure non-blocking send
var sendVanilla = true
// goroutine to send off sell tx every 400 until confirmed
go func() {
for {
select {
case <-ticker.C:
// alternate between jito and vanilla each iteration, in case of no jito leader
sendVanilla = !sendVanilla
go b.sellCoinWrapper(coin, result, sendVanilla)
case <-ctx.Done():
return // Stop the ticker loop when context is cancelled
}
}
}()
// wait for first result to come back
<-result
time.Sleep(1 * time.Second)
}
func (b *Bot) sellCoinWrapper(coin *Coin, result chan int, sendVanilla bool) {
sellSignature, err := b.sellCoin(coin, sendVanilla)
if err != nil {
if err != context.Canceled {
if sellSignature != nil {
b.statusr(fmt.Sprintf("Sell transaction %s failed: %s", sellSignature.String(), err))
} else {
b.statusr(fmt.Sprintf("Sell transaction failed: %s", err))
}
}
return
}
if sellSignature == nil {
fmt.Println("Sell signature is nil")
return
}
result <- 1
}
func (b *Bot) sellCoin(coin *Coin, sendVanilla bool) (*solana.Signature, error) {
if coin == nil {
return nil, errNilCoin
}
sellInstruction := b.createSellInstruction(coin)
culInst := cb.NewSetComputeUnitLimitInstruction(uint32(computeUnitLimits))
cupInst := cb.NewSetComputeUnitPriceInstruction(b.feeMicroLamport)
instructions := []solana.Instruction{cupInst.Build(), culInst.Build(), sellInstruction.Build()}
// enable jito if it's jito leader and we do not force vanilla tx
enableJito := b.jitoManager.isJitoLeader() && !sendVanilla
if enableJito {
coin.status("Jito leader, setting tip & removing priority fee inst")
tipInst, err := b.jitoManager.generateTipInstruction()
if err != nil {
log.Fatal(err)
}
instructions = append(instructions, tipInst)
// IMPORTANT: remove priority fee when we jito tip
instructions = instructions[1:]
}
tx, err := b.createTransaction(instructions...)
if err != nil {
return nil, err
}
return b.signAndSendTx(tx, enableJito)
}
func (b *Bot) createSellInstruction(coin *Coin) *pump.Sell {
// we want a minimum of 1 lamport, which ensures we should get filled at any price
// as long as any of the 15 tx land
minimumLamports := uint64(1)
return pump.NewSellInstruction(
coin.tokensHeld.Uint64(),
minimumLamports,
globalAddr,
feeRecipient,
coin.mintAddr,
coin.tokenBondingCurve,
coin.associatedBondingCurve,
coin.associatedTokenAccount,
b.privateKey.PublicKey(),
solana.SystemProgramID,
associatedtokenaccount.ProgramID,
token.ProgramID,
coin.eventAuthority,
pumpProgramID,
)
}
func (c *Coin) setExitedSellCoinTrue() {
c.exitedSellCoin = true
}