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/swap on create pool #163

Merged
merged 7 commits into from
Oct 9, 2024
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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Security

## @mercurial-finance/dynamic-amm-sdk [1.1.6] - PR[#161](https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk/pull/163)

### Added

- Added `poolFees` in `getFeeConfigurations` function

## @mercurial-finance/dynamic-amm-sdk [1.1.5] - PR[#161](https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk/pull/163)

### Added

- Swap options on create pool config
- Added `calculateSwapQuoteForGoingToCreateMemecoinPool` to allow swap quote on going to be created pool.

## @mercurial-finance/dynamic-amm-sdk [1.1.4] - PR[#161](https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk/pull/164)

### Changed
Expand Down
1 change: 1 addition & 0 deletions ts-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Amm, IDL as AmmIdl } from './src/amm/idl';

export default AmmImpl;
export {
AmmImpl,
// Classes
ConstantProductSwap,
StableSwap,
Expand Down
2 changes: 1 addition & 1 deletion ts-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mercurial-finance/dynamic-amm-sdk",
"version": "1.1.4",
"version": "1.1.5",
"description": "Mercurial Vaults SDK is a typescript library that allows you to interact with Mercurial v2's AMM.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
215 changes: 148 additions & 67 deletions ts-client/src/amm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
calculateUnclaimedLockEscrowFee,
derivePoolAddressWithConfig as deriveConstantProductPoolAddressWithConfig,
deriveConfigPda,
deriveProtocolTokenFee,
} from './utils';
import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes';

Expand Down Expand Up @@ -262,6 +263,11 @@ export default class AmmImpl implements AmmImplementation {
cluster?: Cluster;
programId?: string;
lockLiquidity?: boolean;
swapLiquidity?: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add comment or docs to indicate that the SDK only do token B to A?

// always swap B to A
inAmount: BN;
minAmountOut: BN;
};
activationPoint?: BN;
},
) {
Expand Down Expand Up @@ -337,6 +343,45 @@ export default class AmmImpl implements AmmImplementation {
const [mintMetadata, _mintMetadataBump] = deriveMintMetadata(lpMint);
const activationPoint = opt?.activationPoint || null;

const createPoolPostInstructions: TransactionInstruction[] = [];
if (opt?.lockLiquidity) {
const [lockEscrowPK] = deriveLockEscrowPda(poolPubkey, payer, ammProgram.programId);
const createLockEscrowIx = await ammProgram.methods
.createLockEscrow()
.accounts({
pool: poolPubkey,
lockEscrow: lockEscrowPK,
owner: payer,
lpMint,
payer,
systemProgram: SystemProgram.programId,
})
.instruction();
createPoolPostInstructions.push(createLockEscrowIx);
const [escrowAta, createEscrowAtaIx] = await getOrCreateATAInstruction(lpMint, lockEscrowPK, connection, payer);

createEscrowAtaIx && createPoolPostInstructions.push(createEscrowAtaIx);
const lockIx = await ammProgram.methods
.lock(U64_MAX)
.accounts({
pool: poolPubkey,
lockEscrow: lockEscrowPK,
owner: payer,
lpMint,
sourceTokens: payerPoolLp,
escrowVault: escrowAta,
tokenProgram: TOKEN_PROGRAM_ID,
aVault,
bVault,
aVaultLp,
bVaultLp,
aVaultLpMint,
bVaultLpMint,
})
.instruction();
createPoolPostInstructions.push(lockIx);
}

const createPermissionlessPoolTx = await ammProgram.methods
.initializePermissionlessConstantProductPoolWithConfig2(tokenAAmount, tokenBAmount, activationPoint)
.accounts({
Expand Down Expand Up @@ -367,13 +412,17 @@ export default class AmmImpl implements AmmImplementation {
systemProgram: SystemProgram.programId,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
})
.postInstructions(createPoolPostInstructions)
.transaction();

const latestBlockHash = await ammProgram.provider.connection.getLatestBlockhash(
ammProgram.provider.connection.commitment,
);
const resultTx: Transaction[] = [];
if (preInstructions.length) {
const preInstructionTx = new Transaction({
feePayer: payer,
...(await ammProgram.provider.connection.getLatestBlockhash(ammProgram.provider.connection.commitment)),
...latestBlockHash,
}).add(...preInstructions);
resultTx.push(preInstructionTx);
}
Expand All @@ -388,48 +437,37 @@ export default class AmmImpl implements AmmImplementation {
.add(setComputeUnitLimitIx)
.add(createPermissionlessPoolTx);

if (opt?.lockLiquidity) {
const preLockLiquidityIx: TransactionInstruction[] = [];
const [lockEscrowPK] = deriveLockEscrowPda(poolPubkey, payer, ammProgram.programId);
const createLockEscrowIx = await ammProgram.methods
.createLockEscrow()
.accounts({
pool: poolPubkey,
lockEscrow: lockEscrowPK,
owner: payer,
lpMint,
payer,
systemProgram: SystemProgram.programId,
})
.instruction();
preLockLiquidityIx.push(createLockEscrowIx);
const [escrowAta, createEscrowAtaIx] = await getOrCreateATAInstruction(lpMint, lockEscrowPK, connection, payer);
resultTx.push(mainTx);

createEscrowAtaIx && preLockLiquidityIx.push(createEscrowAtaIx);
const lockTx = await ammProgram.methods
.lock(U64_MAX)
if (opt?.swapLiquidity) {
const protocolTokenFee = deriveProtocolTokenFee(poolPubkey, tokenBMint, ammProgram.programId);
const swapTx = await ammProgram.methods
.swap(opt.swapLiquidity.inAmount, opt.swapLiquidity.minAmountOut)
.accounts({
pool: poolPubkey,
lockEscrow: lockEscrowPK,
owner: payer,
lpMint,
sourceTokens: payerPoolLp,
escrowVault: escrowAta,
tokenProgram: TOKEN_PROGRAM_ID,
aTokenVault,
bTokenVault,
aVault,
bVault,
aVaultLp,
bVaultLp,
aVaultLpMint,
bVaultLpMint,
userSourceToken: payerTokenB,
userDestinationToken: payerTokenA,
user: payer,
protocolTokenFee,
pool: poolPubkey,
tokenProgram: TOKEN_PROGRAM_ID,
vaultProgram: vaultProgram.programId,
})
.preInstructions(preLockLiquidityIx)
.transaction();
mainTx.add(lockTx);
const newSwapTx = new Transaction({
feePayer: payer,
...latestBlockHash,
}).add(swapTx);
resultTx.push(newSwapTx);
}

resultTx.push(mainTx);

return resultTx;
}

Expand All @@ -445,6 +483,10 @@ export default class AmmImpl implements AmmImplementation {
cluster?: Cluster;
programId?: string;
lockLiquidity?: boolean;
swapLiquidity?: {
inAmount: BN;
minAmountOut: BN;
};
skipAAta?: boolean;
skipBAta?: boolean;
},
Expand Down Expand Up @@ -508,18 +550,63 @@ export default class AmmImpl implements AmmImplementation {
),
];

const payerPoolLp = await getAssociatedTokenAccount(lpMint, payer);
const payerPoolLp = getAssociatedTokenAccount(lpMint, payer);

if (tokenAMint.equals(NATIVE_MINT)) {
preInstructions = preInstructions.concat(wrapSOLInstruction(payer, payerTokenA, BigInt(tokenAAmount.toString())));
}

if (tokenBMint.equals(NATIVE_MINT)) {
preInstructions = preInstructions.concat(wrapSOLInstruction(payer, payerTokenB, BigInt(tokenBAmount.toString())));
preInstructions = preInstructions.concat(
wrapSOLInstruction(
payer,
payerTokenB,
BigInt(tokenBAmount.add(opt?.swapLiquidity?.inAmount ?? new BN(0)).toString()),
),
);
}

const [mintMetadata, _mintMetadataBump] = deriveMintMetadata(lpMint);

const createPoolPostInstructions: TransactionInstruction[] = [];
if (opt?.lockLiquidity) {
const [lockEscrowPK] = deriveLockEscrowPda(poolPubkey, payer, ammProgram.programId);
const createLockEscrowIx = await ammProgram.methods
.createLockEscrow()
.accounts({
pool: poolPubkey,
lockEscrow: lockEscrowPK,
owner: payer,
lpMint,
payer,
systemProgram: SystemProgram.programId,
})
.instruction();
createPoolPostInstructions.push(createLockEscrowIx);
const [escrowAta, createEscrowAtaIx] = await getOrCreateATAInstruction(lpMint, lockEscrowPK, connection, payer);

createEscrowAtaIx && createPoolPostInstructions.push(createEscrowAtaIx);
const lockIx = await ammProgram.methods
.lock(U64_MAX)
.accounts({
pool: poolPubkey,
lockEscrow: lockEscrowPK,
owner: payer,
lpMint,
sourceTokens: payerPoolLp,
escrowVault: escrowAta,
tokenProgram: TOKEN_PROGRAM_ID,
aVault,
bVault,
aVaultLp,
bVaultLp,
aVaultLpMint,
bVaultLpMint,
})
.instruction();
createPoolPostInstructions.push(lockIx);
}

const createPermissionlessPoolTx = await ammProgram.methods
.initializePermissionlessConstantProductPoolWithConfig(tokenAAmount, tokenBAmount)
.accounts({
Expand Down Expand Up @@ -550,69 +637,62 @@ export default class AmmImpl implements AmmImplementation {
systemProgram: SystemProgram.programId,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
})
.postInstructions(createPoolPostInstructions)
.transaction();

const resultTx: Transaction[] = [];
const latestBlockHash = await ammProgram.provider.connection.getLatestBlockhash(
ammProgram.provider.connection.commitment,
);
if (preInstructions.length) {
const preInstructionTx = new Transaction({
feePayer: payer,
...(await ammProgram.provider.connection.getLatestBlockhash(ammProgram.provider.connection.commitment)),
...latestBlockHash,
}).add(...preInstructions);
resultTx.push(preInstructionTx);
}

const setComputeUnitLimitIx = ComputeBudgetProgram.setComputeUnitLimit({
units: 600_000,
units: 1_400_000,
});
const mainTx = new Transaction({
const createPoolTx = new Transaction({
feePayer: payer,
...(await ammProgram.provider.connection.getLatestBlockhash(ammProgram.provider.connection.commitment)),
...latestBlockHash,
})
.add(setComputeUnitLimitIx)
.add(createPermissionlessPoolTx);

if (opt?.lockLiquidity) {
const preLockLiquidityIx: TransactionInstruction[] = [];
const [lockEscrowPK] = deriveLockEscrowPda(poolPubkey, payer, ammProgram.programId);
const createLockEscrowIx = await ammProgram.methods
.createLockEscrow()
.accounts({
pool: poolPubkey,
lockEscrow: lockEscrowPK,
owner: payer,
lpMint,
payer,
systemProgram: SystemProgram.programId,
})
.instruction();
preLockLiquidityIx.push(createLockEscrowIx);
const [escrowAta, createEscrowAtaIx] = await getOrCreateATAInstruction(lpMint, lockEscrowPK, connection, payer);
resultTx.push(createPoolTx);

createEscrowAtaIx && preLockLiquidityIx.push(createEscrowAtaIx);
const lockTx = await ammProgram.methods
.lock(U64_MAX)
if (opt?.swapLiquidity) {
const protocolTokenFee = deriveProtocolTokenFee(poolPubkey, tokenBMint, ammProgram.programId);
const swapTx = await ammProgram.methods
.swap(opt.swapLiquidity.inAmount, opt.swapLiquidity.minAmountOut)
.accounts({
pool: poolPubkey,
lockEscrow: lockEscrowPK,
owner: payer,
lpMint,
sourceTokens: payerPoolLp,
escrowVault: escrowAta,
tokenProgram: TOKEN_PROGRAM_ID,
aTokenVault,
bTokenVault,
aVault,
bVault,
aVaultLp,
bVaultLp,
aVaultLpMint,
bVaultLpMint,
userSourceToken: payerTokenB,
userDestinationToken: payerTokenA,
user: payer,
protocolTokenFee,
pool: poolPubkey,
tokenProgram: TOKEN_PROGRAM_ID,
vaultProgram: vaultProgram.programId,
})
.preInstructions(preLockLiquidityIx)
.transaction();
mainTx.add(lockTx);
const newSwapTx = new Transaction({
feePayer: payer,
...latestBlockHash,
}).add(swapTx);
resultTx.push(newSwapTx);
}

resultTx.push(mainTx);

return resultTx;
}

Expand Down Expand Up @@ -970,6 +1050,7 @@ export default class AmmImpl implements AmmImplementation {

return {
publicKey: configAccount.publicKey,
poolFees,
tradeFeeBps: poolFees.tradeFeeNumerator.mul(new BN(10000)).div(poolFees.tradeFeeDenominator),
protocolTradeFeeBps: poolFees.protocolTradeFeeNumerator
.mul(new BN(10000))
Expand Down
Loading
Loading