@magmaprotocol/magma-clmm-sdk 0.5.34 → 0.5.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -303,6 +303,7 @@ var Voter = "voter";
303
303
  var RewardDistributor = "reward_distributor";
304
304
  var Gauge = "gauge";
305
305
  var Minter = "minter";
306
+ var DlmmScript = "dlmm_script";
306
307
  var CoinInfoAddress = "0x1::coin::CoinInfo";
307
308
  var CoinStoreAddress = "0x1::coin::CoinStore";
308
309
  var DeepbookCustodianV2Moudle = "custodian_v2";
@@ -9846,6 +9847,277 @@ var GaugeModule = class {
9846
9847
  }
9847
9848
  };
9848
9849
 
9850
+ // src/modules/dlmm.ts
9851
+ import { Transaction as Transaction12 } from "@mysten/sui/transactions";
9852
+ import Decimal7 from "decimal.js";
9853
+ import { get_real_id_from_price, get_storage_id_from_real_id } from "@magmaprotocol/calc_dlmm";
9854
+ var DlmmModule = class {
9855
+ _sdk;
9856
+ constructor(sdk) {
9857
+ this._sdk = sdk;
9858
+ }
9859
+ get sdk() {
9860
+ return this._sdk;
9861
+ }
9862
+ // eg: fetch pool active_index
9863
+ async fetchPairParams(params) {
9864
+ const tx = new Transaction12();
9865
+ const { integrate, simulationAccount } = this.sdk.sdkOptions;
9866
+ const typeArguments = [params.coinTypeA, params.coinTypeB];
9867
+ const args = [tx.object(params.pair)];
9868
+ tx.moveCall({
9869
+ target: `${integrate.published_at}::${DlmmScript}::fetch_pair_params`,
9870
+ arguments: args,
9871
+ typeArguments
9872
+ });
9873
+ const simulateRes = await this.sdk.fullClient.devInspectTransactionBlock({
9874
+ transactionBlock: tx,
9875
+ sender: simulationAccount.address
9876
+ });
9877
+ if (simulateRes.error != null) {
9878
+ throw new Error(`fetchBins error code: ${simulateRes.error ?? "unknown error"}`);
9879
+ }
9880
+ let res = {
9881
+ base_factor: 0,
9882
+ filter_period: 0,
9883
+ decay_period: 0,
9884
+ reduction_factor: 0,
9885
+ variable_fee_control: 0,
9886
+ protocol_share: 0,
9887
+ max_volatility_accumulator: 0,
9888
+ volatility_accumulator: 0,
9889
+ volatility_reference: 0,
9890
+ index_reference: 0,
9891
+ time_of_last_update: 0,
9892
+ oracle_index: 0,
9893
+ active_index: 0
9894
+ };
9895
+ simulateRes.events?.forEach((item) => {
9896
+ console.log(extractStructTagFromType(item.type).name);
9897
+ if (extractStructTagFromType(item.type).name === `EventPairParams`) {
9898
+ res = item.parsedJson.params;
9899
+ }
9900
+ });
9901
+ return res;
9902
+ }
9903
+ // params price: input (b/(10^b_decimal))/(a/(10^a_decimal))
9904
+ price_to_storage_id(price, bin_step, tokenADecimal, tokenBDecimal) {
9905
+ const priceDec = new Decimal7(price);
9906
+ const tenDec = new Decimal7(10);
9907
+ const twoDec = new Decimal7(2);
9908
+ const price_x128 = priceDec.mul(tenDec.pow(tokenBDecimal)).div(tenDec.pow(tokenADecimal)).mul(twoDec.pow(128));
9909
+ const active_id = get_real_id_from_price(price_x128.toFixed(0).toString(), bin_step);
9910
+ return get_storage_id_from_real_id(active_id);
9911
+ }
9912
+ // NOTE: x, y should be sorted
9913
+ async createPairPayload(params) {
9914
+ const storage_id = this.price_to_storage_id(params.priceTokenBPerTokenA, params.bin_step, params.coinADecimal, params.coinBDecimal);
9915
+ const tx = new Transaction12();
9916
+ tx.setSender(this.sdk.senderAddress);
9917
+ const { clmm_pool, dlmm_pool, integrate } = this.sdk.sdkOptions;
9918
+ const { global_config_id } = getPackagerConfigs(clmm_pool);
9919
+ const dlmmConfig = getPackagerConfigs(dlmm_pool);
9920
+ const typeArguments = [params.coinTypeA, params.coinTypeB];
9921
+ const args = [tx.object(dlmmConfig.factory), tx.object(global_config_id), tx.pure.u16(params.bin_step), tx.pure.u32(storage_id)];
9922
+ tx.moveCall({
9923
+ target: `${integrate.published_at}::${DlmmScript}::create_pair`,
9924
+ typeArguments,
9925
+ arguments: args
9926
+ });
9927
+ return tx;
9928
+ }
9929
+ async createAddLiquidityPayload() {
9930
+ const tx = new Transaction12();
9931
+ return tx;
9932
+ }
9933
+ // Create a position by percent
9934
+ async mintPercent(params) {
9935
+ const tx = new Transaction12();
9936
+ tx.setSender(this.sdk.senderAddress);
9937
+ const { dlmm_pool, integrate } = this.sdk.sdkOptions;
9938
+ const dlmmConfig = getPackagerConfigs(dlmm_pool);
9939
+ const typeArguments = [params.coinTypeA, params.coinTypeB];
9940
+ const allCoins = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
9941
+ const primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(params.amountATotal), params.coinTypeA, false, true);
9942
+ const primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(params.amountBTotal), params.coinTypeB, false, true);
9943
+ const args = [
9944
+ tx.object(params.pair),
9945
+ tx.object(dlmmConfig.factory),
9946
+ primaryCoinAInputs.targetCoin,
9947
+ primaryCoinBInputs.targetCoin,
9948
+ tx.pure.u64(params.amountATotal),
9949
+ tx.pure.u64(params.amountBTotal),
9950
+ tx.pure.vector("u32", params.storageIds),
9951
+ tx.pure.vector("u64", params.binsAPercent),
9952
+ tx.pure.vector("u64", params.binsBPercent),
9953
+ tx.pure.address(params.to),
9954
+ tx.object(CLOCK_ADDRESS)
9955
+ ];
9956
+ tx.moveCall({
9957
+ target: `${integrate.published_at}::${DlmmScript}::mint_percent`,
9958
+ typeArguments,
9959
+ arguments: args
9960
+ });
9961
+ return tx;
9962
+ }
9963
+ // Create a position by amount
9964
+ async createPositionByAmount(params) {
9965
+ const tx = new Transaction12();
9966
+ tx.setSender(this.sdk.senderAddress);
9967
+ const { dlmm_pool, integrate } = this.sdk.sdkOptions;
9968
+ const dlmmConfig = getPackagerConfigs(dlmm_pool);
9969
+ const typeArguments = [params.coinTypeA, params.coinTypeB];
9970
+ const allCoins = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
9971
+ const primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(params.amountATotal), params.coinTypeA, false, true);
9972
+ const primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoins, BigInt(params.amountBTotal), params.coinTypeB, false, true);
9973
+ const args = [
9974
+ tx.object(params.pair),
9975
+ tx.object(dlmmConfig.factory),
9976
+ primaryCoinAInputs.targetCoin,
9977
+ primaryCoinBInputs.targetCoin,
9978
+ tx.pure.vector("u32", params.storageIds),
9979
+ tx.pure.vector("u64", params.amountsA),
9980
+ tx.pure.vector("u64", params.amountsB),
9981
+ tx.pure.address(params.to),
9982
+ tx.object(CLOCK_ADDRESS)
9983
+ ];
9984
+ tx.moveCall({
9985
+ target: `${integrate.published_at}::${DlmmScript}::mint_amounts`,
9986
+ typeArguments,
9987
+ arguments: args
9988
+ });
9989
+ return tx;
9990
+ }
9991
+ async swap(params) {
9992
+ const tx = new Transaction12();
9993
+ tx.setSender(this.sdk.senderAddress);
9994
+ const { clmm_pool, integrate } = this.sdk.sdkOptions;
9995
+ const { global_config_id } = getPackagerConfigs(clmm_pool);
9996
+ const typeArguments = [params.coinTypeA, params.coinTypeB];
9997
+ const args = [
9998
+ tx.object(params.pair),
9999
+ tx.object(global_config_id),
10000
+ tx.object(params.coinTypeA),
10001
+ tx.object(params.coinTypeB),
10002
+ tx.pure.u64(params.amountIn),
10003
+ tx.pure.u64(params.minAmountOut),
10004
+ tx.pure.bool(params.swapForY),
10005
+ tx.pure.address(params.to),
10006
+ tx.object(CLOCK_ADDRESS)
10007
+ ];
10008
+ tx.moveCall({
10009
+ target: `${integrate.published_at}::${DlmmScript}::swap`,
10010
+ typeArguments,
10011
+ arguments: args
10012
+ });
10013
+ return tx;
10014
+ }
10015
+ async fetchBins(params) {
10016
+ const tx = new Transaction12();
10017
+ const { integrate, simulationAccount } = this.sdk.sdkOptions;
10018
+ const typeArguments = [params.coinTypeA, params.coinTypeB];
10019
+ const args = [tx.object(params.pair), tx.pure.u64(params.offset), tx.pure.u64(params.limit)];
10020
+ tx.moveCall({
10021
+ target: `${integrate.published_at}::${DlmmScript}::fetch_bins`,
10022
+ arguments: args,
10023
+ typeArguments
10024
+ });
10025
+ const simulateRes = await this.sdk.fullClient.devInspectTransactionBlock({
10026
+ transactionBlock: tx,
10027
+ sender: simulationAccount.address
10028
+ });
10029
+ if (simulateRes.error != null) {
10030
+ throw new Error(`fetchBins error code: ${simulateRes.error ?? "unknown error"}`);
10031
+ }
10032
+ const res = [];
10033
+ simulateRes.events?.forEach((item) => {
10034
+ if (extractStructTagFromType(item.type).name === `EventFetchBins`) {
10035
+ }
10036
+ });
10037
+ return res;
10038
+ }
10039
+ async getPositionLiquidity(params) {
10040
+ const tx = new Transaction12();
10041
+ const { integrate, simulationAccount } = this.sdk.sdkOptions;
10042
+ const typeArguments = [params.coinTypeA, params.coinTypeB];
10043
+ const args = [tx.object(params.pair), tx.object(params.positionId)];
10044
+ tx.moveCall({
10045
+ target: `${integrate.published_at}::${DlmmScript}::position_liquidity`,
10046
+ arguments: args,
10047
+ typeArguments
10048
+ });
10049
+ const simulateRes = await this.sdk.fullClient.devInspectTransactionBlock({
10050
+ transactionBlock: tx,
10051
+ sender: simulationAccount.address
10052
+ });
10053
+ if (simulateRes.error != null) {
10054
+ throw new Error(`fetchBins error code: ${simulateRes.error ?? "unknown error"}`);
10055
+ }
10056
+ const out = {
10057
+ shares: 0,
10058
+ liquidity: 0,
10059
+ x_equivalent: 0,
10060
+ y_equivalent: 0,
10061
+ bin_ids: [],
10062
+ bin_x_eq: [],
10063
+ bin_y_eq: [],
10064
+ bin_liquidity: []
10065
+ };
10066
+ simulateRes.events?.forEach((item) => {
10067
+ if (extractStructTagFromType(item.type).name === `EventPositionLiquidity`) {
10068
+ out.shares = item.parsedJson.shares;
10069
+ out.liquidity = item.parsedJson.liquidity;
10070
+ out.x_equivalent = item.parsedJson.x_equivalent;
10071
+ out.y_equivalent = item.parsedJson.y_equivalent;
10072
+ out.bin_ids = item.parsedJson.bin_id;
10073
+ out.bin_x_eq = item.parsedJson.bin_x_eq;
10074
+ out.bin_y_eq = item.parsedJson.bin_y_eq;
10075
+ out.bin_liquidity = item.parsedJson.bin_liquidity;
10076
+ }
10077
+ });
10078
+ return out;
10079
+ }
10080
+ async getPairLiquidity(params) {
10081
+ const tx = new Transaction12();
10082
+ const { integrate, simulationAccount } = this.sdk.sdkOptions;
10083
+ const typeArguments = [params.coinTypeA, params.coinTypeB];
10084
+ const args = [tx.object(params.pair)];
10085
+ tx.moveCall({
10086
+ target: `${integrate.published_at}::${DlmmScript}::pair_liquidity`,
10087
+ arguments: args,
10088
+ typeArguments
10089
+ });
10090
+ const simulateRes = await this.sdk.fullClient.devInspectTransactionBlock({
10091
+ transactionBlock: tx,
10092
+ sender: simulationAccount.address
10093
+ });
10094
+ if (simulateRes.error != null) {
10095
+ throw new Error(`fetchBins error code: ${simulateRes.error ?? "unknown error"}`);
10096
+ }
10097
+ const out = {
10098
+ shares: 0,
10099
+ liquidity: 0,
10100
+ x: 0,
10101
+ y: 0,
10102
+ bin_ids: [],
10103
+ bin_x: [],
10104
+ bin_y: []
10105
+ };
10106
+ simulateRes.events?.forEach((item) => {
10107
+ if (extractStructTagFromType(item.type).name === `EventPositionLiquidity`) {
10108
+ out.shares = item.parsedJson.shares;
10109
+ out.liquidity = item.parsedJson.liquidity;
10110
+ out.x = item.parsedJson.x;
10111
+ out.y = item.parsedJson.y;
10112
+ out.bin_ids = item.bin_ids;
10113
+ out.bin_x = item.bin_x;
10114
+ out.bin_y = item.bin_y;
10115
+ }
10116
+ });
10117
+ return out;
10118
+ }
10119
+ };
10120
+
9849
10121
  // src/sdk.ts
9850
10122
  var MagmaClmmSDK = class {
9851
10123
  _cache = {};
@@ -9870,6 +10142,7 @@ var MagmaClmmSDK = class {
9870
10142
  */
9871
10143
  _lock;
9872
10144
  _gauge;
10145
+ _dlmm;
9873
10146
  /**
9874
10147
  * Provide interact with a position rewarder interface.
9875
10148
  */
@@ -9907,6 +10180,7 @@ var MagmaClmmSDK = class {
9907
10180
  this._swap = new SwapModule(this);
9908
10181
  this._lock = new LockModule(this);
9909
10182
  this._gauge = new GaugeModule(this);
10183
+ this._dlmm = new DlmmModule(this);
9910
10184
  this._pool = new PoolModule(this);
9911
10185
  this._position = new PositionModule(this);
9912
10186
  this._rewarder = new RewarderModule(this);
@@ -9944,6 +10218,9 @@ var MagmaClmmSDK = class {
9944
10218
  get Gauge() {
9945
10219
  return this._gauge;
9946
10220
  }
10221
+ get Dlmm() {
10222
+ return this._dlmm;
10223
+ }
9947
10224
  /**
9948
10225
  * Getter for the fullClient property.
9949
10226
  * @returns {RpcModule} The fullClient property value.
@@ -10117,6 +10394,7 @@ var main_default = MagmaClmmSDK;
10117
10394
  var SDKConfig = {
10118
10395
  clmmConfig: {
10119
10396
  pools_id: "0xfa145b9de10fe858be81edd1c6cdffcf27be9d016de02a1345eb1009a68ba8b2",
10397
+ // clmm and dlmm both use this global_config
10120
10398
  global_config_id: "0x4c4e1402401f72c7d8533d0ed8d5f8949da363c7a3319ccef261ffe153d32f8a",
10121
10399
  global_vault_id: "0xa7e1102f222b6eb81ccc8a126e7feb2353342be9df6f6646a77c4519da29c071",
10122
10400
  admin_cap_id: "0x89c1a321291d15ddae5a086c9abc533dff697fde3d89e0ca836c41af73e36a75"
@@ -10136,7 +10414,11 @@ var SDKConfig = {
10136
10414
  distribution_cfg: "0xaff8d151ac29317201151f97d28c546b3c5923d8cfc5499f40dea61c4022c949",
10137
10415
  magma_token: "0x7161c6c6bb65f852797c8f7f5c4f8d57adaf796e1b840921f9e23fabeadfd54e::magma::MAGMA",
10138
10416
  minter_id: "0x4fa5766cd83b33b215b139fec27ac344040f3bbd84fcbee7b61fc671aadc51fa"
10139
- }
10417
+ },
10418
+ dlmmConfig: {
10419
+ factory: ""
10420
+ },
10421
+ gaugeConfig: {}
10140
10422
  };
10141
10423
  var clmmMainnet = {
10142
10424
  fullRpcUrl: getFullnodeUrl("mainnet"),
@@ -10153,6 +10435,11 @@ var clmmMainnet = {
10153
10435
  published_at: "0x4a35d3dfef55ed3631b7158544c6322a23bc434fe4fca1234cb680ce0505f82d",
10154
10436
  config: SDKConfig.clmmConfig
10155
10437
  },
10438
+ dlmm_pool: {
10439
+ package_id: "",
10440
+ published_at: "",
10441
+ config: SDKConfig.dlmmConfig
10442
+ },
10156
10443
  distribution: {
10157
10444
  package_id: "0xee4a1f231dc45a303389998fe26c4e39278cf68b404b32e4f0b9769129b8267b",
10158
10445
  published_at: "0xee4a1f231dc45a303389998fe26c4e39278cf68b404b32e4f0b9769129b8267b"
@@ -10206,6 +10493,9 @@ var SDKConfig2 = {
10206
10493
  distribution_cfg: "0x94e23846c975e2faf89a61bfc2b10ad64decab9069eb1f9fc39752b010868c74",
10207
10494
  magma_token: "0x45ac2371c33ca0df8dc784d62c8ce5126d42edd8c56820396524dff2ae0619b1::magma_token::MAGMA_TOKEN",
10208
10495
  minter_id: "0x89435d6b2a510ba50ca23303f10e91ec058f138a88f69a43fe03cd22edb214c5"
10496
+ },
10497
+ dlmmConfig: {
10498
+ factory: ""
10209
10499
  }
10210
10500
  };
10211
10501
  var clmmTestnet = {
@@ -10220,6 +10510,11 @@ var clmmTestnet = {
10220
10510
  published_at: "0x23e0b5ab4aa63d0e6fd98fa5e247bcf9b36ad716b479d39e56b2ba9ff631e09d",
10221
10511
  config: SDKConfig2.clmmConfig
10222
10512
  },
10513
+ dlmm_pool: {
10514
+ package_id: "",
10515
+ published_at: "",
10516
+ config: SDKConfig2.dlmmConfig
10517
+ },
10223
10518
  distribution: {
10224
10519
  package_id: "0x45ac2371c33ca0df8dc784d62c8ce5126d42edd8c56820396524dff2ae0619b1",
10225
10520
  published_at: "0x45ac2371c33ca0df8dc784d62c8ce5126d42edd8c56820396524dff2ae0619b1"
@@ -10294,6 +10589,7 @@ export {
10294
10589
  DeepbookCustodianV2Moudle,
10295
10590
  DeepbookEndpointsV2Moudle,
10296
10591
  DeepbookUtils,
10592
+ DlmmScript,
10297
10593
  FEE_RATE_DENOMINATOR,
10298
10594
  GAS_SYMBOL,
10299
10595
  GAS_TYPE_ARG,