Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds conditional balance with check for swap #259

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"type": "module",
"license": "BSL-1.0",
"scripts": {
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
Expand Down
3 changes: 2 additions & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@metadaoproject/futarchy",
"version": "0.4.0-alpha.33",
"version": "0.4.0-alpha.35",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"license": "BSL-1.0",
"files": [
"/dist"
],
Expand Down
36 changes: 26 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 @@ -68,15 +70,29 @@ export class FutarchyClient {
? [quoteVault, quoteMint]
: [baseVault, baseMint];

const mintTx = await this.conditionalVaultClient
.mintConditionalTokensIx(
underlyingVault,
underlyingTokenMint,
inputAmount,
user,
payer
)
.transaction();
let mintTx: Transaction | undefined;

// 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 +141,6 @@ export class FutarchyClient {
)
.transaction();

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