@mayanfinance/blink-sdk 0.1.8
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/LICENSE +21 -0
- package/README.md +15 -0
- package/dist/amount.d.ts +4 -0
- package/dist/amount.js +32 -0
- package/dist/client.d.ts +17 -0
- package/dist/client.js +32 -0
- package/dist/deposit.d.ts +64 -0
- package/dist/deposit.js +526 -0
- package/dist/evm.d.ts +237 -0
- package/dist/evm.js +679 -0
- package/dist/gen/blink/v1/admin_pb.d.ts +1338 -0
- package/dist/gen/blink/v1/admin_pb.js +234 -0
- package/dist/gen/blink/v1/api_pb.d.ts +1811 -0
- package/dist/gen/blink/v1/api_pb.js +294 -0
- package/dist/gen/blink/v1/partner_pb.d.ts +1020 -0
- package/dist/gen/blink/v1/partner_pb.js +247 -0
- package/dist/gen/blink/v1/ratelimit_pb.d.ts +10 -0
- package/dist/gen/blink/v1/ratelimit_pb.js +13 -0
- package/dist/gen/google/api/annotations_pb.d.ts +11 -0
- package/dist/gen/google/api/annotations_pb.js +14 -0
- package/dist/gen/google/api/http_pb.d.ts +111 -0
- package/dist/gen/google/api/http_pb.js +23 -0
- package/dist/hash.d.ts +7 -0
- package/dist/hash.js +76 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +13 -0
- package/dist/order.d.ts +42 -0
- package/dist/order.js +173 -0
- package/dist/sui.d.ts +74 -0
- package/dist/sui.js +310 -0
- package/dist/svm.d.ts +36 -0
- package/dist/svm.js +504 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/dist/wallets.d.ts +84 -0
- package/dist/wallets.js +409 -0
- package/package.json +62 -0
package/dist/evm.js
ADDED
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
import { createPublicClient, encodeAbiParameters, encodeFunctionData, erc20Abi, http, keccak256, stringToHex, toHex, } from "viem";
|
|
2
|
+
const ROUTER_ABI = [
|
|
3
|
+
{
|
|
4
|
+
type: "function",
|
|
5
|
+
name: "depositNative",
|
|
6
|
+
stateMutability: "payable",
|
|
7
|
+
inputs: [{ name: "quoteHash", type: "bytes32" }],
|
|
8
|
+
outputs: [],
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
type: "function",
|
|
12
|
+
name: "depositToken",
|
|
13
|
+
stateMutability: "nonpayable",
|
|
14
|
+
inputs: [
|
|
15
|
+
{ name: "quoteHash", type: "bytes32" },
|
|
16
|
+
{ name: "token", type: "address" },
|
|
17
|
+
{ name: "amount", type: "uint256" },
|
|
18
|
+
],
|
|
19
|
+
outputs: [],
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
type: "function",
|
|
23
|
+
name: "depositWithSwap",
|
|
24
|
+
stateMutability: "payable",
|
|
25
|
+
inputs: [
|
|
26
|
+
{ name: "quoteHash", type: "bytes32" },
|
|
27
|
+
{ name: "srcToken", type: "address" },
|
|
28
|
+
{ name: "amountIn", type: "uint256" },
|
|
29
|
+
{ name: "aggregator", type: "address" },
|
|
30
|
+
{ name: "swapData", type: "bytes" },
|
|
31
|
+
{ name: "bridgeToken", type: "address" },
|
|
32
|
+
{ name: "minBridgeOut", type: "uint256" },
|
|
33
|
+
],
|
|
34
|
+
outputs: [],
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
type: "function",
|
|
38
|
+
name: "swapAndForward",
|
|
39
|
+
stateMutability: "payable",
|
|
40
|
+
inputs: [
|
|
41
|
+
{ name: "quoteHash", type: "bytes32" },
|
|
42
|
+
{ name: "srcToken", type: "address" },
|
|
43
|
+
{ name: "amountIn", type: "uint256" },
|
|
44
|
+
{ name: "aggregator", type: "address" },
|
|
45
|
+
{ name: "swapData", type: "bytes" },
|
|
46
|
+
{ name: "dstToken", type: "address" },
|
|
47
|
+
{ name: "minAmountOut", type: "uint256" },
|
|
48
|
+
{ name: "amountOut", type: "uint256" },
|
|
49
|
+
{ name: "referrerBps", type: "uint16" },
|
|
50
|
+
{ name: "recipient", type: "address" },
|
|
51
|
+
],
|
|
52
|
+
outputs: [],
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
type: "function",
|
|
56
|
+
name: "depositTokenWithPermit",
|
|
57
|
+
stateMutability: "nonpayable",
|
|
58
|
+
inputs: [
|
|
59
|
+
{ name: "quoteHash", type: "bytes32" },
|
|
60
|
+
{ name: "token", type: "address" },
|
|
61
|
+
{ name: "amount", type: "uint256" },
|
|
62
|
+
{ name: "deadline", type: "uint256" },
|
|
63
|
+
{ name: "v", type: "uint8" },
|
|
64
|
+
{ name: "r", type: "bytes32" },
|
|
65
|
+
{ name: "s", type: "bytes32" },
|
|
66
|
+
],
|
|
67
|
+
outputs: [],
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
type: "function",
|
|
71
|
+
name: "depositTokenWithPermit2",
|
|
72
|
+
stateMutability: "nonpayable",
|
|
73
|
+
inputs: [
|
|
74
|
+
{ name: "quoteHash", type: "bytes32" },
|
|
75
|
+
{ name: "token", type: "address" },
|
|
76
|
+
{ name: "amount", type: "uint256" },
|
|
77
|
+
{ name: "nonce", type: "uint256" },
|
|
78
|
+
{ name: "deadline", type: "uint256" },
|
|
79
|
+
{ name: "signature", type: "bytes" },
|
|
80
|
+
],
|
|
81
|
+
outputs: [],
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
type: "function",
|
|
85
|
+
name: "depositWithSwapAndPermit",
|
|
86
|
+
stateMutability: "nonpayable",
|
|
87
|
+
inputs: [
|
|
88
|
+
{ name: "quoteHash", type: "bytes32" },
|
|
89
|
+
{ name: "srcToken", type: "address" },
|
|
90
|
+
{ name: "amountIn", type: "uint256" },
|
|
91
|
+
{ name: "aggregator", type: "address" },
|
|
92
|
+
{ name: "swapData", type: "bytes" },
|
|
93
|
+
{ name: "bridgeToken", type: "address" },
|
|
94
|
+
{ name: "minBridgeOut", type: "uint256" },
|
|
95
|
+
{ name: "deadline", type: "uint256" },
|
|
96
|
+
{ name: "v", type: "uint8" },
|
|
97
|
+
{ name: "r", type: "bytes32" },
|
|
98
|
+
{ name: "s", type: "bytes32" },
|
|
99
|
+
],
|
|
100
|
+
outputs: [],
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
type: "function",
|
|
104
|
+
name: "depositWithSwapAndPermit2",
|
|
105
|
+
stateMutability: "nonpayable",
|
|
106
|
+
inputs: [
|
|
107
|
+
{ name: "quoteHash", type: "bytes32" },
|
|
108
|
+
{ name: "srcToken", type: "address" },
|
|
109
|
+
{ name: "amountIn", type: "uint256" },
|
|
110
|
+
{ name: "aggregator", type: "address" },
|
|
111
|
+
{ name: "swapData", type: "bytes" },
|
|
112
|
+
{ name: "bridgeToken", type: "address" },
|
|
113
|
+
{ name: "minBridgeOut", type: "uint256" },
|
|
114
|
+
{ name: "nonce", type: "uint256" },
|
|
115
|
+
{ name: "deadline", type: "uint256" },
|
|
116
|
+
{ name: "signature", type: "bytes" },
|
|
117
|
+
],
|
|
118
|
+
outputs: [],
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
type: "function",
|
|
122
|
+
name: "swapAndForwardWithPermit",
|
|
123
|
+
stateMutability: "nonpayable",
|
|
124
|
+
inputs: [
|
|
125
|
+
{ name: "quoteHash", type: "bytes32" },
|
|
126
|
+
{ name: "srcToken", type: "address" },
|
|
127
|
+
{ name: "amountIn", type: "uint256" },
|
|
128
|
+
{ name: "aggregator", type: "address" },
|
|
129
|
+
{ name: "swapData", type: "bytes" },
|
|
130
|
+
{ name: "dstToken", type: "address" },
|
|
131
|
+
{ name: "minAmountOut", type: "uint256" },
|
|
132
|
+
{ name: "amountOut", type: "uint256" },
|
|
133
|
+
{ name: "referrerBps", type: "uint16" },
|
|
134
|
+
{ name: "recipient", type: "address" },
|
|
135
|
+
{ name: "deadline", type: "uint256" },
|
|
136
|
+
{ name: "v", type: "uint8" },
|
|
137
|
+
{ name: "r", type: "bytes32" },
|
|
138
|
+
{ name: "s", type: "bytes32" },
|
|
139
|
+
],
|
|
140
|
+
outputs: [],
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
type: "function",
|
|
144
|
+
name: "swapAndForwardWithPermit2",
|
|
145
|
+
stateMutability: "nonpayable",
|
|
146
|
+
inputs: [
|
|
147
|
+
{ name: "quoteHash", type: "bytes32" },
|
|
148
|
+
{ name: "srcToken", type: "address" },
|
|
149
|
+
{ name: "amountIn", type: "uint256" },
|
|
150
|
+
{ name: "aggregator", type: "address" },
|
|
151
|
+
{ name: "swapData", type: "bytes" },
|
|
152
|
+
{ name: "dstToken", type: "address" },
|
|
153
|
+
{ name: "minAmountOut", type: "uint256" },
|
|
154
|
+
{ name: "amountOut", type: "uint256" },
|
|
155
|
+
{ name: "referrerBps", type: "uint16" },
|
|
156
|
+
{ name: "recipient", type: "address" },
|
|
157
|
+
{ name: "nonce", type: "uint256" },
|
|
158
|
+
{ name: "deadline", type: "uint256" },
|
|
159
|
+
{ name: "signature", type: "bytes" },
|
|
160
|
+
],
|
|
161
|
+
outputs: [],
|
|
162
|
+
},
|
|
163
|
+
];
|
|
164
|
+
const PERMIT_ABI = [
|
|
165
|
+
{
|
|
166
|
+
type: "function",
|
|
167
|
+
name: "nonces",
|
|
168
|
+
stateMutability: "view",
|
|
169
|
+
inputs: [{ name: "owner", type: "address" }],
|
|
170
|
+
outputs: [{ type: "uint256" }],
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
type: "function",
|
|
174
|
+
name: "DOMAIN_SEPARATOR",
|
|
175
|
+
stateMutability: "view",
|
|
176
|
+
inputs: [],
|
|
177
|
+
outputs: [{ type: "bytes32" }],
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
type: "function",
|
|
181
|
+
name: "name",
|
|
182
|
+
stateMutability: "view",
|
|
183
|
+
inputs: [],
|
|
184
|
+
outputs: [{ type: "string" }],
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
type: "function",
|
|
188
|
+
name: "version",
|
|
189
|
+
stateMutability: "view",
|
|
190
|
+
inputs: [],
|
|
191
|
+
outputs: [{ type: "string" }],
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
type: "function",
|
|
195
|
+
name: "PERMIT_TYPEHASH",
|
|
196
|
+
stateMutability: "view",
|
|
197
|
+
inputs: [],
|
|
198
|
+
outputs: [{ type: "bytes32" }],
|
|
199
|
+
},
|
|
200
|
+
];
|
|
201
|
+
export const PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
|
|
202
|
+
const EIP712_DOMAIN_TYPEHASH = keccak256(stringToHex("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"));
|
|
203
|
+
const STANDARD_PERMIT_TYPEHASH = keccak256(stringToHex("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"));
|
|
204
|
+
export const isNativeEvmToken = (address) => /^0x0*$/i.test(address);
|
|
205
|
+
const hex = (value) => (value.startsWith("0x") ? value : `0x${value}`);
|
|
206
|
+
const client = (rpcUrl) => createPublicClient({ transport: http(rpcUrl) });
|
|
207
|
+
export const evmGasPrice = (rpcUrl) => client(rpcUrl).getGasPrice();
|
|
208
|
+
export const evmEstimateGas = (rpcUrl, from, call, allowance) => client(rpcUrl).estimateGas({
|
|
209
|
+
account: from,
|
|
210
|
+
to: call.to,
|
|
211
|
+
data: call.data,
|
|
212
|
+
value: call.value === undefined ? undefined : BigInt(call.value),
|
|
213
|
+
stateOverride: allowance && [
|
|
214
|
+
{
|
|
215
|
+
address: allowance.token,
|
|
216
|
+
stateDiff: [
|
|
217
|
+
{ slot: allowance.slot, value: toHex(allowance.amount, { size: 32 }) },
|
|
218
|
+
],
|
|
219
|
+
},
|
|
220
|
+
],
|
|
221
|
+
});
|
|
222
|
+
export const evmAllowanceSlot = async (rpcUrl, token, owner, spender) => {
|
|
223
|
+
const rpc = client(rpcUrl);
|
|
224
|
+
const slotsOf = async (holder) => {
|
|
225
|
+
const { accessList } = await rpc.createAccessList({
|
|
226
|
+
account: holder,
|
|
227
|
+
to: token,
|
|
228
|
+
data: encodeFunctionData({
|
|
229
|
+
abi: erc20Abi,
|
|
230
|
+
functionName: "allowance",
|
|
231
|
+
args: [holder, spender],
|
|
232
|
+
}),
|
|
233
|
+
});
|
|
234
|
+
return accessList
|
|
235
|
+
.filter((entry) => entry.address.toLowerCase() === token.toLowerCase())
|
|
236
|
+
.flatMap((entry) => entry.storageKeys);
|
|
237
|
+
};
|
|
238
|
+
const other = toHex(BigInt(owner) ^ 1n, { size: 20 });
|
|
239
|
+
const [slots, shared] = await Promise.all([slotsOf(owner), slotsOf(other)]);
|
|
240
|
+
const own = slots.filter((slot) => !shared.includes(slot));
|
|
241
|
+
return own.length === 1 ? own[0] : undefined;
|
|
242
|
+
};
|
|
243
|
+
export const evmBalance = async (rpcUrl, token, owner) => {
|
|
244
|
+
if (isNativeEvmToken(token)) {
|
|
245
|
+
return client(rpcUrl).getBalance({ address: owner });
|
|
246
|
+
}
|
|
247
|
+
return client(rpcUrl).readContract({
|
|
248
|
+
address: token,
|
|
249
|
+
abi: erc20Abi,
|
|
250
|
+
functionName: "balanceOf",
|
|
251
|
+
args: [owner],
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
export const evmAllowance = (rpcUrl, token, owner, spender) => client(rpcUrl).readContract({
|
|
255
|
+
address: token,
|
|
256
|
+
abi: erc20Abi,
|
|
257
|
+
functionName: "allowance",
|
|
258
|
+
args: [owner, spender],
|
|
259
|
+
});
|
|
260
|
+
const domainSeparator = (name, version, chainId, token) => keccak256(encodeAbiParameters([
|
|
261
|
+
{ type: "bytes32" },
|
|
262
|
+
{ type: "bytes32" },
|
|
263
|
+
{ type: "bytes32" },
|
|
264
|
+
{ type: "uint256" },
|
|
265
|
+
{ type: "address" },
|
|
266
|
+
], [
|
|
267
|
+
EIP712_DOMAIN_TYPEHASH,
|
|
268
|
+
keccak256(stringToHex(name)),
|
|
269
|
+
keccak256(stringToHex(version)),
|
|
270
|
+
chainId,
|
|
271
|
+
token,
|
|
272
|
+
]));
|
|
273
|
+
export const detectPermit = async (rpcUrl, token, owner, chainId) => {
|
|
274
|
+
const rpc = client(rpcUrl);
|
|
275
|
+
const contract = { address: token, abi: PERMIT_ABI };
|
|
276
|
+
let nonce;
|
|
277
|
+
let separator;
|
|
278
|
+
let name;
|
|
279
|
+
try {
|
|
280
|
+
[nonce, separator, name] = await Promise.all([
|
|
281
|
+
rpc.readContract({ ...contract, functionName: "nonces", args: [owner] }),
|
|
282
|
+
rpc.readContract({ ...contract, functionName: "DOMAIN_SEPARATOR" }),
|
|
283
|
+
rpc.readContract({ ...contract, functionName: "name" }),
|
|
284
|
+
]);
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|
|
289
|
+
try {
|
|
290
|
+
const typehash = await rpc.readContract({
|
|
291
|
+
...contract,
|
|
292
|
+
functionName: "PERMIT_TYPEHASH",
|
|
293
|
+
});
|
|
294
|
+
if (typehash !== STANDARD_PERMIT_TYPEHASH) {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
catch { }
|
|
299
|
+
const versions = ["1", "2"];
|
|
300
|
+
try {
|
|
301
|
+
versions.unshift(await rpc.readContract({ ...contract, functionName: "version" }));
|
|
302
|
+
}
|
|
303
|
+
catch { }
|
|
304
|
+
for (const version of versions) {
|
|
305
|
+
if (domainSeparator(name, version, chainId, token) === separator) {
|
|
306
|
+
return { name, version, nonce };
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return undefined;
|
|
310
|
+
};
|
|
311
|
+
export const permitTypedData = (support, chainId, token, owner, spender, value, deadline) => ({
|
|
312
|
+
types: {
|
|
313
|
+
EIP712Domain: [
|
|
314
|
+
{ name: "name", type: "string" },
|
|
315
|
+
{ name: "version", type: "string" },
|
|
316
|
+
{ name: "chainId", type: "uint256" },
|
|
317
|
+
{ name: "verifyingContract", type: "address" },
|
|
318
|
+
],
|
|
319
|
+
Permit: [
|
|
320
|
+
{ name: "owner", type: "address" },
|
|
321
|
+
{ name: "spender", type: "address" },
|
|
322
|
+
{ name: "value", type: "uint256" },
|
|
323
|
+
{ name: "nonce", type: "uint256" },
|
|
324
|
+
{ name: "deadline", type: "uint256" },
|
|
325
|
+
],
|
|
326
|
+
},
|
|
327
|
+
primaryType: "Permit",
|
|
328
|
+
domain: {
|
|
329
|
+
name: support.name,
|
|
330
|
+
version: support.version,
|
|
331
|
+
chainId: Number(chainId),
|
|
332
|
+
verifyingContract: token,
|
|
333
|
+
},
|
|
334
|
+
message: {
|
|
335
|
+
owner,
|
|
336
|
+
spender,
|
|
337
|
+
value: value.toString(),
|
|
338
|
+
nonce: support.nonce.toString(),
|
|
339
|
+
deadline: deadline.toString(),
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
const permit2WitnessBase = {
|
|
343
|
+
EIP712Domain: [
|
|
344
|
+
{ name: "name", type: "string" },
|
|
345
|
+
{ name: "chainId", type: "uint256" },
|
|
346
|
+
{ name: "verifyingContract", type: "address" },
|
|
347
|
+
],
|
|
348
|
+
TokenPermissions: [
|
|
349
|
+
{ name: "token", type: "address" },
|
|
350
|
+
{ name: "amount", type: "uint256" },
|
|
351
|
+
],
|
|
352
|
+
};
|
|
353
|
+
export const permit2WitnessDepositTypedData = (chainId, token, amount, spender, nonce, deadline, quoteHash) => ({
|
|
354
|
+
types: {
|
|
355
|
+
...permit2WitnessBase,
|
|
356
|
+
PermitWitnessTransferFrom: [
|
|
357
|
+
{ name: "permitted", type: "TokenPermissions" },
|
|
358
|
+
{ name: "spender", type: "address" },
|
|
359
|
+
{ name: "nonce", type: "uint256" },
|
|
360
|
+
{ name: "deadline", type: "uint256" },
|
|
361
|
+
{ name: "witness", type: "BlinkDeposit" },
|
|
362
|
+
],
|
|
363
|
+
BlinkDeposit: [{ name: "quoteHash", type: "bytes32" }],
|
|
364
|
+
},
|
|
365
|
+
primaryType: "PermitWitnessTransferFrom",
|
|
366
|
+
domain: {
|
|
367
|
+
name: "Permit2",
|
|
368
|
+
chainId: Number(chainId),
|
|
369
|
+
verifyingContract: PERMIT2_ADDRESS,
|
|
370
|
+
},
|
|
371
|
+
message: {
|
|
372
|
+
permitted: { token, amount: amount.toString() },
|
|
373
|
+
spender,
|
|
374
|
+
nonce: nonce.toString(),
|
|
375
|
+
deadline: deadline.toString(),
|
|
376
|
+
witness: { quoteHash: hex(quoteHash) },
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
export const permit2WitnessSwapDepositTypedData = (chainId, token, amount, spender, nonce, deadline, quoteHash, bridgeToken, minBridgeOut) => ({
|
|
380
|
+
types: {
|
|
381
|
+
...permit2WitnessBase,
|
|
382
|
+
PermitWitnessTransferFrom: [
|
|
383
|
+
{ name: "permitted", type: "TokenPermissions" },
|
|
384
|
+
{ name: "spender", type: "address" },
|
|
385
|
+
{ name: "nonce", type: "uint256" },
|
|
386
|
+
{ name: "deadline", type: "uint256" },
|
|
387
|
+
{ name: "witness", type: "BlinkSwapDeposit" },
|
|
388
|
+
],
|
|
389
|
+
BlinkSwapDeposit: [
|
|
390
|
+
{ name: "quoteHash", type: "bytes32" },
|
|
391
|
+
{ name: "bridgeToken", type: "address" },
|
|
392
|
+
{ name: "minBridgeOut", type: "uint256" },
|
|
393
|
+
],
|
|
394
|
+
},
|
|
395
|
+
primaryType: "PermitWitnessTransferFrom",
|
|
396
|
+
domain: {
|
|
397
|
+
name: "Permit2",
|
|
398
|
+
chainId: Number(chainId),
|
|
399
|
+
verifyingContract: PERMIT2_ADDRESS,
|
|
400
|
+
},
|
|
401
|
+
message: {
|
|
402
|
+
permitted: { token, amount: amount.toString() },
|
|
403
|
+
spender,
|
|
404
|
+
nonce: nonce.toString(),
|
|
405
|
+
deadline: deadline.toString(),
|
|
406
|
+
witness: {
|
|
407
|
+
quoteHash: hex(quoteHash),
|
|
408
|
+
bridgeToken,
|
|
409
|
+
minBridgeOut: minBridgeOut.toString(),
|
|
410
|
+
},
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
export const permit2TypedData = (chainId, token, amount, spender, nonce, deadline) => ({
|
|
414
|
+
types: {
|
|
415
|
+
EIP712Domain: [
|
|
416
|
+
{ name: "name", type: "string" },
|
|
417
|
+
{ name: "chainId", type: "uint256" },
|
|
418
|
+
{ name: "verifyingContract", type: "address" },
|
|
419
|
+
],
|
|
420
|
+
PermitTransferFrom: [
|
|
421
|
+
{ name: "permitted", type: "TokenPermissions" },
|
|
422
|
+
{ name: "spender", type: "address" },
|
|
423
|
+
{ name: "nonce", type: "uint256" },
|
|
424
|
+
{ name: "deadline", type: "uint256" },
|
|
425
|
+
],
|
|
426
|
+
TokenPermissions: [
|
|
427
|
+
{ name: "token", type: "address" },
|
|
428
|
+
{ name: "amount", type: "uint256" },
|
|
429
|
+
],
|
|
430
|
+
},
|
|
431
|
+
primaryType: "PermitTransferFrom",
|
|
432
|
+
domain: {
|
|
433
|
+
name: "Permit2",
|
|
434
|
+
chainId: Number(chainId),
|
|
435
|
+
verifyingContract: PERMIT2_ADDRESS,
|
|
436
|
+
},
|
|
437
|
+
message: {
|
|
438
|
+
permitted: { token, amount: amount.toString() },
|
|
439
|
+
spender,
|
|
440
|
+
nonce: nonce.toString(),
|
|
441
|
+
deadline: deadline.toString(),
|
|
442
|
+
},
|
|
443
|
+
});
|
|
444
|
+
export const randomPermit2Nonce = () => {
|
|
445
|
+
const bytes = crypto.getRandomValues(new Uint8Array(32));
|
|
446
|
+
return BigInt(toHex(bytes));
|
|
447
|
+
};
|
|
448
|
+
export const permitDeadline = () => BigInt(Math.floor(Date.now() / 1000) + 1800);
|
|
449
|
+
export const splitSignature = (signature) => {
|
|
450
|
+
const raw = signature.startsWith("0x") ? signature.slice(2) : signature;
|
|
451
|
+
const v = parseInt(raw.slice(128, 130), 16);
|
|
452
|
+
return {
|
|
453
|
+
v: v < 27 ? v + 27 : v,
|
|
454
|
+
r: `0x${raw.slice(0, 64)}`,
|
|
455
|
+
s: `0x${raw.slice(64, 128)}`,
|
|
456
|
+
};
|
|
457
|
+
};
|
|
458
|
+
export const waitForEvmTransaction = async (rpcUrl, hash) => {
|
|
459
|
+
const receipt = await client(rpcUrl).waitForTransactionReceipt({
|
|
460
|
+
hash: hash,
|
|
461
|
+
});
|
|
462
|
+
if (receipt.status === "reverted") {
|
|
463
|
+
throw new Error("transaction reverted");
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
export const encodeApprove = (spender, amount) => encodeFunctionData({
|
|
467
|
+
abi: erc20Abi,
|
|
468
|
+
functionName: "approve",
|
|
469
|
+
args: [spender, amount],
|
|
470
|
+
});
|
|
471
|
+
export const encodeDirectTransfer = (inventory, amount, quoteHash) => encodeFunctionData({
|
|
472
|
+
abi: erc20Abi,
|
|
473
|
+
functionName: "transfer",
|
|
474
|
+
args: [inventory, amount],
|
|
475
|
+
}) + hex(quoteHash).slice(2);
|
|
476
|
+
export const encodeDepositNative = (quoteHash) => encodeFunctionData({
|
|
477
|
+
abi: ROUTER_ABI,
|
|
478
|
+
functionName: "depositNative",
|
|
479
|
+
args: [hex(quoteHash)],
|
|
480
|
+
});
|
|
481
|
+
export const encodeDepositToken = (quoteHash, token, amount) => encodeFunctionData({
|
|
482
|
+
abi: ROUTER_ABI,
|
|
483
|
+
functionName: "depositToken",
|
|
484
|
+
args: [hex(quoteHash), token, amount],
|
|
485
|
+
});
|
|
486
|
+
export const encodeDepositTokenWithPermit = (quoteHash, token, amount, deadline, signature) => encodeFunctionData({
|
|
487
|
+
abi: ROUTER_ABI,
|
|
488
|
+
functionName: "depositTokenWithPermit",
|
|
489
|
+
args: [
|
|
490
|
+
hex(quoteHash),
|
|
491
|
+
token,
|
|
492
|
+
amount,
|
|
493
|
+
deadline,
|
|
494
|
+
signature.v,
|
|
495
|
+
signature.r,
|
|
496
|
+
signature.s,
|
|
497
|
+
],
|
|
498
|
+
});
|
|
499
|
+
export const encodeDepositTokenWithPermit2 = (quoteHash, token, amount, nonce, deadline, signature) => encodeFunctionData({
|
|
500
|
+
abi: ROUTER_ABI,
|
|
501
|
+
functionName: "depositTokenWithPermit2",
|
|
502
|
+
args: [hex(quoteHash), token, amount, nonce, deadline, hex(signature)],
|
|
503
|
+
});
|
|
504
|
+
export const encodeDepositWithSwap = (quoteHash, swap) => encodeFunctionData({
|
|
505
|
+
abi: ROUTER_ABI,
|
|
506
|
+
functionName: "depositWithSwap",
|
|
507
|
+
args: [
|
|
508
|
+
hex(quoteHash),
|
|
509
|
+
swap.srcToken,
|
|
510
|
+
BigInt(swap.amountIn),
|
|
511
|
+
swap.aggregator,
|
|
512
|
+
hex(swap.swapCalldata),
|
|
513
|
+
swap.bridgeToken,
|
|
514
|
+
BigInt(swap.minBridgeOut),
|
|
515
|
+
],
|
|
516
|
+
});
|
|
517
|
+
export const encodeDepositWithSwapAndPermit = (quoteHash, swap, deadline, signature) => encodeFunctionData({
|
|
518
|
+
abi: ROUTER_ABI,
|
|
519
|
+
functionName: "depositWithSwapAndPermit",
|
|
520
|
+
args: [
|
|
521
|
+
hex(quoteHash),
|
|
522
|
+
swap.srcToken,
|
|
523
|
+
BigInt(swap.amountIn),
|
|
524
|
+
swap.aggregator,
|
|
525
|
+
hex(swap.swapCalldata),
|
|
526
|
+
swap.bridgeToken,
|
|
527
|
+
BigInt(swap.minBridgeOut),
|
|
528
|
+
deadline,
|
|
529
|
+
signature.v,
|
|
530
|
+
signature.r,
|
|
531
|
+
signature.s,
|
|
532
|
+
],
|
|
533
|
+
});
|
|
534
|
+
export const encodeDepositWithSwapAndPermit2 = (quoteHash, swap, nonce, deadline, signature) => encodeFunctionData({
|
|
535
|
+
abi: ROUTER_ABI,
|
|
536
|
+
functionName: "depositWithSwapAndPermit2",
|
|
537
|
+
args: [
|
|
538
|
+
hex(quoteHash),
|
|
539
|
+
swap.srcToken,
|
|
540
|
+
BigInt(swap.amountIn),
|
|
541
|
+
swap.aggregator,
|
|
542
|
+
hex(swap.swapCalldata),
|
|
543
|
+
swap.bridgeToken,
|
|
544
|
+
BigInt(swap.minBridgeOut),
|
|
545
|
+
nonce,
|
|
546
|
+
deadline,
|
|
547
|
+
hex(signature),
|
|
548
|
+
],
|
|
549
|
+
});
|
|
550
|
+
const referrerTotalBps = (referrers) => referrers.reduce((total, r) => total + r.bps, 0);
|
|
551
|
+
export const encodeSwapAndForward = (quoteHash, params, swap) => encodeFunctionData({
|
|
552
|
+
abi: ROUTER_ABI,
|
|
553
|
+
functionName: "swapAndForward",
|
|
554
|
+
args: [
|
|
555
|
+
hex(quoteHash),
|
|
556
|
+
params.srcToken,
|
|
557
|
+
BigInt(params.amountIn),
|
|
558
|
+
swap.aggregator,
|
|
559
|
+
hex(swap.swapCalldata),
|
|
560
|
+
params.dstToken,
|
|
561
|
+
BigInt(params.minAmountOut),
|
|
562
|
+
BigInt(params.amountOut),
|
|
563
|
+
referrerTotalBps(params.referrers),
|
|
564
|
+
params.recipient,
|
|
565
|
+
],
|
|
566
|
+
});
|
|
567
|
+
export const encodeSwapAndForwardWithPermit = (quoteHash, params, swap, deadline, signature) => encodeFunctionData({
|
|
568
|
+
abi: ROUTER_ABI,
|
|
569
|
+
functionName: "swapAndForwardWithPermit",
|
|
570
|
+
args: [
|
|
571
|
+
hex(quoteHash),
|
|
572
|
+
params.srcToken,
|
|
573
|
+
BigInt(params.amountIn),
|
|
574
|
+
swap.aggregator,
|
|
575
|
+
hex(swap.swapCalldata),
|
|
576
|
+
params.dstToken,
|
|
577
|
+
BigInt(params.minAmountOut),
|
|
578
|
+
BigInt(params.amountOut),
|
|
579
|
+
referrerTotalBps(params.referrers),
|
|
580
|
+
params.recipient,
|
|
581
|
+
deadline,
|
|
582
|
+
signature.v,
|
|
583
|
+
signature.r,
|
|
584
|
+
signature.s,
|
|
585
|
+
],
|
|
586
|
+
});
|
|
587
|
+
export const encodeSwapAndForwardWithPermit2 = (quoteHash, params, swap, nonce, deadline, signature) => encodeFunctionData({
|
|
588
|
+
abi: ROUTER_ABI,
|
|
589
|
+
functionName: "swapAndForwardWithPermit2",
|
|
590
|
+
args: [
|
|
591
|
+
hex(quoteHash),
|
|
592
|
+
params.srcToken,
|
|
593
|
+
BigInt(params.amountIn),
|
|
594
|
+
swap.aggregator,
|
|
595
|
+
hex(swap.swapCalldata),
|
|
596
|
+
params.dstToken,
|
|
597
|
+
BigInt(params.minAmountOut),
|
|
598
|
+
BigInt(params.amountOut),
|
|
599
|
+
referrerTotalBps(params.referrers),
|
|
600
|
+
params.recipient,
|
|
601
|
+
nonce,
|
|
602
|
+
deadline,
|
|
603
|
+
hex(signature),
|
|
604
|
+
],
|
|
605
|
+
});
|
|
606
|
+
export const hexValue = (value) => toHex(value);
|
|
607
|
+
export const evmSwapOf = (instructions) => {
|
|
608
|
+
if (instructions.payload.case !== "evm") {
|
|
609
|
+
throw new Error("expected evm swap instructions");
|
|
610
|
+
}
|
|
611
|
+
const evm = instructions.payload.value;
|
|
612
|
+
return {
|
|
613
|
+
srcToken: instructions.srcToken,
|
|
614
|
+
amountIn: instructions.amountIn,
|
|
615
|
+
aggregator: evm.aggregator,
|
|
616
|
+
swapCalldata: evm.swapCalldata,
|
|
617
|
+
bridgeToken: instructions.bridgeToken,
|
|
618
|
+
minBridgeOut: instructions.minBridgeOut,
|
|
619
|
+
value: evm.value,
|
|
620
|
+
};
|
|
621
|
+
};
|
|
622
|
+
export const usesDirectTransfer = (request) => !!request.directTransfer &&
|
|
623
|
+
!isNativeEvmToken(request.params.srcToken) &&
|
|
624
|
+
request.params.srcChain !== request.params.dstChain &&
|
|
625
|
+
request.params.srcToken === request.params.bridgeTokenSrc;
|
|
626
|
+
const permitRouterOf = (request) => {
|
|
627
|
+
if (!request.router) {
|
|
628
|
+
throw new Error("permit deposits without a swap need the chain router");
|
|
629
|
+
}
|
|
630
|
+
return request.router;
|
|
631
|
+
};
|
|
632
|
+
export const buildEvmDepositCall = (request, signature) => {
|
|
633
|
+
const { quoteHash, params } = request;
|
|
634
|
+
const amountIn = BigInt(params.amountIn);
|
|
635
|
+
const native = isNativeEvmToken(params.srcToken);
|
|
636
|
+
if (!signature && usesDirectTransfer(request)) {
|
|
637
|
+
return {
|
|
638
|
+
to: params.srcToken,
|
|
639
|
+
data: encodeDirectTransfer(params.routerAddress, amountIn, quoteHash),
|
|
640
|
+
value: hexValue(0n),
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
if (native && signature) {
|
|
644
|
+
throw new Error("native deposits do not take a permit");
|
|
645
|
+
}
|
|
646
|
+
const sameChain = params.srcChain === params.dstChain;
|
|
647
|
+
const evmSwap = request.swap ? evmSwapOf(request.swap) : undefined;
|
|
648
|
+
const to = request.swap
|
|
649
|
+
? request.swap.routerAddress
|
|
650
|
+
: signature
|
|
651
|
+
? permitRouterOf(request)
|
|
652
|
+
: params.routerAddress;
|
|
653
|
+
const value = evmSwap ? BigInt(evmSwap.value) : native ? amountIn : 0n;
|
|
654
|
+
let data;
|
|
655
|
+
if (!signature) {
|
|
656
|
+
data = evmSwap
|
|
657
|
+
? sameChain
|
|
658
|
+
? encodeSwapAndForward(quoteHash, params, evmSwap)
|
|
659
|
+
: encodeDepositWithSwap(quoteHash, evmSwap)
|
|
660
|
+
: native
|
|
661
|
+
? encodeDepositNative(quoteHash)
|
|
662
|
+
: encodeDepositToken(quoteHash, params.srcToken, amountIn);
|
|
663
|
+
}
|
|
664
|
+
else if (signature.kind === "permit") {
|
|
665
|
+
data = evmSwap
|
|
666
|
+
? sameChain
|
|
667
|
+
? encodeSwapAndForwardWithPermit(quoteHash, params, evmSwap, signature.deadline, signature.signature)
|
|
668
|
+
: encodeDepositWithSwapAndPermit(quoteHash, evmSwap, signature.deadline, signature.signature)
|
|
669
|
+
: encodeDepositTokenWithPermit(quoteHash, params.srcToken, amountIn, signature.deadline, signature.signature);
|
|
670
|
+
}
|
|
671
|
+
else {
|
|
672
|
+
data = evmSwap
|
|
673
|
+
? sameChain
|
|
674
|
+
? encodeSwapAndForwardWithPermit2(quoteHash, params, evmSwap, signature.nonce, signature.deadline, signature.signature)
|
|
675
|
+
: encodeDepositWithSwapAndPermit2(quoteHash, evmSwap, signature.nonce, signature.deadline, signature.signature)
|
|
676
|
+
: encodeDepositTokenWithPermit2(quoteHash, params.srcToken, amountIn, signature.nonce, signature.deadline, signature.signature);
|
|
677
|
+
}
|
|
678
|
+
return { to, data, value: hexValue(value) };
|
|
679
|
+
};
|