@1stdex/first-sdk 1.0.4 → 1.0.6
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/cjs/calls/approval/order.js +17 -26
- package/dist/cjs/calls/approval/order.js.map +1 -1
- package/dist/cjs/calls/approval/token.js +34 -121
- package/dist/cjs/calls/approval/token.js.map +1 -1
- package/dist/esm/calls/approval/order.js +33 -55
- package/dist/esm/calls/approval/order.js.map +1 -1
- package/dist/esm/calls/approval/token.js +47 -146
- package/dist/esm/calls/approval/token.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/calls/approval/order.d.ts +23 -36
- package/dist/types/calls/approval/order.d.ts.map +1 -1
- package/dist/types/calls/approval/token.d.ts +16 -18
- package/dist/types/calls/approval/token.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -4,41 +4,38 @@ import { fetchCurrency } from '../../entities/currency/apis';
|
|
|
4
4
|
import { fetchAllowance } from '../../entities/currency/apis/allowance';
|
|
5
5
|
import { CONTRACT_ADDRESSES } from '../../constants/chain-configs/addresses';
|
|
6
6
|
import { getVault } from '../../views/vault';
|
|
7
|
+
import { buildTransaction } from '../../utils/build-transaction';
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
-
* Sets maximum allowance for a token and its vault to the respective contracts.
|
|
9
|
+
* Prepares approval transactions for a token and its vault.
|
|
10
10
|
* This includes:
|
|
11
11
|
* - Max allowance for the token to controller
|
|
12
12
|
* - Max allowance for the token's vault to controller
|
|
13
13
|
* - Max allowance for the token's vault to book manager
|
|
14
14
|
*
|
|
15
|
-
* Only
|
|
15
|
+
* Only includes approvals where current allowance is below threshold (max - 1 million tokens).
|
|
16
16
|
*
|
|
17
17
|
* @param {CHAIN_IDS} chainId The chain ID.
|
|
18
|
-
* @param {
|
|
18
|
+
* @param {`0x${string}`} userAddress The user address.
|
|
19
19
|
* @param {`0x${string}`} tokenAddress The token address.
|
|
20
20
|
* @param options
|
|
21
21
|
* @param options.rpcUrl The RPC URL of the blockchain.
|
|
22
|
-
* @returns {Promise
|
|
22
|
+
* @returns {Promise<Transaction[]>} Promise resolving to array of approval transactions. Empty array if no approvals needed.
|
|
23
23
|
* @example
|
|
24
24
|
* import { setTokenAllowances } from '@clober/v2-sdk'
|
|
25
25
|
*
|
|
26
|
-
* const
|
|
27
|
-
* chain: arbitrumSepolia,
|
|
28
|
-
* account: mnemonicToAccount('legal ...'),
|
|
29
|
-
* transport: http(),
|
|
30
|
-
* })
|
|
31
|
-
*
|
|
32
|
-
* const hashes = await setTokenAllowances({
|
|
26
|
+
* const transactions = await setTokenAllowances({
|
|
33
27
|
* chainId: 421614,
|
|
34
|
-
*
|
|
28
|
+
* userAddress: '0xF8c1869Ecd4df136693C45EcE1b67f85B6bDaE69',
|
|
35
29
|
* tokenAddress: '0x00bfd44e79fb7f6dd5887a9426c8ef85a0cd23e0',
|
|
36
30
|
* })
|
|
31
|
+
*
|
|
32
|
+
* // Send transactions sequentially
|
|
33
|
+
* for (const tx of transactions) {
|
|
34
|
+
* const hash = await walletClient.sendTransaction(tx)
|
|
35
|
+
* await publicClient.waitForTransactionReceipt({ hash })
|
|
36
|
+
* }
|
|
37
37
|
*/
|
|
38
|
-
export const setTokenAllowances = async ({ chainId,
|
|
39
|
-
if (!walletClient.account) {
|
|
40
|
-
throw new Error('Account is not found');
|
|
41
|
-
}
|
|
38
|
+
export const setTokenAllowances = async ({ chainId, userAddress, tokenAddress, options, }) => {
|
|
42
39
|
const publicClient = createPublicClient({
|
|
43
40
|
chain: CHAIN_MAP[chainId],
|
|
44
41
|
transport: options?.rpcUrl ? http(options.rpcUrl) : http(),
|
|
@@ -60,153 +57,57 @@ export const setTokenAllowances = async ({ chainId, walletClient, tokenAddress,
|
|
|
60
57
|
const threshold = maxValue - oneMillionTokens;
|
|
61
58
|
// Check current allowances
|
|
62
59
|
const [tokenToControllerAllowance, vaultToControllerAllowance, vaultToBookManagerAllowance,] = await Promise.all([
|
|
63
|
-
fetchAllowance(publicClient, tokenAddress,
|
|
64
|
-
fetchAllowance(publicClient, vault.address,
|
|
65
|
-
fetchAllowance(publicClient, vault.address,
|
|
60
|
+
fetchAllowance(publicClient, tokenAddress, userAddress, controllerAddress),
|
|
61
|
+
fetchAllowance(publicClient, vault.address, userAddress, controllerAddress),
|
|
62
|
+
fetchAllowance(publicClient, vault.address, userAddress, bookManagerAddress),
|
|
66
63
|
]);
|
|
67
64
|
const approvalAbi = [
|
|
68
65
|
{
|
|
69
66
|
inputs: [
|
|
70
|
-
{
|
|
71
|
-
|
|
72
|
-
name: 'spender',
|
|
73
|
-
type: 'address',
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
internalType: 'uint256',
|
|
77
|
-
name: 'value',
|
|
78
|
-
type: 'uint256',
|
|
79
|
-
},
|
|
67
|
+
{ internalType: 'address', name: 'spender', type: 'address' },
|
|
68
|
+
{ internalType: 'uint256', name: 'value', type: 'uint256' },
|
|
80
69
|
],
|
|
81
70
|
name: 'approve',
|
|
82
|
-
outputs: [
|
|
83
|
-
{
|
|
84
|
-
internalType: 'bool',
|
|
85
|
-
name: '',
|
|
86
|
-
type: 'bool',
|
|
87
|
-
},
|
|
88
|
-
],
|
|
71
|
+
outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
|
|
89
72
|
stateMutability: 'nonpayable',
|
|
90
73
|
type: 'function',
|
|
91
74
|
},
|
|
92
75
|
];
|
|
93
|
-
const
|
|
76
|
+
const transactions = [];
|
|
94
77
|
// Approve token to controller if needed
|
|
95
78
|
if (tokenToControllerAllowance < threshold) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
functionName: 'approve',
|
|
105
|
-
args: [controllerAddress, maxValue],
|
|
106
|
-
});
|
|
107
|
-
hash = await walletClient.writeContract(request);
|
|
108
|
-
}
|
|
109
|
-
catch (error) {
|
|
110
|
-
if (error.message?.includes('Nonce provided for the transaction is lower than the current nonce')) {
|
|
111
|
-
// Wait 2 seconds and retry once
|
|
112
|
-
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
113
|
-
const { request } = await publicClient.simulateContract({
|
|
114
|
-
account: walletClient.account,
|
|
115
|
-
chain: CHAIN_MAP[chainId],
|
|
116
|
-
address: tokenAddress,
|
|
117
|
-
abi: approvalAbi,
|
|
118
|
-
functionName: 'approve',
|
|
119
|
-
args: [controllerAddress, maxValue],
|
|
120
|
-
});
|
|
121
|
-
hash = await walletClient.writeContract(request);
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
throw error;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
transactionHashes.push(hash);
|
|
128
|
-
// Wait for transaction confirmation to ensure nonce is updated
|
|
129
|
-
await publicClient.waitForTransactionReceipt({ hash });
|
|
130
|
-
// Wait 1 second before proceeding (to avoid nonce issues)
|
|
131
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
79
|
+
transactions.push(await buildTransaction(publicClient, {
|
|
80
|
+
chain: CHAIN_MAP[chainId],
|
|
81
|
+
account: userAddress,
|
|
82
|
+
address: tokenAddress,
|
|
83
|
+
abi: approvalAbi,
|
|
84
|
+
functionName: 'approve',
|
|
85
|
+
args: [controllerAddress, maxValue],
|
|
86
|
+
}, options?.gasLimit));
|
|
132
87
|
}
|
|
133
88
|
// Approve vault to controller if needed
|
|
134
89
|
if (vaultToControllerAllowance < threshold) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
functionName: 'approve',
|
|
144
|
-
args: [controllerAddress, maxValue],
|
|
145
|
-
});
|
|
146
|
-
hash = await walletClient.writeContract(request);
|
|
147
|
-
}
|
|
148
|
-
catch (error) {
|
|
149
|
-
if (error.message?.includes('Nonce provided for the transaction is lower than the current nonce')) {
|
|
150
|
-
// Wait 2 seconds and retry once
|
|
151
|
-
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
152
|
-
const { request } = await publicClient.simulateContract({
|
|
153
|
-
account: walletClient.account,
|
|
154
|
-
chain: CHAIN_MAP[chainId],
|
|
155
|
-
address: vault.address,
|
|
156
|
-
abi: approvalAbi,
|
|
157
|
-
functionName: 'approve',
|
|
158
|
-
args: [controllerAddress, maxValue],
|
|
159
|
-
});
|
|
160
|
-
hash = await walletClient.writeContract(request);
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
throw error;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
transactionHashes.push(hash);
|
|
167
|
-
// Wait for transaction confirmation to ensure nonce is updated
|
|
168
|
-
await publicClient.waitForTransactionReceipt({ hash });
|
|
169
|
-
// Wait 1 second before proceeding (to avoid nonce issues)
|
|
170
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
90
|
+
transactions.push(await buildTransaction(publicClient, {
|
|
91
|
+
chain: CHAIN_MAP[chainId],
|
|
92
|
+
account: userAddress,
|
|
93
|
+
address: vault.address,
|
|
94
|
+
abi: approvalAbi,
|
|
95
|
+
functionName: 'approve',
|
|
96
|
+
args: [controllerAddress, maxValue],
|
|
97
|
+
}, options?.gasLimit));
|
|
171
98
|
}
|
|
172
99
|
// Approve vault to book manager if needed
|
|
173
100
|
if (vaultToBookManagerAllowance < threshold) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
functionName: 'approve',
|
|
183
|
-
args: [bookManagerAddress, maxValue],
|
|
184
|
-
});
|
|
185
|
-
hash = await walletClient.writeContract(request);
|
|
186
|
-
}
|
|
187
|
-
catch (error) {
|
|
188
|
-
if (error.message?.includes('Nonce provided for the transaction is lower than the current nonce')) {
|
|
189
|
-
// Wait 2 seconds and retry once
|
|
190
|
-
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
191
|
-
const { request } = await publicClient.simulateContract({
|
|
192
|
-
account: walletClient.account,
|
|
193
|
-
chain: CHAIN_MAP[chainId],
|
|
194
|
-
address: vault.address,
|
|
195
|
-
abi: approvalAbi,
|
|
196
|
-
functionName: 'approve',
|
|
197
|
-
args: [bookManagerAddress, maxValue],
|
|
198
|
-
});
|
|
199
|
-
hash = await walletClient.writeContract(request);
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
throw error;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
transactionHashes.push(hash);
|
|
206
|
-
// Wait for transaction confirmation to ensure nonce is updated
|
|
207
|
-
await publicClient.waitForTransactionReceipt({ hash });
|
|
101
|
+
transactions.push(await buildTransaction(publicClient, {
|
|
102
|
+
chain: CHAIN_MAP[chainId],
|
|
103
|
+
account: userAddress,
|
|
104
|
+
address: vault.address,
|
|
105
|
+
abi: approvalAbi,
|
|
106
|
+
functionName: 'approve',
|
|
107
|
+
args: [bookManagerAddress, maxValue],
|
|
108
|
+
}, options?.gasLimit));
|
|
208
109
|
}
|
|
209
|
-
// Return all
|
|
210
|
-
return
|
|
110
|
+
// Return all transactions - caller should send sequentially with proper nonce management
|
|
111
|
+
return transactions;
|
|
211
112
|
};
|
|
212
113
|
//# sourceMappingURL=token.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../../../src/calls/approval/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,IAAI,
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../../../src/calls/approval/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAEhD,OAAO,EAAa,SAAS,EAAE,MAAM,qCAAqC,CAAC;AAE3E,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,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