-
Notifications
You must be signed in to change notification settings - Fork 4
/
instruction_transferwithauthority.go
141 lines (120 loc) · 3.31 KB
/
instruction_transferwithauthority.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
package timelock_token
import (
"bytes"
"crypto/ed25519"
)
var transferWithAuthorityInstructionDiscriminator = []byte{
68, 128, 222, 192, 129, 69, 71, 165,
}
const (
TransferWithAuthorityInstructionArgsSize = (1 + // TimelockBump
8) // amount
TransferWithAuthorityInstructionAccountsSize = (32 + // timelock
32 + // vault
32 + // vaultOwner
32 + // timeAuthority
32 + // destination
32 + // payer
32 + // splTokenProgram
32) // systemProgram
TransferWithAuthorityInstructionSize = (8 + // discriminator
TransferWithAuthorityInstructionArgsSize + // args
TransferWithAuthorityInstructionAccountsSize) // accounts
)
type TransferWithAuthorityInstructionArgs struct {
TimelockBump uint8
Amount uint64
}
type TransferWithAuthorityInstructionAccounts struct {
Timelock ed25519.PublicKey
Vault ed25519.PublicKey
VaultOwner ed25519.PublicKey
TimeAuthority ed25519.PublicKey
Destination ed25519.PublicKey
Payer ed25519.PublicKey
}
func NewTransferWithAuthorityInstruction(
accounts *TransferWithAuthorityInstructionAccounts,
args *TransferWithAuthorityInstructionArgs,
) Instruction {
var offset int
// Serialize instruction arguments
data := make([]byte,
len(transferWithAuthorityInstructionDiscriminator)+
TransferWithAuthorityInstructionArgsSize)
putDiscriminator(data, transferWithAuthorityInstructionDiscriminator, &offset)
putUint8(data, args.TimelockBump, &offset)
putUint64(data, args.Amount, &offset)
return Instruction{
Program: PROGRAM_ADDRESS,
// Instruction args
Data: data,
// Instruction accounts
Accounts: []AccountMeta{
{
PublicKey: accounts.Timelock,
IsWritable: false,
IsSigner: false,
},
{
PublicKey: accounts.Vault,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: accounts.VaultOwner,
IsWritable: false,
IsSigner: true,
},
{
PublicKey: accounts.TimeAuthority,
IsWritable: false,
IsSigner: true,
},
{
PublicKey: accounts.Destination,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: accounts.Payer,
IsWritable: true,
IsSigner: true,
},
{
PublicKey: SPL_TOKEN_PROGRAM_ID,
IsWritable: false,
IsSigner: false,
},
{
PublicKey: SYSTEM_PROGRAM_ID,
IsWritable: false,
IsSigner: false,
},
},
}
}
func TransferWithAuthorityInstructionFromBinary(data []byte) (*TransferWithAuthorityInstructionArgs, *TransferWithAuthorityInstructionAccounts, error) {
var offset int
var discriminator []byte
if len(data) < TransferWithAuthorityInstructionSize {
return nil, nil, ErrInvalidInstructionData
}
getDiscriminator(data, &discriminator, &offset)
if !bytes.Equal(discriminator, transferWithAuthorityInstructionDiscriminator) {
return nil, nil, ErrInvalidInstructionData
}
var args TransferWithAuthorityInstructionArgs
var accounts TransferWithAuthorityInstructionAccounts
// Instruction Args
getUint8(data, &args.TimelockBump, &offset)
getUint64(data, &args.Amount, &offset)
// Instruction Accounts
getKey(data, &accounts.Timelock, &offset)
getKey(data, &accounts.Vault, &offset)
getKey(data, &accounts.VaultOwner, &offset)
getKey(data, &accounts.TimeAuthority, &offset)
getKey(data, &accounts.Destination, &offset)
getKey(data, &accounts.Payer, &offset)
return &args, &accounts, nil
}