-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateVestingConfig.ts
214 lines (192 loc) · 5.97 KB
/
createVestingConfig.ts
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Usage: npx ts-node app/deploy/devnet/vesting/createVestingConfig.ts
import { Wallet, AnchorProvider } from "@coral-xyz/anchor";
import {
Connection,
PublicKey,
SystemProgram,
Transaction,
} from "@solana/web3.js";
import {
VESTING_ADMIN_KEYPAIR,
USER_AUTHORITY_KEYPAIR,
WORMHOLE_TOKEN,
RPC_NODE,
} from "../constants";
import { StakeConnection } from "../../../StakeConnection";
import BN from "bn.js";
import { randomBytes } from "crypto";
import * as wasm from "@wormhole/staking-wasm";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
createTransferCheckedInstruction,
getAssociatedTokenAddressSync,
} from "@solana/spl-token";
async function main() {
const admin = VESTING_ADMIN_KEYPAIR;
const vester = USER_AUTHORITY_KEYPAIR;
const connection = new Connection(RPC_NODE);
const provider = new AnchorProvider(connection, new Wallet(admin), {});
const stakeConnection = await StakeConnection.createStakeConnection(
connection,
provider.wallet as Wallet,
);
const confirm = async (signature: string): Promise<string> => {
const block =
await stakeConnection.provider.connection.getLatestBlockhash();
await stakeConnection.provider.connection.confirmTransaction({
signature,
...block,
});
return signature;
};
const NOW = new BN(Math.floor(new Date().getTime() / 1000));
const LATER = NOW.add(new BN(1000));
const EVEN_LATER = LATER.add(new BN(1000));
console.log("Vesting claim times:");
console.log(`NOW: ${NOW.toString()} (${new Date(NOW.toNumber() * 1000).toISOString()})`);
console.log(`LATER: ${LATER.toString()} (${new Date(LATER.toNumber() * 1000).toISOString()})`);
console.log(`EVEN_LATER: ${EVEN_LATER.toString()} (${new Date(EVEN_LATER.toNumber() * 1000).toISOString()})`);
const seed = new BN(randomBytes(8));
console.log("Vesting config random seed:", seed);
const config = PublicKey.findProgramAddressSync(
[
Buffer.from(wasm.Constants.VESTING_CONFIG_SEED()),
WORMHOLE_TOKEN.toBuffer(),
seed.toBuffer("le", 8),
],
stakeConnection.program.programId,
)[0];
console.log("Vesting config account:", config);
const vault = getAssociatedTokenAddressSync(
WORMHOLE_TOKEN,
config,
true,
TOKEN_PROGRAM_ID,
);
const vesterTa = getAssociatedTokenAddressSync(
WORMHOLE_TOKEN,
vester.publicKey,
false,
TOKEN_PROGRAM_ID,
);
const adminAta = getAssociatedTokenAddressSync(
WORMHOLE_TOKEN,
admin.publicKey,
false,
TOKEN_PROGRAM_ID,
);
const vestingBalance = PublicKey.findProgramAddressSync(
[
Buffer.from(wasm.Constants.VESTING_BALANCE_SEED()),
config.toBuffer(),
vester.publicKey.toBuffer(),
],
stakeConnection.program.programId,
)[0];
console.log("Vesting balance account for vester:", vestingBalance);
let accounts = {
admin: admin.publicKey,
mint: WORMHOLE_TOKEN,
config,
vault,
vester: vester.publicKey,
vesterTa,
adminAta,
recovery: adminAta,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
tokenProgram: TOKEN_PROGRAM_ID,
systemProgram: SystemProgram.programId,
vestingBalance,
};
console.log("Initializing vesting config...");
await stakeConnection.program.methods
.initializeVestingConfig(seed)
.accounts({ ...accounts })
.signers([admin])
.rpc()
.then(confirm);
console.log("Vesting config initialized");
console.log("Creating vesting balance for vester...");
await stakeConnection.program.methods
.createVestingBalance()
.accounts({ ...accounts })
.signers([admin])
.rpc()
.then(confirm);
console.log("Vesting balance for vester created");
console.log(`Creating vest for vester at NOW (${NOW.toString()})...`);
await stakeConnection.program.methods
.createVesting(NOW, new BN(20e6))
.accounts({ ...accounts })
.signers([admin])
.rpc()
.then(confirm);
console.log(`Vest for vester at NOW (${NOW.toString()}) created`);
console.log(`Creating vest for vester at LATER (${LATER.toString()})...`);
await stakeConnection.program.methods
.createVesting(LATER, new BN(20e6))
.accounts({ ...accounts })
.signers([admin])
.rpc()
.then(confirm);
console.log(`Vest for vester at LATER (${LATER.toString()}) created`);
console.log(`Creating vest for vester at EVEN_LATER (${EVEN_LATER.toString()})...`);
await stakeConnection.program.methods
.createVesting(EVEN_LATER, new BN(20e6))
.accounts({ ...accounts })
.signers([admin])
.rpc()
.then(confirm);
console.log(`Vest for vester at EVEN_LATER (${EVEN_LATER.toString()}) created`);
const vestLater = PublicKey.findProgramAddressSync(
[
Buffer.from(wasm.Constants.VEST_SEED()),
config.toBuffer(),
vesterTa.toBuffer(),
LATER.toBuffer("le", 8),
],
stakeConnection.program.programId,
)[0];
console.log(`Canceling vest for vester at LATER (${LATER.toString()})...`);
await stakeConnection.program.methods
.cancelVesting()
.accountsPartial({ ...accounts, vest: vestLater })
.signers([admin])
.rpc()
.then(confirm);
console.log(`Vest for vester at LATER (${LATER.toString()}) canceled`);
console.log("Transferring WH tokens to Vault...");
const tx = new Transaction();
tx.add(
createTransferCheckedInstruction(
adminAta,
WORMHOLE_TOKEN,
vault,
admin.publicKey,
100e6,
6,
undefined,
TOKEN_PROGRAM_ID,
),
);
await stakeConnection.provider.sendAndConfirm(tx, [admin]);
console.log("WH tokens transferred to Vault");
console.log("Withdrawing surplus...");
await stakeConnection.program.methods
.withdrawSurplus()
.accounts({ ...accounts })
.signers([admin])
.rpc()
.then(confirm);
console.log("Surplus withdrawn");
console.log("Finalizing vesting config...");
await stakeConnection.program.methods
.finalizeVestingConfig()
.accounts({ ...accounts })
.signers([admin])
.rpc()
.then(confirm);
console.log("Vesting config finalized");
}
main();