-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathunstakeFromProposal.test.ts
More file actions
290 lines (252 loc) · 7.62 KB
/
unstakeFromProposal.test.ts
File metadata and controls
290 lines (252 loc) · 7.62 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import {
InstructionUtils,
PERMISSIONLESS_ACCOUNT,
} from "@metadaoproject/futarchy/v0.6";
import {
ComputeBudgetProgram,
PublicKey,
Transaction,
TransactionMessage,
} from "@solana/web3.js";
import BN from "bn.js";
import { expectError, setupBasicDao } from "../../utils.js";
import { assert } from "chai";
import * as multisig from "@sqds/multisig";
export default function suite() {
let META: PublicKey,
USDC: PublicKey,
dao: PublicKey,
proposal: PublicKey,
squadsProposalPda: PublicKey;
beforeEach(async function () {
META = await this.createMint(this.payer.publicKey, 6);
USDC = await this.createMint(this.payer.publicKey, 6);
// Ensure the clock is well above zero so `timestamp_enqueued == 0`
// (the draft-state sentinel) is unambiguously in the past, regardless
// of what earlier test files did to the clock.
await this.advanceBySeconds(3600);
await this.createTokenAccount(META, this.payer.publicKey);
await this.createTokenAccount(USDC, this.payer.publicKey);
await this.mintTo(META, this.payer.publicKey, this.payer, 1_000 * 10 ** 6);
await this.mintTo(
USDC,
this.payer.publicKey,
this.payer,
200_000 * 10 ** 6,
);
dao = await setupBasicDao({
context: this,
baseMint: META,
quoteMint: USDC,
});
await this.futarchy
.provideLiquidityIx({
dao,
baseMint: META,
quoteMint: USDC,
quoteAmount: new BN(100_000 * 10 ** 6),
maxBaseAmount: new BN(100 * 10 ** 6),
minLiquidity: new BN(0),
positionAuthority: this.payer.publicKey,
liquidityProvider: this.payer.publicKey,
})
.preInstructions([
ComputeBudgetProgram.setComputeUnitLimit({ units: 300_000 }),
])
.rpc();
const updateDaoIx = await this.futarchy
.updateDaoIx({
dao,
params: {
passThresholdBps: 500,
secondsPerProposal: null,
baseToStake: null,
twapInitialObservation: null,
twapMaxObservationChangePerUpdate: null,
minQuoteFutarchicLiquidity: null,
minBaseFutarchicLiquidity: null,
twapStartDelaySeconds: null,
teamSponsoredPassThresholdBps: null,
teamAddress: null,
},
})
.instruction();
const updateDaoMessage = new TransactionMessage({
payerKey: this.payer.publicKey,
recentBlockhash: (await this.banksClient.getLatestBlockhash())[0],
instructions: [updateDaoIx],
});
const multisigPda = multisig.getMultisigPda({ createKey: dao })[0];
const vaultTxCreate = multisig.instructions.vaultTransactionCreate({
multisigPda,
transactionIndex: 1n,
creator: PERMISSIONLESS_ACCOUNT.publicKey,
rentPayer: this.payer.publicKey,
vaultIndex: 0,
ephemeralSigners: 0,
transactionMessage: updateDaoMessage,
});
const proposalCreateIx = multisig.instructions.proposalCreate({
multisigPda,
transactionIndex: 1n,
creator: PERMISSIONLESS_ACCOUNT.publicKey,
rentPayer: this.payer.publicKey,
});
[squadsProposalPda] = multisig.getProposalPda({
multisigPda,
transactionIndex: 1n,
});
const tx = new Transaction().add(vaultTxCreate, proposalCreateIx);
tx.recentBlockhash = (await this.banksClient.getLatestBlockhash())[0];
tx.feePayer = this.payer.publicKey;
tx.sign(this.payer, PERMISSIONLESS_ACCOUNT);
await this.banksClient.processTransaction(tx);
proposal = await this.futarchy.initializeProposal(dao, squadsProposalPda);
});
it("allows unstaking from a Draft proposal", async function () {
const stakeAmount = new BN(100 * 10 ** 6);
await this.futarchy
.stakeToProposalIx({
proposal,
dao,
baseMint: META,
amount: stakeAmount,
})
.rpc();
const beforeBalance = await this.getTokenBalance(
META,
this.payer.publicKey,
);
await this.futarchy
.unstakeFromProposalIx({
proposal,
dao,
baseMint: META,
amount: stakeAmount,
})
.rpc();
const afterBalance = await this.getTokenBalance(META, this.payer.publicKey);
assert.equal(
(afterBalance - beforeBalance).toString(),
stakeAmount.toString(),
);
});
it("allows unstaking from a launched proposal after the delay", async function () {
const stakeAmount = new BN(100 * 10 ** 6);
await this.futarchy
.stakeToProposalIx({
proposal,
dao,
baseMint: META,
amount: stakeAmount,
})
.rpc();
await this.futarchy
.launchProposalIx({
proposal,
dao,
baseMint: META,
quoteMint: USDC,
squadsProposal: squadsProposalPda,
})
.rpc();
await this.advanceBySeconds(5);
const beforeBalance = await this.getTokenBalance(
META,
this.payer.publicKey,
);
await this.futarchy
.unstakeFromProposalIx({
proposal,
dao,
baseMint: META,
amount: stakeAmount,
})
.rpc();
const afterBalance = await this.getTokenBalance(META, this.payer.publicKey);
assert.equal(
(afterBalance - beforeBalance).toString(),
stakeAmount.toString(),
);
});
it("fails when unstaking one second before the delay has elapsed", async function () {
const stakeAmount = new BN(100 * 10 ** 6);
await this.futarchy
.stakeToProposalIx({
proposal,
dao,
baseMint: META,
amount: stakeAmount,
})
.rpc();
await this.futarchy
.launchProposalIx({
proposal,
dao,
baseMint: META,
quoteMint: USDC,
squadsProposal: squadsProposalPda,
})
.rpc();
// MIN_PROPOSAL_UNSTAKE_DELAY_SECONDS is 5, so advancing by 4 is the
// largest advance that still leaves the check `clock >= enqueued + 5` false.
await this.advanceBySeconds(4);
const callbacks = expectError(
"ProposalNotReadyToUnstake",
"Unstaking one second before the delay should fail",
);
await this.futarchy
.unstakeFromProposalIx({
proposal,
dao,
baseMint: META,
amount: stakeAmount,
})
.rpc()
.then(callbacks[0], callbacks[1]);
});
it("fails when unstaking in the same transaction as launch (flash-loan)", async function () {
const stakeAmount = new BN(100 * 10 ** 6);
// stakeIxs includes the SDK's ATA-creation preInstructions for the
// staker/proposal base accounts, which we need because this is the
// first stake on this proposal.
const stakeIxs = await InstructionUtils.getInstructions(
this.futarchy.stakeToProposalIx({
proposal,
dao,
baseMint: META,
amount: stakeAmount,
}),
);
// Use .instruction() for launch so we don't pull in its own
// ComputeBudget preInstruction — Solana rejects transactions with
// duplicate compute-budget instructions.
const launchIx = await this.futarchy
.launchProposalIx({
proposal,
dao,
baseMint: META,
quoteMint: USDC,
squadsProposal: squadsProposalPda,
})
.instruction();
const callbacks = expectError(
"ProposalNotReadyToUnstake",
"Unstaking in the same tx as launch should fail the delay check",
);
await this.futarchy
.unstakeFromProposalIx({
proposal,
dao,
baseMint: META,
amount: stakeAmount,
})
.preInstructions([
ComputeBudgetProgram.setComputeUnitLimit({ units: 800_000 }),
...stakeIxs,
launchIx,
])
.rpc()
.then(callbacks[0], callbacks[1]);
});
}