@myx-trade/sdk 0.1.133 → 0.1.135
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 +10 -3
- package/dist/index.d.ts +10 -3
- package/dist/index.js +50 -51
- package/dist/index.mjs +50 -51
- package/package.json +1 -1
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.
|
|
1825
|
+
version: "0.1.135",
|
|
1826
1826
|
private: false,
|
|
1827
1827
|
publishConfig: {
|
|
1828
1828
|
access: "public"
|
|
@@ -12251,11 +12251,9 @@ var getLpPrice = async (chainId, poolId) => {
|
|
|
12251
12251
|
const pool = await getPoolInfo(chainId, poolId);
|
|
12252
12252
|
const contract = await getBasePoolContract(chainId);
|
|
12253
12253
|
let price = 0n;
|
|
12254
|
-
|
|
12255
|
-
|
|
12256
|
-
|
|
12257
|
-
price = (0, import_ethers13.parseUnits)(res.price, COMMON_PRICE_DECIMALS);
|
|
12258
|
-
}
|
|
12254
|
+
const res = await getPriceData(chainId, poolId);
|
|
12255
|
+
if (res?.price) {
|
|
12256
|
+
price = (0, import_ethers13.parseUnits)(res.price, COMMON_PRICE_DECIMALS);
|
|
12259
12257
|
}
|
|
12260
12258
|
const data = await contract.getPoolTokenPrice(poolId, price);
|
|
12261
12259
|
return data;
|
|
@@ -12618,11 +12616,9 @@ var getLpPrice2 = async (chainId, poolId) => {
|
|
|
12618
12616
|
const pool = await getPoolInfo(chainId, poolId);
|
|
12619
12617
|
const contract = await getQuotePoolContract(chainId);
|
|
12620
12618
|
let price = 0n;
|
|
12621
|
-
|
|
12622
|
-
|
|
12623
|
-
|
|
12624
|
-
price = (0, import_ethers19.parseUnits)(res.price, COMMON_PRICE_DECIMALS);
|
|
12625
|
-
}
|
|
12619
|
+
const res = await getPriceData(chainId, poolId);
|
|
12620
|
+
if (res?.price) {
|
|
12621
|
+
price = (0, import_ethers19.parseUnits)(res.price, COMMON_PRICE_DECIMALS);
|
|
12626
12622
|
}
|
|
12627
12623
|
const data = await contract.getPoolTokenPrice(poolId, price);
|
|
12628
12624
|
return data;
|
|
@@ -14311,15 +14307,7 @@ var Order = class {
|
|
|
14311
14307
|
params.executionFeeToken,
|
|
14312
14308
|
params.collateralAmount
|
|
14313
14309
|
);
|
|
14314
|
-
const
|
|
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));
|
|
14310
|
+
const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params.poolId, chainId: params.chainId, address: params.address });
|
|
14323
14311
|
const needAmount = BigInt(tradingFee) + BigInt(params.collateralAmount) + totalNetWorkFee;
|
|
14324
14312
|
let depositAmount = BigInt(0);
|
|
14325
14313
|
const diff = needAmount - availableAccountMarginBalance;
|
|
@@ -14492,17 +14480,20 @@ var Order = class {
|
|
|
14492
14480
|
};
|
|
14493
14481
|
}
|
|
14494
14482
|
}
|
|
14495
|
-
async closeAllPositions(chainId, params) {
|
|
14483
|
+
async closeAllPositions(chainId, params, tradingFee) {
|
|
14496
14484
|
try {
|
|
14497
14485
|
const config = this.configManager.getConfig();
|
|
14498
|
-
|
|
14499
|
-
throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
|
|
14500
|
-
}
|
|
14486
|
+
const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params[0].poolId, chainId, address: params[0].address });
|
|
14501
14487
|
const networkFee = await this.utils.getNetworkFee(
|
|
14502
14488
|
params[0].executionFeeToken,
|
|
14503
14489
|
chainId
|
|
14504
14490
|
);
|
|
14505
|
-
const
|
|
14491
|
+
const tradingFeeAmount = BigInt(tradingFee) * BigInt(params.length);
|
|
14492
|
+
const needAmount = tradingFeeAmount + BigInt(params.length) * BigInt(networkFee);
|
|
14493
|
+
let depositAmount = BigInt(0);
|
|
14494
|
+
if (availableAccountMarginBalance < needAmount) {
|
|
14495
|
+
depositAmount = needAmount - availableAccountMarginBalance;
|
|
14496
|
+
}
|
|
14506
14497
|
const needsApproval = await this.utils.needsApproval(
|
|
14507
14498
|
chainId,
|
|
14508
14499
|
params[0].executionFeeToken,
|
|
@@ -14579,6 +14570,9 @@ var Order = class {
|
|
|
14579
14570
|
data: rs
|
|
14580
14571
|
};
|
|
14581
14572
|
}
|
|
14573
|
+
if (!config.signer) {
|
|
14574
|
+
throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
|
|
14575
|
+
}
|
|
14582
14576
|
const brokerContract = await getBrokerSingerContract(
|
|
14583
14577
|
chainId,
|
|
14584
14578
|
this.configManager.getConfig().brokerAddress
|
|
@@ -14623,17 +14617,19 @@ var Order = class {
|
|
|
14623
14617
|
};
|
|
14624
14618
|
}
|
|
14625
14619
|
}
|
|
14626
|
-
async createDecreaseOrder(params) {
|
|
14620
|
+
async createDecreaseOrder(params, tradingFee) {
|
|
14627
14621
|
try {
|
|
14628
14622
|
const config = this.configManager.getConfig();
|
|
14629
|
-
const brokerContract = await getBrokerSingerContract(
|
|
14630
|
-
params.chainId,
|
|
14631
|
-
this.configManager.getConfig().brokerAddress
|
|
14632
|
-
);
|
|
14633
14623
|
const networkFee = await this.utils.getNetworkFee(
|
|
14634
14624
|
params.executionFeeToken,
|
|
14635
14625
|
params.chainId
|
|
14636
14626
|
);
|
|
14627
|
+
let depositAmount = BigInt(0);
|
|
14628
|
+
const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params.poolId, chainId: params.chainId, address: params.address });
|
|
14629
|
+
const needAmount = BigInt(tradingFee) + BigInt(networkFee);
|
|
14630
|
+
if (availableAccountMarginBalance < needAmount) {
|
|
14631
|
+
depositAmount = needAmount - availableAccountMarginBalance;
|
|
14632
|
+
}
|
|
14637
14633
|
const data = {
|
|
14638
14634
|
user: params.address,
|
|
14639
14635
|
poolId: params.poolId,
|
|
@@ -14656,12 +14652,12 @@ var Order = class {
|
|
|
14656
14652
|
};
|
|
14657
14653
|
const depositData = {
|
|
14658
14654
|
token: params.executionFeeToken,
|
|
14659
|
-
amount:
|
|
14655
|
+
amount: depositAmount.toString()
|
|
14660
14656
|
};
|
|
14661
14657
|
const needsApproval = await this.utils.needsApproval(
|
|
14662
14658
|
params.chainId,
|
|
14663
14659
|
params.executionFeeToken,
|
|
14664
|
-
|
|
14660
|
+
depositAmount.toString()
|
|
14665
14661
|
);
|
|
14666
14662
|
const authorized = this.configManager.getConfig().seamlessAccount?.authorized;
|
|
14667
14663
|
const seamlessWallet = this.configManager.getConfig().seamlessAccount?.wallet;
|
|
@@ -14712,13 +14708,16 @@ var Order = class {
|
|
|
14712
14708
|
};
|
|
14713
14709
|
this.logger.info("create decrease order forward tx params --->", forwardTxParams);
|
|
14714
14710
|
const rs = await this.seamless.forwarderTx(forwardTxParams, params.chainId, seamlessWallet);
|
|
14715
|
-
console.log("rs-->", rs);
|
|
14716
14711
|
return {
|
|
14717
14712
|
code: 0,
|
|
14718
14713
|
message: "create decrease order success",
|
|
14719
14714
|
data: rs
|
|
14720
14715
|
};
|
|
14721
14716
|
}
|
|
14717
|
+
const brokerContract = await getBrokerSingerContract(
|
|
14718
|
+
params.chainId,
|
|
14719
|
+
this.configManager.getConfig().brokerAddress
|
|
14720
|
+
);
|
|
14722
14721
|
if (!config.signer) {
|
|
14723
14722
|
throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
|
|
14724
14723
|
}
|
|
@@ -14734,16 +14733,12 @@ var Order = class {
|
|
|
14734
14733
|
}
|
|
14735
14734
|
let transaction;
|
|
14736
14735
|
if (!params.positionId) {
|
|
14737
|
-
const depositData2 = {
|
|
14738
|
-
token: params.executionFeeToken,
|
|
14739
|
-
amount: networkFee
|
|
14740
|
-
};
|
|
14741
14736
|
const positionId = 1;
|
|
14742
14737
|
this.logger.info("createDecreaseOrder salt position params--->", [positionId, { data }]);
|
|
14743
|
-
const gasLimit = await brokerContract.placeOrderWithSalt.estimateGas(positionId.toString(),
|
|
14738
|
+
const gasLimit = await brokerContract.placeOrderWithSalt.estimateGas(positionId.toString(), depositData, data);
|
|
14744
14739
|
transaction = await brokerContract.placeOrderWithSalt(
|
|
14745
14740
|
positionId.toString(),
|
|
14746
|
-
|
|
14741
|
+
depositData,
|
|
14747
14742
|
data,
|
|
14748
14743
|
{
|
|
14749
14744
|
gasLimit: gasLimit * 130n / 100n
|
|
@@ -21017,24 +21012,16 @@ var Account = class {
|
|
|
21017
21012
|
/**
|
|
21018
21013
|
* get tradable amount
|
|
21019
21014
|
*/
|
|
21020
|
-
async getTradableAmount({ poolId }) {
|
|
21021
|
-
const
|
|
21022
|
-
|
|
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);
|
|
21015
|
+
async getTradableAmount({ poolId, chainId, address }) {
|
|
21016
|
+
const contractAddress = getContractAddressByChainId(chainId);
|
|
21017
|
+
const provider = await getJSONProvider(chainId);
|
|
21030
21018
|
const accountContract = new import_ethers26.ethers.Contract(
|
|
21031
21019
|
contractAddress.Account,
|
|
21032
21020
|
Account_default,
|
|
21033
21021
|
provider
|
|
21034
21022
|
);
|
|
21035
21023
|
try {
|
|
21036
|
-
const
|
|
21037
|
-
const assets = await accountContract.getTradableAmount(targetAddress, poolId);
|
|
21024
|
+
const assets = await accountContract.getTradableAmount(address, poolId);
|
|
21038
21025
|
const data = {
|
|
21039
21026
|
profitIsReleased: assets[0],
|
|
21040
21027
|
freeAmount: assets[1],
|
|
@@ -21050,6 +21037,18 @@ var Account = class {
|
|
|
21050
21037
|
};
|
|
21051
21038
|
}
|
|
21052
21039
|
}
|
|
21040
|
+
async getAvailableMarginBalance({ poolId, chainId, address }) {
|
|
21041
|
+
const marginAccountBalanceRes = await this.getTradableAmount({ poolId, chainId, address });
|
|
21042
|
+
const marginAccountBalance = marginAccountBalanceRes?.data;
|
|
21043
|
+
if (marginAccountBalanceRes.code !== 0) {
|
|
21044
|
+
throw new MyxSDKError(
|
|
21045
|
+
"INSUFFICIENT_MARGIN_BALANCE" /* InsufficientMarginBalance */,
|
|
21046
|
+
"Insufficient margin balance"
|
|
21047
|
+
);
|
|
21048
|
+
}
|
|
21049
|
+
const availableAccountMarginBalance = BigInt(marginAccountBalance?.freeAmount.toString() ?? 0) + (!marginAccountBalance?.tradeableProfit ? BigInt(marginAccountBalance?.tradeableProfit) : BigInt(0));
|
|
21050
|
+
return availableAccountMarginBalance;
|
|
21051
|
+
}
|
|
21053
21052
|
async getTradeFlow(params) {
|
|
21054
21053
|
const accessToken = await this.configManager.getAccessToken();
|
|
21055
21054
|
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.
|
|
1735
|
+
version: "0.1.135",
|
|
1736
1736
|
private: false,
|
|
1737
1737
|
publishConfig: {
|
|
1738
1738
|
access: "public"
|
|
@@ -12161,11 +12161,9 @@ var getLpPrice = async (chainId, poolId) => {
|
|
|
12161
12161
|
const pool = await getPoolInfo(chainId, poolId);
|
|
12162
12162
|
const contract = await getBasePoolContract(chainId);
|
|
12163
12163
|
let price = 0n;
|
|
12164
|
-
|
|
12165
|
-
|
|
12166
|
-
|
|
12167
|
-
price = parseUnits10(res.price, COMMON_PRICE_DECIMALS);
|
|
12168
|
-
}
|
|
12164
|
+
const res = await getPriceData(chainId, poolId);
|
|
12165
|
+
if (res?.price) {
|
|
12166
|
+
price = parseUnits10(res.price, COMMON_PRICE_DECIMALS);
|
|
12169
12167
|
}
|
|
12170
12168
|
const data = await contract.getPoolTokenPrice(poolId, price);
|
|
12171
12169
|
return data;
|
|
@@ -12528,11 +12526,9 @@ var getLpPrice2 = async (chainId, poolId) => {
|
|
|
12528
12526
|
const pool = await getPoolInfo(chainId, poolId);
|
|
12529
12527
|
const contract = await getQuotePoolContract(chainId);
|
|
12530
12528
|
let price = 0n;
|
|
12531
|
-
|
|
12532
|
-
|
|
12533
|
-
|
|
12534
|
-
price = parseUnits16(res.price, COMMON_PRICE_DECIMALS);
|
|
12535
|
-
}
|
|
12529
|
+
const res = await getPriceData(chainId, poolId);
|
|
12530
|
+
if (res?.price) {
|
|
12531
|
+
price = parseUnits16(res.price, COMMON_PRICE_DECIMALS);
|
|
12536
12532
|
}
|
|
12537
12533
|
const data = await contract.getPoolTokenPrice(poolId, price);
|
|
12538
12534
|
return data;
|
|
@@ -14221,15 +14217,7 @@ var Order = class {
|
|
|
14221
14217
|
params.executionFeeToken,
|
|
14222
14218
|
params.collateralAmount
|
|
14223
14219
|
);
|
|
14224
|
-
const
|
|
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));
|
|
14220
|
+
const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params.poolId, chainId: params.chainId, address: params.address });
|
|
14233
14221
|
const needAmount = BigInt(tradingFee) + BigInt(params.collateralAmount) + totalNetWorkFee;
|
|
14234
14222
|
let depositAmount = BigInt(0);
|
|
14235
14223
|
const diff = needAmount - availableAccountMarginBalance;
|
|
@@ -14402,17 +14390,20 @@ var Order = class {
|
|
|
14402
14390
|
};
|
|
14403
14391
|
}
|
|
14404
14392
|
}
|
|
14405
|
-
async closeAllPositions(chainId, params) {
|
|
14393
|
+
async closeAllPositions(chainId, params, tradingFee) {
|
|
14406
14394
|
try {
|
|
14407
14395
|
const config = this.configManager.getConfig();
|
|
14408
|
-
|
|
14409
|
-
throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
|
|
14410
|
-
}
|
|
14396
|
+
const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params[0].poolId, chainId, address: params[0].address });
|
|
14411
14397
|
const networkFee = await this.utils.getNetworkFee(
|
|
14412
14398
|
params[0].executionFeeToken,
|
|
14413
14399
|
chainId
|
|
14414
14400
|
);
|
|
14415
|
-
const
|
|
14401
|
+
const tradingFeeAmount = BigInt(tradingFee) * BigInt(params.length);
|
|
14402
|
+
const needAmount = tradingFeeAmount + BigInt(params.length) * BigInt(networkFee);
|
|
14403
|
+
let depositAmount = BigInt(0);
|
|
14404
|
+
if (availableAccountMarginBalance < needAmount) {
|
|
14405
|
+
depositAmount = needAmount - availableAccountMarginBalance;
|
|
14406
|
+
}
|
|
14416
14407
|
const needsApproval = await this.utils.needsApproval(
|
|
14417
14408
|
chainId,
|
|
14418
14409
|
params[0].executionFeeToken,
|
|
@@ -14489,6 +14480,9 @@ var Order = class {
|
|
|
14489
14480
|
data: rs
|
|
14490
14481
|
};
|
|
14491
14482
|
}
|
|
14483
|
+
if (!config.signer) {
|
|
14484
|
+
throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
|
|
14485
|
+
}
|
|
14492
14486
|
const brokerContract = await getBrokerSingerContract(
|
|
14493
14487
|
chainId,
|
|
14494
14488
|
this.configManager.getConfig().brokerAddress
|
|
@@ -14533,17 +14527,19 @@ var Order = class {
|
|
|
14533
14527
|
};
|
|
14534
14528
|
}
|
|
14535
14529
|
}
|
|
14536
|
-
async createDecreaseOrder(params) {
|
|
14530
|
+
async createDecreaseOrder(params, tradingFee) {
|
|
14537
14531
|
try {
|
|
14538
14532
|
const config = this.configManager.getConfig();
|
|
14539
|
-
const brokerContract = await getBrokerSingerContract(
|
|
14540
|
-
params.chainId,
|
|
14541
|
-
this.configManager.getConfig().brokerAddress
|
|
14542
|
-
);
|
|
14543
14533
|
const networkFee = await this.utils.getNetworkFee(
|
|
14544
14534
|
params.executionFeeToken,
|
|
14545
14535
|
params.chainId
|
|
14546
14536
|
);
|
|
14537
|
+
let depositAmount = BigInt(0);
|
|
14538
|
+
const availableAccountMarginBalance = await this.account.getAvailableMarginBalance({ poolId: params.poolId, chainId: params.chainId, address: params.address });
|
|
14539
|
+
const needAmount = BigInt(tradingFee) + BigInt(networkFee);
|
|
14540
|
+
if (availableAccountMarginBalance < needAmount) {
|
|
14541
|
+
depositAmount = needAmount - availableAccountMarginBalance;
|
|
14542
|
+
}
|
|
14547
14543
|
const data = {
|
|
14548
14544
|
user: params.address,
|
|
14549
14545
|
poolId: params.poolId,
|
|
@@ -14566,12 +14562,12 @@ var Order = class {
|
|
|
14566
14562
|
};
|
|
14567
14563
|
const depositData = {
|
|
14568
14564
|
token: params.executionFeeToken,
|
|
14569
|
-
amount:
|
|
14565
|
+
amount: depositAmount.toString()
|
|
14570
14566
|
};
|
|
14571
14567
|
const needsApproval = await this.utils.needsApproval(
|
|
14572
14568
|
params.chainId,
|
|
14573
14569
|
params.executionFeeToken,
|
|
14574
|
-
|
|
14570
|
+
depositAmount.toString()
|
|
14575
14571
|
);
|
|
14576
14572
|
const authorized = this.configManager.getConfig().seamlessAccount?.authorized;
|
|
14577
14573
|
const seamlessWallet = this.configManager.getConfig().seamlessAccount?.wallet;
|
|
@@ -14622,13 +14618,16 @@ var Order = class {
|
|
|
14622
14618
|
};
|
|
14623
14619
|
this.logger.info("create decrease order forward tx params --->", forwardTxParams);
|
|
14624
14620
|
const rs = await this.seamless.forwarderTx(forwardTxParams, params.chainId, seamlessWallet);
|
|
14625
|
-
console.log("rs-->", rs);
|
|
14626
14621
|
return {
|
|
14627
14622
|
code: 0,
|
|
14628
14623
|
message: "create decrease order success",
|
|
14629
14624
|
data: rs
|
|
14630
14625
|
};
|
|
14631
14626
|
}
|
|
14627
|
+
const brokerContract = await getBrokerSingerContract(
|
|
14628
|
+
params.chainId,
|
|
14629
|
+
this.configManager.getConfig().brokerAddress
|
|
14630
|
+
);
|
|
14632
14631
|
if (!config.signer) {
|
|
14633
14632
|
throw new MyxSDKError("INVALID_SIGNER" /* InvalidSigner */, "Invalid signer");
|
|
14634
14633
|
}
|
|
@@ -14644,16 +14643,12 @@ var Order = class {
|
|
|
14644
14643
|
}
|
|
14645
14644
|
let transaction;
|
|
14646
14645
|
if (!params.positionId) {
|
|
14647
|
-
const depositData2 = {
|
|
14648
|
-
token: params.executionFeeToken,
|
|
14649
|
-
amount: networkFee
|
|
14650
|
-
};
|
|
14651
14646
|
const positionId = 1;
|
|
14652
14647
|
this.logger.info("createDecreaseOrder salt position params--->", [positionId, { data }]);
|
|
14653
|
-
const gasLimit = await brokerContract.placeOrderWithSalt.estimateGas(positionId.toString(),
|
|
14648
|
+
const gasLimit = await brokerContract.placeOrderWithSalt.estimateGas(positionId.toString(), depositData, data);
|
|
14654
14649
|
transaction = await brokerContract.placeOrderWithSalt(
|
|
14655
14650
|
positionId.toString(),
|
|
14656
|
-
|
|
14651
|
+
depositData,
|
|
14657
14652
|
data,
|
|
14658
14653
|
{
|
|
14659
14654
|
gasLimit: gasLimit * 130n / 100n
|
|
@@ -20927,24 +20922,16 @@ var Account = class {
|
|
|
20927
20922
|
/**
|
|
20928
20923
|
* get tradable amount
|
|
20929
20924
|
*/
|
|
20930
|
-
async getTradableAmount({ poolId }) {
|
|
20931
|
-
const
|
|
20932
|
-
|
|
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);
|
|
20925
|
+
async getTradableAmount({ poolId, chainId, address }) {
|
|
20926
|
+
const contractAddress = getContractAddressByChainId(chainId);
|
|
20927
|
+
const provider = await getJSONProvider(chainId);
|
|
20940
20928
|
const accountContract = new ethers8.Contract(
|
|
20941
20929
|
contractAddress.Account,
|
|
20942
20930
|
Account_default,
|
|
20943
20931
|
provider
|
|
20944
20932
|
);
|
|
20945
20933
|
try {
|
|
20946
|
-
const
|
|
20947
|
-
const assets = await accountContract.getTradableAmount(targetAddress, poolId);
|
|
20934
|
+
const assets = await accountContract.getTradableAmount(address, poolId);
|
|
20948
20935
|
const data = {
|
|
20949
20936
|
profitIsReleased: assets[0],
|
|
20950
20937
|
freeAmount: assets[1],
|
|
@@ -20960,6 +20947,18 @@ var Account = class {
|
|
|
20960
20947
|
};
|
|
20961
20948
|
}
|
|
20962
20949
|
}
|
|
20950
|
+
async getAvailableMarginBalance({ poolId, chainId, address }) {
|
|
20951
|
+
const marginAccountBalanceRes = await this.getTradableAmount({ poolId, chainId, address });
|
|
20952
|
+
const marginAccountBalance = marginAccountBalanceRes?.data;
|
|
20953
|
+
if (marginAccountBalanceRes.code !== 0) {
|
|
20954
|
+
throw new MyxSDKError(
|
|
20955
|
+
"INSUFFICIENT_MARGIN_BALANCE" /* InsufficientMarginBalance */,
|
|
20956
|
+
"Insufficient margin balance"
|
|
20957
|
+
);
|
|
20958
|
+
}
|
|
20959
|
+
const availableAccountMarginBalance = BigInt(marginAccountBalance?.freeAmount.toString() ?? 0) + (!marginAccountBalance?.tradeableProfit ? BigInt(marginAccountBalance?.tradeableProfit) : BigInt(0));
|
|
20960
|
+
return availableAccountMarginBalance;
|
|
20961
|
+
}
|
|
20963
20962
|
async getTradeFlow(params) {
|
|
20964
20963
|
const accessToken = await this.configManager.getAccessToken();
|
|
20965
20964
|
if (!accessToken) {
|