Skip to content

Commit

Permalink
add optional position mint params (#121)
Browse files Browse the repository at this point in the history
* add optional position mint params

* switch from keypair to pubkey

* PR fixes
  • Loading branch information
fuzzyyeti authored Nov 13, 2023
1 parent 728586c commit 2dfb9f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
33 changes: 21 additions & 12 deletions sdk/src/impl/whirlpool-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,18 @@ export class WhirlpoolImpl implements Whirlpool {
tickUpper: number,
liquidityInput: IncreaseLiquidityInput,
wallet?: Address,
funder?: Address
funder?: Address,
positionMint?: PublicKey
) {
await this.refresh();
return this.getOpenPositionWithOptMetadataTx(
tickLower,
tickUpper,
liquidityInput,
!!wallet ? AddressUtil.toPubKey(wallet) : this.ctx.wallet.publicKey,
!!funder ? AddressUtil.toPubKey(funder) : this.ctx.wallet.publicKey
!!funder ? AddressUtil.toPubKey(funder) : this.ctx.wallet.publicKey,
false,
positionMint
);
}

Expand All @@ -111,8 +114,8 @@ export class WhirlpoolImpl implements Whirlpool {
tickUpper: number,
liquidityInput: IncreaseLiquidityInput,
sourceWallet?: Address,
positionWallet?: Address,
funder?: Address
funder?: Address,
positionMint?: PublicKey
) {
await this.refresh();
return this.getOpenPositionWithOptMetadataTx(
Expand All @@ -121,7 +124,8 @@ export class WhirlpoolImpl implements Whirlpool {
liquidityInput,
!!sourceWallet ? AddressUtil.toPubKey(sourceWallet) : this.ctx.wallet.publicKey,
!!funder ? AddressUtil.toPubKey(funder) : this.ctx.wallet.publicKey,
true
true,
positionMint
);
}

Expand Down Expand Up @@ -253,7 +257,8 @@ export class WhirlpoolImpl implements Whirlpool {
liquidityInput: IncreaseLiquidityInput,
wallet: PublicKey,
funder: PublicKey,
withMetadata: boolean = false
withMetadata: boolean = false,
positionMint?: PublicKey
): Promise<{ positionMint: PublicKey; tx: TransactionBuilder }> {
invariant(TickUtil.checkTickInBounds(tickLower), "tickLower is out of bounds.");
invariant(TickUtil.checkTickInBounds(tickUpper), "tickUpper is out of bounds.");
Expand All @@ -277,13 +282,14 @@ export class WhirlpoolImpl implements Whirlpool {
);

const positionMintKeypair = Keypair.generate();
const positionMintPubkey = positionMint ?? positionMintKeypair.publicKey;
const positionPda = PDAUtil.getPosition(
this.ctx.program.programId,
positionMintKeypair.publicKey
positionMintPubkey
);
const metadataPda = PDAUtil.getPositionMetadata(positionMintKeypair.publicKey);
const metadataPda = PDAUtil.getPositionMetadata(positionMintPubkey);
const positionTokenAccountAddress = getAssociatedTokenAddressSync(
positionMintKeypair.publicKey,
positionMintPubkey,
wallet,
this.ctx.accountResolverOpts.allowPDAOwnerAddress
);
Expand All @@ -301,14 +307,17 @@ export class WhirlpoolImpl implements Whirlpool {
owner: wallet,
positionPda,
metadataPda,
positionMintAddress: positionMintKeypair.publicKey,
positionMintAddress: positionMintPubkey,
positionTokenAccount: positionTokenAccountAddress,
whirlpool: this.address,
tickLowerIndex: tickLower,
tickUpperIndex: tickUpper,
}
);
txBuilder.addInstruction(positionIx).addSigner(positionMintKeypair);
txBuilder.addInstruction(positionIx);
if(positionMint === undefined) {
txBuilder.addSigner(positionMintKeypair);
}

const [ataA, ataB] = await resolveOrCreateATAs(
this.ctx.connection,
Expand Down Expand Up @@ -360,7 +369,7 @@ export class WhirlpoolImpl implements Whirlpool {
txBuilder.addInstruction(liquidityIx);

return {
positionMint: positionMintKeypair.publicKey,
positionMint: positionMintPubkey,
tx: txBuilder,
};
}
Expand Down
8 changes: 6 additions & 2 deletions sdk/src/whirlpool-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,16 @@ export interface Whirlpool {
* @param liquidityInput - an InputLiquidityInput type to define the desired liquidity amount to deposit
* @param wallet - the wallet to withdraw tokens to deposit into the position and house the position token. If null, the WhirlpoolContext wallet is used.
* @param funder - the wallet that will fund the cost needed to initialize the position. If null, the WhirlpoolContext wallet is used.
* @param positionMint - the mint address of the position token to be created. If null, a new mint address will be created.
* @return `positionMint` - the position to be created. `tx` - The transaction containing the instructions to perform the operation on chain.
*/
openPosition: (
tickLower: number,
tickUpper: number,
liquidityInput: IncreaseLiquidityInput,
wallet?: Address,
funder?: Address
funder?: Address,
positionMint?: PublicKey
) => Promise<{ positionMint: PublicKey; tx: TransactionBuilder }>;

/**
Expand All @@ -233,14 +235,16 @@ export interface Whirlpool {
* @param liquidityInput - input that defines the desired liquidity amount and maximum tokens willing to be to deposited.
* @param wallet - the wallet to withdraw tokens to deposit into the position and house the position token. If null, the WhirlpoolContext wallet is used.
* @param funder - the wallet that will fund the cost needed to initialize the position. If null, the WhirlpoolContext wallet is used.
* @param positionMint - the mint address of the position token to be created. If null, a new mint address will be created.
* @return `positionMint` - the position to be created. `tx` - The transaction containing the instructions to perform the operation on chain.
*/
openPositionWithMetadata: (
tickLower: number,
tickUpper: number,
liquidityInput: IncreaseLiquidityInput,
wallet?: Address,
funder?: Address
funder?: Address,
positionMint?: PublicKey
) => Promise<{ positionMint: PublicKey; tx: TransactionBuilder }>;

/**
Expand Down

0 comments on commit 2dfb9f7

Please sign in to comment.