@gbozee/ultimate 0.0.2-26 → 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;
@@ -801,10 +813,20 @@ declare class ExchangeAccount$1 {
801
813
  }
802
814
  declare class App {
803
815
  app_db: AppDatabase;
816
+ proxyOptions?: {
817
+ proxy?: any;
818
+ ignore_proxy?: boolean;
819
+ canWithdraw?: boolean;
820
+ };
804
821
  private getCredentials;
805
822
  constructor(app_db: AppDatabase, getCredentials: (account: string, exchange: string) => {
806
823
  api_key: string;
807
824
  api_secret: string;
825
+ email: string;
826
+ }, proxyOptions?: {
827
+ proxy?: any;
828
+ ignore_proxy?: boolean;
829
+ canWithdraw?: boolean;
808
830
  });
809
831
  getExchangeAccount(account: ExchangeType): Promise<ExchangeAccount$1>;
810
832
  syncAccount(payload: {
@@ -920,9 +942,18 @@ export declare function initApp(payload: {
920
942
  getCredentials: (account: string, exchange: string) => {
921
943
  api_key: string;
922
944
  api_secret: string;
945
+ email: string;
923
946
  };
947
+ proxy?: any;
948
+ ignore_proxy?: boolean;
949
+ canWithdraw?: boolean;
950
+ }): Promise<App>;
951
+ export declare function initialize(payload: {
952
+ password?: string;
953
+ proxy?: any;
954
+ ignore_proxy?: boolean;
955
+ canWithdraw?: boolean;
924
956
  }): Promise<App>;
925
- export declare function initialize(password?: string): Promise<App>;
926
957
 
927
958
  export {
928
959
  ExchangeAccount$1 as ExchangeAccount,
package/dist/index.js CHANGED
@@ -33796,19 +33796,210 @@ 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 {
33989
+ const axiosOptions = proxyAgent ? {
33990
+ httpAgent: proxyAgent,
33991
+ httpsAgent: proxyAgent
33992
+ } : undefined;
33993
+ if (!proxyAgent) {
33994
+ console.log("using no proxy");
33995
+ }
33802
33996
  const Klass = type === "future" ? import_binance.USDMClient : type === "spot" ? import_binance.MainClient : import_binance.MainClient;
33803
33997
  const client = new Klass({
33804
33998
  api_key: credentials.api_key,
33805
33999
  api_secret: credentials.api_secret,
33806
34000
  recvWindow: 1e4,
33807
34001
  beautifyResponses: true
33808
- }, {
33809
- httpAgent: proxyAgent,
33810
- httpsAgent: proxyAgent
33811
- });
34002
+ }, axiosOptions);
33812
34003
  return client;
33813
34004
  } catch (e2) {
33814
34005
  console.log(e2);
@@ -34342,10 +34533,16 @@ async function getAllOpenOrders(payload) {
34342
34533
  class BinanceExchange {
34343
34534
  client;
34344
34535
  main_client;
34536
+ getCredentials;
34537
+ proxyAgent;
34345
34538
  constructor(client, main_client) {
34346
34539
  this.client = client;
34347
34540
  this.main_client = main_client;
34348
34541
  }
34542
+ setAccountDetails(payload) {
34543
+ this.getCredentials = payload.getCredentials;
34544
+ this.proxyAgent = payload.proxyAgent;
34545
+ }
34349
34546
  async placeStopOrders(payload) {
34350
34547
  if (payload.place) {
34351
34548
  return placeStopOrder(this.client, {
@@ -34613,6 +34810,22 @@ class BinanceExchange {
34613
34810
  const response = await this.client.getPositionsV3();
34614
34811
  return Array.from(new Set(response.map((x) => x.symbol)));
34615
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
+ }
34616
34829
  }
34617
34830
  function getPricePlaces(target) {
34618
34831
  const numStr = target.toString();
@@ -34628,14 +34841,15 @@ var import_bybit_api = __toESM(require_lib2(), 1);
34628
34841
  async function initClient2(credentials, options) {
34629
34842
  const { proxyAgent } = options;
34630
34843
  try {
34844
+ const axiosOptions = proxyAgent ? {
34845
+ httpAgent: proxyAgent,
34846
+ httpsAgent: proxyAgent
34847
+ } : undefined;
34631
34848
  const client = new import_bybit_api.RestClientV5({
34632
34849
  key: credentials.api_key,
34633
34850
  secret: credentials.api_secret,
34634
34851
  recv_window: 1e4
34635
- }, {
34636
- httpAgent: proxyAgent,
34637
- httpsAgent: proxyAgent
34638
- });
34852
+ }, axiosOptions);
34639
34853
  return client;
34640
34854
  } catch (e2) {
34641
34855
  console.log(e2);
@@ -35282,6 +35496,8 @@ class BybitExchange {
35282
35496
  async getDelistedSpotSymbols() {
35283
35497
  return [];
35284
35498
  }
35499
+ async crossAccountTransfer(payload) {
35500
+ }
35285
35501
  }
35286
35502
 
35287
35503
  // src/helpers/accounts.ts
@@ -36363,13 +36579,13 @@ class ExchangeAccount {
36363
36579
  if (db_position) {
36364
36580
  const config = db_position.expand?.config;
36365
36581
  const params = {
36366
- entry: payload.params.entry !== undefined ? payload.params.entry : config.entry,
36367
- stop: payload.params.stop !== undefined ? payload.params.stop : config.stop,
36368
- risk_reward: payload.params.risk_reward !== undefined ? payload.params.risk_reward : config.risk_reward,
36369
- risk: payload.params.risk !== undefined ? payload.params.risk : config.risk,
36370
- 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,
36371
36587
  place_tp: payload.params.place_tp !== undefined ? payload.params.place_tp : true,
36372
- profit: payload.params.profit !== undefined ? payload.params.profit : config.profit
36588
+ profit: payload.params.profit !== undefined ? payload.params.profit : config?.profit
36373
36589
  };
36374
36590
  return await this.app_db.createOrUpdatePositionConfig(db_position, params);
36375
36591
  }
@@ -37497,16 +37713,25 @@ function getExchangeKlass(exchange) {
37497
37713
  if (!client) {
37498
37714
  throw new Error(`Failed to initialize ${exchange} client`);
37499
37715
  }
37500
- 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;
37501
37724
  };
37502
37725
  }
37503
37726
  async function getExchangeAccount(payload) {
37504
- const { account, app_db } = payload;
37505
- const proxyAgent = await app_db.getProxyForAccount(account);
37727
+ const { account, app_db, proxyOptions = {}, canWithdraw = false } = payload;
37728
+ const _proxyAgent = await app_db.getProxyForAccount(account);
37729
+ const proxyAgent = proxyOptions?.ignore_proxy ? null : proxyOptions?.proxy || _proxyAgent;
37506
37730
  const exchange_instance = await getExchangeKlass(account.exchange)({
37507
37731
  account: account.owner,
37508
37732
  getCredentials: payload.getCredentials,
37509
- proxyAgent
37733
+ proxyAgent,
37734
+ canWithdraw
37510
37735
  });
37511
37736
  return new ExchangeAccount(payload.account, {
37512
37737
  exchange: exchange_instance,
@@ -37517,16 +37742,20 @@ async function getExchangeAccount(payload) {
37517
37742
  // src/app.ts
37518
37743
  class App {
37519
37744
  app_db;
37745
+ proxyOptions;
37520
37746
  getCredentials;
37521
- constructor(app_db, getCredentials) {
37747
+ constructor(app_db, getCredentials, proxyOptions) {
37522
37748
  this.app_db = app_db;
37523
37749
  this.getCredentials = getCredentials;
37750
+ this.proxyOptions = proxyOptions;
37524
37751
  }
37525
37752
  async getExchangeAccount(account) {
37526
37753
  return await getExchangeAccount({
37527
37754
  account,
37528
37755
  app_db: this.app_db,
37529
- getCredentials: this.getCredentials
37756
+ getCredentials: this.getCredentials,
37757
+ proxyOptions: this.proxyOptions,
37758
+ canWithdraw: this.proxyOptions?.canWithdraw
37530
37759
  });
37531
37760
  }
37532
37761
  async syncAccount(payload) {
@@ -37905,7 +38134,8 @@ async function initApp(payload) {
37905
38134
  }
37906
38135
  return {
37907
38136
  api_key: credential?.api_key,
37908
- api_secret: credential?.api_secret
38137
+ api_secret: credential?.api_secret,
38138
+ email: credential?.email
37909
38139
  };
37910
38140
  };
37911
38141
  }
@@ -37913,7 +38143,11 @@ async function initApp(payload) {
37913
38143
  console.log("error", error);
37914
38144
  }
37915
38145
  }
37916
- const app = new App(app_db, _getCredentials);
38146
+ const app = new App(app_db, _getCredentials, {
38147
+ proxy: payload.proxy,
38148
+ ignore_proxy: payload.ignore_proxy,
38149
+ canWithdraw: payload.canWithdraw
38150
+ });
37917
38151
  return app;
37918
38152
  }
37919
38153
  function getCredentials(account, exchange) {
@@ -37948,10 +38182,12 @@ function getCredentials(account, exchange) {
37948
38182
  }
37949
38183
  return {
37950
38184
  api_key: apiKey,
37951
- api_secret: apiSecret
38185
+ api_secret: apiSecret,
38186
+ email: process.env.POCKETBASE_EMAIL
37952
38187
  };
37953
38188
  }
37954
- async function initialize(password) {
38189
+ async function initialize(payload) {
38190
+ const { password, proxy, ignore_proxy, canWithdraw } = payload;
37955
38191
  const app = await initApp({
37956
38192
  db: {
37957
38193
  host: process.env.POCKETBASE_HOST,
@@ -37959,7 +38195,10 @@ async function initialize(password) {
37959
38195
  password: process.env.POCKETBASE_PASSWORD
37960
38196
  },
37961
38197
  password,
37962
- getCredentials
38198
+ getCredentials,
38199
+ proxy,
38200
+ ignore_proxy,
38201
+ canWithdraw
37963
38202
  });
37964
38203
  return app;
37965
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-26",
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",