@1stdex/first-sdk 1.0.68 → 1.0.70

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.
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.prepareMarketOrderWithSetup = exports.prepareLimitOrderWithSetup = void 0;
3
+ exports.NotEnoughFundsError = exports.prepareWithdraw = exports.prepareMarketOrderWithSetup = exports.prepareLimitOrderWithSetup = void 0;
4
4
  var prepare_limit_order_with_setup_1 = require("./prepare-limit-order-with-setup");
5
5
  Object.defineProperty(exports, "prepareLimitOrderWithSetup", { enumerable: true, get: function () { return prepare_limit_order_with_setup_1.prepareLimitOrderWithSetup; } });
6
6
  var prepare_market_order_with_setup_1 = require("./prepare-market-order-with-setup");
7
7
  Object.defineProperty(exports, "prepareMarketOrderWithSetup", { enumerable: true, get: function () { return prepare_market_order_with_setup_1.prepareMarketOrderWithSetup; } });
8
+ var prepare_withdraw_1 = require("./prepare-withdraw");
9
+ Object.defineProperty(exports, "prepareWithdraw", { enumerable: true, get: function () { return prepare_withdraw_1.prepareWithdraw; } });
10
+ Object.defineProperty(exports, "NotEnoughFundsError", { enumerable: true, get: function () { return prepare_withdraw_1.NotEnoughFundsError; } });
8
11
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/calls/batch/index.ts"],"names":[],"mappings":";;;AAAA,mFAA8E;AAArE,4IAAA,0BAA0B,OAAA;AACnC,qFAAgF;AAAvE,8IAAA,2BAA2B,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/calls/batch/index.ts"],"names":[],"mappings":";;;AAAA,mFAA8E;AAArE,4IAAA,0BAA0B,OAAA;AACnC,qFAAgF;AAAvE,8IAAA,2BAA2B,OAAA;AACpC,uDAA0E;AAAjE,mHAAA,eAAe,OAAA;AAAE,uHAAA,mBAAmB,OAAA"}
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepareWithdraw = exports.NotEnoughFundsError = void 0;
4
+ const viem_1 = require("viem");
5
+ const chain_1 = require("../../constants/chain-configs/chain");
6
+ const addresses_1 = require("../../constants/chain-configs/addresses");
7
+ const dex_vault_abi_1 = require("../../constants/abis/core/dex-vault-abi");
8
+ const vault_1 = require("../../views/vault");
9
+ const withdraw_from_vault_1 = require("../vault/withdraw-from-vault");
10
+ class NotEnoughFundsError extends Error {
11
+ constructor(message, required, available) {
12
+ super(message);
13
+ Object.defineProperty(this, "required", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: required
18
+ });
19
+ Object.defineProperty(this, "available", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: available
24
+ });
25
+ this.name = 'NotEnoughFundsError';
26
+ Object.setPrototypeOf(this, NotEnoughFundsError.prototype);
27
+ }
28
+ }
29
+ exports.NotEnoughFundsError = NotEnoughFundsError;
30
+ const prepareWithdraw = async ({ chainId, userAddress, toAddress, currency, amount, options, }) => {
31
+ const controllerAddress = addresses_1.CONTRACT_ADDRESSES[chainId].Controller;
32
+ const publicClient = (0, viem_1.createPublicClient)({
33
+ chain: chain_1.CHAIN_MAP[chainId],
34
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
35
+ });
36
+ const vault = await (0, vault_1.getVault)({
37
+ chainId,
38
+ tokenAddress: currency,
39
+ ...(options?.rpcUrl && { options: { rpcUrl: options.rpcUrl } }),
40
+ });
41
+ const results = await publicClient.multicall({
42
+ contracts: [
43
+ {
44
+ address: currency,
45
+ abi: viem_1.erc20Abi,
46
+ functionName: 'balanceOf',
47
+ args: [userAddress],
48
+ },
49
+ {
50
+ address: vault.address,
51
+ abi: dex_vault_abi_1.DEX_VAULT_ABI,
52
+ functionName: 'maxWithdraw',
53
+ args: [userAddress],
54
+ },
55
+ {
56
+ address: vault.address,
57
+ abi: viem_1.erc20Abi,
58
+ functionName: 'allowance',
59
+ args: [userAddress, controllerAddress],
60
+ },
61
+ ],
62
+ });
63
+ const walletBalance = results[0].result || 0n;
64
+ const vaultBalance = results[1].result || 0n;
65
+ const vaultAllowance = results[2].result || 0n;
66
+ const totalBalance = walletBalance + vaultBalance;
67
+ if (totalBalance < amount) {
68
+ throw new NotEnoughFundsError(`Insufficient funds: need ${amount}, but only have ${totalBalance} (wallet: ${walletBalance}, vault: ${vaultBalance})`, amount, totalBalance);
69
+ }
70
+ const calls = [];
71
+ const maxValue = 2n ** 256n - 1n;
72
+ if (walletBalance >= amount) {
73
+ calls.push({
74
+ to: currency,
75
+ data: (0, viem_1.encodeFunctionData)({
76
+ abi: viem_1.erc20Abi,
77
+ functionName: 'transfer',
78
+ args: [toAddress, amount],
79
+ }),
80
+ value: 0n,
81
+ });
82
+ return calls;
83
+ }
84
+ const amountToWithdraw = amount - walletBalance;
85
+ if (vaultAllowance < amountToWithdraw) {
86
+ calls.push({
87
+ to: vault.address,
88
+ data: (0, viem_1.encodeFunctionData)({
89
+ abi: viem_1.erc20Abi,
90
+ functionName: 'approve',
91
+ args: [controllerAddress, maxValue],
92
+ }),
93
+ value: 0n,
94
+ });
95
+ }
96
+ const withdrawTx = await (0, withdraw_from_vault_1.withdrawFromVault)({
97
+ chainId,
98
+ userAddress,
99
+ currency,
100
+ amount: amountToWithdraw,
101
+ options: {
102
+ ...options,
103
+ skipGasEstimation: true,
104
+ },
105
+ });
106
+ calls.push({
107
+ to: withdrawTx.to,
108
+ data: withdrawTx.data,
109
+ value: withdrawTx.value,
110
+ });
111
+ calls.push({
112
+ to: currency,
113
+ data: (0, viem_1.encodeFunctionData)({
114
+ abi: viem_1.erc20Abi,
115
+ functionName: 'transfer',
116
+ args: [toAddress, amount],
117
+ }),
118
+ value: 0n,
119
+ });
120
+ return calls;
121
+ };
122
+ exports.prepareWithdraw = prepareWithdraw;
123
+ //# sourceMappingURL=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
- exports.prepareMarketOrderWithSetup = exports.prepareLimitOrderWithSetup = exports.withdrawFromVault = exports.depositToVault = exports.removeVault = exports.setVault = exports.approveAllForMarket = exports.setTokenAllowances = exports.setApprovalOfOpenOrdersForAll = exports.cancelOrders = exports.cancelOrder = exports.claimOrders = exports.claimOrder = exports.ZeroTakenAmountError = exports.InsufficientLiquidityError = exports.marketOrder = exports.limitOrder = exports.placeMarketMakingQuotes = exports.openMarket = void 0;
3
+ exports.NotEnoughFundsError = exports.prepareWithdraw = exports.prepareMarketOrderWithSetup = exports.prepareLimitOrderWithSetup = exports.withdrawFromVault = exports.depositToVault = exports.removeVault = exports.setVault = exports.approveAllForMarket = exports.setTokenAllowances = exports.setApprovalOfOpenOrdersForAll = exports.cancelOrders = exports.cancelOrder = exports.claimOrders = exports.claimOrder = exports.ZeroTakenAmountError = exports.InsufficientLiquidityError = exports.marketOrder = exports.limitOrder = exports.placeMarketMakingQuotes = exports.openMarket = void 0;
4
4
  var open_1 = require("./market/open");
5
5
  Object.defineProperty(exports, "openMarket", { enumerable: true, get: function () { return open_1.openMarket; } });
6
6
  var make_1 = require("./market/make");
@@ -31,4 +31,6 @@ Object.defineProperty(exports, "withdrawFromVault", { enumerable: true, get: fun
31
31
  var batch_1 = require("./batch");
32
32
  Object.defineProperty(exports, "prepareLimitOrderWithSetup", { enumerable: true, get: function () { return batch_1.prepareLimitOrderWithSetup; } });
33
33
  Object.defineProperty(exports, "prepareMarketOrderWithSetup", { enumerable: true, get: function () { return batch_1.prepareMarketOrderWithSetup; } });
34
+ Object.defineProperty(exports, "prepareWithdraw", { enumerable: true, get: function () { return batch_1.prepareWithdraw; } });
35
+ Object.defineProperty(exports, "NotEnoughFundsError", { enumerable: true, get: function () { return batch_1.NotEnoughFundsError; } });
34
36
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/calls/index.ts"],"names":[],"mappings":";;;AAAA,sCAA2C;AAAlC,kGAA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AA,QAAQ,OAAA;AACR,oGAAA,WAAW,OAAA;AACX,uGAAA,cAAc,OAAA;AACd,0GAAA,iBAAiB,OAAA;AAGnB,iCAKiB;AAJf,mHAAA,0BAA0B,OAAA;AAC1B,oHAAA,2BAA2B,OAAA;AAC3B,wGAAA,eAAe,OAAA;AACf,4GAAA,mBAAmB,OAAA"}
@@ -1,3 +1,4 @@
1
1
  export { prepareLimitOrderWithSetup } from './prepare-limit-order-with-setup';
2
2
  export { prepareMarketOrderWithSetup } from './prepare-market-order-with-setup';
3
+ export { prepareWithdraw, NotEnoughFundsError } from './prepare-withdraw';
3
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/calls/batch/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,kCAAkC,CAAC;AAC9E,OAAO,EAAE,2BAA2B,EAAE,MAAM,mCAAmC,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/calls/batch/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,kCAAkC,CAAC;AAC9E,OAAO,EAAE,2BAA2B,EAAE,MAAM,mCAAmC,CAAC;AAChF,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,155 @@
1
+ import { createPublicClient, encodeFunctionData, erc20Abi, http } from 'viem';
2
+ import { CHAIN_MAP } from '../../constants/chain-configs/chain';
3
+ import { CONTRACT_ADDRESSES } from '../../constants/chain-configs/addresses';
4
+ import { DEX_VAULT_ABI } from '../../constants/abis/core/dex-vault-abi';
5
+ import { getVault } from '../../views/vault';
6
+ import { withdrawFromVault } from '../vault/withdraw-from-vault';
7
+ /**
8
+ * Error thrown when there are insufficient funds in wallet and vault combined.
9
+ */
10
+ export class NotEnoughFundsError extends Error {
11
+ constructor(message, required, available) {
12
+ super(message);
13
+ Object.defineProperty(this, "required", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: required
18
+ });
19
+ Object.defineProperty(this, "available", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: available
24
+ });
25
+ this.name = 'NotEnoughFundsError';
26
+ Object.setPrototypeOf(this, NotEnoughFundsError.prototype);
27
+ }
28
+ }
29
+ /**
30
+ * Prepares transactions to withdraw tokens optimally from wallet and/or vault to a recipient.
31
+ * If wallet has sufficient balance, transfers directly. Otherwise, withdraws the shortfall from
32
+ * vault to wallet (with approval if needed), then transfers the full amount to recipient.
33
+ * Throws NotEnoughFundsError if combined wallet + vault balance is insufficient.
34
+ *
35
+ * @param chainId The chain ID.
36
+ * @param userAddress The user address (wallet and vault owner).
37
+ * @param toAddress The recipient address.
38
+ * @param currency The currency/token address.
39
+ * @param amount The amount to send (in wei).
40
+ * @param options Optional parameters.
41
+ * @returns Promise resolving to array of batch calls.
42
+ * @example
43
+ * import { prepareWithdraw } from '@1stdex/first-sdk'
44
+ * import { useSmartWallets } from '@privy-io/react-auth/smart-wallets'
45
+ *
46
+ * const calls = await prepareWithdraw({
47
+ * chainId: 421614,
48
+ * userAddress: '0xF8c1869Ecd4df136693C45EcE1b67f85B6bDaE69',
49
+ * toAddress: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
50
+ * currency: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
51
+ * amount: 1000000000000000000n // 1 token (18 decimals)
52
+ * })
53
+ *
54
+ * // Send all calls in one batched transaction
55
+ * const { client } = useSmartWallets()
56
+ * const txHash = await client.sendUserOperation({ calls })
57
+ */
58
+ export const prepareWithdraw = async ({ chainId, userAddress, toAddress, currency, amount, options, }) => {
59
+ const controllerAddress = CONTRACT_ADDRESSES[chainId].Controller;
60
+ const publicClient = createPublicClient({
61
+ chain: CHAIN_MAP[chainId],
62
+ transport: options?.rpcUrl ? http(options.rpcUrl) : http(),
63
+ });
64
+ const vault = await getVault({
65
+ chainId,
66
+ tokenAddress: currency,
67
+ ...(options?.rpcUrl && { options: { rpcUrl: options.rpcUrl } }),
68
+ });
69
+ const results = await publicClient.multicall({
70
+ contracts: [
71
+ {
72
+ address: currency,
73
+ abi: erc20Abi,
74
+ functionName: 'balanceOf',
75
+ args: [userAddress],
76
+ },
77
+ {
78
+ address: vault.address,
79
+ abi: DEX_VAULT_ABI,
80
+ functionName: 'maxWithdraw',
81
+ args: [userAddress],
82
+ },
83
+ {
84
+ address: vault.address,
85
+ abi: erc20Abi,
86
+ functionName: 'allowance',
87
+ args: [userAddress, controllerAddress],
88
+ },
89
+ ],
90
+ });
91
+ const walletBalance = results[0].result || 0n;
92
+ const vaultBalance = results[1].result || 0n;
93
+ const vaultAllowance = results[2].result || 0n;
94
+ const totalBalance = walletBalance + vaultBalance;
95
+ if (totalBalance < amount) {
96
+ throw new NotEnoughFundsError(`Insufficient funds: need ${amount}, but only have ${totalBalance} (wallet: ${walletBalance}, vault: ${vaultBalance})`, amount, totalBalance);
97
+ }
98
+ const calls = [];
99
+ const maxValue = 2n ** 256n - 1n;
100
+ // If wallet has enough, just transfer
101
+ if (walletBalance >= amount) {
102
+ calls.push({
103
+ to: currency,
104
+ data: encodeFunctionData({
105
+ abi: erc20Abi,
106
+ functionName: 'transfer',
107
+ args: [toAddress, amount],
108
+ }),
109
+ value: 0n,
110
+ });
111
+ return calls;
112
+ }
113
+ // Need to withdraw from vault - determine how much
114
+ const amountToWithdraw = amount - walletBalance;
115
+ // Add approval if needed
116
+ if (vaultAllowance < amountToWithdraw) {
117
+ calls.push({
118
+ to: vault.address,
119
+ data: encodeFunctionData({
120
+ abi: erc20Abi,
121
+ functionName: 'approve',
122
+ args: [controllerAddress, maxValue],
123
+ }),
124
+ value: 0n,
125
+ });
126
+ }
127
+ // Withdraw from vault to wallet
128
+ const withdrawTx = await withdrawFromVault({
129
+ chainId,
130
+ userAddress,
131
+ currency,
132
+ amount: amountToWithdraw,
133
+ options: {
134
+ ...options,
135
+ skipGasEstimation: true,
136
+ },
137
+ });
138
+ calls.push({
139
+ to: withdrawTx.to,
140
+ data: withdrawTx.data,
141
+ value: withdrawTx.value,
142
+ });
143
+ // Transfer from wallet to recipient
144
+ calls.push({
145
+ to: currency,
146
+ data: encodeFunctionData({
147
+ abi: erc20Abi,
148
+ functionName: 'transfer',
149
+ args: [toAddress, amount],
150
+ }),
151
+ value: 0n,
152
+ });
153
+ return calls;
154
+ };
155
+ //# sourceMappingURL=prepare-withdraw.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prepare-withdraw.js","sourceRoot":"","sources":["../../../../src/calls/batch/prepare-withdraw.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE9E,OAAO,EAAa,SAAS,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AACxE,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAKjE;;GAEG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC5C,YACE,OAAe,EACC,QAAgB,EAChB,SAAiB;QAEjC,KAAK,CAAC,OAAO,CAAC,CAAC;QAHf;;;;mBAAgB,QAAQ;WAAQ;QAChC;;;;mBAAgB,SAAS;WAAQ;QAGjC,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAClC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,mBAAmB,CAAC,SAAS,CAAC,CAAC;IAC7D,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,EAAE,EACpC,OAAO,EACP,WAAW,EACX,SAAS,EACT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QACxB,OAAO,EAAE;YACP,GAAG,OAAO;YACV,iBAAiB,EAAE,IAAI;SACO;KACjC,CAAC,CAAC;IAEH,KAAK,CAAC,IAAI,CAAC;QACT,EAAE,EAAE,UAAU,CAAC,EAAE;QACjB,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,KAAK,EAAE,UAAU,CAAC,KAAK;KACxB,CAAC,CAAC;IAEH,oCAAoC;IACpC,KAAK,CAAC,IAAI,CAAC;QACT,EAAE,EAAE,QAAQ;QACZ,IAAI,EAAE,kBAAkB,CAAC;YACvB,GAAG,EAAE,QAAQ;YACb,YAAY,EAAE,UAAU;YACxB,IAAI,EAAE,CAAC,SAAS,EAAE,MAAM,CAAC;SAC1B,CAAC;QACF,KAAK,EAAE,EAAE;KACV,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACf,CAAC,CAAC"}
@@ -8,5 +8,5 @@ export { setApprovalOfOpenOrdersForAll } from './approval/open-order';
8
8
  export { setTokenAllowances } from './approval/token';
9
9
  export { approveAllForMarket } from './approval/market';
10
10
  export { setVault, removeVault, depositToVault, withdrawFromVault, } from './vault';
11
- export { prepareLimitOrderWithSetup, prepareMarketOrderWithSetup, } from './batch';
11
+ export { prepareLimitOrderWithSetup, prepareMarketOrderWithSetup, prepareWithdraw, NotEnoughFundsError, } from './batch';
12
12
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/calls/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EACL,WAAW,EACX,0BAA0B,EAC1B,oBAAoB,GACrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAA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