-
Notifications
You must be signed in to change notification settings - Fork 1
/
account_meta.go
110 lines (91 loc) · 2.26 KB
/
account_meta.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
package base
import "github.com/cielu/go-solana/common"
type AccountsSettable interface {
SetAccounts(accounts []*AccountMeta) error
}
type AccountsGettable interface {
GetAccounts() (accounts []*AccountMeta)
}
type AccountMeta struct {
PublicKey common.Address `json:"publickey"`
IsWritable bool
IsSigner bool
}
// Meta intializes a new AccountMeta with the provided pubKey.
func Meta(pubKey common.Address) *AccountMeta {
return &AccountMeta{
PublicKey: pubKey,
}
}
// WRITE sets IsWritable to true.
func (meta *AccountMeta) WRITE() *AccountMeta {
meta.IsWritable = true
return meta
}
// SIGNER sets IsSigner to true.
func (meta *AccountMeta) SIGNER() *AccountMeta {
meta.IsSigner = true
return meta
}
func NewAccountMeta(pubKey common.Address, WRITE bool, SIGNER bool) *AccountMeta {
return &AccountMeta{
PublicKey: pubKey,
IsWritable: WRITE,
IsSigner: SIGNER,
}
}
func (meta *AccountMeta) Less(act *AccountMeta) bool {
if meta.IsSigner != act.IsSigner {
return meta.IsSigner
}
if meta.IsWritable != act.IsWritable {
return meta.IsWritable
}
return false
}
type AccountMetaSlice []*AccountMeta
func (slice *AccountMetaSlice) Append(account *AccountMeta) {
*slice = append(*slice, account)
}
func (slice *AccountMetaSlice) SetAccounts(accounts []*AccountMeta) error {
*slice = accounts
return nil
}
func (slice AccountMetaSlice) GetAccounts() (accounts []*AccountMeta) {
// range slice
for i := range slice {
if slice[i] != nil {
accounts = append(accounts, slice[i])
}
}
return
}
// Get returns the AccountMeta at the desired index.
// If the index is not present, it returns nil.
func (slice AccountMetaSlice) Get(index int) *AccountMeta {
if len(slice) > index {
return slice[index]
}
return nil
}
// GetSigners returns the accounts that are signers.
func (slice AccountMetaSlice) GetSigners() []*AccountMeta {
signers := make([]*AccountMeta, 0, len(slice))
for _, ac := range slice {
if ac.IsSigner {
signers = append(signers, ac)
}
}
return signers
}
// GetKeys returns the pubkeys of all AccountMeta.
func (slice AccountMetaSlice) GetKeys() (keys []common.Address) {
// range slice
for _, ac := range slice {
keys = append(keys, ac.PublicKey)
}
return
}
func (slice AccountMetaSlice) Len() int {
return len(slice)
}