/
transaction.go
114 lines (94 loc) · 3.46 KB
/
transaction.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
package solana
import (
"fmt"
"strconv"
"github.com/easypmnt/checkout-api/internal/utils"
"github.com/pkg/errors"
"github.com/portto/solana-go-sdk/client"
"github.com/portto/solana-go-sdk/types"
)
// EncodeTransaction returns a base64 encoded transaction.
func EncodeTransaction(tx types.Transaction) (string, error) {
txb, err := tx.Serialize()
if err != nil {
return "", errors.Wrap(err, "failed to build transaction: serialize")
}
return utils.BytesToBase64(txb), nil
}
// DecodeTransaction returns a transaction from a base64 encoded transaction.
func DecodeTransaction(base64Tx string) (types.Transaction, error) {
txb, err := utils.Base64ToBytes(base64Tx)
if err != nil {
return types.Transaction{}, errors.Wrap(err, "failed to deserialize transaction: base64 to bytes")
}
tx, err := types.TransactionDeserialize(txb)
if err != nil {
return types.Transaction{}, errors.Wrap(err, "failed to deserialize transaction: deserialize")
}
return tx, nil
}
// SignTransaction signs a transaction and returns a base64 encoded transaction.
func SignTransaction(txSource string, signer types.Account) (string, error) {
tx, err := DecodeTransaction(txSource)
if err != nil {
return "", fmt.Errorf("failed to sign transaction: base64 to bytes: %w", err)
}
msg, err := tx.Message.Serialize()
if err != nil {
return "", fmt.Errorf("failed to sign transaction: serialize message: %w", err)
}
if err := tx.AddSignature(signer.Sign(msg)); err != nil {
return "", fmt.Errorf("failed to sign transaction: add signature: %w", err)
}
result, err := EncodeTransaction(tx)
if err != nil {
return "", fmt.Errorf("failed to sign transaction: encode transaction: %w", err)
}
return result, nil
}
// CheckSolTransferTransaction checks if a transaction is a SOL transfer transaction.
// Verifies that destination account has been credited with the correct amount.
func CheckSolTransferTransaction(meta *client.TransactionMeta, tx types.Transaction, destination string, amount uint64) error {
var destIdx int
for i, acc := range tx.Message.Accounts {
if acc.ToBase58() == destination {
destIdx = i
break
}
}
txAmount := meta.PostBalances[destIdx] - meta.PreBalances[destIdx]
if txAmount != int64(amount) {
return fmt.Errorf("amount is not equal to the amount in the transaction: %d != %d", amount, txAmount)
}
return nil
}
// CheckTokenTransferTransaction checks if a transaction is a token transfer transaction.
// Verifies that destination account has been credited with the correct amount of the token.
func CheckTokenTransferTransaction(meta *client.TransactionMeta, tx types.Transaction, mint, destination string, amount uint64) error {
var preBalance uint64
var postBalance uint64
for _, balance := range meta.PreTokenBalances {
if balance.Mint == mint && balance.Owner == destination {
amount, err := strconv.ParseUint(balance.UITokenAmount.Amount, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse pre balance: %w", err)
}
preBalance = amount
break
}
}
for _, balance := range meta.PostTokenBalances {
if balance.Mint == mint && balance.Owner == destination {
amount, err := strconv.ParseUint(balance.UITokenAmount.Amount, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse post balance: %w", err)
}
postBalance = amount
break
}
}
if postBalance-preBalance != amount {
return fmt.Errorf("amount is not equal to the amount in the transaction: %d != %d", amount, postBalance-preBalance)
}
return nil
}