dipcoin-cli 0.6.0 → 0.8.0
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/cli.cjs +345 -44
- package/dist/cli.cjs.map +1 -1
- package/dist/index.cjs.js +2 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +2 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/dist/src/onchain/exchange.d.ts +41 -0
- package/dist/src/onchain/exchange.d.ts.map +1 -0
- package/dist/src/onchain/index.d.ts +5 -0
- package/dist/src/onchain/index.d.ts.map +1 -0
- package/dist/src/onchain/order-signer.d.ts +17 -0
- package/dist/src/onchain/order-signer.d.ts.map +1 -0
- package/dist/src/onchain/transaction-builder.d.ts +46 -0
- package/dist/src/onchain/transaction-builder.d.ts.map +1 -0
- package/dist/src/sdk/sdk.d.ts +0 -3
- package/dist/src/sdk/sdk.d.ts.map +1 -1
- package/package.json +1 -2
package/dist/cli.cjs
CHANGED
|
@@ -9,7 +9,6 @@ var path = require('path');
|
|
|
9
9
|
var os = require('os');
|
|
10
10
|
var fs = require('fs');
|
|
11
11
|
var ed25519 = require('@mysten/sui/keypairs/ed25519');
|
|
12
|
-
var perpTsLibrary = require('@dipcoinlab/perp-ts-library');
|
|
13
12
|
var transactions = require('@mysten/sui/transactions');
|
|
14
13
|
var BigNumber = require('bignumber.js');
|
|
15
14
|
var pythSuiJs = require('@pythnetwork/pyth-sui-js');
|
|
@@ -5017,7 +5016,7 @@ function asciiToBase16(ch) {
|
|
|
5017
5016
|
* Convert hex string to byte array. Uses built-in function, when available.
|
|
5018
5017
|
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
5019
5018
|
*/
|
|
5020
|
-
function hexToBytes(hex) {
|
|
5019
|
+
function hexToBytes$1(hex) {
|
|
5021
5020
|
if (typeof hex !== 'string')
|
|
5022
5021
|
throw new Error('hex string expected, got ' + typeof hex);
|
|
5023
5022
|
// @ts-ignore
|
|
@@ -9498,6 +9497,322 @@ class HttpClient {
|
|
|
9498
9497
|
}
|
|
9499
9498
|
}
|
|
9500
9499
|
|
|
9500
|
+
// Copyright (c) 2025 Dipcoin LLC
|
|
9501
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
9502
|
+
function computeOrderFlag(order) {
|
|
9503
|
+
let flag = 0;
|
|
9504
|
+
if (order.ioc)
|
|
9505
|
+
flag += 1;
|
|
9506
|
+
if (order.postOnly)
|
|
9507
|
+
flag += 2;
|
|
9508
|
+
if (order.reduceOnly)
|
|
9509
|
+
flag += 4;
|
|
9510
|
+
if (order.isLong)
|
|
9511
|
+
flag += 8;
|
|
9512
|
+
if (order.orderbookOnly)
|
|
9513
|
+
flag += 16;
|
|
9514
|
+
return flag;
|
|
9515
|
+
}
|
|
9516
|
+
function packPayload(fields) {
|
|
9517
|
+
let result = "{\n";
|
|
9518
|
+
for (const [key, value] of fields) {
|
|
9519
|
+
result += `"${key}":"${value}",\n`;
|
|
9520
|
+
}
|
|
9521
|
+
result += '"domain":"dipcoin.io"\n';
|
|
9522
|
+
result += "}";
|
|
9523
|
+
return result;
|
|
9524
|
+
}
|
|
9525
|
+
function getOrderMessageForUIWallet(order) {
|
|
9526
|
+
const flag = computeOrderFlag(order);
|
|
9527
|
+
const fields = [
|
|
9528
|
+
["market", `${order.market}`],
|
|
9529
|
+
["creator", `${order.creator}`],
|
|
9530
|
+
["isLong", `${order.isLong}`],
|
|
9531
|
+
["reduceOnly", `${order.reduceOnly}`],
|
|
9532
|
+
["postOnly", `${order.postOnly}`],
|
|
9533
|
+
["orderbookOnly", `${order.orderbookOnly}`],
|
|
9534
|
+
["ioc", `${order.ioc || false}`],
|
|
9535
|
+
["quantity", `${order.quantity.toFixed(0)}`],
|
|
9536
|
+
["price", `${order.price.toFixed(0)}`],
|
|
9537
|
+
["leverage", `${order.leverage.toFixed(0)}`],
|
|
9538
|
+
["expiration", `${order.expiration.toFixed(0)}`],
|
|
9539
|
+
["salt", `${order.salt.toFixed(0)}`],
|
|
9540
|
+
["orderFlag", `${flag}`],
|
|
9541
|
+
];
|
|
9542
|
+
return packPayload(fields);
|
|
9543
|
+
}
|
|
9544
|
+
|
|
9545
|
+
// Copyright (c) 2025 Dipcoin LLC
|
|
9546
|
+
const CLOCK_OBJECT_ID = "0x6";
|
|
9547
|
+
// ---------------------------------------------------------------------------
|
|
9548
|
+
// Deployment config helpers
|
|
9549
|
+
// ---------------------------------------------------------------------------
|
|
9550
|
+
function getPackageId(deployment) {
|
|
9551
|
+
const pkgs = deployment?.packages;
|
|
9552
|
+
if (!pkgs || !pkgs.length)
|
|
9553
|
+
throw new Error("Deployment config missing packages array");
|
|
9554
|
+
return pkgs[pkgs.length - 1];
|
|
9555
|
+
}
|
|
9556
|
+
function getProtocolConfigId(deployment) {
|
|
9557
|
+
const id = deployment?.objects?.ProtocolConfig?.id;
|
|
9558
|
+
if (!id)
|
|
9559
|
+
throw new Error("Deployment config missing ProtocolConfig id");
|
|
9560
|
+
return id;
|
|
9561
|
+
}
|
|
9562
|
+
function getBankId(deployment) {
|
|
9563
|
+
const id = deployment?.objects?.Bank?.id;
|
|
9564
|
+
if (!id)
|
|
9565
|
+
throw new Error("Deployment config missing Bank id");
|
|
9566
|
+
return id;
|
|
9567
|
+
}
|
|
9568
|
+
function getSubAccountsId(deployment) {
|
|
9569
|
+
const id = deployment?.objects?.SubAccounts?.id;
|
|
9570
|
+
if (!id)
|
|
9571
|
+
throw new Error("Deployment config missing SubAccounts id");
|
|
9572
|
+
return id;
|
|
9573
|
+
}
|
|
9574
|
+
function getTxIndexerId(deployment) {
|
|
9575
|
+
const id = deployment?.objects?.TxIndexer?.id;
|
|
9576
|
+
if (!id)
|
|
9577
|
+
throw new Error("Deployment config missing TxIndexer id");
|
|
9578
|
+
return id;
|
|
9579
|
+
}
|
|
9580
|
+
function getCurrencyType(deployment) {
|
|
9581
|
+
const dt = deployment?.objects?.Currency?.dataType;
|
|
9582
|
+
if (!dt)
|
|
9583
|
+
throw new Error("Deployment config missing Currency dataType");
|
|
9584
|
+
return dt;
|
|
9585
|
+
}
|
|
9586
|
+
function getPerpetualId(deployment, market) {
|
|
9587
|
+
const m = market?.toUpperCase();
|
|
9588
|
+
const id = deployment?.markets?.[m]?.Objects?.Perpetual?.id;
|
|
9589
|
+
if (!id)
|
|
9590
|
+
throw new Error(`Deployment config missing Perpetual id for ${m}`);
|
|
9591
|
+
return id;
|
|
9592
|
+
}
|
|
9593
|
+
function getPriceOracleObjectId(deployment, market) {
|
|
9594
|
+
const m = market?.toUpperCase() || "ETH-PERP";
|
|
9595
|
+
const id = deployment?.markets?.[m]?.Objects?.PriceInfoObject?.id;
|
|
9596
|
+
if (!id)
|
|
9597
|
+
throw new Error(`Deployment config missing PriceInfoObject id for ${m}`);
|
|
9598
|
+
return id;
|
|
9599
|
+
}
|
|
9600
|
+
function getPriceOracleFeedId(deployment, market) {
|
|
9601
|
+
const m = market?.toUpperCase() || "ETH-PERP";
|
|
9602
|
+
const feedId = deployment?.markets?.[m]?.Config?.priceInfoFeedId;
|
|
9603
|
+
if (!feedId)
|
|
9604
|
+
throw new Error(`Deployment config missing priceInfoFeedId for ${m}`);
|
|
9605
|
+
return feedId;
|
|
9606
|
+
}
|
|
9607
|
+
function getPythPkgId(deployment, market) {
|
|
9608
|
+
const m = market?.toUpperCase() || "ETH-PERP";
|
|
9609
|
+
const dataType = deployment?.markets?.[m]?.Objects?.PriceInfoObject?.dataType;
|
|
9610
|
+
if (!dataType)
|
|
9611
|
+
throw new Error(`Deployment config missing PriceInfoObject dataType for ${m}`);
|
|
9612
|
+
return dataType.split("::")[0];
|
|
9613
|
+
}
|
|
9614
|
+
function hexToBytes(hex) {
|
|
9615
|
+
const h = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
9616
|
+
const bytes = [];
|
|
9617
|
+
for (let i = 0; i < h.length; i += 2) {
|
|
9618
|
+
bytes.push(parseInt(h.substr(i, 2), 16));
|
|
9619
|
+
}
|
|
9620
|
+
return bytes;
|
|
9621
|
+
}
|
|
9622
|
+
function toBigIntStr(value, decimals) {
|
|
9623
|
+
// Equivalent to library's J(value, decimals) → multiply by 10^decimals and return integer string
|
|
9624
|
+
const factor = Math.pow(10, decimals);
|
|
9625
|
+
return Math.round(value * factor).toString();
|
|
9626
|
+
}
|
|
9627
|
+
// ---------------------------------------------------------------------------
|
|
9628
|
+
// Transaction builders
|
|
9629
|
+
// ---------------------------------------------------------------------------
|
|
9630
|
+
function buildAddMarginTx(deployment, args, tx, gasBudget, sender) {
|
|
9631
|
+
const t = tx || new transactions.Transaction();
|
|
9632
|
+
if (gasBudget)
|
|
9633
|
+
t.setGasBudget(gasBudget);
|
|
9634
|
+
if (sender)
|
|
9635
|
+
t.setSender(sender);
|
|
9636
|
+
const packageId = getPackageId(deployment);
|
|
9637
|
+
t.moveCall({
|
|
9638
|
+
target: `${packageId}::exchange::add_margin_v2`,
|
|
9639
|
+
arguments: [
|
|
9640
|
+
t.object(getProtocolConfigId(deployment)),
|
|
9641
|
+
t.object(CLOCK_OBJECT_ID),
|
|
9642
|
+
t.object(args.perpID || getPerpetualId(deployment, args.market)),
|
|
9643
|
+
t.object(getBankId(deployment)),
|
|
9644
|
+
t.object(args.subAccountsMapID || getSubAccountsId(deployment)),
|
|
9645
|
+
t.object(getTxIndexerId(deployment)),
|
|
9646
|
+
t.object(getPriceOracleObjectId(deployment, args.market)),
|
|
9647
|
+
t.pure.address(args.account || sender || ""),
|
|
9648
|
+
t.pure.u128(args.amount.toString()),
|
|
9649
|
+
],
|
|
9650
|
+
typeArguments: [getCurrencyType(deployment)],
|
|
9651
|
+
});
|
|
9652
|
+
return t;
|
|
9653
|
+
}
|
|
9654
|
+
function buildRemoveMarginTx(deployment, args, tx, gasBudget, sender) {
|
|
9655
|
+
const t = tx || new transactions.Transaction();
|
|
9656
|
+
if (gasBudget)
|
|
9657
|
+
t.setGasBudget(gasBudget);
|
|
9658
|
+
if (sender)
|
|
9659
|
+
t.setSender(sender);
|
|
9660
|
+
const packageId = getPackageId(deployment);
|
|
9661
|
+
t.moveCall({
|
|
9662
|
+
target: `${packageId}::exchange::remove_margin_v2`,
|
|
9663
|
+
arguments: [
|
|
9664
|
+
t.object(getProtocolConfigId(deployment)),
|
|
9665
|
+
t.object(CLOCK_OBJECT_ID),
|
|
9666
|
+
t.object(args.perpID || getPerpetualId(deployment, args.market)),
|
|
9667
|
+
t.object(getBankId(deployment)),
|
|
9668
|
+
t.object(args.subAccountsMapID || getSubAccountsId(deployment)),
|
|
9669
|
+
t.object(getTxIndexerId(deployment)),
|
|
9670
|
+
t.object(getPriceOracleObjectId(deployment, args.market)),
|
|
9671
|
+
t.pure.address(args.account || sender || ""),
|
|
9672
|
+
t.pure.u128(args.amount.toString()),
|
|
9673
|
+
],
|
|
9674
|
+
typeArguments: [getCurrencyType(deployment)],
|
|
9675
|
+
});
|
|
9676
|
+
return t;
|
|
9677
|
+
}
|
|
9678
|
+
function buildDepositTx(deployment, args, tx, gasBudget, sender) {
|
|
9679
|
+
const t = tx || new transactions.Transaction();
|
|
9680
|
+
if (gasBudget)
|
|
9681
|
+
t.setGasBudget(gasBudget);
|
|
9682
|
+
if (sender)
|
|
9683
|
+
t.setSender(sender);
|
|
9684
|
+
// The library uses a coin-splitting intent for deposit.
|
|
9685
|
+
// We replicate the same Move call structure: bank::deposit_v2
|
|
9686
|
+
// with a SplitCoins + the amount.
|
|
9687
|
+
const coinType = getCurrencyType(deployment);
|
|
9688
|
+
const coin = t.splitCoins(t.gas, [BigInt(args.amount)]);
|
|
9689
|
+
const packageId = getPackageId(deployment);
|
|
9690
|
+
t.moveCall({
|
|
9691
|
+
target: `${packageId}::bank::deposit_v2`,
|
|
9692
|
+
arguments: [
|
|
9693
|
+
t.object(getProtocolConfigId(deployment)),
|
|
9694
|
+
t.object(args.bankID || getBankId(deployment)),
|
|
9695
|
+
t.object(getTxIndexerId(deployment)),
|
|
9696
|
+
t.pure.address(args.accountAddress || sender || ""),
|
|
9697
|
+
t.pure.u64(args.amount),
|
|
9698
|
+
coin,
|
|
9699
|
+
],
|
|
9700
|
+
typeArguments: [coinType],
|
|
9701
|
+
});
|
|
9702
|
+
return t;
|
|
9703
|
+
}
|
|
9704
|
+
function buildWithdrawTx(deployment, args, tx, gasBudget, sender) {
|
|
9705
|
+
const t = tx || new transactions.Transaction();
|
|
9706
|
+
if (gasBudget)
|
|
9707
|
+
t.setGasBudget(gasBudget);
|
|
9708
|
+
if (sender)
|
|
9709
|
+
t.setSender(sender);
|
|
9710
|
+
const packageId = getPackageId(deployment);
|
|
9711
|
+
t.moveCall({
|
|
9712
|
+
target: `${packageId}::bank::withdraw_v2`,
|
|
9713
|
+
arguments: [
|
|
9714
|
+
t.object(getProtocolConfigId(deployment)),
|
|
9715
|
+
t.object(args.bankID || getBankId(deployment)),
|
|
9716
|
+
t.object(getTxIndexerId(deployment)),
|
|
9717
|
+
t.pure.address(args.accountAddress || sender || ""),
|
|
9718
|
+
t.pure.u128(args.amount),
|
|
9719
|
+
],
|
|
9720
|
+
typeArguments: [getCurrencyType(deployment)],
|
|
9721
|
+
});
|
|
9722
|
+
return t;
|
|
9723
|
+
}
|
|
9724
|
+
function buildSetSubAccountTx(deployment, args, tx, gasBudget, sender) {
|
|
9725
|
+
const t = tx || new transactions.Transaction();
|
|
9726
|
+
if (gasBudget)
|
|
9727
|
+
t.setGasBudget(gasBudget);
|
|
9728
|
+
if (sender)
|
|
9729
|
+
t.setSender(sender);
|
|
9730
|
+
const packageId = getPackageId(deployment);
|
|
9731
|
+
t.moveCall({
|
|
9732
|
+
target: `${packageId}::sub_accounts::set_sub_account`,
|
|
9733
|
+
arguments: [
|
|
9734
|
+
t.object(getProtocolConfigId(deployment)),
|
|
9735
|
+
t.object(args.subAccountsMapID || getSubAccountsId(deployment)),
|
|
9736
|
+
t.pure.address(args.account),
|
|
9737
|
+
t.pure.bool(args.status),
|
|
9738
|
+
],
|
|
9739
|
+
typeArguments: [],
|
|
9740
|
+
});
|
|
9741
|
+
return t;
|
|
9742
|
+
}
|
|
9743
|
+
function buildSetOraclePriceTx(deployment, args, tx, gasBudget, sender) {
|
|
9744
|
+
const t = tx || new transactions.Transaction();
|
|
9745
|
+
if (gasBudget)
|
|
9746
|
+
t.setGasBudget(gasBudget);
|
|
9747
|
+
if (sender)
|
|
9748
|
+
t.setSender(sender);
|
|
9749
|
+
const pythPkgId = getPythPkgId(deployment, args.market);
|
|
9750
|
+
const oracleObjId = getPriceOracleObjectId(deployment, args.market);
|
|
9751
|
+
const feedIdHex = getPriceOracleFeedId(deployment, args.market);
|
|
9752
|
+
const feedIdBytes = hexToBytes(feedIdHex);
|
|
9753
|
+
const priceU64 = toBigIntStr(args.price, 6);
|
|
9754
|
+
t.moveCall({
|
|
9755
|
+
target: `${pythPkgId}::price_info::update_price_info_object_for_test`,
|
|
9756
|
+
arguments: [
|
|
9757
|
+
t.object(oracleObjId),
|
|
9758
|
+
t.object(CLOCK_OBJECT_ID),
|
|
9759
|
+
t.pure.u64(priceU64),
|
|
9760
|
+
t.pure.u64(args.confidence || "10"),
|
|
9761
|
+
t.pure(suiBcs.vector(suiBcs.u8()).serialize(new Uint8Array(feedIdBytes))),
|
|
9762
|
+
],
|
|
9763
|
+
typeArguments: [],
|
|
9764
|
+
});
|
|
9765
|
+
return t;
|
|
9766
|
+
}
|
|
9767
|
+
function buildBatchSetOraclePriceTx(deployment, args, tx, gasBudget, sender) {
|
|
9768
|
+
let t = tx || new transactions.Transaction();
|
|
9769
|
+
for (const priceArgs of args.prices) {
|
|
9770
|
+
t = buildSetOraclePriceTx(deployment, priceArgs, t, undefined, undefined);
|
|
9771
|
+
}
|
|
9772
|
+
if (gasBudget)
|
|
9773
|
+
t.setGasBudget(gasBudget);
|
|
9774
|
+
if (sender)
|
|
9775
|
+
t.setSender(sender);
|
|
9776
|
+
return t;
|
|
9777
|
+
}
|
|
9778
|
+
|
|
9779
|
+
// Copyright (c) 2025 Dipcoin LLC
|
|
9780
|
+
async function executeTxBlock(suiClient, tx, signer) {
|
|
9781
|
+
return suiClient.signAndExecuteTransaction({
|
|
9782
|
+
signer,
|
|
9783
|
+
transaction: tx,
|
|
9784
|
+
options: {
|
|
9785
|
+
showObjectChanges: true,
|
|
9786
|
+
showEffects: true,
|
|
9787
|
+
showEvents: true,
|
|
9788
|
+
showInput: true,
|
|
9789
|
+
},
|
|
9790
|
+
});
|
|
9791
|
+
}
|
|
9792
|
+
function getDeploymentPerpetualID(deployment, market) {
|
|
9793
|
+
return getPerpetualId(deployment, market);
|
|
9794
|
+
}
|
|
9795
|
+
async function getOraclePrice(suiClient, deployment, market) {
|
|
9796
|
+
const objId = getPriceOracleObjectId(deployment, market);
|
|
9797
|
+
const obj = await suiClient.getObject({ id: objId, options: { showContent: true } });
|
|
9798
|
+
const fields = obj.data?.content?.fields?.price_info?.fields?.price_feed?.fields?.price?.fields;
|
|
9799
|
+
if (!fields)
|
|
9800
|
+
throw new Error(`Failed to read oracle price for ${market}`);
|
|
9801
|
+
return Number(fields.price.fields.magnitude) / Math.pow(10, Number(fields.expo.fields.magnitude));
|
|
9802
|
+
}
|
|
9803
|
+
async function depositToBank(suiClient, deployment, args, signer) {
|
|
9804
|
+
const tx = buildDepositTx(deployment, args, undefined, args.gasBudget, signer.getPublicKey().toSuiAddress());
|
|
9805
|
+
return executeTxBlock(suiClient, tx, signer);
|
|
9806
|
+
}
|
|
9807
|
+
async function withdrawFromBank(suiClient, deployment, args, signer) {
|
|
9808
|
+
const tx = buildWithdrawTx(deployment, args, undefined, args.gasBudget, signer.getPublicKey().toSuiAddress());
|
|
9809
|
+
return executeTxBlock(suiClient, tx, signer);
|
|
9810
|
+
}
|
|
9811
|
+
async function setSubAccount(suiClient, deployment, args, signer) {
|
|
9812
|
+
const tx = buildSetSubAccountTx(deployment, args, undefined, args.gasBudget, signer.getPublicKey().toSuiAddress());
|
|
9813
|
+
return executeTxBlock(suiClient, tx, signer);
|
|
9814
|
+
}
|
|
9815
|
+
|
|
9501
9816
|
// Copyright (c) 2025 Dipcoin LLC
|
|
9502
9817
|
// SPDX-License-Identifier: Apache-2.0
|
|
9503
9818
|
/**
|
|
@@ -9935,7 +10250,7 @@ function bytesToNumberLE(bytes) {
|
|
|
9935
10250
|
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
9936
10251
|
}
|
|
9937
10252
|
function numberToBytesBE(n, len) {
|
|
9938
|
-
return hexToBytes(n.toString(16).padStart(len * 2, '0'));
|
|
10253
|
+
return hexToBytes$1(n.toString(16).padStart(len * 2, '0'));
|
|
9939
10254
|
}
|
|
9940
10255
|
function numberToBytesLE(n, len) {
|
|
9941
10256
|
return numberToBytesBE(n, len).reverse();
|
|
@@ -9953,7 +10268,7 @@ function ensureBytes(title, hex, expectedLength) {
|
|
|
9953
10268
|
let res;
|
|
9954
10269
|
if (typeof hex === 'string') {
|
|
9955
10270
|
try {
|
|
9956
|
-
res = hexToBytes(hex);
|
|
10271
|
+
res = hexToBytes$1(hex);
|
|
9957
10272
|
}
|
|
9958
10273
|
catch (e) {
|
|
9959
10274
|
throw new Error(title + ' must be hex string or Uint8Array, cause: ' + e);
|
|
@@ -11699,7 +12014,7 @@ function ecdsa(Point, hash, ecdsaOpts = {}) {
|
|
|
11699
12014
|
throw new Error('invalid format');
|
|
11700
12015
|
}
|
|
11701
12016
|
static fromHex(hex, format) {
|
|
11702
|
-
return this.fromBytes(hexToBytes(hex), format);
|
|
12017
|
+
return this.fromBytes(hexToBytes$1(hex), format);
|
|
11703
12018
|
}
|
|
11704
12019
|
addRecoveryBit(recovery) {
|
|
11705
12020
|
return new Signature(this.r, this.s, recovery);
|
|
@@ -11748,7 +12063,7 @@ function ecdsa(Point, hash, ecdsaOpts = {}) {
|
|
|
11748
12063
|
if (format === 'compact')
|
|
11749
12064
|
return concatBytes(Fn.toBytes(this.r), Fn.toBytes(this.s));
|
|
11750
12065
|
if (format === 'der')
|
|
11751
|
-
return hexToBytes(DER.hexFromSig(this));
|
|
12066
|
+
return hexToBytes$1(DER.hexFromSig(this));
|
|
11752
12067
|
throw new Error('invalid format');
|
|
11753
12068
|
}
|
|
11754
12069
|
toHex(format) {
|
|
@@ -12381,7 +12696,7 @@ function bytesToNumber(bytes) {
|
|
|
12381
12696
|
function numberToBytes(num) {
|
|
12382
12697
|
if (typeof num !== 'bigint')
|
|
12383
12698
|
throw new Error('bigint expected');
|
|
12384
|
-
return hexToBytes(num.toString(16).padStart(64, '0'));
|
|
12699
|
+
return hexToBytes$1(num.toString(16).padStart(64, '0'));
|
|
12385
12700
|
}
|
|
12386
12701
|
const MASTER_SECRET = utf8ToBytes('Bitcoin seed');
|
|
12387
12702
|
// Bitcoin hardcoded by default
|
|
@@ -15324,7 +15639,7 @@ function findFirstNonZeroIndex(bytes) {
|
|
|
15324
15639
|
}
|
|
15325
15640
|
function toPaddedBigEndianBytes(num, width) {
|
|
15326
15641
|
const hex = num.toString(16);
|
|
15327
|
-
return hexToBytes(hex.padStart(width * 2, "0").slice(-width * 2));
|
|
15642
|
+
return hexToBytes$1(hex.padStart(width * 2, "0").slice(-width * 2));
|
|
15328
15643
|
}
|
|
15329
15644
|
function toBigEndianBytes(num, width) {
|
|
15330
15645
|
const bytes = toPaddedBigEndianBytes(num, width);
|
|
@@ -16607,10 +16922,8 @@ class DipCoinPerpSDK {
|
|
|
16607
16922
|
subJwtToken;
|
|
16608
16923
|
isAuthenticating = false;
|
|
16609
16924
|
isSubAuthenticating = false;
|
|
16610
|
-
exchangeOnChain;
|
|
16611
16925
|
deploymentConfig;
|
|
16612
16926
|
suiClient;
|
|
16613
|
-
transactionBuilder;
|
|
16614
16927
|
priceServiceConnection;
|
|
16615
16928
|
pythClient;
|
|
16616
16929
|
tradingPairsCache;
|
|
@@ -16646,10 +16959,6 @@ class DipCoinPerpSDK {
|
|
|
16646
16959
|
this.deploymentConfig = readFile(`config/deployed/${options.network}/main_contract.json`);
|
|
16647
16960
|
const rpcUrl = options.customRpc || getFullnodeUrl(options.network);
|
|
16648
16961
|
this.suiClient = new SuiJsonRpcClient({ url: rpcUrl });
|
|
16649
|
-
this.exchangeOnChain = new perpTsLibrary.ExchangeOnChain(this.deploymentConfig, this.suiClient, this.keypair);
|
|
16650
|
-
const packageId = this.getDeploymentPackageId();
|
|
16651
|
-
const protocolConfigId = this.getDeploymentProtocolConfigId();
|
|
16652
|
-
this.transactionBuilder = new perpTsLibrary.TransactionBuilder(packageId, protocolConfigId, this.deploymentConfig, this.suiClient);
|
|
16653
16962
|
}
|
|
16654
16963
|
/**
|
|
16655
16964
|
* Get wallet address
|
|
@@ -16923,21 +17232,21 @@ class DipCoinPerpSDK {
|
|
|
16923
17232
|
};
|
|
16924
17233
|
}
|
|
16925
17234
|
// Generate order message for signing
|
|
16926
|
-
const orderMsg =
|
|
17235
|
+
const orderMsg = getOrderMessageForUIWallet(order);
|
|
16927
17236
|
const orderHashBytes = new TextEncoder().encode(orderMsg);
|
|
16928
17237
|
// Sign main order
|
|
16929
17238
|
const orderSignature = await signMessage(signingKeypair, orderHashBytes);
|
|
16930
17239
|
// Sign TP order if exists
|
|
16931
17240
|
let tpOrderSignature;
|
|
16932
17241
|
if (tpOrder) {
|
|
16933
|
-
const tpOrderMsg =
|
|
17242
|
+
const tpOrderMsg = getOrderMessageForUIWallet(tpOrder);
|
|
16934
17243
|
const tpOrderHashBytes = new TextEncoder().encode(tpOrderMsg);
|
|
16935
17244
|
tpOrderSignature = await signMessage(signingKeypair, tpOrderHashBytes);
|
|
16936
17245
|
}
|
|
16937
17246
|
// Sign SL order if exists
|
|
16938
17247
|
let slOrderSignature;
|
|
16939
17248
|
if (slOrder) {
|
|
16940
|
-
const slOrderMsg =
|
|
17249
|
+
const slOrderMsg = getOrderMessageForUIWallet(slOrder);
|
|
16941
17250
|
const slOrderHashBytes = new TextEncoder().encode(slOrderMsg);
|
|
16942
17251
|
slOrderSignature = await signMessage(signingKeypair, slOrderHashBytes);
|
|
16943
17252
|
}
|
|
@@ -17325,7 +17634,7 @@ class DipCoinPerpSDK {
|
|
|
17325
17634
|
expiration: expirationBN,
|
|
17326
17635
|
salt: tpSaltValue,
|
|
17327
17636
|
};
|
|
17328
|
-
const tpOrderMsg =
|
|
17637
|
+
const tpOrderMsg = getOrderMessageForUIWallet(tpOrder);
|
|
17329
17638
|
const tpOrderSignature = await signMessage(signingKeypair, new TextEncoder().encode(tpOrderMsg));
|
|
17330
17639
|
tpPayload = {
|
|
17331
17640
|
...planPayloadBase,
|
|
@@ -17370,7 +17679,7 @@ class DipCoinPerpSDK {
|
|
|
17370
17679
|
expiration: expirationBN,
|
|
17371
17680
|
salt: slSaltValue,
|
|
17372
17681
|
};
|
|
17373
|
-
const slOrderMsg =
|
|
17682
|
+
const slOrderMsg = getOrderMessageForUIWallet(slOrder);
|
|
17374
17683
|
const slOrderSignature = await signMessage(signingKeypair, new TextEncoder().encode(slOrderMsg));
|
|
17375
17684
|
slPayload = {
|
|
17376
17685
|
...planPayloadBase,
|
|
@@ -18251,10 +18560,11 @@ class DipCoinPerpSDK {
|
|
|
18251
18560
|
async addMargin(params) {
|
|
18252
18561
|
const transaction = await this.buildMarginTransaction(params, "add");
|
|
18253
18562
|
if (transaction) {
|
|
18254
|
-
return this.
|
|
18563
|
+
return executeTxBlock(this.suiClient, transaction, this.keypair);
|
|
18255
18564
|
}
|
|
18256
18565
|
const fallbackPayload = this.buildMarginCallArgs(params, "add");
|
|
18257
|
-
|
|
18566
|
+
const tx = buildAddMarginTx(this.deploymentConfig, fallbackPayload, undefined, fallbackPayload.gasBudget, this.keypair.getPublicKey().toSuiAddress());
|
|
18567
|
+
return executeTxBlock(this.suiClient, tx, this.keypair);
|
|
18258
18568
|
}
|
|
18259
18569
|
/**
|
|
18260
18570
|
* Remove isolated margin from an existing position (on-chain)
|
|
@@ -18263,10 +18573,11 @@ class DipCoinPerpSDK {
|
|
|
18263
18573
|
async removeMargin(params) {
|
|
18264
18574
|
const transaction = await this.buildMarginTransaction(params, "remove");
|
|
18265
18575
|
if (transaction) {
|
|
18266
|
-
return this.
|
|
18576
|
+
return executeTxBlock(this.suiClient, transaction, this.keypair);
|
|
18267
18577
|
}
|
|
18268
18578
|
const fallbackPayload = this.buildMarginCallArgs(params, "remove");
|
|
18269
|
-
|
|
18579
|
+
const tx = buildRemoveMarginTx(this.deploymentConfig, fallbackPayload, undefined, fallbackPayload.gasBudget, this.keypair.getPublicKey().toSuiAddress());
|
|
18580
|
+
return executeTxBlock(this.suiClient, tx, this.keypair);
|
|
18270
18581
|
}
|
|
18271
18582
|
/**
|
|
18272
18583
|
* Build ExchangeOnChain call args for margin adjustments
|
|
@@ -18296,13 +18607,6 @@ class DipCoinPerpSDK {
|
|
|
18296
18607
|
txHash,
|
|
18297
18608
|
};
|
|
18298
18609
|
}
|
|
18299
|
-
getDeploymentPackageId() {
|
|
18300
|
-
const packages = this.deploymentConfig?.packages;
|
|
18301
|
-
if (!packages || !packages.length) {
|
|
18302
|
-
throw new Error("Deployment config missing packages array");
|
|
18303
|
-
}
|
|
18304
|
-
return packages[packages.length - 1];
|
|
18305
|
-
}
|
|
18306
18610
|
getDeploymentProtocolConfigId() {
|
|
18307
18611
|
const protocolId = this.deploymentConfig?.objects?.ProtocolConfig?.id;
|
|
18308
18612
|
if (!protocolId) {
|
|
@@ -18311,18 +18615,15 @@ class DipCoinPerpSDK {
|
|
|
18311
18615
|
return protocolId;
|
|
18312
18616
|
}
|
|
18313
18617
|
async buildMarginTransaction(params, action) {
|
|
18314
|
-
if (!this.transactionBuilder) {
|
|
18315
|
-
return undefined;
|
|
18316
|
-
}
|
|
18317
18618
|
const payload = this.buildMarginCallArgs(params, action);
|
|
18318
18619
|
const updatePriceTx = payload.market
|
|
18319
18620
|
? await this.buildUpdatePriceTransaction(payload.market)
|
|
18320
18621
|
: undefined;
|
|
18321
18622
|
const baseTx = updatePriceTx || new transactions.Transaction();
|
|
18322
18623
|
if (action === "add") {
|
|
18323
|
-
return this.
|
|
18624
|
+
return buildAddMarginTx(this.deploymentConfig, payload, baseTx, params.gasBudget);
|
|
18324
18625
|
}
|
|
18325
|
-
return this.
|
|
18626
|
+
return buildRemoveMarginTx(this.deploymentConfig, payload, baseTx, params.gasBudget);
|
|
18326
18627
|
}
|
|
18327
18628
|
async buildUpdatePriceTransaction(symbol) {
|
|
18328
18629
|
if (!symbol) {
|
|
@@ -18365,11 +18666,11 @@ class DipCoinPerpSDK {
|
|
|
18365
18666
|
}
|
|
18366
18667
|
async buildTestnetPriceUpdateTransaction(symbol) {
|
|
18367
18668
|
try {
|
|
18368
|
-
const oraclePrice = await this.
|
|
18669
|
+
const oraclePrice = await getOraclePrice(this.suiClient, this.deploymentConfig, symbol);
|
|
18369
18670
|
if (oraclePrice === undefined || oraclePrice === null) {
|
|
18370
18671
|
return undefined;
|
|
18371
18672
|
}
|
|
18372
|
-
return this.
|
|
18673
|
+
return buildSetOraclePriceTx(this.deploymentConfig, {
|
|
18373
18674
|
price: Number(oraclePrice),
|
|
18374
18675
|
market: symbol,
|
|
18375
18676
|
});
|
|
@@ -18464,7 +18765,7 @@ class DipCoinPerpSDK {
|
|
|
18464
18765
|
*/
|
|
18465
18766
|
resolvePerpIdFromDeployment(market) {
|
|
18466
18767
|
try {
|
|
18467
|
-
const perpId = this.
|
|
18768
|
+
const perpId = getDeploymentPerpetualID(this.deploymentConfig, market);
|
|
18468
18769
|
return perpId || undefined;
|
|
18469
18770
|
}
|
|
18470
18771
|
catch (error) {
|
|
@@ -18478,7 +18779,7 @@ class DipCoinPerpSDK {
|
|
|
18478
18779
|
* @returns On-chain transaction result
|
|
18479
18780
|
*/
|
|
18480
18781
|
async setSubAccount(subAddress) {
|
|
18481
|
-
return await this.
|
|
18782
|
+
return await setSubAccount(this.suiClient, this.deploymentConfig, {
|
|
18482
18783
|
account: subAddress,
|
|
18483
18784
|
status: true,
|
|
18484
18785
|
}, this.keypair);
|
|
@@ -18494,7 +18795,7 @@ class DipCoinPerpSDK {
|
|
|
18494
18795
|
* ```
|
|
18495
18796
|
*/
|
|
18496
18797
|
async depositToBank(amount) {
|
|
18497
|
-
return await this.
|
|
18798
|
+
return await depositToBank(this.suiClient, this.deploymentConfig, {
|
|
18498
18799
|
amount: formatNormalToWei(amount, DECIMALS.USDC),
|
|
18499
18800
|
accountAddress: this.address,
|
|
18500
18801
|
}, this.keypair);
|
|
@@ -18510,7 +18811,7 @@ class DipCoinPerpSDK {
|
|
|
18510
18811
|
* ```
|
|
18511
18812
|
*/
|
|
18512
18813
|
async withdrawFromBank(amount) {
|
|
18513
|
-
return await this.
|
|
18814
|
+
return await withdrawFromBank(this.suiClient, this.deploymentConfig, {
|
|
18514
18815
|
amount: formatNormalToWei(amount, DECIMALS.USDC),
|
|
18515
18816
|
accountAddress: this.address,
|
|
18516
18817
|
}, this.keypair);
|
|
@@ -18610,11 +18911,11 @@ class DipCoinPerpSDK {
|
|
|
18610
18911
|
const currencyType = this.getCurrencyType();
|
|
18611
18912
|
const symbols = markets && markets.length > 0 ? markets : this.getMarketSymbols();
|
|
18612
18913
|
// On testnet, prepend price oracle updates so PriceInfoObjects are fresh
|
|
18613
|
-
if (this.options.network === "testnet"
|
|
18914
|
+
if (this.options.network === "testnet") {
|
|
18614
18915
|
const prices = [];
|
|
18615
18916
|
for (const sym of symbols) {
|
|
18616
18917
|
try {
|
|
18617
|
-
const oraclePrice = await this.
|
|
18918
|
+
const oraclePrice = await getOraclePrice(this.suiClient, this.deploymentConfig, sym);
|
|
18618
18919
|
if (oraclePrice !== undefined && oraclePrice !== null) {
|
|
18619
18920
|
prices.push({ price: Number(oraclePrice), market: sym });
|
|
18620
18921
|
}
|
|
@@ -18624,7 +18925,7 @@ class DipCoinPerpSDK {
|
|
|
18624
18925
|
}
|
|
18625
18926
|
}
|
|
18626
18927
|
if (prices.length > 0) {
|
|
18627
|
-
this.
|
|
18928
|
+
buildBatchSetOraclePriceTx(this.deploymentConfig, { prices }, t);
|
|
18628
18929
|
}
|
|
18629
18930
|
}
|
|
18630
18931
|
const [nav] = t.moveCall({
|