@myx-trade/sdk 0.1.156 → 0.1.159
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.js +76 -24
- package/dist/index.mjs +76 -24
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1821,7 +1821,7 @@ var RotationProvider = class extends import_providers.BaseProvider {
|
|
|
1821
1821
|
// package.json
|
|
1822
1822
|
var package_default = {
|
|
1823
1823
|
name: "@myx-trade/sdk",
|
|
1824
|
-
version: "0.1.
|
|
1824
|
+
version: "0.1.159",
|
|
1825
1825
|
private: false,
|
|
1826
1826
|
publishConfig: {
|
|
1827
1827
|
access: "public"
|
|
@@ -14374,19 +14374,12 @@ var Order = class {
|
|
|
14374
14374
|
);
|
|
14375
14375
|
const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params.poolId, chainId: params.chainId, address: params.address });
|
|
14376
14376
|
const totalCollateralAmount = BigInt(params.collateralAmount) + BigInt(tradingFee);
|
|
14377
|
-
this.logger.info("availableAccountMarginBalance-->", availableAccountMarginBalance.toString());
|
|
14378
|
-
this.logger.info("totalCollateralAmount-->", totalCollateralAmount.toString());
|
|
14379
|
-
this.logger.info("totalNetWorkFee-->", totalNetWorkFee.toString());
|
|
14380
14377
|
const needAmount = BigInt(tradingFee) + BigInt(params.collateralAmount) + totalNetWorkFee;
|
|
14381
|
-
this.logger.info("needAmount-->", needAmount.toString());
|
|
14382
14378
|
let depositAmount = BigInt(0);
|
|
14383
|
-
this.logger.info("availableAccountMarginBalance-->", availableAccountMarginBalance.toString());
|
|
14384
14379
|
const diff = needAmount - availableAccountMarginBalance;
|
|
14385
|
-
this.logger.info("diff-->", diff.toString());
|
|
14386
14380
|
if (diff > BigInt(0)) {
|
|
14387
14381
|
depositAmount = diff;
|
|
14388
14382
|
}
|
|
14389
|
-
this.logger.info("depositAmount-->", depositAmount.toString());
|
|
14390
14383
|
const depositData = {
|
|
14391
14384
|
token: params.executionFeeToken,
|
|
14392
14385
|
amount: depositAmount.toString()
|
|
@@ -21033,6 +21026,13 @@ var Account_default = [
|
|
|
21033
21026
|
|
|
21034
21027
|
// src/manager/account/index.ts
|
|
21035
21028
|
var import_dayjs3 = __toESM(require("dayjs"));
|
|
21029
|
+
|
|
21030
|
+
// src/api/pool/index.ts
|
|
21031
|
+
var getPoolList = async () => {
|
|
21032
|
+
return http.get(`${baseUrl}/openapi/gateway/scan/market/list`);
|
|
21033
|
+
};
|
|
21034
|
+
|
|
21035
|
+
// src/manager/account/index.ts
|
|
21036
21036
|
var Account = class {
|
|
21037
21037
|
constructor(configManager, logger, utils, client2) {
|
|
21038
21038
|
this.configManager = configManager;
|
|
@@ -21090,16 +21090,53 @@ var Account = class {
|
|
|
21090
21090
|
}
|
|
21091
21091
|
}
|
|
21092
21092
|
async getAvailableMarginBalance({ poolId, chainId, address }) {
|
|
21093
|
-
|
|
21094
|
-
|
|
21095
|
-
|
|
21093
|
+
try {
|
|
21094
|
+
const poolListRes = await getPoolList();
|
|
21095
|
+
if (poolListRes.code !== 0) {
|
|
21096
|
+
throw new MyxSDKError(
|
|
21097
|
+
"REQUEST_FAILED" /* RequestFailed */,
|
|
21098
|
+
"Failed to get pool list"
|
|
21099
|
+
);
|
|
21100
|
+
}
|
|
21101
|
+
const poolList = poolListRes.data;
|
|
21102
|
+
const pool = poolList?.find((pool2) => pool2.poolId === poolId);
|
|
21103
|
+
const orderRes = await this.client.order.getOrders();
|
|
21104
|
+
if (orderRes.code !== 0) {
|
|
21105
|
+
throw new MyxSDKError(
|
|
21106
|
+
"REQUEST_FAILED" /* RequestFailed */,
|
|
21107
|
+
"Failed to get orders"
|
|
21108
|
+
);
|
|
21109
|
+
}
|
|
21110
|
+
const orders = orderRes.data;
|
|
21111
|
+
const openOrders = orders?.filter((order) => order.poolId === poolId);
|
|
21112
|
+
const used = openOrders?.reduce((acc, order) => {
|
|
21113
|
+
const prev = BigInt(acc);
|
|
21114
|
+
const curr = BigInt(order.collateralAmount ?? 0) + prev;
|
|
21115
|
+
return curr.toString();
|
|
21116
|
+
}, "0");
|
|
21117
|
+
const marginAccountBalanceRes = await this.getAccountInfo(chainId, address, poolId);
|
|
21118
|
+
if (marginAccountBalanceRes.code !== 0) {
|
|
21119
|
+
throw new MyxSDKError(
|
|
21120
|
+
"REQUEST_FAILED" /* RequestFailed */,
|
|
21121
|
+
"Failed to get account info"
|
|
21122
|
+
);
|
|
21123
|
+
}
|
|
21124
|
+
const marginAccountBalance = marginAccountBalanceRes.data;
|
|
21125
|
+
const usedMargin = import_ethers26.ethers.parseUnits(used ?? "0", pool?.quoteDecimals ?? 6);
|
|
21126
|
+
const quoteProfit = BigInt(marginAccountBalance.quoteProfit ?? 0);
|
|
21127
|
+
const freeAmount = BigInt(marginAccountBalance?.freeAmount.toString() ?? 0);
|
|
21128
|
+
const accountMargin = freeAmount + quoteProfit;
|
|
21129
|
+
if (accountMargin < usedMargin) {
|
|
21130
|
+
return BigInt(0);
|
|
21131
|
+
}
|
|
21132
|
+
const availableAccountMarginBalance = accountMargin - usedMargin;
|
|
21133
|
+
return availableAccountMarginBalance;
|
|
21134
|
+
} catch (error) {
|
|
21096
21135
|
throw new MyxSDKError(
|
|
21097
|
-
"
|
|
21098
|
-
"
|
|
21136
|
+
"REQUEST_FAILED" /* RequestFailed */,
|
|
21137
|
+
"Failed to get orders"
|
|
21099
21138
|
);
|
|
21100
21139
|
}
|
|
21101
|
-
const availableAccountMarginBalance = BigInt(marginAccountBalance?.freeAmount.toString() ?? 0) + (!marginAccountBalance?.tradeableProfit ? BigInt(marginAccountBalance?.tradeableProfit) : BigInt(0));
|
|
21102
|
-
return availableAccountMarginBalance;
|
|
21103
21140
|
}
|
|
21104
21141
|
async getTradeFlow(params) {
|
|
21105
21142
|
const accessToken = await this.configManager.getAccessToken();
|
|
@@ -21470,6 +21507,8 @@ var Seamless = class {
|
|
|
21470
21507
|
throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Signer is required for permit");
|
|
21471
21508
|
}
|
|
21472
21509
|
const masterAddress = await config.signer.getAddress();
|
|
21510
|
+
const forwarderContract = await getForwarderContract(chainId);
|
|
21511
|
+
const forwarderAddress = forwarderContract.target;
|
|
21473
21512
|
const brokerAddress = config.brokerAddress;
|
|
21474
21513
|
const contractAddress = getContractAddressByChainId(chainId);
|
|
21475
21514
|
const erc20Contract = new import_ethers27.ethers.Contract(
|
|
@@ -21490,6 +21529,17 @@ var Seamless = class {
|
|
|
21490
21529
|
deadline.toString(),
|
|
21491
21530
|
chainId
|
|
21492
21531
|
);
|
|
21532
|
+
const forwarderSignPermit = await signPermit(
|
|
21533
|
+
config.signer,
|
|
21534
|
+
// 使用 signer 而不是 provider
|
|
21535
|
+
erc20Contract,
|
|
21536
|
+
masterAddress,
|
|
21537
|
+
forwarderAddress,
|
|
21538
|
+
import_ethers27.ethers.MaxUint256.toString(),
|
|
21539
|
+
(nonces + 1).toString(),
|
|
21540
|
+
deadline.toString(),
|
|
21541
|
+
chainId
|
|
21542
|
+
);
|
|
21493
21543
|
const brokerSeamlessUSDPermitParams = {
|
|
21494
21544
|
token: erc20Contract.target,
|
|
21495
21545
|
owner: masterAddress,
|
|
@@ -21500,7 +21550,17 @@ var Seamless = class {
|
|
|
21500
21550
|
r: brokerSignPermit.r,
|
|
21501
21551
|
s: brokerSignPermit.s
|
|
21502
21552
|
};
|
|
21503
|
-
|
|
21553
|
+
const forwarderPermitParams = {
|
|
21554
|
+
token: erc20Contract.target,
|
|
21555
|
+
owner: masterAddress,
|
|
21556
|
+
spender: forwarderAddress,
|
|
21557
|
+
value: import_ethers27.ethers.MaxUint256,
|
|
21558
|
+
deadline,
|
|
21559
|
+
v: forwarderSignPermit.v,
|
|
21560
|
+
r: forwarderSignPermit.r,
|
|
21561
|
+
s: forwarderSignPermit.s
|
|
21562
|
+
};
|
|
21563
|
+
return [brokerSeamlessUSDPermitParams, forwarderPermitParams];
|
|
21504
21564
|
} catch (error) {
|
|
21505
21565
|
throw new MyxSDKError("INVALID_PRIVATE_KEY" /* InvalidPrivateKey */, "Invalid private key generated");
|
|
21506
21566
|
}
|
|
@@ -21731,14 +21791,6 @@ var Seamless = class {
|
|
|
21731
21791
|
wallet,
|
|
21732
21792
|
authorized: isAuthorized
|
|
21733
21793
|
});
|
|
21734
|
-
const forwarderContract = await getForwarderContract(chainId);
|
|
21735
|
-
const erc20Address = getContractAddressByChainId(chainId).ERC20;
|
|
21736
|
-
await this.utils.approveAuthorization({
|
|
21737
|
-
chainId,
|
|
21738
|
-
quoteAddress: erc20Address,
|
|
21739
|
-
amount: import_ethers27.ethers.MaxUint256.toString(),
|
|
21740
|
-
spenderAddress: forwarderContract.target
|
|
21741
|
-
});
|
|
21742
21794
|
return {
|
|
21743
21795
|
code: 0,
|
|
21744
21796
|
data: {
|
package/dist/index.mjs
CHANGED
|
@@ -1731,7 +1731,7 @@ var RotationProvider = class extends BaseProvider {
|
|
|
1731
1731
|
// package.json
|
|
1732
1732
|
var package_default = {
|
|
1733
1733
|
name: "@myx-trade/sdk",
|
|
1734
|
-
version: "0.1.
|
|
1734
|
+
version: "0.1.159",
|
|
1735
1735
|
private: false,
|
|
1736
1736
|
publishConfig: {
|
|
1737
1737
|
access: "public"
|
|
@@ -14284,19 +14284,12 @@ var Order = class {
|
|
|
14284
14284
|
);
|
|
14285
14285
|
const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params.poolId, chainId: params.chainId, address: params.address });
|
|
14286
14286
|
const totalCollateralAmount = BigInt(params.collateralAmount) + BigInt(tradingFee);
|
|
14287
|
-
this.logger.info("availableAccountMarginBalance-->", availableAccountMarginBalance.toString());
|
|
14288
|
-
this.logger.info("totalCollateralAmount-->", totalCollateralAmount.toString());
|
|
14289
|
-
this.logger.info("totalNetWorkFee-->", totalNetWorkFee.toString());
|
|
14290
14287
|
const needAmount = BigInt(tradingFee) + BigInt(params.collateralAmount) + totalNetWorkFee;
|
|
14291
|
-
this.logger.info("needAmount-->", needAmount.toString());
|
|
14292
14288
|
let depositAmount = BigInt(0);
|
|
14293
|
-
this.logger.info("availableAccountMarginBalance-->", availableAccountMarginBalance.toString());
|
|
14294
14289
|
const diff = needAmount - availableAccountMarginBalance;
|
|
14295
|
-
this.logger.info("diff-->", diff.toString());
|
|
14296
14290
|
if (diff > BigInt(0)) {
|
|
14297
14291
|
depositAmount = diff;
|
|
14298
14292
|
}
|
|
14299
|
-
this.logger.info("depositAmount-->", depositAmount.toString());
|
|
14300
14293
|
const depositData = {
|
|
14301
14294
|
token: params.executionFeeToken,
|
|
14302
14295
|
amount: depositAmount.toString()
|
|
@@ -20943,6 +20936,13 @@ var Account_default = [
|
|
|
20943
20936
|
|
|
20944
20937
|
// src/manager/account/index.ts
|
|
20945
20938
|
import dayjs3 from "dayjs";
|
|
20939
|
+
|
|
20940
|
+
// src/api/pool/index.ts
|
|
20941
|
+
var getPoolList = async () => {
|
|
20942
|
+
return http.get(`${baseUrl}/openapi/gateway/scan/market/list`);
|
|
20943
|
+
};
|
|
20944
|
+
|
|
20945
|
+
// src/manager/account/index.ts
|
|
20946
20946
|
var Account = class {
|
|
20947
20947
|
constructor(configManager, logger, utils, client2) {
|
|
20948
20948
|
this.configManager = configManager;
|
|
@@ -21000,16 +21000,53 @@ var Account = class {
|
|
|
21000
21000
|
}
|
|
21001
21001
|
}
|
|
21002
21002
|
async getAvailableMarginBalance({ poolId, chainId, address }) {
|
|
21003
|
-
|
|
21004
|
-
|
|
21005
|
-
|
|
21003
|
+
try {
|
|
21004
|
+
const poolListRes = await getPoolList();
|
|
21005
|
+
if (poolListRes.code !== 0) {
|
|
21006
|
+
throw new MyxSDKError(
|
|
21007
|
+
"REQUEST_FAILED" /* RequestFailed */,
|
|
21008
|
+
"Failed to get pool list"
|
|
21009
|
+
);
|
|
21010
|
+
}
|
|
21011
|
+
const poolList = poolListRes.data;
|
|
21012
|
+
const pool = poolList?.find((pool2) => pool2.poolId === poolId);
|
|
21013
|
+
const orderRes = await this.client.order.getOrders();
|
|
21014
|
+
if (orderRes.code !== 0) {
|
|
21015
|
+
throw new MyxSDKError(
|
|
21016
|
+
"REQUEST_FAILED" /* RequestFailed */,
|
|
21017
|
+
"Failed to get orders"
|
|
21018
|
+
);
|
|
21019
|
+
}
|
|
21020
|
+
const orders = orderRes.data;
|
|
21021
|
+
const openOrders = orders?.filter((order) => order.poolId === poolId);
|
|
21022
|
+
const used = openOrders?.reduce((acc, order) => {
|
|
21023
|
+
const prev = BigInt(acc);
|
|
21024
|
+
const curr = BigInt(order.collateralAmount ?? 0) + prev;
|
|
21025
|
+
return curr.toString();
|
|
21026
|
+
}, "0");
|
|
21027
|
+
const marginAccountBalanceRes = await this.getAccountInfo(chainId, address, poolId);
|
|
21028
|
+
if (marginAccountBalanceRes.code !== 0) {
|
|
21029
|
+
throw new MyxSDKError(
|
|
21030
|
+
"REQUEST_FAILED" /* RequestFailed */,
|
|
21031
|
+
"Failed to get account info"
|
|
21032
|
+
);
|
|
21033
|
+
}
|
|
21034
|
+
const marginAccountBalance = marginAccountBalanceRes.data;
|
|
21035
|
+
const usedMargin = ethers8.parseUnits(used ?? "0", pool?.quoteDecimals ?? 6);
|
|
21036
|
+
const quoteProfit = BigInt(marginAccountBalance.quoteProfit ?? 0);
|
|
21037
|
+
const freeAmount = BigInt(marginAccountBalance?.freeAmount.toString() ?? 0);
|
|
21038
|
+
const accountMargin = freeAmount + quoteProfit;
|
|
21039
|
+
if (accountMargin < usedMargin) {
|
|
21040
|
+
return BigInt(0);
|
|
21041
|
+
}
|
|
21042
|
+
const availableAccountMarginBalance = accountMargin - usedMargin;
|
|
21043
|
+
return availableAccountMarginBalance;
|
|
21044
|
+
} catch (error) {
|
|
21006
21045
|
throw new MyxSDKError(
|
|
21007
|
-
"
|
|
21008
|
-
"
|
|
21046
|
+
"REQUEST_FAILED" /* RequestFailed */,
|
|
21047
|
+
"Failed to get orders"
|
|
21009
21048
|
);
|
|
21010
21049
|
}
|
|
21011
|
-
const availableAccountMarginBalance = BigInt(marginAccountBalance?.freeAmount.toString() ?? 0) + (!marginAccountBalance?.tradeableProfit ? BigInt(marginAccountBalance?.tradeableProfit) : BigInt(0));
|
|
21012
|
-
return availableAccountMarginBalance;
|
|
21013
21050
|
}
|
|
21014
21051
|
async getTradeFlow(params) {
|
|
21015
21052
|
const accessToken = await this.configManager.getAccessToken();
|
|
@@ -21380,6 +21417,8 @@ var Seamless = class {
|
|
|
21380
21417
|
throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Signer is required for permit");
|
|
21381
21418
|
}
|
|
21382
21419
|
const masterAddress = await config.signer.getAddress();
|
|
21420
|
+
const forwarderContract = await getForwarderContract(chainId);
|
|
21421
|
+
const forwarderAddress = forwarderContract.target;
|
|
21383
21422
|
const brokerAddress = config.brokerAddress;
|
|
21384
21423
|
const contractAddress = getContractAddressByChainId(chainId);
|
|
21385
21424
|
const erc20Contract = new ethers9.Contract(
|
|
@@ -21400,6 +21439,17 @@ var Seamless = class {
|
|
|
21400
21439
|
deadline.toString(),
|
|
21401
21440
|
chainId
|
|
21402
21441
|
);
|
|
21442
|
+
const forwarderSignPermit = await signPermit(
|
|
21443
|
+
config.signer,
|
|
21444
|
+
// 使用 signer 而不是 provider
|
|
21445
|
+
erc20Contract,
|
|
21446
|
+
masterAddress,
|
|
21447
|
+
forwarderAddress,
|
|
21448
|
+
ethers9.MaxUint256.toString(),
|
|
21449
|
+
(nonces + 1).toString(),
|
|
21450
|
+
deadline.toString(),
|
|
21451
|
+
chainId
|
|
21452
|
+
);
|
|
21403
21453
|
const brokerSeamlessUSDPermitParams = {
|
|
21404
21454
|
token: erc20Contract.target,
|
|
21405
21455
|
owner: masterAddress,
|
|
@@ -21410,7 +21460,17 @@ var Seamless = class {
|
|
|
21410
21460
|
r: brokerSignPermit.r,
|
|
21411
21461
|
s: brokerSignPermit.s
|
|
21412
21462
|
};
|
|
21413
|
-
|
|
21463
|
+
const forwarderPermitParams = {
|
|
21464
|
+
token: erc20Contract.target,
|
|
21465
|
+
owner: masterAddress,
|
|
21466
|
+
spender: forwarderAddress,
|
|
21467
|
+
value: ethers9.MaxUint256,
|
|
21468
|
+
deadline,
|
|
21469
|
+
v: forwarderSignPermit.v,
|
|
21470
|
+
r: forwarderSignPermit.r,
|
|
21471
|
+
s: forwarderSignPermit.s
|
|
21472
|
+
};
|
|
21473
|
+
return [brokerSeamlessUSDPermitParams, forwarderPermitParams];
|
|
21414
21474
|
} catch (error) {
|
|
21415
21475
|
throw new MyxSDKError("INVALID_PRIVATE_KEY" /* InvalidPrivateKey */, "Invalid private key generated");
|
|
21416
21476
|
}
|
|
@@ -21641,14 +21701,6 @@ var Seamless = class {
|
|
|
21641
21701
|
wallet,
|
|
21642
21702
|
authorized: isAuthorized
|
|
21643
21703
|
});
|
|
21644
|
-
const forwarderContract = await getForwarderContract(chainId);
|
|
21645
|
-
const erc20Address = getContractAddressByChainId(chainId).ERC20;
|
|
21646
|
-
await this.utils.approveAuthorization({
|
|
21647
|
-
chainId,
|
|
21648
|
-
quoteAddress: erc20Address,
|
|
21649
|
-
amount: ethers9.MaxUint256.toString(),
|
|
21650
|
-
spenderAddress: forwarderContract.target
|
|
21651
|
-
});
|
|
21652
21704
|
return {
|
|
21653
21705
|
code: 0,
|
|
21654
21706
|
data: {
|