Skip to content

Commit

Permalink
fix: swap and stake amount (#177)
Browse files Browse the repository at this point in the history
* fix: swap and stake amount

* update changelog
  • Loading branch information
00xSam authored Nov 15, 2024
1 parent 429f6dd commit 7f13c97
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 67 deletions.
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

0 comments on commit 7f13c97

Please sign in to comment.