-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathfinalizeProposal.test.ts
More file actions
576 lines (506 loc) · 17.3 KB
/
finalizeProposal.test.ts
File metadata and controls
576 lines (506 loc) · 17.3 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import {
PERMISSIONLESS_ACCOUNT,
PriceMath,
} from "@metadaoproject/futarchy/v0.6";
import {
ComputeBudgetProgram,
PublicKey,
Transaction,
TransactionMessage,
} from "@solana/web3.js";
import {
createAssociatedTokenAccountIdempotentInstruction,
getAssociatedTokenAddressSync,
} from "@solana/spl-token";
import BN from "bn.js";
import { expectError, setupBasicDao } from "../../utils.js";
import { assert } from "chai";
import * as multisig from "@sqds/multisig";
const { Permissions, Permission } = multisig.types;
const THOUSAND_BUCK_PRICE = PriceMath.getAmmPrice(1000, 6, 6);
export default function suite() {
let META: PublicKey, USDC: PublicKey, dao: PublicKey, proposal: PublicKey;
beforeEach(async function () {
META = await this.createMint(this.payer.publicKey, 6);
USDC = await this.createMint(this.payer.publicKey, 6);
await this.createTokenAccount(META, this.payer.publicKey);
await this.createTokenAccount(USDC, this.payer.publicKey);
await this.mintTo(META, this.payer.publicKey, this.payer, 100 * 10 ** 9);
await this.mintTo(
USDC,
this.payer.publicKey,
this.payer,
200_000 * 1_000_000,
);
// const nonce = new BN(Math.floor(Math.random() * 1000000));
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), // 100,000 USDC
maxBaseAmount: new BN(100 * 10 ** 6), // 100 META
minLiquidity: new BN(0),
positionAuthority: this.payer.publicKey,
liquidityProvider: this.payer.publicKey,
})
.preInstructions([
ComputeBudgetProgram.setComputeUnitLimit({ units: 300_000 }),
])
.rpc();
// Create a simple instruction for the proposal
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,
});
const [squadsProposalPda] = multisig.getProposalPda({
multisigPda,
transactionIndex: 1n,
});
// Create the squads proposal first
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);
// Now initialize the autocrat proposal
proposal = await this.futarchy.initializeProposal(dao, squadsProposalPda);
await this.futarchy
.launchProposalIx({
proposal,
dao,
baseMint: META,
quoteMint: USDC,
squadsProposal: squadsProposalPda,
})
.rpc();
});
it("doesn't finalize proposals that are too young", async function () {
const callbacks = expectError(
"ProposalTooYoung",
"proposal is too young to finalize",
);
await this.futarchy
.finalizeProposal(proposal)
.then(callbacks[0], callbacks[1]);
});
it("passes proposals when Pass TWAP > Fail TWAP", async function () {
// Split tokens into the vaults
const { baseVault, quoteVault, question } = this.futarchy.getProposalPdas(
proposal,
META,
USDC,
dao,
);
await this.conditionalVault
.splitTokensIx(question, baseVault, META, new BN(10 * 10 ** 9), 2)
.rpc();
await this.conditionalVault
.splitTokensIx(question, quoteVault, USDC, new BN(11_000 * 1_000_000), 2)
.rpc();
const { passBaseMint, passQuoteMint } = this.futarchy.getProposalPdas(
proposal,
META,
USDC,
dao,
);
const { failBaseMint, failQuoteMint } = this.futarchy.getProposalPdas(
proposal,
META,
USDC,
dao,
);
// await this.autocratClient.
// await this.futarchy.autocrat.methods.launchProposal()
// .accounts({
// proposal,
// dao,
// baseVault,
// quoteVault,
// passBaseMint,
// passQuoteMint,
// failBaseMint,
// failQuoteMint,
// ammPassBaseVault: getAssociatedTokenAddressSync(passBaseMint, dao, true),
// ammPassQuoteVault: getAssociatedTokenAddressSync(passQuoteMint, dao, true),
// ammFailBaseVault: getAssociatedTokenAddressSync(failBaseMint, dao, true),
// ammFailQuoteVault: getAssociatedTokenAddressSync(failQuoteMint, dao, true),
// payer: this.payer.publicKey,
// })
// .preInstructions([ComputeBudgetProgram.setComputeUnitLimit({ units: 300_000 })])
// .rpc();
await this.futarchy
.conditionalSwapIx({
dao,
baseMint: META,
quoteMint: USDC,
proposal,
market: "pass",
swapType: "buy",
inputAmount: new BN(10_000 * 1_000_000),
minOutputAmount: new BN(0),
})
.rpc();
for (let i = 0; i < 100; i++) {
// await this.advanceBySlots(20_000n);
await this.advanceBySeconds(20_000);
await this.futarchy
.conditionalSwapIx({
dao,
baseMint: META,
quoteMint: USDC,
proposal,
market: "pass",
swapType: "buy",
inputAmount: new BN(10),
minOutputAmount: new BN(0),
})
.preInstructions([
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: i }),
])
.rpc();
}
// Finalize the proposal
await this.futarchy.finalizeProposal(proposal);
const storedProposal = await this.futarchy.getProposal(proposal);
assert.exists(storedProposal.state.passed);
// Create ATAs for the metadao multisig vault (hardcoded fee destination)
const METADAO_MULTISIG = new PublicKey(
"6awyHMshBGVjJ3ozdSJdyyDE1CTAXUwrpNMaRGMsb4sf",
);
await this.createTokenAccount(META, METADAO_MULTISIG);
await this.createTokenAccount(USDC, METADAO_MULTISIG);
await this.futarchy
.collectFeesIx({
dao,
baseMint: META,
quoteMint: USDC,
})
.rpc();
});
it("fails proposals when Pass TWAP < Fail TWAP", async function () {
const { quoteVault, question, passBaseMint } =
this.futarchy.getProposalPdas(proposal, META, USDC, dao);
await this.conditionalVault
.splitTokensIx(question, quoteVault, USDC, new BN(11_000 * 1_000_000), 2)
.rpc();
for (let i = 0; i < 100; i++) {
await this.futarchy
.conditionalSwapIx({
dao,
baseMint: META,
quoteMint: USDC,
proposal,
market: "pass",
swapType: "buy",
inputAmount: new BN(10),
minOutputAmount: new BN(0),
})
.preInstructions([
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: i }),
createAssociatedTokenAccountIdempotentInstruction(
this.payer.publicKey,
getAssociatedTokenAddressSync(
passBaseMint,
this.payer.publicKey,
true,
),
this.payer.publicKey,
passBaseMint,
),
])
.rpc();
await this.advanceBySeconds(20_000);
// await this.ammClient
// .crankThatTwapIx(passAmm)
// .preInstructions([
// // this is to get around bankrun thinking we've processed the same transaction multiple times
// ComputeBudgetProgram.setComputeUnitPrice({
// microLamports: i,
// }),
// await this.ammClient.crankThatTwapIx(failAmm).instruction(),
// ])
// .rpc();
}
// Finalize the proposal
await this.futarchy.finalizeProposal(proposal);
const storedProposal = await this.futarchy.getProposal(proposal);
assert.exists(storedProposal.state.failed);
// Verify Squads proposal is rejected
const multisigPda = multisig.getMultisigPda({ createKey: dao })[0];
const [squadsProposalPda] = multisig.getProposalPda({
multisigPda,
transactionIndex: 1n,
});
const squadsProposal = await multisig.accounts.Proposal.fromAccountAddress(
this.squadsConnection,
squadsProposalPda,
);
assert.isTrue(
multisig.generated.isProposalStatusRejected(squadsProposal.status),
);
});
it("finalizes when last trade is before the deadline (virtual crank covers the gap)", async function () {
// Trade for ~200,000s of the 259,200s proposal duration, then stop
// trading and advance the clock past the deadline before finalizing.
// The virtual crank in get_twap() fills in the gap after the last trade.
const { baseVault, quoteVault, question } = this.futarchy.getProposalPdas(
proposal,
META,
USDC,
dao,
);
await this.conditionalVault
.splitTokensIx(question, baseVault, META, new BN(10 * 10 ** 9), 2)
.rpc();
await this.conditionalVault
.splitTokensIx(question, quoteVault, USDC, new BN(11_000 * 1_000_000), 2)
.rpc();
// Initial swap to seed the pass market
await this.futarchy
.conditionalSwapIx({
dao,
baseMint: META,
quoteMint: USDC,
proposal,
market: "pass",
swapType: "buy",
inputAmount: new BN(10_000 * 1_000_000),
minOutputAmount: new BN(0),
})
.rpc();
// Trade for ~200,000 seconds (10 swaps × 20,000s each)
// This is ~77% of the 259,200s proposal duration
for (let i = 0; i < 10; i++) {
await this.advanceBySeconds(20_000);
await this.futarchy
.conditionalSwapIx({
dao,
baseMint: META,
quoteMint: USDC,
proposal,
market: "pass",
swapType: "buy",
inputAmount: new BN(10),
minOutputAmount: new BN(0),
})
.preInstructions([
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: i }),
])
.rpc();
}
// At ~200,000s into a 259,200s proposal — finalization should fail
// because wall-clock time hasn't reached the deadline yet.
const earlyCallbacks = expectError(
"ProposalTooYoung",
"proposal should not finalize before the deadline",
);
await this.futarchy
.finalizeProposal(proposal)
.then(earlyCallbacks[0], earlyCallbacks[1]);
// Stop trading. Advance time past the proposal deadline (259,200s).
// Last trade was at ~200,000s. We need at least 60,000 more seconds.
await this.advanceBySeconds(70_000);
// Finalize — should succeed because:
// 1. Wall-clock time is past the deadline (validate() passes)
// 2. At least one trade occurred after TWAP start delay (new check passes)
// 3. get_twap()'s virtual crank extends the last observation to current time
await this.futarchy.finalizeProposal(proposal);
const storedProposal = await this.futarchy.getProposal(proposal);
assert.exists(storedProposal.state.passed);
});
it("passes proposals when the team sponsors them and pass twap is slightly below fail twap", async function () {
// Create a new DAO with -5% team-sponsored threshold
const META = await this.createMint(this.payer.publicKey, 6);
const USDC = await this.createMint(this.payer.publicKey, 6);
await this.createTokenAccount(META, this.payer.publicKey);
await this.createTokenAccount(USDC, this.payer.publicKey);
await this.mintTo(
META,
this.payer.publicKey,
this.payer,
200_000 * 10 ** 6,
);
await this.mintTo(
USDC,
this.payer.publicKey,
this.payer,
200_000 * 1_000_000,
);
const daoWithTeamSponsorship = await setupBasicDao({
context: this,
baseMint: META,
quoteMint: USDC,
teamSponsoredPassThresholdBps: -500, // -5% threshold
});
await this.futarchy
.provideLiquidityIx({
dao: daoWithTeamSponsorship,
baseMint: META,
quoteMint: USDC,
quoteAmount: new BN(100_000 * 10 ** 6), // 100,000 USDC
maxBaseAmount: new BN(100_000 * 10 ** 6), // 100,000 META
minLiquidity: new BN(0),
positionAuthority: this.payer.publicKey,
liquidityProvider: this.payer.publicKey,
})
.preInstructions([
ComputeBudgetProgram.setComputeUnitLimit({ units: 300_000 }),
])
.rpc();
// Create a simple instruction for the proposal
const updateDaoIx = await this.futarchy
.updateDaoIx({
dao: daoWithTeamSponsorship,
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: daoWithTeamSponsorship,
})[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,
});
const [squadsProposalPda] = multisig.getProposalPda({
multisigPda,
transactionIndex: 1n,
});
// Create the squads proposal first
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);
// Now initialize the autocrat proposal
const teamSponsoredProposal = await this.futarchy.initializeProposal(
daoWithTeamSponsorship,
squadsProposalPda,
);
// Sponsor the proposal
await this.futarchy
.sponsorProposalIx({
proposal: teamSponsoredProposal,
dao: daoWithTeamSponsorship,
teamAddress: this.payer.publicKey,
})
.rpc();
await this.futarchy
.launchProposalIx({
proposal: teamSponsoredProposal,
dao: daoWithTeamSponsorship,
baseMint: META,
quoteMint: USDC,
squadsProposal: squadsProposalPda,
})
.rpc();
// Split tokens into the vaults
const { baseVault, quoteVault, question } = this.futarchy.getProposalPdas(
teamSponsoredProposal,
META,
USDC,
daoWithTeamSponsorship,
);
await this.conditionalVault
.splitTokensIx(question, baseVault, META, new BN(10 * 10 ** 6), 2)
.rpc();
await this.conditionalVault
.splitTokensIx(question, quoteVault, USDC, new BN(11_000 * 1_000_000), 2)
.rpc();
// Swap in fail market to make fail TWAP higher than pass TWAP, but within 5% threshold
for (let i = 0; i < 100; i++) {
await this.futarchy
.conditionalSwapIx({
dao: daoWithTeamSponsorship,
baseMint: META,
quoteMint: USDC,
proposal: teamSponsoredProposal,
market: "fail",
swapType: "buy",
inputAmount: new BN(10 * 1_000_000), // Buy in fail market
minOutputAmount: new BN(0),
})
.preInstructions([
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: i }),
])
.rpc();
await this.advanceBySeconds(20_000);
}
// Finalize the proposal - should pass because it's team-sponsored
// and the pass TWAP is within the -5% threshold
await this.futarchy.finalizeProposal(teamSponsoredProposal);
const storedProposal = await this.futarchy.getProposal(
teamSponsoredProposal,
);
assert.exists(
storedProposal.state.passed,
"Team-sponsored proposal should pass when within threshold",
);
});
}