Momentum 백서

Momentum

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 18쪽 중 7쪽

on></figure> You can adjust the **Maximum Slippage** in Momentum by clicking the settings button in the top right corner of the Swap menu. <figure><img src="https://1760493472-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvMYfd5Y4I2ZxHbqdOD88%2Fuploads%2FyHJWirRS426siN8roqoH%2Fswap04.png?alt=media&#x26;token=92a08208-3ffe-484a-b2d7-01f1a1db15b8" alt=""><figcaption><p>Slippage Adjustment</p></figcaption></figure> Once everything is configured, click **"Swap Now"** to proceed. <figure><img src="https://1760493472-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvMYfd5Y4I2ZxHbqdOD88%2Fuploads%2FaVj846C7PYbNTPZ3sxHC%2Fswap05.png?alt=media&#x26;token=33b0676f-299f-4c18-a61a-187943f13147" alt=""><figcaption><p>Swap Now</p></figcaption></figure> Confirm the transaction in your wallet and transaction will be complete within seconds!\\ <figure><img src="https://1760493472-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvMYfd5Y4I2ZxHbqdOD88%2Fuploads%2FIr4pGqWZvVsOkTq2onmW%2Fswap06.png?alt=media&#x26;token=edf51d7d-ab9f-4256-b5bf-8c0141578b98" alt=""><figcaption><p>Confirm Swap</p></figcaption></figure> # Adding Liquidity You can access Liquidity Pools page of Momentum by visiting <https://app.mmt.finance/liquidity>, or by clicking on the **"Liquidity"** button in in the header in the top left. <figure><img src="https://1760493472-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvMYfd5Y4I2ZxHbqdOD88%2Fuploads%2FaUj3swd4fnhEvDYluhaJ%2Faddliq1.png?alt=media&#x26;token=87ee8bc4-fda1-4b8c-82a0-1e5c5bbc6f32" alt=""><figcaption></figcaption></figure> Find the token pair you want to provide liquidity for, then click the **"Add Liquidity"** button on the right-hand side. <figure><img src="https://1760493472-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvMYfd5Y4I2ZxHbqdOD88%2Fuploads%2FFIln1Fx09QTqNUo18TmZ%2Fimage.png?alt=media&#x26;token=2e020411-b5b9-4ccc-ac9b-36a847652e0e" alt=""><figcaption></figcaption></figure> Set your **Price Range** by either:\ A) Selecting a **±% Preset**,\ B) Dragging the sliders in the liquidity chart, or\ C) Manually entering values at the bottom. <figure><img src="https://1760493472-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvMYfd5Y4I2ZxHbqdOD88%2Fuploads%2FQs3z6bhPKEtBmPPZlex0%2Fimage.png?alt=media&#x26;token=acafdc86-828a-43a6-bb16-7031863a9c93" alt=""><figcaption><p>Set Price Range</p></figcaption></figure> To deposit a single token, enable the **"Single Token Deposit"** slider in the **Set Deposit Amounts** menu.\ Enter the amount of tokens you want to deposit and click **"Add Liquidity"**. <figure><img src="https://1760493472-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvMYfd5Y4I2ZxHbqdOD88%2Fuploads%2FDnx2dOPPBlwTFtBHz3yd%2Fimage.png?alt=media&#x26;token=64b23f62-de94-469b-b5fe-08a70f266c6d" alt=""><figcaption></figcaption></figure> Click **"Confirm"** once you receive the prompt from your wallet. <figure><img src="https://1760493472-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvMYfd5Y4I2ZxHbqdOD88%2Fuploads%2FWoi2YsFvv4Qox7p2RkeO%2Fimage.png?alt=media&#x26;token=0d1168ef-6043-4f83-a06c-08efc8b661bc" alt=""><figcaption></figcaption></figure> You’ll receive a notification in the bottom right confirming that you’ve successfully added liquidity. Congratulations! <figure><img src="https://1760493472-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvMYfd5Y4I2ZxHbqdOD88%2Fuploads%2FtifR6XvqUQODLHCc3KaC%2Fimage.png?alt=media&#x26;token=446713e0-0f16-47e3-b687-ed904bb3072d" alt=""><figcaption></figcaption></figure> # Developers # CLMM SDK # Overview MOMENTUM Finance offers official SDK to allow developers to seamlessly integrate with MOMENTUM CLMM Dex. You shall find @mmt-finance/clmm-sdk in both npmjs and github. {% embed url="<https://www.npmjs.com/package/@mmt-finance/clmm-sdk>" %} {% embed url="<https://github.com/mmt-finance/clmm-sdk>" %} To check the latest deployment and contract address, please visit # Getting Started #### Installation ``` npm install @mmt-finance/clmm-sdk ``` #### SDK Initialization Our SDK has pre-configured network settings that allows you to connect to MOMENTUM CLMM on both mainnet and testnet. Use `MmtSDK.NEW` method to swiftly initialize the configuration. ``` import { MmtSDK } from '@mmt-finance/clmm-sdk'; const sdk = MmtSDK.NEW({ network: 'mainnet', }); ``` Now, you can start using MMT SDK. # Features {% content-ref url="/pages/edCNGlidtIJSSVb5CvQc" %} [Read Pool Data](/core-products/momentum-dex/developers/clmm-sdk/features/read-pool-data) {% endcontent-ref %} {% content-ref url="/pages/DnSV9kkhpAgY9g7XF322" %} [Read User Data](/core-products/momentum-dex/developers/clmm-sdk/features/read-user-data) {% endcontent-ref %} {% content-ref url="/pages/x2gUW8O5S6gAevNpgEdn" %} [Swap](/core-products/momentum-dex/developers/clmm-sdk/features/swap) {% endcontent-ref %} {% content-ref url="/pages/1UvIEqhXqn1MrK6gcYXZ" %} [Open and Add Liquidity](/core-products/momentum-dex/developers/clmm-sdk/features/open-and-add-liquidity) {% endcontent-ref %} {% content-ref url="/pages/d3PEtNEUcu3VamLG6XTa" %} [Position Management](/core-products/momentum-dex/developers/clmm-sdk/features/position-management) {% endcontent-ref %} {% content-ref url="/pages/vpqI98v5C33UO8DreIon" %} [Claim Fees and Rewards](/core-products/momentum-dex/developers/clmm-sdk/features/claim-fees-and-rewards) {% endcontent-ref %} {% content-ref url="/pages/e2LgJjPSSHzt7JcCsaP3" %} [Close Position](/core-products/momentum-dex/developers/clmm-sdk/features/close-position) {% endcontent-ref %} # Read Pool Data ## #### Initialize SDK ```typescript import { MmtSDK } from '@mmt-finance/clmm-sdk'; const sdk = MmtSDK.NEW({ network: 'mainnet', }); ``` #### Read all available pools ```typescript const pools = await sdk.Pool.getAllPools(); ``` #### Read info for a certain pool ```typescript const poolId = '0x...'; const poolInfo = await sdk.Pool.getPool(poolId); ``` #### Retrieve tick info for a certain pool ```typescript const poolId = '0x...'; const tickInfo = await sdk.Pool.fetchAllTickLiquidities(poolId); ``` #### Fetch incentive rewards for a certain pool ```typescript const poolId = '0x...'; const rewarderInfo = await sdk.Pool.fetchRewardersApy(poolId); ``` #### Retrieve all supported tokens ```typescript const tokens = await sdk.Pool.getAllTokens(); ``` # Read User Data ## #### Initialize SDK ```typescript import { MmtSDK } from '@mmt-finance/clmm-sdk'; const sdk = MmtSDK.NEW({ network: 'mainnet', }); ``` #### Read all user positions ```typescript const userAddress = '0x...'; const positions = await sdk.Position.getAllUserPositions(userAddress); ``` #### Read all user rewards (incentives and fees) ```typescript const userAddress = '0x...'; const pools: ExtendedPool[] = await sdk.Pool.getAllPools(); const objects = await fetchUserObjectsByPkg( sdk.rpcClient, sdk.contractConst.publishedAt, userAddress, ); const positions = objects.filter( (obj: any) => obj.type === `${sdk.PackageId}::position::Position`, ); const rewardsAndFees = await sdk.Position.fetchRewards( positions, pools, userAddress, sdk.rpcClient, ); ``` #### Read user rewards for a position (incentives and fees) ```typescript const positionId = '0x...'; const userAddress = '0x...'; const poolId = '0x...'; const pool = await sdk.poolModule.getPool(poolId); const rewardsAndFees = await sdk.Position.fetchAllRewards( positionId, userAddress, pool, ); ``` #### Read info from a position ```typescript const positionId = '0x...'; const liquidity = await sdk.Position.getLiquidity(positionId); const lowerTick = await sdk.Position.getTickLowerIndex(positionId); const upperTick = await sdk.Position.getTickUpperIndex(positionId); ``` # Swap ## #### Initialize SDK ```typescript import { MmtSDK } from '@mmt-finance/clmm-sdk'; const sdk = MmtSDK.NEW({ network: 'mainnet', }); ``` #### Execute swap (x to y) If swapping token from `TokenXType` to `TokenYType` for a certain pool ```typescript import { Transaction } from '@mysten/sui/transactions'; import { TickMath } from '@mmt-finance/clmm-sdk'; const tx = new Transaction(); const userAddress = '0x...'; // Optional, prepare the coin to be transferred tx.mergeCoins(...); // Prepare the swap data const limitSqrtPrice = TickMath.priceToSqrtPriceX64( new Decimal(priceLimit), // Price limit after slippage decimalX, // decimal of token X decimalY, // deciaml of token Y ) const swapAmount = BigInt(...) // Number of token X to be swapped // Execute swap in tx sdk.Pool.swap( tx, { objectId: poolId, tokenXType, tokenYType, tickSpacing, }, swapAmount, coin, // Prepared coin in previous tx true, userAddress, limitSqrtPrice, ) // Execute the transaction const res = await suiClient.signAndExecuteTransaction({ signer, transaction: tx, }); const fin = await suiClient.waitForTransaction({ digest: res.digest }); ``` #### Execute swap (y to x) ```typescript import { Transaction } from '@mysten/sui/transactions'; import { TickMath } from '@mmt-finance/clmm-sdk'; const tx = new Transaction(); const userAddress = '0x...'; // Prepare the coin to be transferred const [coin] = tx.splitCoins(...); // Prepare the swap data const limitSqrtPrice = TickMath.priceToSqrtPriceX64( new Decimal(priceLimit), // Price limit after slippage decimalX, // decimal of token Y decimalY, // deciaml of token X ) const swapAmount = BigInt(...) // Number of token X to be swapped // Execute swap in tx sdk.Pool.swap( tx, { objectId: poolId, tokenXType, tokenYType, tickSpacing, }, swapAmount, coin, // Prepared coin in previous tx false, userAddress, limitSqrtPrice, ) // Execute the transaction const res = await suiClient.signAndExecuteTransaction({ signer, transaction: tx, }); const fin = await suiClient.waitForTransaction({ digest: res.digest }); ``` # Open and Add Liquidity ## #### Initialize liquidity ```typescript import { MmtSDK } from '@mmt-finance/clmm-sdk'; const sdk = MmtSDK.NEW({ network: 'mainnet', }); ``` #### Open position and add liquidity(The number of coinX is fixed) ```typescript import { TickMath } from '@mmt-finance/clmm-sdk'; import { Transaction } from '@mysten/sui/transactions'; const tx = new Transaction(); // Compute lower and upper price const lowerSqrtPrice = TickMath.priceToSqrtPriceX64(new Decimal(lowerPrice), decimalX, decimalY); const upperSqrtPrice = TickMath.priceToSqrtPriceX64(new Decimal(upperPrice), decimalX, decimalY); // Estimate liquidity using the given price range and amount const liquidity = estimateLiquidityForCoinA(upperSqrtPrice, lowerSqrtPrice, new BN(amontX)); // Calculate the required coin amounts based on estimated liquidity const coinAmounts = getCoinAmountFromLiquidity( liquidity, new BN(currentSqrtPrice), lowerSqrtPrice, upperSqrtPrice, false, ); // Get the coinX split coin const coinA = prepareSplitCoin(coinAmounts.coinX) // Get the coinY split coin const coinB = prepareSplitCoin(coinAmounts.coinY) // Open position const position = positionModule.openPosition( tx, { objectId: poolId, tokenXType: coinAType, tokenYType: coinBType, tickSpacing: tickSpacing, }, lowerSqrtPrice.toString(), upperSqrtPrice.toString(), ); // Add liquidity poolModule.addLiquidity( tx, { objectId: poolId, tokenXType: coinAType, tokenYType: coinBType, tickSpacing: tickSpacing, }, position, // Position from previous tx coinA, coinB, BigInt(minAddAmountA), // Min a added BigInt(minAddAmountB), // Min b added senderAddress, ); // Transfer position back to sender tx.transferObjects([position], senderAddress); // Execute transaction const result = await client.signAndExecuteTransaction({ signer: keypair, transaction: tx }); const responce = await client.waitForTransaction({ digest: result.digest }); ``` ```typescript export function estimateLiquidityForCoinA( sqrtPriceX: BN, // Square root price of token X sqrtPriceY: BN, // Square root price of token Y coinAmount: BN // Amount of token A (in BN) ) { // Determine the lower and upper square root price boundaries const lowerSqrtPriceX64 = BN.min(sqrtPriceX, sqrtPriceY); const upperSqrtPriceX64 = BN.max(sqrtPriceX, sqrtPriceY); // Compute the numerator: coinAmount * upperPrice * lowerPrice const num = MathUtil.fromX64_BN(coinAmount.mul(upperSqrtPriceX64).mul(lowerSqrtPriceX64)); // Compute the denominator: upperPrice - lowerPrice const dem = upperSqrtPriceX64.sub(lowerSqrtPriceX64); // Return the estimated liquidity return num.div(dem); } ``` ```typescript export function getCoinAmountFromLiquidity( liquidity: BN, // Total liquidity provided curSqrtPrice: BN, // Current square root price of the pool lowerSqrtPrice: BN, // Lower bound of the price range upperSqrtPrice: BN, // Upper bound of the price range roundUp: boolean, // Whether to round up the result ): { coinX: string; // Amount of token X coinY: string; // Amount of token Y } { // Convert BN values to Decimal for precise calculations const liq = new Decimal(liquidity.toString()); const curSqrtPriceStr = new Decimal(curSqrtPrice.toString()); const lowerPriceStr = new Decimal(lowerSqrtPrice.toString()); const upperPriceStr = new Decimal(upperSqrtPrice.toString()); let coinX; let coinY; // Case 1: Current price is below the lower price, only token X is required if (curSqrtPrice.lt(lowerSqrtPrice)) { coinX = MathUtil.toX64_Decimal(liq) .mul(upperPriceStr.sub(lowerPriceStr)) .div(lowerPriceStr.mul(upperPriceStr)); coinY = new Decimal(0); // No token Y is needed // Case 2: Current price is within the range, both token X and token Y are required } else if (curSqrtPrice.lt(upperSqrtPrice)) { coinX = MathUtil.toX64_Decimal(liq) .mul(upperPriceStr.sub(curSqrtPriceStr)) .div(curSqrtPriceStr.mul(upperPriceStr)); coinY = MathUtil.fromX64_Decimal(liq.mul(curSqrtPriceStr.sub(lowerPriceStr))); // Case 3: Current price is above the upper price, only token Y is required } else { coinX = new Decimal(0); // No token X is needed coinY = MathUtil.fromX64_Decimal(liq.mul(upperPriceStr.sub(lowerPriceStr))); } // Round up or round down based on the parameter if (roundUp) { return { coinX: coinX.ceil().toString(), coinY: coinY.ceil().toString(), }; } return { coinX: coinX.floor().toString(), coinY: coinY.floor().toString(), }; } ``` # Position Management ## #### Add Liquidity ```typescript import { MmtSDK } from '@mmt-finance/clmm-sdk'; import { Transaction } from '@mysten/sui/transactions'; const sdk = MmtSDK.NEW({ network: 'mainnet', }); const poolParams = { objectId: poolId, tokenXType, tokenYType }; const tx = new Transaction(); // Optionally merge coins tx.mergeCoins(...); sdk.Pool.addLiquidity( tx, poolParams, positionId, coinX, coinY, minX, // Min X amount based on slippage settings minY, // Min Y amount based on slippage settings userAddress, ); // Execute transaction const result = await client.signAndExecuteTransaction({ signer: keypair, transaction: tx }); const responce = await client.waitForTransaction({ digest: result.digest }); ``` #### Remove liquidity ```typescript import { MmtSDK } from '@mmt-finance/clmm-sdk'; import { Transaction } from '@mysten/sui/transactions'; const sdk = MmtSDK.NEW({ network: 'mainnet', }); const poolParams = { objectId: poolId, tokenXType, tokenYType }; const liquidity = await sdk.Position.getLiquidity(positionId); const tx = new Transaction(); sdk.Pool.removeLiquidity( tx, poolParams, positionId, removeLiquidity, // < liquidity minX, // Min X amount based on slippage settings minY, // Min Y amount based on slippage settings userAddress, ); // Execute transaction const result = await client.signAndExecuteTransaction({ signer: keypair, transaction: tx }); const responce = await client.waitForTransaction({ digest: result.digest }); ``` # Claim Fees and Rewards ## #### Claim all fees and rewards ```typescript import { MmtSDK } from '@mmt-finance/clmm-sdk'; const sdk = MmtSDK.NEW({ network: 'mainnet', }); const userAddress = '0x...'; const pools = await sdk.Pool.getAllPools(); const tx = new Transaction(userAddress, pools); await sdk.Pool.collectAllPoolsRewards(userAddress, pools); // Execute transaction const result = await client.signAndExecuteTransaction({ signer: keypair, transaction: tx }); const responce = await client.waitForTransaction({ digest: result.digest }); ``` # Close Position ## #### Close Position ```typescript import { MmtSDK } from '@mmt-finance/clmm-sdk'; import { Transaction } from '@mysten/sui/transactions'; const sdk = MmtSDK.NEW({ network: 'mainnet', }); // Prepare data const poolParams = { objectId: poolId, tokenXType, tokenYType }; const liquidity = await sdk.Position.getLiquidity(positionId); const pool = await sdk.poolModule.getPool(poolId); const rewarders = pool.rewarders; const tx = new Transaction(); // Remove liquidity await sdk.Pool.removeLiquidity( tx, poolParams, positionId, liquidity, min_amount_x, // Compute based on slippage settings min_amount_y, // Compute based on slippage settings userAddress, ); // Claim rewards and fees if (position.rewarders && position.rewarders.length !== 0) { await sdk.Pool.collectAllRewards(tx, poolParams, rewarders, positionId, userAddress); } sdk.Pool.collectFee(tx, poolParams, positionId, userAddress); // Close position await sdk.Position.closePosition(tx, positionId); // Execute transaction const result = await client.signAndExecuteTransaction({ signer: keypair, transaction: tx }); const responce = await client.waitForTransaction({ digest: result.digest }); ``` # Examples ## A comprehensive set of examples has been provided to demonstrate the fundamental usage of the SDK. Please refer to the [detailed guidelines](https://github.com/mmt-finance/clmm-sdk/tree/main/examples) for further information {% embed url="<https://github.com/mmt-finance/clmm-sdk/blob/main/examples/README.md>" %} A set of typescript scripts are provided to walk through the basic usage of @mmt-finance/clmm-sdk. **Running an example** Install dependencies ```typescript yarn ``` Run a specific example (E.g. swap.ts) ```typescript yarn scripts examples/swap.ts ``` **Execute transaction** By default, transaction is evaluated in dryRun mode. To execute transaction, update the script as follows: ```typescript const mnemonic = ''; // Replace mnemonic here const signer = Ed25519Keypair.deriveKeypair(mnemonic); // Define the user's mnemonic (should be replaced with an actual mnemonic) const resp = await executeTxExample({ tx, sdk, execution: { dryrun: false, signer: signer }, }); ``` **Set network to Mainnet** Configure SDK to run on Mainnet network: ```typescript const sdk = MmtSDK.NEW({ network: 'mainnet' }); ``` \\ # CLMM Smart Contracts {% content-ref url="/pages/Y4AjqHqLCY1b9i6L4bSR" %} [Deployments](/core-products/momentum-dex/developers/clmm-smart-contracts/deployments) {% endcontent-ref %} {% content-ref url="/pages/8H9STQlXPBy4teYh8jQK" %} [Contract Interface](/core-products/momentum-dex/developers/clmm-smart-contracts/contract-interface) {% endcontent-ref %} {% content-ref url="/pages/q7xi8kdcAu55IrvZ6tC0" %} [Contract Event](/core-products/momentum-dex/developers/clmm-smart-contracts/contract-event) {% endcontent-ref %} {% content-ref url="/pages/u5GdzdPwF2ZdpoE8GXOK" %} [Admin and Operations](/core-products/momentum-dex/developers/clmm-smart-contracts/admin-and-operations) {% endcontent-ref %} # Deployments ## Contract Addresses | Role | ObjectId | | --------------- | ------------------------------------------------------------------ | | ClmmPackageId | 0x70285592c97965e811e0c6f98dccc3a9c2b4ad854b3594faab9597ada267b860 | | ClmmPublishedAt | 0xcf60a40f45d46fc1e828871a647c1e25a0915dec860d2662eb10fdb382c3c1d1 | | Acl | 0xe7f25417d58cd086a64e3daed2ef04d065b606458becce3
프로젝트로 돌아가기