forked from solana-labs/solana-ping-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transfer.go
153 lines (137 loc) · 4.79 KB
/
Transfer.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/blocto/solana-go-sdk/client"
"github.com/blocto/solana-go-sdk/common"
"github.com/blocto/solana-go-sdk/program/cmptbdgprog"
"github.com/blocto/solana-go-sdk/program/memoprog"
"github.com/blocto/solana-go-sdk/program/sysprog"
"github.com/blocto/solana-go-sdk/rpc"
"github.com/blocto/solana-go-sdk/types"
)
var (
txTimeoutDefault = 10 * time.Second
waitConfirmationTimeoutDefault = 50 * time.Second
statusCheckTimeDefault = 1 * time.Second
)
func Transfer(c *client.Client, sender types.Account, feePayer types.Account, receiverPubkey string, txTimeout time.Duration) (txHash string, pingErr PingResultError) {
// to fetch recent blockhash
res, err := c.GetLatestBlockhash(context.Background())
if err != nil {
log.Println("Failed to get latest blockhash, err: ", err)
return "", PingResultError(fmt.Sprintf("Failed to get latest blockhash, err: %v", err))
}
// create a message
message := types.NewMessage(types.NewMessageParam{
FeePayer: feePayer.PublicKey,
RecentBlockhash: res.Blockhash, // recent blockhash
Instructions: []types.Instruction{
sysprog.Transfer(sysprog.TransferParam{
From: sender.PublicKey, // from
To: common.PublicKeyFromString(receiverPubkey), // to
Amount: 1, // SOL
}),
},
})
// create tx by message + signer
tx, err := types.NewTransaction(types.NewTransactionParam{
Message: message,
Signers: []types.Account{feePayer, sender},
})
if err != nil {
log.Printf("Error: Failed to create a new transaction, err: %v", err)
return "", PingResultError(fmt.Sprintf("Failed to new a tx, err: %v", err))
}
// send tx
if txTimeout <= 0 {
txTimeout = time.Duration(txTimeoutDefault)
}
ctx, _ := context.WithTimeout(context.TODO(), txTimeout)
txHash, err = c.SendTransaction(ctx, tx)
if err != nil {
log.Printf("Error: Failed to send tx, err: %v", err)
return "", PingResultError(fmt.Sprintf("Failed to send a tx, err: %v", err))
}
return txHash, EmptyPingResultError
}
type SendPingTxParam struct {
Client *client.Client
Ctx context.Context
FeePayer types.Account
RequestComputeUnits uint32
ComputeUnitPrice uint64 // micro lamports
}
func SendPingTx(param SendPingTxParam) (string, PingResultError) {
latestBlockhashResponse, err := param.Client.GetLatestBlockhash(param.Ctx)
if err != nil {
return "", PingResultError(fmt.Sprintf("failed to get latest blockhash, err: %v", err))
}
tx, err := types.NewTransaction(types.NewTransactionParam{
Signers: []types.Account{param.FeePayer},
Message: types.NewMessage(types.NewMessageParam{
FeePayer: param.FeePayer.PublicKey,
RecentBlockhash: latestBlockhashResponse.Blockhash,
Instructions: []types.Instruction{
cmptbdgprog.SetComputeUnitLimit(cmptbdgprog.SetComputeUnitLimitParam{
Units: param.RequestComputeUnits,
}),
cmptbdgprog.SetComputeUnitPrice(cmptbdgprog.SetComputeUnitPriceParam{
MicroLamports: param.ComputeUnitPrice,
}),
memoprog.BuildMemo(memoprog.BuildMemoParam{
Memo: []byte("ping"),
}),
},
}),
})
if err != nil {
return "", PingResultError(fmt.Sprintf("failed to new a tx, err: %v", err))
}
txhash, err := param.Client.SendTransaction(param.Ctx, tx)
if err != nil {
return "", PingResultError(fmt.Sprintf("failed to send a tx, err: %v", err))
}
return txhash, PingResultError("")
}
/*
timeout: timeout for checking a block with the assigned htxHash status
requestTimeout: timeout for GetSignatureStatus
checkInterval: interval to check for status
*/
func waitConfirmation(c *client.Client, txHash string, timeout time.Duration, requestTimeout time.Duration, checkInterval time.Duration) PingResultError {
if timeout <= 0 {
timeout = waitConfirmationTimeoutDefault
log.Println("timeout is not set! Use default timeout", timeout, " sec")
}
ctx, _ := context.WithTimeout(context.TODO(), requestTimeout)
elapse := time.Now()
for {
resp, err := c.GetSignatureStatus(ctx, txHash)
now := time.Now()
if err != nil {
if now.Sub(elapse).Seconds() < timeout.Seconds() {
continue
} else {
return PingResultError(fmt.Sprintf("failed to get signatureStatus, err: %v", err))
}
}
if resp != nil {
if *resp.ConfirmationStatus == rpc.CommitmentConfirmed || *resp.ConfirmationStatus == rpc.CommitmentFinalized {
return EmptyPingResultError
}
}
if now.Sub(elapse).Seconds() > timeout.Seconds() {
if resp != nil && *resp.ConfirmationStatus == rpc.CommitmentProcessed {
return PingResultError(ErrInProcessedStateTimeout.Error())
}
return PingResultError(ErrWaitForConfirmedTimeout.Error())
}
if checkInterval <= 0 {
checkInterval = statusCheckTimeDefault
}
time.Sleep(checkInterval)
}
}