forked from weeaa/jito-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
89 lines (75 loc) · 2.12 KB
/
util.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
package pkg
import (
"context"
"fmt"
"math/big"
"time"
"github.com/MintyFinance/solana-go-custom"
"github.com/gorilla/websocket"
)
// ExtractSigFromTx extracts the transaction's signature.
func ExtractSigFromTx(tx *solana.Transaction) solana.Signature {
return tx.Signatures[0]
}
func BatchExtractSigFromTx(txns []*solana.Transaction) []solana.Signature {
sigs := make([]solana.Signature, 0, len(txns))
for _, tx := range txns {
sigs = append(sigs, tx.Signatures[0])
}
return sigs
}
func BuildTransactionLinks(txns []solana.Signature, platform Platform) []string {
txs := make([]string, 0, len(txns))
for _, tx := range txns {
txs = append(txs, fmt.Sprintf("%s%s", platform, tx.String()))
}
return txs
}
// NewKeyPair creates a Keypair from a private key.
func NewKeyPair(privateKey solana.PrivateKey) *Keypair {
return &Keypair{PrivateKey: privateKey, PublicKey: privateKey.PublicKey()}
}
// SubscribeTipStream establishes a connection to the Jito websocket and receives TipStreamInfo.
func SubscribeTipStream(ctx context.Context) (<-chan *TipStreamInfo, <-chan error, error) {
dialer := websocket.Dialer{}
ch := make(chan *TipStreamInfo)
chErr := make(chan error)
conn, _, err := dialer.Dial(tipStreamURL, nil)
if err != nil {
return nil, nil, err
}
go func() {
defer conn.Close()
for {
select {
case <-ctx.Done():
return
default:
var r *TipStreamInfo
if err = conn.ReadJSON(r); err != nil {
chErr <- err
time.Sleep(500 * time.Millisecond)
continue
}
ch <- r
}
}
}()
return ch, chErr, nil
}
// GenerateKeypair creates a new Solana Keypair.
func GenerateKeypair() *Keypair {
wallet := solana.NewWallet()
return &Keypair{PublicKey: wallet.PublicKey(), PrivateKey: wallet.PrivateKey}
}
func LamportsToSol(lamports *big.Float) *big.Float {
return new(big.Float).Quo(lamports, new(big.Float).SetUint64(solana.LAMPORTS_PER_SOL))
}
// StrSliceToByteSlice converts a string array to a byte array.
func StrSliceToByteSlice(s []string) [][]byte {
byteSlice := make([][]byte, 0, len(s))
for _, b := range s {
byteSlice = append(byteSlice, []byte(b))
}
return byteSlice
}