@gbozee/ultimate 0.0.2-27 → 0.0.2-28

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.ts CHANGED
@@ -151,6 +151,18 @@ export interface BaseExchange {
151
151
  }): Promise<any>;
152
152
  getDelistedSpotSymbols(): Promise<any>;
153
153
  getOpenPositions(): Promise<any>;
154
+ crossAccountTransfer(payload: {
155
+ from: {
156
+ owner: string;
157
+ wallet: string;
158
+ };
159
+ to: {
160
+ owner: string;
161
+ wallet: string;
162
+ };
163
+ asset: string;
164
+ amount: number;
165
+ }): Promise<any>;
154
166
  }
155
167
  export interface BaseSystemFields {
156
168
  id: string;
@@ -804,14 +816,17 @@ declare class App {
804
816
  proxyOptions?: {
805
817
  proxy?: any;
806
818
  ignore_proxy?: boolean;
819
+ canWithdraw?: boolean;
807
820
  };
808
821
  private getCredentials;
809
822
  constructor(app_db: AppDatabase, getCredentials: (account: string, exchange: string) => {
810
823
  api_key: string;
811
824
  api_secret: string;
825
+ email: string;
812
826
  }, proxyOptions?: {
813
827
  proxy?: any;
814
828
  ignore_proxy?: boolean;
829
+ canWithdraw?: boolean;
815
830
  });
816
831
  getExchangeAccount(account: ExchangeType): Promise<ExchangeAccount$1>;
817
832
  syncAccount(payload: {
@@ -927,14 +942,17 @@ export declare function initApp(payload: {
927
942
  getCredentials: (account: string, exchange: string) => {
928
943
  api_key: string;
929
944
  api_secret: string;
945
+ email: string;
930
946
  };
931
947
  proxy?: any;
932
948
  ignore_proxy?: boolean;
949
+ canWithdraw?: boolean;
933
950
  }): Promise<App>;
934
951
  export declare function initialize(payload: {
935
952
  password?: string;
936
953
  proxy?: any;
937
954
  ignore_proxy?: boolean;
955
+ canWithdraw?: boolean;
938
956
  }): Promise<App>;
939
957
 
940
958
  export {
package/dist/index.js CHANGED
@@ -33796,6 +33796,193 @@ function createGapPairs(arr, gap, item) {
33796
33796
  }
33797
33797
 
33798
33798
  // src/exchanges/binance.ts
33799
+ var CONSTANTS = {
33800
+ SPOT_TO_FIAT: "MAIN_C2C",
33801
+ SPOT_TO_USDT_FUTURE: "MAIN_UMFUTURE",
33802
+ SPOT_TO_COIN_FUTURE: "MAIN_CMFUTURE",
33803
+ SPOT_TO_MARGIN_CROSS: "MAIN_MARGIN",
33804
+ SPOT_TO_MINING: "MAIN_MINING",
33805
+ FIAT_TO_SPOT: "C2C_MAIN",
33806
+ FIAT_TO_USDT_FUTURE: "C2C_UMFUTURE",
33807
+ FIAT_TO_MINING: "C2C_MINING",
33808
+ USDT_FUTURE_TO_SPOT: "UMFUTURE_MAIN",
33809
+ USDT_FUTURE_TO_FIAT: "UMFUTURE_C2C",
33810
+ USDT_FUTURE_TO_MARGIN_CROSS: "UMFUTURE_MARGIN",
33811
+ COIN_FUTURE_TO_SPOT: "CMFUTURE_MAIN",
33812
+ MARGIN_CROSS_TO_SPOT: "MARGIN_MAIN",
33813
+ MARGIN_CROSS_TO_USDT_FUTURE: "MARGIN_UMFUTURE",
33814
+ MINING_TO_SPOT: "MINING_MAIN",
33815
+ MINING_TO_USDT_FUTURE: "MINING_UMFUTURE",
33816
+ MINING_TO_FIAT: "MINING_C2C"
33817
+ };
33818
+
33819
+ class AccountService {
33820
+ getCredentials;
33821
+ proxyAgent;
33822
+ constructor(payload) {
33823
+ this.getCredentials = payload.getCredentials;
33824
+ this.proxyAgent = payload.proxyAgent;
33825
+ }
33826
+ async getAccount(owner) {
33827
+ const credentials = this.getCredentials(owner, "binance");
33828
+ const email = credentials.email;
33829
+ const is_sub_account = owner !== "main_account";
33830
+ return {
33831
+ email,
33832
+ owner,
33833
+ is_sub_account: Boolean(is_sub_account),
33834
+ getFutureAccountClient: (...args) => initClient(credentials, {
33835
+ type: "main",
33836
+ proxyAgent: this.proxyAgent
33837
+ })
33838
+ };
33839
+ }
33840
+ async crossAccountTransfer(symbol, params) {
33841
+ const { to, from, asset, amount } = params;
33842
+ if (to && from && asset && amount) {
33843
+ const toAccount = await this.getAccount(to.owner);
33844
+ const fromAccount = await this.getAccount(from.owner);
33845
+ const mainAccount = await this.getAccount("main_account");
33846
+ const [mainFutureClient, toFutureClient] = await Promise.all([
33847
+ mainAccount.getFutureAccountClient(symbol),
33848
+ toAccount.getFutureAccountClient(symbol)
33849
+ ]);
33850
+ const [fromSpotClient, fromFutureClient] = await Promise.all([
33851
+ fromAccount.getFutureAccountClient(symbol),
33852
+ fromAccount.getFutureAccountClient(symbol)
33853
+ ]);
33854
+ const isSubAccountTransfer = toAccount.is_sub_account && fromAccount.is_sub_account;
33855
+ const is_coin = !["USDT", "USDC", "BUSD"].includes(asset.toUpperCase());
33856
+ if (from.owner === to.owner) {
33857
+ if (from.wallet === "spot" && to.wallet === "future") {
33858
+ const type = is_coin ? CONSTANTS.SPOT_TO_COIN_FUTURE : CONSTANTS.SPOT_TO_USDT_FUTURE;
33859
+ await fromFutureClient.submitUniversalTransfer({
33860
+ asset: asset.toUpperCase(),
33861
+ amount,
33862
+ type,
33863
+ fromSymbol: symbol,
33864
+ toSymbol: symbol
33865
+ });
33866
+ } else {
33867
+ const type = is_coin ? CONSTANTS.COIN_FUTURE_TO_SPOT : CONSTANTS.USDT_FUTURE_TO_SPOT;
33868
+ await fromFutureClient.submitUniversalTransfer({
33869
+ asset: asset.toUpperCase(),
33870
+ amount,
33871
+ type,
33872
+ fromSymbol: symbol,
33873
+ toSymbol: symbol
33874
+ });
33875
+ }
33876
+ } else {
33877
+ if (from.wallet === "future") {
33878
+ const futureType = is_coin ? 2 : 1;
33879
+ const rr = await mainFutureClient.subAccountFuturesAssetTransfer({
33880
+ email: toAccount.email,
33881
+ toEmail: toAccount.email,
33882
+ fromEmail: fromAccount.email,
33883
+ futuresType: futureType,
33884
+ asset: asset.toUpperCase(),
33885
+ amount
33886
+ });
33887
+ console.log({
33888
+ rr
33889
+ });
33890
+ if (to.wallet === "spot") {
33891
+ const type = is_coin ? CONSTANTS.COIN_FUTURE_TO_SPOT : CONSTANTS.USDT_FUTURE_TO_SPOT;
33892
+ await toFutureClient.submitUniversalTransfer({
33893
+ asset: asset.toUpperCase(),
33894
+ amount,
33895
+ type,
33896
+ fromSymbol: symbol,
33897
+ toSymbol: symbol
33898
+ });
33899
+ }
33900
+ return rr;
33901
+ }
33902
+ if (from.wallet === "spot" && to.wallet === "spot") {
33903
+ if (isSubAccountTransfer) {
33904
+ await fromSpotClient.subAccountTransferToSameMaster({
33905
+ toEmail: toAccount.email,
33906
+ amount,
33907
+ asset: asset.toUpperCase()
33908
+ });
33909
+ } else {
33910
+ const type = is_coin ? CONSTANTS.SPOT_TO_COIN_FUTURE : CONSTANTS.SPOT_TO_USDT_FUTURE;
33911
+ await fromFutureClient.submitUniversalTransfer({
33912
+ asset: asset.toUpperCase(),
33913
+ amount,
33914
+ type,
33915
+ fromSymbol: symbol,
33916
+ toSymbol: symbol
33917
+ });
33918
+ const future_type = is_coin ? 2 : 1;
33919
+ await mainFutureClient.subAccountFuturesAssetTransfer({
33920
+ toEmail: toAccount.email,
33921
+ fromEmail: fromAccount.email,
33922
+ futuresType: future_type,
33923
+ asset: asset.toUpperCase(),
33924
+ amount
33925
+ });
33926
+ const _type_second = is_coin ? CONSTANTS.COIN_FUTURE_TO_SPOT : CONSTANTS.USDT_FUTURE_TO_SPOT;
33927
+ await toFutureClient.submitUniversalTransfer({
33928
+ asset: asset.toUpperCase(),
33929
+ amount,
33930
+ type: _type_second,
33931
+ fromSymbol: symbol,
33932
+ toSymbol: symbol
33933
+ });
33934
+ }
33935
+ }
33936
+ if (from.wallet === "spot" && to.wallet === "future") {
33937
+ if (isSubAccountTransfer) {
33938
+ await fromSpotClient.subAccountTransferToSameMaster({
33939
+ toEmail: toAccount.email,
33940
+ amount,
33941
+ asset: asset.toUpperCase()
33942
+ });
33943
+ const type = is_coin ? CONSTANTS.SPOT_TO_COIN_FUTURE : CONSTANTS.SPOT_TO_USDT_FUTURE;
33944
+ await toFutureClient.submitUniversalTransfer({
33945
+ asset: asset.toUpperCase(),
33946
+ amount,
33947
+ type,
33948
+ fromSymbol: symbol,
33949
+ toSymbol: symbol
33950
+ });
33951
+ } else {
33952
+ if (!mainAccount.is_sub_account) {
33953
+ const type = is_coin ? CONSTANTS.SPOT_TO_COIN_FUTURE : CONSTANTS.SPOT_TO_USDT_FUTURE;
33954
+ await mainFutureClient.submitUniversalTransfer({
33955
+ asset: asset.toUpperCase(),
33956
+ amount,
33957
+ type,
33958
+ fromSymbol: symbol,
33959
+ toSymbol: symbol
33960
+ });
33961
+ }
33962
+ if (!toAccount.is_sub_account) {
33963
+ const type = is_coin ? CONSTANTS.SPOT_TO_COIN_FUTURE : CONSTANTS.SPOT_TO_USDT_FUTURE;
33964
+ await fromFutureClient.submitUniversalTransfer({
33965
+ asset: asset.toUpperCase(),
33966
+ amount,
33967
+ type,
33968
+ fromSymbol: symbol,
33969
+ toSymbol: symbol
33970
+ });
33971
+ }
33972
+ const future_type = is_coin ? 2 : 1;
33973
+ await mainFutureClient.subAccountFuturesAssetTransfer({
33974
+ toEmail: toAccount.email,
33975
+ fromEmail: fromAccount.email,
33976
+ futuresType: future_type,
33977
+ asset: asset.toUpperCase(),
33978
+ amount
33979
+ });
33980
+ }
33981
+ }
33982
+ }
33983
+ }
33984
+ }
33985
+ }
33799
33986
  async function initClient(credentials, options) {
33800
33987
  const { proxyAgent, type = "future" } = options || {};
33801
33988
  try {
@@ -34346,10 +34533,16 @@ async function getAllOpenOrders(payload) {
34346
34533
  class BinanceExchange {
34347
34534
  client;
34348
34535
  main_client;
34536
+ getCredentials;
34537
+ proxyAgent;
34349
34538
  constructor(client, main_client) {
34350
34539
  this.client = client;
34351
34540
  this.main_client = main_client;
34352
34541
  }
34542
+ setAccountDetails(payload) {
34543
+ this.getCredentials = payload.getCredentials;
34544
+ this.proxyAgent = payload.proxyAgent;
34545
+ }
34353
34546
  async placeStopOrders(payload) {
34354
34547
  if (payload.place) {
34355
34548
  return placeStopOrder(this.client, {
@@ -34617,6 +34810,22 @@ class BinanceExchange {
34617
34810
  const response = await this.client.getPositionsV3();
34618
34811
  return Array.from(new Set(response.map((x) => x.symbol)));
34619
34812
  }
34813
+ async crossAccountTransfer(payload) {
34814
+ const { from, to, asset, amount } = payload;
34815
+ if (this.getCredentials) {
34816
+ const instance = new AccountService({
34817
+ getCredentials: this.getCredentials,
34818
+ proxyAgent: this.proxyAgent
34819
+ });
34820
+ const result = await instance.crossAccountTransfer("BTCUSDT", {
34821
+ from: { owner: from.owner, wallet: from.wallet },
34822
+ to: { owner: to.owner, wallet: to.wallet },
34823
+ asset,
34824
+ amount
34825
+ });
34826
+ return result;
34827
+ }
34828
+ }
34620
34829
  }
34621
34830
  function getPricePlaces(target) {
34622
34831
  const numStr = target.toString();
@@ -35287,6 +35496,8 @@ class BybitExchange {
35287
35496
  async getDelistedSpotSymbols() {
35288
35497
  return [];
35289
35498
  }
35499
+ async crossAccountTransfer(payload) {
35500
+ }
35290
35501
  }
35291
35502
 
35292
35503
  // src/helpers/accounts.ts
@@ -36368,13 +36579,13 @@ class ExchangeAccount {
36368
36579
  if (db_position) {
36369
36580
  const config = db_position.expand?.config;
36370
36581
  const params = {
36371
- entry: payload.params.entry !== undefined ? payload.params.entry : config.entry,
36372
- stop: payload.params.stop !== undefined ? payload.params.stop : config.stop,
36373
- risk_reward: payload.params.risk_reward !== undefined ? payload.params.risk_reward : config.risk_reward,
36374
- risk: payload.params.risk !== undefined ? payload.params.risk : config.risk,
36375
- profit_percent: payload.params.profit_percent !== undefined ? payload.params.profit_percent : config.profit_percent,
36582
+ entry: payload.params.entry !== undefined ? payload.params.entry : config?.entry,
36583
+ stop: payload.params.stop !== undefined ? payload.params.stop : config?.stop,
36584
+ risk_reward: payload.params.risk_reward !== undefined ? payload.params.risk_reward : config?.risk_reward,
36585
+ risk: payload.params.risk !== undefined ? payload.params.risk : config?.risk,
36586
+ profit_percent: payload.params.profit_percent !== undefined ? payload.params.profit_percent : config?.profit_percent,
36376
36587
  place_tp: payload.params.place_tp !== undefined ? payload.params.place_tp : true,
36377
- profit: payload.params.profit !== undefined ? payload.params.profit : config.profit
36588
+ profit: payload.params.profit !== undefined ? payload.params.profit : config?.profit
36378
36589
  };
36379
36590
  return await this.app_db.createOrUpdatePositionConfig(db_position, params);
36380
36591
  }
@@ -37502,17 +37713,25 @@ function getExchangeKlass(exchange) {
37502
37713
  if (!client) {
37503
37714
  throw new Error(`Failed to initialize ${exchange} client`);
37504
37715
  }
37505
- return new func(client, main_client);
37716
+ const instance = new func(client, main_client);
37717
+ if (payload.canWithdraw) {
37718
+ instance.setAccountDetails({
37719
+ getCredentials: payload.getCredentials,
37720
+ proxyAgent: payload.proxyAgent
37721
+ });
37722
+ }
37723
+ return instance;
37506
37724
  };
37507
37725
  }
37508
37726
  async function getExchangeAccount(payload) {
37509
- const { account, app_db, proxyOptions = {} } = payload;
37727
+ const { account, app_db, proxyOptions = {}, canWithdraw = false } = payload;
37510
37728
  const _proxyAgent = await app_db.getProxyForAccount(account);
37511
37729
  const proxyAgent = proxyOptions?.ignore_proxy ? null : proxyOptions?.proxy || _proxyAgent;
37512
37730
  const exchange_instance = await getExchangeKlass(account.exchange)({
37513
37731
  account: account.owner,
37514
37732
  getCredentials: payload.getCredentials,
37515
- proxyAgent
37733
+ proxyAgent,
37734
+ canWithdraw
37516
37735
  });
37517
37736
  return new ExchangeAccount(payload.account, {
37518
37737
  exchange: exchange_instance,
@@ -37535,7 +37754,8 @@ class App {
37535
37754
  account,
37536
37755
  app_db: this.app_db,
37537
37756
  getCredentials: this.getCredentials,
37538
- proxyOptions: this.proxyOptions
37757
+ proxyOptions: this.proxyOptions,
37758
+ canWithdraw: this.proxyOptions?.canWithdraw
37539
37759
  });
37540
37760
  }
37541
37761
  async syncAccount(payload) {
@@ -37914,7 +38134,8 @@ async function initApp(payload) {
37914
38134
  }
37915
38135
  return {
37916
38136
  api_key: credential?.api_key,
37917
- api_secret: credential?.api_secret
38137
+ api_secret: credential?.api_secret,
38138
+ email: credential?.email
37918
38139
  };
37919
38140
  };
37920
38141
  }
@@ -37924,7 +38145,8 @@ async function initApp(payload) {
37924
38145
  }
37925
38146
  const app = new App(app_db, _getCredentials, {
37926
38147
  proxy: payload.proxy,
37927
- ignore_proxy: payload.ignore_proxy
38148
+ ignore_proxy: payload.ignore_proxy,
38149
+ canWithdraw: payload.canWithdraw
37928
38150
  });
37929
38151
  return app;
37930
38152
  }
@@ -37960,11 +38182,12 @@ function getCredentials(account, exchange) {
37960
38182
  }
37961
38183
  return {
37962
38184
  api_key: apiKey,
37963
- api_secret: apiSecret
38185
+ api_secret: apiSecret,
38186
+ email: process.env.POCKETBASE_EMAIL
37964
38187
  };
37965
38188
  }
37966
38189
  async function initialize(payload) {
37967
- const { password, proxy, ignore_proxy } = payload;
38190
+ const { password, proxy, ignore_proxy, canWithdraw } = payload;
37968
38191
  const app = await initApp({
37969
38192
  db: {
37970
38193
  host: process.env.POCKETBASE_HOST,
@@ -37974,7 +38197,8 @@ async function initialize(payload) {
37974
38197
  password,
37975
38198
  getCredentials,
37976
38199
  proxy,
37977
- ignore_proxy
38200
+ ignore_proxy,
38201
+ canWithdraw
37978
38202
  });
37979
38203
  return app;
37980
38204
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gbozee/ultimate",
3
3
  "type": "module",
4
- "version": "0.0.2-27",
4
+ "version": "0.0.2-28",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",