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 a2df969
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metadaoproject/futarchy",
"version": "0.4.0-alpha.33",
"version": "0.4.0-alpha.34",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
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 a2df969

Please sign in to comment.