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

fix: swap and stake amount #177

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

### Security

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

### Added

- Fix function `swapAndStakeForFee` to accept additional param `inAmountLamport`

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

### Added
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.13",
"version": "1.1.14",
"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
71 changes: 5 additions & 66 deletions ts-client/src/amm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2002,78 +2002,17 @@ export default class AmmImpl implements AmmImplementation {
owner: PublicKey,
inTokenMint: PublicKey,
inAmountLamport: BN,
minOutAmountLamport: BN,
outAmountLamport: BN,
stakeForFee: StakeForFee,
): Promise<Transaction[]> {
const resultTxs: Transaction[] = [];
const [sourceToken, destinationToken] = this.tokenAMint.address.equals(inTokenMint)
? [this.poolState.tokenAMint, this.poolState.tokenBMint]
: [this.poolState.tokenBMint, this.poolState.tokenAMint];

const protocolTokenFee = this.tokenAMint.address.equals(inTokenMint)
? this.poolState.protocolTokenAFee
: this.poolState.protocolTokenBFee;

let preInstructions: Array<TransactionInstruction> = [];
const [[userSourceToken, createUserSourceIx], [userDestinationToken, createUserDestinationIx]] =
await this.createATAPreInstructions(owner, [sourceToken, destinationToken]);

createUserSourceIx && preInstructions.push(createUserSourceIx);
createUserDestinationIx && preInstructions.push(createUserDestinationIx);

if (sourceToken.equals(NATIVE_MINT)) {
preInstructions = preInstructions.concat(
wrapSOLInstruction(owner, userSourceToken, BigInt(inAmountLamport.toString())),
);
}

const postInstructions: Array<TransactionInstruction> = [];
if (NATIVE_MINT.equals(destinationToken)) {
const unwrapSOLIx = await unwrapSOLInstruction(owner);
unwrapSOLIx && postInstructions.push(unwrapSOLIx);
}

const remainingAccounts = this.swapCurve.getRemainingAccounts();

const swapTx = await this.program.methods
.swap(inAmountLamport, outAmountLamport)
.accounts({
aTokenVault: this.vaultA.vaultState.tokenVault,
bTokenVault: this.vaultB.vaultState.tokenVault,
aVault: this.poolState.aVault,
bVault: this.poolState.bVault,
aVaultLp: this.poolState.aVaultLp,
bVaultLp: this.poolState.bVaultLp,
aVaultLpMint: this.vaultA.vaultState.lpMint,
bVaultLpMint: this.vaultB.vaultState.lpMint,
userSourceToken,
userDestinationToken,
user: owner,
protocolTokenFee,
pool: this.address,
tokenProgram: TOKEN_PROGRAM_ID,
vaultProgram: this.vaultProgram.programId,
})
.remainingAccounts(remainingAccounts)
.preInstructions(preInstructions)
.postInstructions(postInstructions)
.transaction();

resultTxs.push(
new Transaction({
feePayer: owner,
...(await this.program.provider.connection.getLatestBlockhash(this.program.provider.connection.commitment)),
}).add(swapTx),
);

const swapTx = await this.swap(owner, inTokenMint, inAmountLamport, minOutAmountLamport);
resultTxs.push(swapTx);

const stakeTx = await stakeForFee.stake(outAmountLamport, owner);

resultTxs.push(
new Transaction({
feePayer: owner,
...(await this.program.provider.connection.getLatestBlockhash(this.program.provider.connection.commitment)),
}).add(stakeTx),
);
resultTxs.push(stakeTx);

return resultTxs;
}
Expand Down
Loading