@1stdex/first-sdk 1.0.69 → 1.0.71

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,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.prepareWithdrawFromVault = 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_from_vault_1 = require("./prepare-withdraw-from-vault");
9
- Object.defineProperty(exports, "prepareWithdrawFromVault", { enumerable: true, get: function () { return prepare_withdraw_from_vault_1.prepareWithdrawFromVault; } });
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; } });
10
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;AACpC,6EAAyE;AAAhE,uIAAA,wBAAwB,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,128 @@
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
+ const apis_1 = require("../../entities/currency/apis");
11
+ class NotEnoughFundsError extends Error {
12
+ constructor(message, required, available) {
13
+ super(message);
14
+ Object.defineProperty(this, "required", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: required
19
+ });
20
+ Object.defineProperty(this, "available", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: available
25
+ });
26
+ this.name = 'NotEnoughFundsError';
27
+ Object.setPrototypeOf(this, NotEnoughFundsError.prototype);
28
+ }
29
+ }
30
+ exports.NotEnoughFundsError = NotEnoughFundsError;
31
+ const prepareWithdraw = async ({ chainId, userAddress, toAddress, currency, amount, options, }) => {
32
+ const controllerAddress = addresses_1.CONTRACT_ADDRESSES[chainId].Controller;
33
+ const publicClient = (0, viem_1.createPublicClient)({
34
+ chain: chain_1.CHAIN_MAP[chainId],
35
+ transport: options?.rpcUrl ? (0, viem_1.http)(options.rpcUrl) : (0, viem_1.http)(),
36
+ });
37
+ const [currencyInfo, vault] = await Promise.all([
38
+ (0, apis_1.fetchCurrency)(publicClient, chainId, currency),
39
+ (0, vault_1.getVault)({
40
+ chainId,
41
+ tokenAddress: currency,
42
+ ...(options?.rpcUrl && { options: { rpcUrl: options.rpcUrl } }),
43
+ }),
44
+ ]);
45
+ const rawAmount = (0, viem_1.parseUnits)(amount, currencyInfo.decimals);
46
+ const results = await publicClient.multicall({
47
+ contracts: [
48
+ {
49
+ address: currency,
50
+ abi: viem_1.erc20Abi,
51
+ functionName: 'balanceOf',
52
+ args: [userAddress],
53
+ },
54
+ {
55
+ address: vault.address,
56
+ abi: dex_vault_abi_1.DEX_VAULT_ABI,
57
+ functionName: 'maxWithdraw',
58
+ args: [userAddress],
59
+ },
60
+ {
61
+ address: vault.address,
62
+ abi: viem_1.erc20Abi,
63
+ functionName: 'allowance',
64
+ args: [userAddress, controllerAddress],
65
+ },
66
+ ],
67
+ });
68
+ const walletBalance = results[0].result || 0n;
69
+ const vaultBalance = results[1].result || 0n;
70
+ const vaultAllowance = results[2].result || 0n;
71
+ const totalBalance = walletBalance + vaultBalance;
72
+ if (totalBalance < rawAmount) {
73
+ throw new NotEnoughFundsError(`Insufficient funds: need ${rawAmount}, but only have ${totalBalance} (wallet: ${walletBalance}, vault: ${vaultBalance})`, rawAmount, totalBalance);
74
+ }
75
+ const calls = [];
76
+ const maxValue = 2n ** 256n - 1n;
77
+ if (walletBalance >= rawAmount) {
78
+ calls.push({
79
+ to: currency,
80
+ data: (0, viem_1.encodeFunctionData)({
81
+ abi: viem_1.erc20Abi,
82
+ functionName: 'transfer',
83
+ args: [toAddress, rawAmount],
84
+ }),
85
+ value: 0n,
86
+ });
87
+ return calls;
88
+ }
89
+ const amountToWithdraw = rawAmount - walletBalance;
90
+ if (vaultAllowance < amountToWithdraw) {
91
+ calls.push({
92
+ to: vault.address,
93
+ data: (0, viem_1.encodeFunctionData)({
94
+ abi: viem_1.erc20Abi,
95
+ functionName: 'approve',
96
+ args: [controllerAddress, maxValue],
97
+ }),
98
+ value: 0n,
99
+ });
100
+ }
101
+ const withdrawTx = await (0, withdraw_from_vault_1.withdrawFromVault)({
102
+ chainId,
103
+ userAddress,
104
+ currency,
105
+ amount: amountToWithdraw,
106
+ options: {
107
+ ...options,
108
+ skipGasEstimation: true,
109
+ },
110
+ });
111
+ calls.push({
112
+ to: withdrawTx.to,
113
+ data: withdrawTx.data,
114
+ value: withdrawTx.value,
115
+ });
116
+ calls.push({
117
+ to: currency,
118
+ data: (0, viem_1.encodeFunctionData)({
119
+ abi: viem_1.erc20Abi,
120
+ functionName: 'transfer',
121
+ args: [toAddress, rawAmount],
122
+ }),
123
+ value: 0n,
124
+ });
125
+ return calls;
126
+ };
127
+ exports.prepareWithdraw = prepareWithdraw;
128
+ //# 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,+BAMc;AAEd,+DAA2E;AAC3E,uEAA6E;AAC7E,2EAAwE;AACxE,6CAA6C;AAC7C,sEAAiE;AAEjE,uDAA6D;AAO7D,MAAa,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;AAVD,kDAUC;AA+BM,MAAM,eAAe,GAAG,KAAK,EAAE,EACpC,OAAO,EACP,WAAW,EACX,SAAS,EACT,QAAQ,EACR,MAAM,EACN,OAAO,GAQR,EAAwB,EAAE;IACzB,MAAM,iBAAiB,GAAG,8BAAkB,CAAC,OAAO,CAAE,CAAC,UAAU,CAAC;IAElE,MAAM,YAAY,GAAG,IAAA,yBAAkB,EAAC;QACtC,KAAK,EAAE,iBAAS,CAAC,OAAO,CAAC;QACzB,SAA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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;IAGH,KAAK,CAAC,IAAI,CAAC;QACT,EAAE,EAAE,QAAQ;QACZ,IAAI,EAAE,IAAA,yBAAkB,EAAC;YACvB,GAAG,EAAE,eAAQ;YACb,YAAY,EAAE,UAAU;YACxB,IAAI,EAAE,CAAC,SAAS,EAAE,SAAS,CAAC;SAC7B,CAAC;QACF,KAAK,EAAE,EAAE;KACV,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AApIW,QAAA,eAAe,mBAoI1B"}
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.prepareWithdrawFromVault = 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,5 +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, "prepareWithdrawFromVault", { enumerable: true, get: function () { return batch_1.prepareWithdrawFromVault; } });
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; } });
35
36
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/calls/index.ts"],"names":[],"mappings":";;;AAAA,sCAA2C;AAAlC,kGAAA,UAAU,OAAA;AACnB,sCAAwD;AAA/C,+GAAA,uBAAuB,OAAA;AAChC,wCAA4C;AAAnC,mGAAA,UAAU,OAAA;AACnB,0CAIyB;AAHvB,qGAAA,WAAW,OAAA;AACX,oHAAA,0BAA0B,OAAA;AAC1B,8GAAA,oBAAoB,OAAA;AAEtB,wCAAyD;AAAhD,mGAAA,UAAU,OAAA;AAAE,oGAAA,WAAW,OAAA;AAChC,0CAA4D;AAAnD,qGAAA,WAAW,OAAA;AAAE,sGAAA,YAAY,OAAA;AAElC,oDAAsE;AAA7D,2HAAA,6BAA6B,OAAA;AACtC,0CAAsD;AAA7C,2GAAA,kBAAkB,OAAA;AAC3B,4CAAwD;AAA/C,6GAAA,mBAAmB,OAAA;AAE5B,iCAKiB;AAJf,iGAAA,QAAQ,OAAA;AACR,oGAAA,WAAW,OAAA;AACX,uGAAA,cAAc,OAAA;AACd,0GAAA,iBAAiB,OAAA;AAGnB,iCAIiB;AAHf,mHAAA,0BAA0B,OAAA;AAC1B,oHAAA,2BAA2B,OAAA;AAC3B,iHAAA,wBAAwB,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/calls/index.ts"],"names":[],"mappings":";;;AAAA,sCAA2C;AAAlC,kGAAA,UAAU,OAAA;AACnB,sCAAwD;AAA/C,+GAAA,uBAAuB,OAAA;AAChC,wCAA4C;AAAnC,mGAAA,UAAU,OAAA;AACnB,0CAIyB;AAHvB,qGAAA,WAAW,OAAA;AACX,oHAAA,0BAA0B,OAAA;AAC1B,8GAAA,oBAAoB,OAAA;AAEtB,wCAAyD;AAAhD,mGAAA,UAAU,OAAA;AAAE,oGAAA,WAAW,OAAA;AAChC,0CAA4D;AAAnD,qGAAA,WAAW,OAAA;AAAE,sGAAA,YAAY,OAAA;AAElC,oDAAsE;AAA7D,2HAAA,6BAA6B,OAAA;AACtC,0CAAsD;AAA7C,2GAAA,kBAAkB,OAAA;AAC3B,4CAAwD;AAA/C,6GAAA,mBAAmB,OAAA;AAE5B,iCAKiB;AAJf,iGAAA,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,4 +1,4 @@
1
1
  export { prepareLimitOrderWithSetup } from './prepare-limit-order-with-setup';
2
2
  export { prepareMarketOrderWithSetup } from './prepare-market-order-with-setup';
3
- export { prepareWithdrawFromVault } from './prepare-withdraw-from-vault';
3
+ export { prepareWithdraw, NotEnoughFundsError } from './prepare-withdraw';
4
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;AAChF,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,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,160 @@
1
+ import { createPublicClient, encodeFunctionData, erc20Abi, http, parseUnits, } 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
+ import { fetchCurrency } from '../../entities/currency/apis';
8
+ /**
9
+ * Error thrown when there are insufficient funds in wallet and vault combined.
10
+ */
11
+ export class NotEnoughFundsError extends Error {
12
+ constructor(message, required, available) {
13
+ super(message);
14
+ Object.defineProperty(this, "required", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: required
19
+ });
20
+ Object.defineProperty(this, "available", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: available
25
+ });
26
+ this.name = 'NotEnoughFundsError';
27
+ Object.setPrototypeOf(this, NotEnoughFundsError.prototype);
28
+ }
29
+ }
30
+ /**
31
+ * Prepares transactions to withdraw tokens optimally from wallet and/or vault to a recipient.
32
+ * If wallet has sufficient balance, transfers directly. Otherwise, withdraws the shortfall from
33
+ * vault to wallet (with approval if needed), then transfers the full amount to recipient.
34
+ * Throws NotEnoughFundsError if combined wallet + vault balance is insufficient.
35
+ *
36
+ * @param chainId The chain ID.
37
+ * @param userAddress The user address (wallet and vault owner).
38
+ * @param toAddress The recipient address.
39
+ * @param currency The currency/token address.
40
+ * @param amount The amount to send (decimal-adjusted string, e.g., '100.5' for 100.5 tokens).
41
+ * @param options Optional parameters.
42
+ * @returns Promise resolving to array of batch calls.
43
+ * @example
44
+ * import { prepareWithdraw } from '@1stdex/first-sdk'
45
+ * import { useSmartWallets } from '@privy-io/react-auth/smart-wallets'
46
+ *
47
+ * const calls = await prepareWithdraw({
48
+ * chainId: 421614,
49
+ * userAddress: '0xF8c1869Ecd4df136693C45EcE1b67f85B6bDaE69',
50
+ * toAddress: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
51
+ * currency: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
52
+ * amount: '100.5' // 100.5 tokens
53
+ * })
54
+ *
55
+ * // Send all calls in one batched transaction
56
+ * const { client } = useSmartWallets()
57
+ * const txHash = await client.sendUserOperation({ calls })
58
+ */
59
+ export const prepareWithdraw = async ({ chainId, userAddress, toAddress, currency, amount, options, }) => {
60
+ const controllerAddress = CONTRACT_ADDRESSES[chainId].Controller;
61
+ const publicClient = createPublicClient({
62
+ chain: CHAIN_MAP[chainId],
63
+ transport: options?.rpcUrl ? http(options.rpcUrl) : http(),
64
+ });
65
+ const [currencyInfo, vault] = await Promise.all([
66
+ fetchCurrency(publicClient, chainId, currency),
67
+ getVault({
68
+ chainId,
69
+ tokenAddress: currency,
70
+ ...(options?.rpcUrl && { options: { rpcUrl: options.rpcUrl } }),
71
+ }),
72
+ ]);
73
+ const rawAmount = parseUnits(amount, currencyInfo.decimals);
74
+ const results = await publicClient.multicall({
75
+ contracts: [
76
+ {
77
+ address: currency,
78
+ abi: erc20Abi,
79
+ functionName: 'balanceOf',
80
+ args: [userAddress],
81
+ },
82
+ {
83
+ address: vault.address,
84
+ abi: DEX_VAULT_ABI,
85
+ functionName: 'maxWithdraw',
86
+ args: [userAddress],
87
+ },
88
+ {
89
+ address: vault.address,
90
+ abi: erc20Abi,
91
+ functionName: 'allowance',
92
+ args: [userAddress, controllerAddress],
93
+ },
94
+ ],
95
+ });
96
+ const walletBalance = results[0].result || 0n;
97
+ const vaultBalance = results[1].result || 0n;
98
+ const vaultAllowance = results[2].result || 0n;
99
+ const totalBalance = walletBalance + vaultBalance;
100
+ if (totalBalance < rawAmount) {
101
+ throw new NotEnoughFundsError(`Insufficient funds: need ${rawAmount}, but only have ${totalBalance} (wallet: ${walletBalance}, vault: ${vaultBalance})`, rawAmount, totalBalance);
102
+ }
103
+ const calls = [];
104
+ const maxValue = 2n ** 256n - 1n;
105
+ // If wallet has enough, just transfer
106
+ if (walletBalance >= rawAmount) {
107
+ calls.push({
108
+ to: currency,
109
+ data: encodeFunctionData({
110
+ abi: erc20Abi,
111
+ functionName: 'transfer',
112
+ args: [toAddress, rawAmount],
113
+ }),
114
+ value: 0n,
115
+ });
116
+ return calls;
117
+ }
118
+ // Need to withdraw from vault - determine how much
119
+ const amountToWithdraw = rawAmount - walletBalance;
120
+ // Add approval if needed
121
+ if (vaultAllowance < amountToWithdraw) {
122
+ calls.push({
123
+ to: vault.address,
124
+ data: encodeFunctionData({
125
+ abi: erc20Abi,
126
+ functionName: 'approve',
127
+ args: [controllerAddress, maxValue],
128
+ }),
129
+ value: 0n,
130
+ });
131
+ }
132
+ // Withdraw from vault to wallet
133
+ const withdrawTx = await withdrawFromVault({
134
+ chainId,
135
+ userAddress,
136
+ currency,
137
+ amount: amountToWithdraw,
138
+ options: {
139
+ ...options,
140
+ skipGasEstimation: true,
141
+ },
142
+ });
143
+ calls.push({
144
+ to: withdrawTx.to,
145
+ data: withdrawTx.data,
146
+ value: withdrawTx.value,
147
+ });
148
+ // Transfer from wallet to recipient
149
+ calls.push({
150
+ to: currency,
151
+ data: encodeFunctionData({
152
+ abi: erc20Abi,
153
+ functionName: 'transfer',
154
+ args: [toAddress, rawAmount],
155
+ }),
156
+ value: 0n,
157
+ });
158
+ return calls;
159
+ };
160
+ //# 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,EACL,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EACR,IAAI,EACJ,UAAU,GACX,MAAM,MAAM,CAAC;AAEd,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;AAEjE,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAI7D;;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,QAAQ,EACR,MAAM,EACN,OAAO,GAQR,EAAwB,EAAE;IACzB,MAAM,iBAAiB,GAAG,kBAAkB,CAAC,OAAO,CAAE,CAAC,UAAU,CAAC;IAElE,MAAM,YAAY,GAAG,kBAAkB,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QACP,WAAW;QACX,QAAQ;QACR,MAAM,EAAE,gBAAgB;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,SAAS,CAAC;SAC7B,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, prepareWithdrawFromVault, } 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,EA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