-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathhandle-sell-coin.go
58 lines (47 loc) · 1.62 KB
/
handle-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
package main
import (
"fmt"
"time"
)
// HandleSellCoins iterates through our list of coins we've purchased,
// or intend to purchase, checks if they are stale (already sold / buy tx failed),
// or if they need to be sold, and handles both of those cases
func (b *Bot) HandleSellCoins() {
for {
coinsToSell := b.fetchCoinsToSell()
for _, coin := range coinsToSell {
go b.SellCoinFast(coin)
}
// check for coins we should sell each 100 ms
time.Sleep(100 * time.Millisecond)
}
}
// fetchCoinsToSell returns coins we should sell,
// but also deletes coins we no longer need to track
func (b *Bot) fetchCoinsToSell() []*Coin {
var coinsToSell []*Coin
b.pendingCoinsLock.Lock()
defer b.pendingCoinsLock.Unlock()
for mintAddr, coin := range b.pendingCoins {
if coin == nil {
continue
}
// if we exited BuyCoin & do not hold tokens, remove this coin
if coin.exitedBuyCoin && !coin.botHoldsTokens() {
fmt.Println("Deleting", coin.mintAddr.String(), "because exited buy but no hold")
delete(b.pendingCoins, mintAddr)
}
// sold coins and stopped listening to creator, delete coin
if coin.exitedSellCoin && coin.exitedCreatorListener {
fmt.Println("Deleting", coin.mintAddr.String(), "because exited creator listener and sellCoins routine")
delete(b.pendingCoins, mintAddr)
}
// we hold tokens & creator sold, must exit
// make sure we are not already selling this coin
if coin.botHoldsTokens() && coin.creatorSold && !coin.isSellingCoin {
b.status(fmt.Sprintf("Selling %s: (decision=creator sold)", coin.mintAddr.String()))
coinsToSell = append(coinsToSell, coin)
}
}
return coinsToSell
}