Skip to content

Commit

Permalink
feat: adds conditional balance with check for swap
Browse files Browse the repository at this point in the history
  • Loading branch information
R-K-H committed Jan 14, 2025
1 parent 49b78b4 commit cdc9081
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions sdk/src/v0.3/FutarchyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class FutarchyClient {
payer,
swapType,
outcome,
conditionalAmount,
}: {
proposal: PublicKey;
inputAmount: BN;
Expand All @@ -52,6 +53,7 @@ export class FutarchyClient {
payer: PublicKey;
swapType: SwapType;
outcome: "pass" | "fail";
conditionalAmount?: BN;
}): Promise<Transaction> {
const [baseVault] = getVaultAddr(
this.conditionalVaultClient.vaultProgram.programId,
Expand All @@ -67,16 +69,25 @@ export class FutarchyClient {
const [underlyingVault, underlyingTokenMint] = swapType.buy
? [quoteVault, quoteMint]
: [baseVault, baseMint];

let mintTx: Transaction | undefined;

const mintTx = await this.conditionalVaultClient
.mintConditionalTokensIx(
underlyingVault,
underlyingTokenMint,
inputAmount,
user,
payer
)
.transaction();
// Check to see if we need to mint conditional tokens
// If conditionalAmount is not passed we will mint all of the input amount
// If conditionalAmount is passed we will mint the difference between the input amount and the conditional amount
if (!conditionalAmount || (conditionalAmount && conditionalAmount.lt(inputAmount))) {
const mintAmount = conditionalAmount ? inputAmount.sub(conditionalAmount) : inputAmount;

mintTx = await this.conditionalVaultClient
.mintConditionalTokensIx(
underlyingVault,
underlyingTokenMint,
mintAmount,
user,
payer
)
.transaction();
}

const [pUSDC] = getVaultFinalizeMintAddr(
this.conditionalVaultClient.vaultProgram.programId,
Expand Down Expand Up @@ -125,6 +136,6 @@ export class FutarchyClient {
)
.transaction();

return new Transaction().add(mintTx, swapTx);
return mintTx ? new Transaction().add(mintTx, swapTx) : swapTx;
}
}

0 comments on commit cdc9081

Please sign in to comment.