@myx-trade/sdk 0.1.133 → 0.1.134

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.d.mts CHANGED
@@ -1438,8 +1438,10 @@ declare class Account {
1438
1438
  /**
1439
1439
  * get tradable amount
1440
1440
  */
1441
- getTradableAmount({ poolId }: {
1441
+ getTradableAmount({ poolId, chainId, address }: {
1442
1442
  poolId: string;
1443
+ chainId: number;
1444
+ address: string;
1443
1445
  }): Promise<{
1444
1446
  code: number;
1445
1447
  data: {
@@ -1451,6 +1453,11 @@ declare class Account {
1451
1453
  code: number;
1452
1454
  data?: undefined;
1453
1455
  }>;
1456
+ getAvailableMarginBalance({ poolId, chainId, address }: {
1457
+ poolId: string;
1458
+ chainId: number;
1459
+ address: string;
1460
+ }): Promise<bigint>;
1454
1461
  getTradeFlow(params: GetHistoryOrdersParams): Promise<{
1455
1462
  code: number;
1456
1463
  data: TradeFlowItem[];
@@ -1678,7 +1685,7 @@ declare class Order {
1678
1685
  message: any;
1679
1686
  data?: undefined;
1680
1687
  }>;
1681
- closeAllPositions(chainId: number, params: PlaceOrderParams[]): Promise<{
1688
+ closeAllPositions(chainId: number, params: PlaceOrderParams[], tradingFee: string): Promise<{
1682
1689
  code: number;
1683
1690
  message: string;
1684
1691
  data: ApiResponse<any>;
@@ -1712,7 +1719,7 @@ declare class Order {
1712
1719
  timestamp?: undefined;
1713
1720
  receipt?: undefined;
1714
1721
  }>;
1715
- createDecreaseOrder(params: PlaceOrderParams): Promise<{
1722
+ createDecreaseOrder(params: PlaceOrderParams, tradingFee: string): Promise<{
1716
1723
  code: number;
1717
1724
  message: string;
1718
1725
  data: ApiResponse<any>;
package/dist/index.d.ts CHANGED
@@ -1438,8 +1438,10 @@ declare class Account {
1438
1438
  /**
1439
1439
  * get tradable amount
1440
1440
  */
1441
- getTradableAmount({ poolId }: {
1441
+ getTradableAmount({ poolId, chainId, address }: {
1442
1442
  poolId: string;
1443
+ chainId: number;
1444
+ address: string;
1443
1445
  }): Promise<{
1444
1446
  code: number;
1445
1447
  data: {
@@ -1451,6 +1453,11 @@ declare class Account {
1451
1453
  code: number;
1452
1454
  data?: undefined;
1453
1455
  }>;
1456
+ getAvailableMarginBalance({ poolId, chainId, address }: {
1457
+ poolId: string;
1458
+ chainId: number;
1459
+ address: string;
1460
+ }): Promise<bigint>;
1454
1461
  getTradeFlow(params: GetHistoryOrdersParams): Promise<{
1455
1462
  code: number;
1456
1463
  data: TradeFlowItem[];
@@ -1678,7 +1685,7 @@ declare class Order {
1678
1685
  message: any;
1679
1686
  data?: undefined;
1680
1687
  }>;
1681
- closeAllPositions(chainId: number, params: PlaceOrderParams[]): Promise<{
1688
+ closeAllPositions(chainId: number, params: PlaceOrderParams[], tradingFee: string): Promise<{
1682
1689
  code: number;
1683
1690
  message: string;
1684
1691
  data: ApiResponse<any>;
@@ -1712,7 +1719,7 @@ declare class Order {
1712
1719
  timestamp?: undefined;
1713
1720
  receipt?: undefined;
1714
1721
  }>;
1715
- createDecreaseOrder(params: PlaceOrderParams): Promise<{
1722
+ createDecreaseOrder(params: PlaceOrderParams, tradingFee: string): Promise<{
1716
1723
  code: number;
1717
1724
  message: string;
1718
1725
  data: ApiResponse<any>;
package/dist/index.js CHANGED
@@ -1822,7 +1822,7 @@ var RotationProvider = class extends import_providers.BaseProvider {
1822
1822
  // package.json
1823
1823
  var package_default = {
1824
1824
  name: "@myx-trade/sdk",
1825
- version: "0.1.133",
1825
+ version: "0.1.134",
1826
1826
  private: false,
1827
1827
  publishConfig: {
1828
1828
  access: "public"
@@ -14311,15 +14311,7 @@ var Order = class {
14311
14311
  params.executionFeeToken,
14312
14312
  params.collateralAmount
14313
14313
  );
14314
- const marginAccountBalanceRes = await this.account.getTradableAmount({ poolId: params.poolId });
14315
- const marginAccountBalance = marginAccountBalanceRes?.data;
14316
- if (marginAccountBalanceRes.code !== 0) {
14317
- return {
14318
- code: -1,
14319
- message: "Failed to get tradable amount or wallet balance"
14320
- };
14321
- }
14322
- const availableAccountMarginBalance = BigInt(marginAccountBalance?.freeAmount.toString() ?? 0) + (!marginAccountBalance?.tradeableProfit ? BigInt(marginAccountBalance?.tradeableProfit) : BigInt(0));
14314
+ const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params.poolId, chainId: params.chainId, address: params.address });
14323
14315
  const needAmount = BigInt(tradingFee) + BigInt(params.collateralAmount) + totalNetWorkFee;
14324
14316
  let depositAmount = BigInt(0);
14325
14317
  const diff = needAmount - availableAccountMarginBalance;
@@ -14492,17 +14484,20 @@ var Order = class {
14492
14484
  };
14493
14485
  }
14494
14486
  }
14495
- async closeAllPositions(chainId, params) {
14487
+ async closeAllPositions(chainId, params, tradingFee) {
14496
14488
  try {
14497
14489
  const config = this.configManager.getConfig();
14498
- if (!config.signer) {
14499
- throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
14500
- }
14490
+ const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params[0].poolId, chainId, address: params[0].address });
14501
14491
  const networkFee = await this.utils.getNetworkFee(
14502
14492
  params[0].executionFeeToken,
14503
14493
  chainId
14504
14494
  );
14505
- const depositAmount = BigInt(params.length) * BigInt(networkFee);
14495
+ const tradingFeeAmount = BigInt(tradingFee) * BigInt(params.length);
14496
+ const needAmount = tradingFeeAmount + BigInt(params.length) * BigInt(networkFee);
14497
+ let depositAmount = BigInt(0);
14498
+ if (availableAccountMarginBalance < needAmount) {
14499
+ depositAmount = needAmount - availableAccountMarginBalance;
14500
+ }
14506
14501
  const needsApproval = await this.utils.needsApproval(
14507
14502
  chainId,
14508
14503
  params[0].executionFeeToken,
@@ -14579,6 +14574,9 @@ var Order = class {
14579
14574
  data: rs
14580
14575
  };
14581
14576
  }
14577
+ if (!config.signer) {
14578
+ throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
14579
+ }
14582
14580
  const brokerContract = await getBrokerSingerContract(
14583
14581
  chainId,
14584
14582
  this.configManager.getConfig().brokerAddress
@@ -14623,17 +14621,19 @@ var Order = class {
14623
14621
  };
14624
14622
  }
14625
14623
  }
14626
- async createDecreaseOrder(params) {
14624
+ async createDecreaseOrder(params, tradingFee) {
14627
14625
  try {
14628
14626
  const config = this.configManager.getConfig();
14629
- const brokerContract = await getBrokerSingerContract(
14630
- params.chainId,
14631
- this.configManager.getConfig().brokerAddress
14632
- );
14633
14627
  const networkFee = await this.utils.getNetworkFee(
14634
14628
  params.executionFeeToken,
14635
14629
  params.chainId
14636
14630
  );
14631
+ let depositAmount = BigInt(0);
14632
+ const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params.poolId, chainId: params.chainId, address: params.address });
14633
+ const needAmount = BigInt(tradingFee) + BigInt(networkFee);
14634
+ if (availableAccountMarginBalance < needAmount) {
14635
+ depositAmount = needAmount - availableAccountMarginBalance;
14636
+ }
14637
14637
  const data = {
14638
14638
  user: params.address,
14639
14639
  poolId: params.poolId,
@@ -14656,12 +14656,12 @@ var Order = class {
14656
14656
  };
14657
14657
  const depositData = {
14658
14658
  token: params.executionFeeToken,
14659
- amount: networkFee
14659
+ amount: depositAmount.toString()
14660
14660
  };
14661
14661
  const needsApproval = await this.utils.needsApproval(
14662
14662
  params.chainId,
14663
14663
  params.executionFeeToken,
14664
- networkFee.toString()
14664
+ depositAmount.toString()
14665
14665
  );
14666
14666
  const authorized = this.configManager.getConfig().seamlessAccount?.authorized;
14667
14667
  const seamlessWallet = this.configManager.getConfig().seamlessAccount?.wallet;
@@ -14712,13 +14712,16 @@ var Order = class {
14712
14712
  };
14713
14713
  this.logger.info("create decrease order forward tx params --->", forwardTxParams);
14714
14714
  const rs = await this.seamless.forwarderTx(forwardTxParams, params.chainId, seamlessWallet);
14715
- console.log("rs-->", rs);
14716
14715
  return {
14717
14716
  code: 0,
14718
14717
  message: "create decrease order success",
14719
14718
  data: rs
14720
14719
  };
14721
14720
  }
14721
+ const brokerContract = await getBrokerSingerContract(
14722
+ params.chainId,
14723
+ this.configManager.getConfig().brokerAddress
14724
+ );
14722
14725
  if (!config.signer) {
14723
14726
  throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
14724
14727
  }
@@ -14734,16 +14737,12 @@ var Order = class {
14734
14737
  }
14735
14738
  let transaction;
14736
14739
  if (!params.positionId) {
14737
- const depositData2 = {
14738
- token: params.executionFeeToken,
14739
- amount: networkFee
14740
- };
14741
14740
  const positionId = 1;
14742
14741
  this.logger.info("createDecreaseOrder salt position params--->", [positionId, { data }]);
14743
- const gasLimit = await brokerContract.placeOrderWithSalt.estimateGas(positionId.toString(), depositData2, data);
14742
+ const gasLimit = await brokerContract.placeOrderWithSalt.estimateGas(positionId.toString(), depositData, data);
14744
14743
  transaction = await brokerContract.placeOrderWithSalt(
14745
14744
  positionId.toString(),
14746
- depositData2,
14745
+ depositData,
14747
14746
  data,
14748
14747
  {
14749
14748
  gasLimit: gasLimit * 130n / 100n
@@ -21017,24 +21016,16 @@ var Account = class {
21017
21016
  /**
21018
21017
  * get tradable amount
21019
21018
  */
21020
- async getTradableAmount({ poolId }) {
21021
- const config = this.configManager.getConfig();
21022
- if (!config.signer) {
21023
- throw new MyxSDKError(
21024
- "INVALID_SIGNER" /* InvalidSigner */,
21025
- "Invalid signer"
21026
- );
21027
- }
21028
- const contractAddress = getContractAddressByChainId(config.chainId);
21029
- const provider = await getJSONProvider(config.chainId);
21019
+ async getTradableAmount({ poolId, chainId, address }) {
21020
+ const contractAddress = getContractAddressByChainId(chainId);
21021
+ const provider = await getJSONProvider(chainId);
21030
21022
  const accountContract = new import_ethers26.ethers.Contract(
21031
21023
  contractAddress.Account,
21032
21024
  Account_default,
21033
21025
  provider
21034
21026
  );
21035
21027
  try {
21036
- const targetAddress = config.seamlessMode ? config.seamlessAccount?.masterAddress : config.signer?.getAddress();
21037
- const assets = await accountContract.getTradableAmount(targetAddress, poolId);
21028
+ const assets = await accountContract.getTradableAmount(address, poolId);
21038
21029
  const data = {
21039
21030
  profitIsReleased: assets[0],
21040
21031
  freeAmount: assets[1],
@@ -21050,6 +21041,18 @@ var Account = class {
21050
21041
  };
21051
21042
  }
21052
21043
  }
21044
+ async getAvailableMarginBalance({ poolId, chainId, address }) {
21045
+ const marginAccountBalanceRes = await this.getTradableAmount({ poolId, chainId, address });
21046
+ const marginAccountBalance = marginAccountBalanceRes?.data;
21047
+ if (marginAccountBalanceRes.code !== 0) {
21048
+ throw new MyxSDKError(
21049
+ "INSUFFICIENT_MARGIN_BALANCE" /* InsufficientMarginBalance */,
21050
+ "Insufficient margin balance"
21051
+ );
21052
+ }
21053
+ const availableAccountMarginBalance = BigInt(marginAccountBalance?.freeAmount.toString() ?? 0) + (!marginAccountBalance?.tradeableProfit ? BigInt(marginAccountBalance?.tradeableProfit) : BigInt(0));
21054
+ return availableAccountMarginBalance;
21055
+ }
21053
21056
  async getTradeFlow(params) {
21054
21057
  const accessToken = await this.configManager.getAccessToken();
21055
21058
  if (!accessToken) {
package/dist/index.mjs CHANGED
@@ -1732,7 +1732,7 @@ var RotationProvider = class extends BaseProvider {
1732
1732
  // package.json
1733
1733
  var package_default = {
1734
1734
  name: "@myx-trade/sdk",
1735
- version: "0.1.133",
1735
+ version: "0.1.134",
1736
1736
  private: false,
1737
1737
  publishConfig: {
1738
1738
  access: "public"
@@ -14221,15 +14221,7 @@ var Order = class {
14221
14221
  params.executionFeeToken,
14222
14222
  params.collateralAmount
14223
14223
  );
14224
- const marginAccountBalanceRes = await this.account.getTradableAmount({ poolId: params.poolId });
14225
- const marginAccountBalance = marginAccountBalanceRes?.data;
14226
- if (marginAccountBalanceRes.code !== 0) {
14227
- return {
14228
- code: -1,
14229
- message: "Failed to get tradable amount or wallet balance"
14230
- };
14231
- }
14232
- const availableAccountMarginBalance = BigInt(marginAccountBalance?.freeAmount.toString() ?? 0) + (!marginAccountBalance?.tradeableProfit ? BigInt(marginAccountBalance?.tradeableProfit) : BigInt(0));
14224
+ const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params.poolId, chainId: params.chainId, address: params.address });
14233
14225
  const needAmount = BigInt(tradingFee) + BigInt(params.collateralAmount) + totalNetWorkFee;
14234
14226
  let depositAmount = BigInt(0);
14235
14227
  const diff = needAmount - availableAccountMarginBalance;
@@ -14402,17 +14394,20 @@ var Order = class {
14402
14394
  };
14403
14395
  }
14404
14396
  }
14405
- async closeAllPositions(chainId, params) {
14397
+ async closeAllPositions(chainId, params, tradingFee) {
14406
14398
  try {
14407
14399
  const config = this.configManager.getConfig();
14408
- if (!config.signer) {
14409
- throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
14410
- }
14400
+ const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params[0].poolId, chainId, address: params[0].address });
14411
14401
  const networkFee = await this.utils.getNetworkFee(
14412
14402
  params[0].executionFeeToken,
14413
14403
  chainId
14414
14404
  );
14415
- const depositAmount = BigInt(params.length) * BigInt(networkFee);
14405
+ const tradingFeeAmount = BigInt(tradingFee) * BigInt(params.length);
14406
+ const needAmount = tradingFeeAmount + BigInt(params.length) * BigInt(networkFee);
14407
+ let depositAmount = BigInt(0);
14408
+ if (availableAccountMarginBalance < needAmount) {
14409
+ depositAmount = needAmount - availableAccountMarginBalance;
14410
+ }
14416
14411
  const needsApproval = await this.utils.needsApproval(
14417
14412
  chainId,
14418
14413
  params[0].executionFeeToken,
@@ -14489,6 +14484,9 @@ var Order = class {
14489
14484
  data: rs
14490
14485
  };
14491
14486
  }
14487
+ if (!config.signer) {
14488
+ throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
14489
+ }
14492
14490
  const brokerContract = await getBrokerSingerContract(
14493
14491
  chainId,
14494
14492
  this.configManager.getConfig().brokerAddress
@@ -14533,17 +14531,19 @@ var Order = class {
14533
14531
  };
14534
14532
  }
14535
14533
  }
14536
- async createDecreaseOrder(params) {
14534
+ async createDecreaseOrder(params, tradingFee) {
14537
14535
  try {
14538
14536
  const config = this.configManager.getConfig();
14539
- const brokerContract = await getBrokerSingerContract(
14540
- params.chainId,
14541
- this.configManager.getConfig().brokerAddress
14542
- );
14543
14537
  const networkFee = await this.utils.getNetworkFee(
14544
14538
  params.executionFeeToken,
14545
14539
  params.chainId
14546
14540
  );
14541
+ let depositAmount = BigInt(0);
14542
+ const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params.poolId, chainId: params.chainId, address: params.address });
14543
+ const needAmount = BigInt(tradingFee) + BigInt(networkFee);
14544
+ if (availableAccountMarginBalance < needAmount) {
14545
+ depositAmount = needAmount - availableAccountMarginBalance;
14546
+ }
14547
14547
  const data = {
14548
14548
  user: params.address,
14549
14549
  poolId: params.poolId,
@@ -14566,12 +14566,12 @@ var Order = class {
14566
14566
  };
14567
14567
  const depositData = {
14568
14568
  token: params.executionFeeToken,
14569
- amount: networkFee
14569
+ amount: depositAmount.toString()
14570
14570
  };
14571
14571
  const needsApproval = await this.utils.needsApproval(
14572
14572
  params.chainId,
14573
14573
  params.executionFeeToken,
14574
- networkFee.toString()
14574
+ depositAmount.toString()
14575
14575
  );
14576
14576
  const authorized = this.configManager.getConfig().seamlessAccount?.authorized;
14577
14577
  const seamlessWallet = this.configManager.getConfig().seamlessAccount?.wallet;
@@ -14622,13 +14622,16 @@ var Order = class {
14622
14622
  };
14623
14623
  this.logger.info("create decrease order forward tx params --->", forwardTxParams);
14624
14624
  const rs = await this.seamless.forwarderTx(forwardTxParams, params.chainId, seamlessWallet);
14625
- console.log("rs-->", rs);
14626
14625
  return {
14627
14626
  code: 0,
14628
14627
  message: "create decrease order success",
14629
14628
  data: rs
14630
14629
  };
14631
14630
  }
14631
+ const brokerContract = await getBrokerSingerContract(
14632
+ params.chainId,
14633
+ this.configManager.getConfig().brokerAddress
14634
+ );
14632
14635
  if (!config.signer) {
14633
14636
  throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
14634
14637
  }
@@ -14644,16 +14647,12 @@ var Order = class {
14644
14647
  }
14645
14648
  let transaction;
14646
14649
  if (!params.positionId) {
14647
- const depositData2 = {
14648
- token: params.executionFeeToken,
14649
- amount: networkFee
14650
- };
14651
14650
  const positionId = 1;
14652
14651
  this.logger.info("createDecreaseOrder salt position params--->", [positionId, { data }]);
14653
- const gasLimit = await brokerContract.placeOrderWithSalt.estimateGas(positionId.toString(), depositData2, data);
14652
+ const gasLimit = await brokerContract.placeOrderWithSalt.estimateGas(positionId.toString(), depositData, data);
14654
14653
  transaction = await brokerContract.placeOrderWithSalt(
14655
14654
  positionId.toString(),
14656
- depositData2,
14655
+ depositData,
14657
14656
  data,
14658
14657
  {
14659
14658
  gasLimit: gasLimit * 130n / 100n
@@ -20927,24 +20926,16 @@ var Account = class {
20927
20926
  /**
20928
20927
  * get tradable amount
20929
20928
  */
20930
- async getTradableAmount({ poolId }) {
20931
- const config = this.configManager.getConfig();
20932
- if (!config.signer) {
20933
- throw new MyxSDKError(
20934
- "INVALID_SIGNER" /* InvalidSigner */,
20935
- "Invalid signer"
20936
- );
20937
- }
20938
- const contractAddress = getContractAddressByChainId(config.chainId);
20939
- const provider = await getJSONProvider(config.chainId);
20929
+ async getTradableAmount({ poolId, chainId, address }) {
20930
+ const contractAddress = getContractAddressByChainId(chainId);
20931
+ const provider = await getJSONProvider(chainId);
20940
20932
  const accountContract = new ethers8.Contract(
20941
20933
  contractAddress.Account,
20942
20934
  Account_default,
20943
20935
  provider
20944
20936
  );
20945
20937
  try {
20946
- const targetAddress = config.seamlessMode ? config.seamlessAccount?.masterAddress : config.signer?.getAddress();
20947
- const assets = await accountContract.getTradableAmount(targetAddress, poolId);
20938
+ const assets = await accountContract.getTradableAmount(address, poolId);
20948
20939
  const data = {
20949
20940
  profitIsReleased: assets[0],
20950
20941
  freeAmount: assets[1],
@@ -20960,6 +20951,18 @@ var Account = class {
20960
20951
  };
20961
20952
  }
20962
20953
  }
20954
+ async getAvailableMarginBalance({ poolId, chainId, address }) {
20955
+ const marginAccountBalanceRes = await this.getTradableAmount({ poolId, chainId, address });
20956
+ const marginAccountBalance = marginAccountBalanceRes?.data;
20957
+ if (marginAccountBalanceRes.code !== 0) {
20958
+ throw new MyxSDKError(
20959
+ "INSUFFICIENT_MARGIN_BALANCE" /* InsufficientMarginBalance */,
20960
+ "Insufficient margin balance"
20961
+ );
20962
+ }
20963
+ const availableAccountMarginBalance = BigInt(marginAccountBalance?.freeAmount.toString() ?? 0) + (!marginAccountBalance?.tradeableProfit ? BigInt(marginAccountBalance?.tradeableProfit) : BigInt(0));
20964
+ return availableAccountMarginBalance;
20965
+ }
20963
20966
  async getTradeFlow(params) {
20964
20967
  const accessToken = await this.configManager.getAccessToken();
20965
20968
  if (!accessToken) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@myx-trade/sdk",
3
- "version": "0.1.133",
3
+ "version": "0.1.134",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"