Skip to content

Commit b07a8e7

Browse files
committed
Improve initializeProposal
1 parent cf69d4c commit b07a8e7

3 files changed

Lines changed: 65 additions & 77 deletions

File tree

futarchy-ts/src/AutocratClient.ts

Lines changed: 48 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
USDC_DECIMALS,
2828
} from "./constants";
2929
import {
30+
InstructionUtils,
3031
getAmmAddr,
3132
getAmmLpMintAddr,
3233
getDaoTreasuryAddr,
@@ -252,7 +253,6 @@ export class AutocratClient {
252253
baseTokensToLP: BN,
253254
quoteTokensToLP: BN
254255
): Promise<PublicKey> {
255-
let vaultProgramId = this.vaultClient.vaultProgram.programId;
256256
const storedDao = await this.getDao(dao);
257257

258258
const nonce = new BN(Math.random() * 2 ** 50);
@@ -263,13 +263,6 @@ export class AutocratClient {
263263
nonce
264264
);
265265

266-
await this.vaultClient
267-
.initializeVaultIx(proposal, storedDao.tokenMint)
268-
.rpc();
269-
await this.vaultClient
270-
.initializeVaultIx(proposal, storedDao.usdcMint)
271-
.rpc();
272-
273266
const {
274267
baseVault,
275268
quoteVault,
@@ -286,85 +279,64 @@ export class AutocratClient {
286279
dao
287280
);
288281

289-
// const [baseVault] = getVaultAddr(
290-
// this.vaultClient.vaultProgram.programId,
291-
// proposal,
292-
// storedDao.tokenMint
293-
// );
294-
// const [quoteVault] = getVaultAddr(
295-
// this.vaultClient.vaultProgram.programId,
296-
// proposal,
297-
// storedDao.usdcMint
298-
// );
299-
282+
// it's important that these happen in a single atomic transaction
300283
await this.vaultClient
301-
.mintConditionalTokensIx(baseVault, storedDao.tokenMint, baseTokensToLP)
284+
.initializeVaultIx(proposal, storedDao.tokenMint)
285+
.postInstructions(
286+
await InstructionUtils.getInstructions(
287+
this.vaultClient.initializeVaultIx(proposal, storedDao.usdcMint),
288+
this.ammClient.createAmmIx(
289+
passBaseMint,
290+
passQuoteMint,
291+
storedDao.twapInitialObservation,
292+
storedDao.twapMaxObservationChangePerUpdate,
293+
proposal
294+
),
295+
this.ammClient.createAmmIx(
296+
failBaseMint,
297+
failQuoteMint,
298+
storedDao.twapInitialObservation,
299+
storedDao.twapMaxObservationChangePerUpdate,
300+
proposal
301+
)
302+
)
303+
)
302304
.rpc();
305+
303306
await this.vaultClient
304-
.mintConditionalTokensIx(quoteVault, storedDao.usdcMint, quoteTokensToLP)
307+
.mintConditionalTokensIx(baseVault, storedDao.tokenMint, baseTokensToLP)
308+
.postInstructions(
309+
await InstructionUtils.getInstructions(
310+
this.vaultClient.mintConditionalTokensIx(
311+
quoteVault,
312+
storedDao.usdcMint,
313+
quoteTokensToLP
314+
)
315+
)
316+
)
305317
.rpc();
306318

307-
// const [passBase] = getVaultFinalizeMintAddr(vaultProgramId, baseVault);
308-
// const [passQuote] = getVaultFinalizeMintAddr(vaultProgramId, quoteVault);
309-
// const [passAmm] = getAmmAddr(
310-
// this.ammClient.program.programId,
311-
// passBase,
312-
// passQuote,
313-
// proposal
314-
// );
315-
316-
// const [failBase] = getVaultRevertMintAddr(vaultProgramId, baseVault);
317-
// const [failQuote] = getVaultRevertMintAddr(vaultProgramId, quoteVault);
318-
// const [failAmm] = getAmmAddr(
319-
// this.ammClient.program.programId,
320-
// failBase,
321-
// failQuote,
322-
// proposal
323-
// );
324-
325-
let tx = await this.ammClient
326-
.createAmmIx(
319+
await this.ammClient
320+
.addLiquidityIx(
321+
passAmm,
327322
passBaseMint,
328323
passQuoteMint,
329-
storedDao.twapInitialObservation,
330-
storedDao.twapMaxObservationChangePerUpdate,
331-
proposal
324+
quoteTokensToLP,
325+
baseTokensToLP,
326+
new BN(0)
332327
)
333-
.postInstructions([
334-
...(
335-
await this.ammClient
336-
.addLiquidityIx(
337-
passAmm,
338-
passBaseMint,
339-
passQuoteMint,
340-
quoteTokensToLP,
341-
baseTokensToLP,
342-
new BN(0)
343-
)
344-
.transaction()
345-
).instructions,
346-
await this.ammClient
347-
.createAmmIx(
328+
.postInstructions(
329+
await InstructionUtils.getInstructions(
330+
this.ammClient.addLiquidityIx(
331+
failAmm,
348332
failBaseMint,
349333
failQuoteMint,
350-
storedDao.twapInitialObservation,
351-
storedDao.twapMaxObservationChangePerUpdate,
352-
proposal
334+
quoteTokensToLP,
335+
baseTokensToLP,
336+
new BN(0)
353337
)
354-
.instruction(),
355-
...(
356-
await this.ammClient
357-
.addLiquidityIx(
358-
failAmm,
359-
failBaseMint,
360-
failQuoteMint,
361-
quoteTokensToLP,
362-
baseTokensToLP,
363-
new BN(0)
364-
)
365-
.transaction()
366-
).instructions,
367-
])
338+
)
339+
)
368340
.rpc();
369341

370342
// this is how many original tokens are created
@@ -399,7 +371,6 @@ export class AutocratClient {
399371
this.provider.publicKey,
400372
nonce
401373
);
402-
let vaultProgramId = this.vaultClient.vaultProgram.programId;
403374
const [daoTreasury] = getDaoTreasuryAddr(this.autocrat.programId, dao);
404375
const { baseVault, quoteVault, passAmm, failAmm } = this.getProposalPdas(
405376
proposal,

futarchy-ts/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from "./pda";
33
export * from "./priceMath";
44
export * from "./metadata";
55
export * from "./cu";
6+
export * from "./instruction";
67

78
import { AccountMeta, ComputeBudgetProgram, PublicKey } from "@solana/web3.js";
89

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as anchor from "@coral-xyz/anchor";
2+
import { TransactionInstruction } from "@solana/web3.js";
3+
4+
export class InstructionUtils {
5+
public static async getInstructions(
6+
...methodBuilders: any[]
7+
): Promise<TransactionInstruction[]> {
8+
let instructions: TransactionInstruction[] = [];
9+
10+
for (const methodBuilder of methodBuilders) {
11+
instructions.push(...(await methodBuilder.transaction()).instructions);
12+
}
13+
14+
return instructions;
15+
}
16+
}

0 commit comments

Comments
 (0)