Skip to content
Open
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.24] - PR[#194](https://github.com/mercurial-finance/mercurial-dynamic-amm-sdk/pull/194)

### Removed

- Remove non popular and unused dependency

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

### Changed
Expand Down
9 changes: 3 additions & 6 deletions 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.23",
"version": "1.1.24",
"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 All @@ -18,15 +18,11 @@
"@mercurial-finance/vault-sdk": "2.2.0",
"@metaplex-foundation/mpl-token-metadata": "~2.13.0",
"@meteora-ag/m3m3": "1.0.4",
"@project-serum/anchor": "^0.24.2",
"@solana/buffer-layout": "^3 || ^4",
"@solana/spl-token": "^0.4.6",
"@solana/spl-token-registry": "^0.2.4574",
"@solana/web3.js": "^1.95.8",
"bn-sqrt": "^1.0.0",
"bn.js": "5.2.1",
"decimal.js": "^10.4.1",
"dotenv": "^16.0.1",
"invariant": "^2.2.4"
},
"devDependencies": {
Expand All @@ -41,7 +37,8 @@
"mocha": "^10.0.0",
"ts-jest": "^28.0.2",
"ts-mocha": "^10.0.0",
"typescript": "^5.5.4"
"typescript": "^5.5.4",
"dotenv": "^16.0.1"
},
"peerDependencies": {
"@solana/buffer-layout": "^3 || ^4"
Expand Down
56 changes: 3 additions & 53 deletions ts-client/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions ts-client/src/amm/curve/constant-product.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sqrt from 'bn-sqrt';
import { BN } from '@coral-xyz/anchor';
import { getPriceImpact, OutResult, SwapCurve, TradeDirection } from '.';
import { PoolFees } from '../types';
import Decimal from 'decimal.js';

// Typescript implementation of https://github.com/solana-labs/solana-program-library/blob/master/libraries/math/src/checked_ceil_div.rs#L29
function ceilDiv(lhs: BN, rhs: BN) {
Expand All @@ -27,7 +27,7 @@ function ceilDiv(lhs: BN, rhs: BN) {
}

export class ConstantProductSwap implements SwapCurve {
constructor() { }
constructor() {}

private computeOutAmountWithoutSlippage(sourceAmount: BN, swapSourceAmount: BN, swapDestinationAmount: BN): BN {
return sourceAmount.mul(swapDestinationAmount).div(swapSourceAmount);
Expand Down Expand Up @@ -58,7 +58,8 @@ export class ConstantProductSwap implements SwapCurve {
};
}
computeD(tokenAAmount: BN, tokenBAmount: BN): BN {
return sqrt(tokenAAmount.mul(tokenBAmount));
const sqrt = new Decimal(tokenAAmount.mul(tokenBAmount).toString()).floor();
return new BN(sqrt.toString());
}
computeInAmount(
destAmount: BN,
Expand Down
47 changes: 23 additions & 24 deletions ts-client/src/amm/curve/stable-swap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BN } from '@coral-xyz/anchor';
import { BorshCoder, Idl } from '@project-serum/anchor';
import { BN, BorshCoder, Idl } from '@coral-xyz/anchor';
import { Fraction, Percent, ZERO, Fees, computeD, computeY, normalizedTradeFee } from './stable-swap-math';
import { AccountInfo, PublicKey } from '@solana/web3.js';
import { OutResult, SwapCurve, TradeDirection, getPriceImpact } from '.';
Expand All @@ -21,7 +20,7 @@ export class StableSwap implements SwapCurve {
private extraAccounts: Map<String, AccountInfo<Buffer>>,
private onChainTime: BN,
private stakePoolPubkey: PublicKey,
) { }
) {}

private getBasePoolVirtualPrice(depegType: DepegType): BN {
if (depegType['marinade']) {
Expand Down Expand Up @@ -127,15 +126,15 @@ export class StableSwap implements SwapCurve {
const [upscaledSourceAmount, upscaledSwapSourceAmount, upscaledSwapDestinationAmount] =
tradeDirection == TradeDirection.AToB
? [
this.upscaleTokenA(sourceAmount),
this.upscaleTokenA(swapSourceAmount),
this.upscaleTokenB(swapDestinationAmount),
]
this.upscaleTokenA(sourceAmount),
this.upscaleTokenA(swapSourceAmount),
this.upscaleTokenB(swapDestinationAmount),
]
: [
this.upscaleTokenB(sourceAmount),
this.upscaleTokenB(swapSourceAmount),
this.upscaleTokenA(swapDestinationAmount),
];
this.upscaleTokenB(sourceAmount),
this.upscaleTokenB(swapSourceAmount),
this.upscaleTokenA(swapDestinationAmount),
];

const invariantD = computeD(
BigInt(this.amp),
Expand Down Expand Up @@ -188,15 +187,15 @@ export class StableSwap implements SwapCurve {
const [upscaledDestAmount, upscaledSwapSourceAmount, upscaledSwapDestinationAmount] =
tradeDirection == TradeDirection.AToB
? [
this.upscaleTokenB(destAmount),
this.upscaleTokenA(swapSourceAmount),
this.upscaleTokenB(swapDestinationAmount),
]
this.upscaleTokenB(destAmount),
this.upscaleTokenA(swapSourceAmount),
this.upscaleTokenB(swapDestinationAmount),
]
: [
this.upscaleTokenA(destAmount),
this.upscaleTokenB(swapSourceAmount),
this.upscaleTokenA(swapDestinationAmount),
];
this.upscaleTokenA(destAmount),
this.upscaleTokenB(swapSourceAmount),
this.upscaleTokenA(swapDestinationAmount),
];

const invariantD = computeD(
BigInt(this.amp),
Expand Down Expand Up @@ -344,14 +343,14 @@ function calculateEstimatedWithdrawOneAmount({
tradeDirection == TradeDirection.BToA ? [reserves[0], reserves[1]] : [reserves[1], reserves[0]];

const d_0 = computeD(ampFactor, baseReserves, quoteReserves);
const d_1 = d_0 - poolTokenAmount * d_0 / lpTotalSupply;
const d_1 = d_0 - (poolTokenAmount * d_0) / lpTotalSupply;

const new_y = computeY(ampFactor, quoteReserves, d_1);

// expected_base_amount = swap_base_amount * d_1 / d_0 - new_y;
const expected_base_amount = baseReserves * d_1 / d_0 - new_y;
const expected_base_amount = (baseReserves * d_1) / d_0 - new_y;
// expected_quote_amount = swap_quote_amount - swap_quote_amount * d_1 / d_0;
const expected_quote_amount = quoteReserves - quoteReserves * d_1 / d_0;
const expected_quote_amount = quoteReserves - (quoteReserves * d_1) / d_0;
// new_base_amount = swap_base_amount - expected_base_amount * fee / fee_denominator;
const new_base_amount = new Fraction(baseReserves.toString(), 1).subtract(
normalizedTradeFee(feeInfo, N_COINS, expected_base_amount),
Expand Down Expand Up @@ -433,8 +432,8 @@ function calculateEstimatedMintAmount(
const d2 = computeD(amp, adjustedBalances[0], adjustedBalances[1]);

const lpSupply = lpTotalSupply;
const mintAmountRaw = lpSupply * (d2 - d0) / d0;
const mintAmountRawBeforeFees = lpSupply * (d1 - d0) / d0;
const mintAmountRaw = (lpSupply * (d2 - d0)) / d0;
const mintAmountRawBeforeFees = (lpSupply * (d1 - d0)) / d0;

const fees = mintAmountRawBeforeFees - mintAmountRaw;

Expand Down
5 changes: 2 additions & 3 deletions ts-client/src/amm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ import {
} from './utils';
import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes';
import Decimal from 'decimal.js';
import sqrt from 'bn-sqrt';
import { DataV2 } from '@metaplex-foundation/mpl-token-metadata';

type Opt = {
Expand Down Expand Up @@ -525,7 +524,7 @@ export default class AmmImpl implements AmmImplementation {
const [mintMetadata, _mintMetadataBump] = deriveMintMetadata(lpMint);
const activationPoint = opt?.activationPoint || null;

const lpAmount = sqrt(tokenAAmount.mul(tokenBAmount));
const lpAmount = new BN(new Decimal(tokenAAmount.mul(tokenBAmount).toString()).floor().toString());
const { userLockAmount, feeWrapperLockAmount } = calculateLockAmounts(lpAmount, opt?.stakeLiquidity?.ratio);

const createPoolPostInstructions: TransactionInstruction[] = [];
Expand Down Expand Up @@ -1084,7 +1083,7 @@ export default class AmmImpl implements AmmImplementation {
}

const [mintMetadata, _mintMetadataBump] = deriveMintMetadata(lpMint);
const lpAmount = sqrt(tokenAAmount.mul(tokenBAmount));
const lpAmount = new BN(new Decimal(tokenAAmount.mul(tokenBAmount).toString()).floor().toString());
const { userLockAmount, feeWrapperLockAmount } = calculateLockAmounts(lpAmount, opt?.stakeLiquidity?.ratio);

const createPoolPostInstructions: TransactionInstruction[] = [];
Expand Down
67 changes: 0 additions & 67 deletions ts-client/src/amm/marinade-finance.json
Original file line number Diff line number Diff line change
Expand Up @@ -1788,73 +1788,6 @@
}
]
}
},
{
"name": "InitializeError",
"type": {
"kind": "enum",
"variants": [
{
"name": "WrongReserveOwner",
"fields": ["publicKey"]
},
{
"name": "NonEmptyReserveData",
"fields": [
{
"defined": "usize"
}
]
},
{
"name": "InvalidInitialReserveLamports",
"fields": ["u64"]
},
{
"name": "ZeroValidatorChunkSize"
},
{
"name": "TooBigValidatorChunkSize",
"fields": ["u32"]
},
{
"name": "ZeroCreditChunkSize"
},
{
"name": "TooBigCreditChunkSize",
"fields": ["u32"]
},
{
"name": "TooLowCreditFee",
"fields": ["u64"]
},
{
"name": "InvalidMintAuthority",
"fields": [
{
"name": "expected",
"type": "publicKey"
},
{
"name": "got",
"type": "publicKey"
}
]
},
{
"name": "MintHasInitialSupply",
"fields": ["u64"]
},
{
"name": "InvalidOwnerFeeState",
"fields": [
{
"defined": "spl_token::state::AccountState"
}
]
}
]
}
}
]
}
2 changes: 1 addition & 1 deletion ts-client/src/examples/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function main() {
const poolAddress = 'Bgf1Sy5kfeDgib4go4NgzHuZwek8wE8NZus56z6uizzi';

// swap 5 9NG token to SOL
// await swap(new PublicKey(poolAddress), new BN(5000_000_000), true);
await swap(new PublicKey(poolAddress), new BN(5000_000_000), true);

// swap 0.01 SOL to 9NG token
await swap(new PublicKey(poolAddress), new BN(10_000_000), false);
Expand Down