-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup_table.go
126 lines (104 loc) · 3.28 KB
/
lookup_table.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
package lookup_table
import (
"bytes"
"encoding/binary"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/programs/system"
)
var PROGRAM_ID = solana.MustPublicKeyFromBase58("AddressLookupTab1e1111111111111111111111111")
func Create(recentSlot uint64, authority, payer solana.PublicKey) (*solana.GenericInstruction, solana.PublicKey, error) {
recentSlotBytes := toBufferLittleEndian(recentSlot)
address, seed, err := solana.FindProgramAddress(
[][]byte{
authority.Bytes(),
recentSlotBytes,
},
PROGRAM_ID,
)
if err != nil {
return nil, solana.PublicKey{}, err
}
data := make([]byte, 13)
binary.LittleEndian.PutUint32(data[0:4], 0)
binary.LittleEndian.PutUint64(data[4:12], recentSlot)
data[12] = seed
keys := []*solana.AccountMeta{
{PublicKey: address, IsSigner: false, IsWritable: true},
{PublicKey: authority, IsSigner: true, IsWritable: false},
{PublicKey: payer, IsSigner: true, IsWritable: true},
{PublicKey: system.ProgramID, IsSigner: false, IsWritable: false},
}
return solana.NewInstruction(
PROGRAM_ID,
keys,
data,
), address, nil
}
func Extend(tableAddress, authority solana.PublicKey, payer *solana.PublicKey, addresses []solana.PublicKey) (*solana.GenericInstruction, error) {
data := make([]byte, 12+(32*len(addresses)))
binary.LittleEndian.PutUint32(data[0:4], 2)
binary.LittleEndian.PutUint64(data[4:12], uint64(len(addresses)))
for i, addr := range addresses {
copy(data[12+(i*32):12+((i+1)*32)], addr.Bytes())
}
keys := []*solana.AccountMeta{
{PublicKey: tableAddress, IsSigner: false, IsWritable: true},
{PublicKey: authority, IsSigner: true, IsWritable: false},
}
if payer != nil {
keys = append(keys, []*solana.AccountMeta{
{PublicKey: *payer, IsSigner: true, IsWritable: true},
{PublicKey: system.ProgramID, IsSigner: false, IsWritable: false},
}...)
}
return solana.NewInstruction(
PROGRAM_ID,
keys,
data,
), nil
}
func Close(tableAddress, authority, recipient solana.PublicKey) (*solana.GenericInstruction, error) {
data := make([]byte, 4)
binary.LittleEndian.PutUint32(data[0:4], 4)
keys := []*solana.AccountMeta{
{PublicKey: tableAddress, IsSigner: false, IsWritable: true},
{PublicKey: authority, IsSigner: true, IsWritable: false},
{PublicKey: recipient, IsSigner: false, IsWritable: true},
}
return solana.NewInstruction(
PROGRAM_ID,
keys,
data,
), nil
}
func Freeze(tableAddress, authority solana.PublicKey) (*solana.GenericInstruction, error) {
data := make([]byte, 4)
binary.LittleEndian.PutUint32(data[0:4], 1)
keys := []*solana.AccountMeta{
{PublicKey: tableAddress, IsSigner: false, IsWritable: true},
{PublicKey: authority, IsSigner: true, IsWritable: false},
}
return solana.NewInstruction(
PROGRAM_ID,
keys,
data,
), nil
}
func Deactivate(tableAddress, authority solana.PublicKey) (*solana.GenericInstruction, error) {
data := make([]byte, 4)
binary.LittleEndian.PutUint32(data[0:4], 3)
keys := []*solana.AccountMeta{
{PublicKey: tableAddress, IsSigner: false, IsWritable: true},
{PublicKey: authority, IsSigner: true, IsWritable: false},
}
return solana.NewInstruction(
PROGRAM_ID,
keys,
data,
), nil
}
func toBufferLittleEndian(value uint64) []byte {
buf := new(bytes.Buffer)
_ = binary.Write(buf, binary.LittleEndian, value)
return buf.Bytes()
}