@shogun-sdk/swap 0.0.2-test.23 → 0.0.2-test.25
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/core.cjs +93 -29
- package/dist/core.d.cts +5 -4
- package/dist/core.d.ts +5 -4
- package/dist/core.js +80 -16
- package/dist/{execute-CKTsf_tD.d.ts → execute-D2qcOzkI.d.ts} +1 -1
- package/dist/{execute-DOv1i2Su.d.cts → execute-Xvw4wXBo.d.cts} +1 -1
- package/dist/index.cjs +107 -27
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +97 -17
- package/dist/react.cjs +95 -32
- package/dist/react.d.cts +3 -3
- package/dist/react.d.ts +3 -3
- package/dist/react.js +79 -16
- package/dist/{wallet-MmUIz8GE.d.cts → wallet-BhuMJ3K_.d.cts} +2 -1
- package/dist/{wallet-MmUIz8GE.d.ts → wallet-BhuMJ3K_.d.ts} +2 -1
- package/dist/wallet-adapter.cjs +33 -2
- package/dist/wallet-adapter.d.cts +1 -1
- package/dist/wallet-adapter.d.ts +1 -1
- package/dist/wallet-adapter.js +36 -4
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -33,6 +33,7 @@ __export(src_exports, {
|
|
|
33
33
|
getTokenList: () => getTokenList,
|
|
34
34
|
isNativeAddress: () => isNativeAddress,
|
|
35
35
|
isViemWalletClient: () => isViemWalletClient,
|
|
36
|
+
normalizeEvmTokenAddress: () => normalizeEvmTokenAddress,
|
|
36
37
|
serializeBigIntsToStrings: () => serializeBigIntsToStrings,
|
|
37
38
|
useBalances: () => useBalances,
|
|
38
39
|
useExecuteOrder: () => useExecuteOrder,
|
|
@@ -49,12 +50,13 @@ async function getTokenList(params) {
|
|
|
49
50
|
|
|
50
51
|
// src/core/getQuote.ts
|
|
51
52
|
var import_intents_sdk4 = require("@shogun-sdk/intents-sdk");
|
|
52
|
-
var
|
|
53
|
+
var import_viem2 = require("viem");
|
|
53
54
|
|
|
54
55
|
// src/core/executeOrder/normalizeNative.ts
|
|
55
56
|
var import_intents_sdk3 = require("@shogun-sdk/intents-sdk");
|
|
56
57
|
|
|
57
58
|
// src/utils/address.ts
|
|
59
|
+
var import_viem = require("viem");
|
|
58
60
|
var NATIVE_TOKEN = {
|
|
59
61
|
ETH: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
|
|
60
62
|
SOL: "So11111111111111111111111111111111111111111",
|
|
@@ -65,6 +67,10 @@ var isNativeAddress = (tokenAddress) => {
|
|
|
65
67
|
const normalizedTokenAddress = tokenAddress.toLowerCase();
|
|
66
68
|
return !!tokenAddress && NATIVE_ADDRESSES.includes(normalizedTokenAddress);
|
|
67
69
|
};
|
|
70
|
+
function normalizeEvmTokenAddress(address) {
|
|
71
|
+
const lower = address.toLowerCase();
|
|
72
|
+
return lower === NATIVE_TOKEN.ETH.toLowerCase() ? import_viem.zeroAddress : address;
|
|
73
|
+
}
|
|
68
74
|
|
|
69
75
|
// src/utils/chain.ts
|
|
70
76
|
var import_intents_sdk2 = require("@shogun-sdk/intents-sdk");
|
|
@@ -235,7 +241,7 @@ function buildQuoteParams({
|
|
|
235
241
|
tokenOut,
|
|
236
242
|
sourceChainId,
|
|
237
243
|
destChainId,
|
|
238
|
-
amount: (0,
|
|
244
|
+
amount: (0, import_viem2.parseUnits)(amount.toString(), tokenIn.decimals ?? 18),
|
|
239
245
|
slippage
|
|
240
246
|
};
|
|
241
247
|
}
|
|
@@ -290,8 +296,8 @@ async function getBalances(params, options) {
|
|
|
290
296
|
}
|
|
291
297
|
|
|
292
298
|
// src/core/executeOrder/execute.ts
|
|
293
|
-
var
|
|
294
|
-
var
|
|
299
|
+
var import_intents_sdk11 = require("@shogun-sdk/intents-sdk");
|
|
300
|
+
var import_viem6 = require("viem");
|
|
295
301
|
|
|
296
302
|
// src/wallet-adapter/svm-wallet-adapter/adapter.ts
|
|
297
303
|
var import_web3 = require("@solana/web3.js");
|
|
@@ -333,7 +339,7 @@ var adaptSolanaWallet = (walletAddress, chainId, rpcUrl, signAndSendTransaction)
|
|
|
333
339
|
// src/wallet-adapter/evm-wallet-adapter/adapter.ts
|
|
334
340
|
var import_ethers = require("ethers/lib/ethers.js");
|
|
335
341
|
var import_utils2 = require("ethers/lib/utils.js");
|
|
336
|
-
var
|
|
342
|
+
var import_viem3 = require("viem");
|
|
337
343
|
function isEVMTransaction(tx) {
|
|
338
344
|
return typeof tx.from === "string";
|
|
339
345
|
}
|
|
@@ -369,6 +375,21 @@ var adaptEthersSigner = (signer, transport) => {
|
|
|
369
375
|
throw error;
|
|
370
376
|
}
|
|
371
377
|
};
|
|
378
|
+
const readContract = async ({
|
|
379
|
+
address,
|
|
380
|
+
abi,
|
|
381
|
+
functionName,
|
|
382
|
+
args = []
|
|
383
|
+
}) => {
|
|
384
|
+
const iface = new import_ethers.utils.Interface(abi);
|
|
385
|
+
const fnArgs = Array.isArray(args) ? args : [];
|
|
386
|
+
const data = iface.encodeFunctionData(functionName, fnArgs);
|
|
387
|
+
const provider = signer.provider;
|
|
388
|
+
if (!provider) throw new Error("Signer has no provider");
|
|
389
|
+
const result = await provider.call({ to: address, data });
|
|
390
|
+
const decoded = iface.decodeFunctionResult(functionName, result);
|
|
391
|
+
return decoded[0];
|
|
392
|
+
};
|
|
372
393
|
return {
|
|
373
394
|
vmType: "EVM" /* EVM */,
|
|
374
395
|
transport,
|
|
@@ -376,7 +397,8 @@ var adaptEthersSigner = (signer, transport) => {
|
|
|
376
397
|
address: async () => signer.getAddress(),
|
|
377
398
|
sendTransaction,
|
|
378
399
|
signTypedData,
|
|
379
|
-
switchChain
|
|
400
|
+
switchChain,
|
|
401
|
+
readContract
|
|
380
402
|
};
|
|
381
403
|
};
|
|
382
404
|
var adaptViemWallet = (wallet) => {
|
|
@@ -435,20 +457,35 @@ var adaptViemWallet = (wallet) => {
|
|
|
435
457
|
if (!addr) throw new Error("No address found");
|
|
436
458
|
return addr;
|
|
437
459
|
};
|
|
460
|
+
const readContract = async ({
|
|
461
|
+
address: address2,
|
|
462
|
+
abi,
|
|
463
|
+
functionName,
|
|
464
|
+
args = []
|
|
465
|
+
}) => {
|
|
466
|
+
const publicClient = wallet.extend(import_viem3.publicActions);
|
|
467
|
+
return await publicClient.readContract({
|
|
468
|
+
address: address2,
|
|
469
|
+
abi,
|
|
470
|
+
functionName,
|
|
471
|
+
args
|
|
472
|
+
});
|
|
473
|
+
};
|
|
438
474
|
return {
|
|
439
475
|
vmType: "EVM" /* EVM */,
|
|
440
|
-
transport: (0,
|
|
476
|
+
transport: (0, import_viem3.custom)(wallet.transport),
|
|
441
477
|
getChainId: async () => wallet.getChainId(),
|
|
442
478
|
address,
|
|
443
479
|
sendTransaction,
|
|
444
480
|
signTypedData,
|
|
445
|
-
switchChain
|
|
481
|
+
switchChain,
|
|
482
|
+
readContract
|
|
446
483
|
};
|
|
447
484
|
};
|
|
448
485
|
|
|
449
486
|
// src/core/executeOrder/handleEvmExecution.ts
|
|
450
|
-
var
|
|
451
|
-
var
|
|
487
|
+
var import_intents_sdk9 = require("@shogun-sdk/intents-sdk");
|
|
488
|
+
var import_viem5 = require("viem");
|
|
452
489
|
|
|
453
490
|
// src/core/executeOrder/stageMessages.ts
|
|
454
491
|
var DEFAULT_STAGE_MESSAGES = {
|
|
@@ -603,6 +640,51 @@ async function handleOrderPollingResult({
|
|
|
603
640
|
};
|
|
604
641
|
}
|
|
605
642
|
|
|
643
|
+
// src/core/executeOrder/ensurePermit2Allowance.ts
|
|
644
|
+
var import_viem4 = require("viem");
|
|
645
|
+
var import_intents_sdk8 = require("@shogun-sdk/intents-sdk");
|
|
646
|
+
async function ensurePermit2Allowance({
|
|
647
|
+
chainId,
|
|
648
|
+
tokenIn,
|
|
649
|
+
wallet,
|
|
650
|
+
accountAddress,
|
|
651
|
+
requiredAmount,
|
|
652
|
+
increaseByDelta = false
|
|
653
|
+
}) {
|
|
654
|
+
const spender = import_intents_sdk8.PERMIT2_ADDRESS[chainId];
|
|
655
|
+
let currentAllowance = 0n;
|
|
656
|
+
try {
|
|
657
|
+
if (!wallet.readContract) {
|
|
658
|
+
throw new Error("Wallet does not implement readContract()");
|
|
659
|
+
}
|
|
660
|
+
currentAllowance = await wallet.readContract({
|
|
661
|
+
address: tokenIn,
|
|
662
|
+
abi: import_viem4.erc20Abi,
|
|
663
|
+
functionName: "allowance",
|
|
664
|
+
args: [accountAddress, spender]
|
|
665
|
+
});
|
|
666
|
+
} catch (error) {
|
|
667
|
+
console.warn(`[Permit2] Failed to read allowance for ${tokenIn}`, error);
|
|
668
|
+
}
|
|
669
|
+
const approvalAmount = increaseByDelta ? currentAllowance + requiredAmount : import_viem4.maxUint256;
|
|
670
|
+
console.debug(
|
|
671
|
+
`[Permit2] Approving ${approvalAmount} for ${tokenIn} (current: ${currentAllowance}, required: ${requiredAmount})`
|
|
672
|
+
);
|
|
673
|
+
await wallet.sendTransaction({
|
|
674
|
+
to: tokenIn,
|
|
675
|
+
from: accountAddress,
|
|
676
|
+
data: (0, import_viem4.encodeFunctionData)({
|
|
677
|
+
abi: import_viem4.erc20Abi,
|
|
678
|
+
functionName: "approve",
|
|
679
|
+
args: [spender, approvalAmount]
|
|
680
|
+
}),
|
|
681
|
+
value: 0n
|
|
682
|
+
});
|
|
683
|
+
console.info(
|
|
684
|
+
`[Permit2] Approval transaction sent for ${tokenIn} on chain ${chainId}`
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
|
|
606
688
|
// src/core/executeOrder/handleEvmExecution.ts
|
|
607
689
|
async function handleEvmExecution({
|
|
608
690
|
recipientAddress,
|
|
@@ -617,12 +699,13 @@ async function handleEvmExecution({
|
|
|
617
699
|
const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage];
|
|
618
700
|
await wallet.switchChain(chainId);
|
|
619
701
|
const tokenIn = normalizeNative(chainId, quote.tokenIn.address);
|
|
702
|
+
quote.tokenOut.address = normalizeEvmTokenAddress(quote.tokenOut.address);
|
|
620
703
|
const shouldWrapNative = isNativeAddress(quote.tokenIn.address);
|
|
621
704
|
update("processing", shouldWrapNative ? `${messageFor("processing")} (wrapping native token)` : messageFor("processing"));
|
|
622
705
|
if (shouldWrapNative) {
|
|
623
706
|
await wallet.sendTransaction({
|
|
624
707
|
to: tokenIn,
|
|
625
|
-
data: (0,
|
|
708
|
+
data: (0, import_viem5.encodeFunctionData)({
|
|
626
709
|
abi: [{ type: "function", name: "deposit", stateMutability: "payable", inputs: [], outputs: [] }],
|
|
627
710
|
functionName: "deposit",
|
|
628
711
|
args: []
|
|
@@ -632,15 +715,12 @@ async function handleEvmExecution({
|
|
|
632
715
|
});
|
|
633
716
|
}
|
|
634
717
|
update("processing", messageFor("approving"));
|
|
635
|
-
await
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
}),
|
|
642
|
-
value: 0n,
|
|
643
|
-
from: accountAddress
|
|
718
|
+
await ensurePermit2Allowance({
|
|
719
|
+
chainId,
|
|
720
|
+
tokenIn,
|
|
721
|
+
wallet,
|
|
722
|
+
accountAddress,
|
|
723
|
+
requiredAmount: BigInt(quote.amountIn)
|
|
644
724
|
});
|
|
645
725
|
update("processing", messageFor("approved"));
|
|
646
726
|
const destination = recipientAddress ?? accountAddress;
|
|
@@ -653,7 +733,7 @@ async function handleEvmExecution({
|
|
|
653
733
|
});
|
|
654
734
|
console.debug(`order`, order);
|
|
655
735
|
update("processing", messageFor("signing"));
|
|
656
|
-
const { orderTypedData, nonce } = isSingleChain ? await (0,
|
|
736
|
+
const { orderTypedData, nonce } = isSingleChain ? await (0, import_intents_sdk9.getEVMSingleChainOrderTypedData)(order) : await (0, import_intents_sdk9.getEVMCrossChainOrderTypedData)(order);
|
|
657
737
|
const typedData = serializeBigIntsToStrings(orderTypedData);
|
|
658
738
|
if (!wallet.signTypedData) {
|
|
659
739
|
throw new Error("Wallet does not support EIP-712 signing");
|
|
@@ -684,7 +764,7 @@ async function handleEvmExecution({
|
|
|
684
764
|
}
|
|
685
765
|
|
|
686
766
|
// src/core/executeOrder/handleSolanaExecution.ts
|
|
687
|
-
var
|
|
767
|
+
var import_intents_sdk10 = require("@shogun-sdk/intents-sdk");
|
|
688
768
|
var import_web32 = require("@solana/web3.js");
|
|
689
769
|
async function handleSolanaExecution({
|
|
690
770
|
recipientAddress,
|
|
@@ -743,11 +823,11 @@ async function getSolanaOrderInstructions({
|
|
|
743
823
|
rpcUrl
|
|
744
824
|
}) {
|
|
745
825
|
if (isSingleChain) {
|
|
746
|
-
return await (0,
|
|
826
|
+
return await (0, import_intents_sdk10.getSolanaSingleChainOrderInstructions)(order, {
|
|
747
827
|
rpcUrl
|
|
748
828
|
});
|
|
749
829
|
}
|
|
750
|
-
return await (0,
|
|
830
|
+
return await (0, import_intents_sdk10.getSolanaCrossChainOrderInstructions)(order, {
|
|
751
831
|
rpcUrl
|
|
752
832
|
});
|
|
753
833
|
}
|
|
@@ -801,7 +881,7 @@ async function executeOrder({
|
|
|
801
881
|
const isSingleChain = tokenIn.chainId === tokenOut.chainId;
|
|
802
882
|
const chainId = Number(tokenIn.chainId);
|
|
803
883
|
update("processing");
|
|
804
|
-
if ((0,
|
|
884
|
+
if ((0, import_intents_sdk11.isEvmChain)(chainId)) {
|
|
805
885
|
log("Detected EVM chain:", chainId);
|
|
806
886
|
const result = await handleEvmExecution({
|
|
807
887
|
recipientAddress,
|
|
@@ -816,7 +896,7 @@ async function executeOrder({
|
|
|
816
896
|
log("EVM execution result:", result);
|
|
817
897
|
return result;
|
|
818
898
|
}
|
|
819
|
-
if (chainId ===
|
|
899
|
+
if (chainId === import_intents_sdk11.ChainID.Solana) {
|
|
820
900
|
log("Detected Solana chain");
|
|
821
901
|
const result = await handleSolanaExecution({
|
|
822
902
|
recipientAddress,
|
|
@@ -835,7 +915,7 @@ async function executeOrder({
|
|
|
835
915
|
log("Error:", unsupported);
|
|
836
916
|
return { status: false, message: unsupported, stage: "error" };
|
|
837
917
|
} catch (error) {
|
|
838
|
-
const message = error instanceof
|
|
918
|
+
const message = error instanceof import_viem6.BaseError ? error.shortMessage : error instanceof Error ? error.message : String(error);
|
|
839
919
|
log("Execution failed:", { message, error });
|
|
840
920
|
update("error", message);
|
|
841
921
|
return { status: false, message, stage: "error" };
|
package/dist/index.d.cts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export { NATIVE_TOKEN, SOLANA_CHAIN_ID, SupportedChains, buildQuoteParams, getBalances, getQuote, getTokenList, isNativeAddress, isViemWalletClient, serializeBigIntsToStrings } from './core.cjs';
|
|
2
|
-
export { B as BalanceRequestParams, c as BalanceResponse, P as PlaceOrderResult, Q as QuoteTokenInfo, b as Stage, S as SwapQuoteParams, a as SwapQuoteResponse, T as TokenBalance, f as TokenInfo, d as TokenSearchResponse, e as executeOrder } from './execute-
|
|
1
|
+
export { NATIVE_TOKEN, SOLANA_CHAIN_ID, SupportedChains, buildQuoteParams, getBalances, getQuote, getTokenList, isNativeAddress, isViemWalletClient, normalizeEvmTokenAddress, serializeBigIntsToStrings } from './core.cjs';
|
|
2
|
+
export { B as BalanceRequestParams, c as BalanceResponse, P as PlaceOrderResult, Q as QuoteTokenInfo, b as Stage, S as SwapQuoteParams, a as SwapQuoteResponse, T as TokenBalance, f as TokenInfo, d as TokenSearchResponse, e as executeOrder } from './execute-Xvw4wXBo.cjs';
|
|
3
3
|
export { ChainID, isEvmChain } from '@shogun-sdk/intents-sdk';
|
|
4
4
|
export { useBalances, useExecuteOrder, useQuote, useTokenList } from './react.cjs';
|
|
5
5
|
export { adaptEthersSigner, adaptSolanaWallet, adaptViemWallet } from './wallet-adapter.cjs';
|
|
6
6
|
import 'viem';
|
|
7
|
-
import './wallet-
|
|
7
|
+
import './wallet-BhuMJ3K_.cjs';
|
|
8
8
|
import '@mysten/sui/transactions';
|
|
9
9
|
import '@solana/web3.js';
|
|
10
10
|
import 'ethers/lib/ethers.js';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export { NATIVE_TOKEN, SOLANA_CHAIN_ID, SupportedChains, buildQuoteParams, getBalances, getQuote, getTokenList, isNativeAddress, isViemWalletClient, serializeBigIntsToStrings } from './core.js';
|
|
2
|
-
export { B as BalanceRequestParams, c as BalanceResponse, P as PlaceOrderResult, Q as QuoteTokenInfo, b as Stage, S as SwapQuoteParams, a as SwapQuoteResponse, T as TokenBalance, f as TokenInfo, d as TokenSearchResponse, e as executeOrder } from './execute-
|
|
1
|
+
export { NATIVE_TOKEN, SOLANA_CHAIN_ID, SupportedChains, buildQuoteParams, getBalances, getQuote, getTokenList, isNativeAddress, isViemWalletClient, normalizeEvmTokenAddress, serializeBigIntsToStrings } from './core.js';
|
|
2
|
+
export { B as BalanceRequestParams, c as BalanceResponse, P as PlaceOrderResult, Q as QuoteTokenInfo, b as Stage, S as SwapQuoteParams, a as SwapQuoteResponse, T as TokenBalance, f as TokenInfo, d as TokenSearchResponse, e as executeOrder } from './execute-D2qcOzkI.js';
|
|
3
3
|
export { ChainID, isEvmChain } from '@shogun-sdk/intents-sdk';
|
|
4
4
|
export { useBalances, useExecuteOrder, useQuote, useTokenList } from './react.js';
|
|
5
5
|
export { adaptEthersSigner, adaptSolanaWallet, adaptViemWallet } from './wallet-adapter.js';
|
|
6
6
|
import 'viem';
|
|
7
|
-
import './wallet-
|
|
7
|
+
import './wallet-BhuMJ3K_.js';
|
|
8
8
|
import '@mysten/sui/transactions';
|
|
9
9
|
import '@solana/web3.js';
|
|
10
10
|
import 'ethers/lib/ethers.js';
|
package/dist/index.js
CHANGED
|
@@ -12,6 +12,7 @@ import { parseUnits } from "viem";
|
|
|
12
12
|
import { isEvmChain } from "@shogun-sdk/intents-sdk";
|
|
13
13
|
|
|
14
14
|
// src/utils/address.ts
|
|
15
|
+
import { zeroAddress } from "viem";
|
|
15
16
|
var NATIVE_TOKEN = {
|
|
16
17
|
ETH: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
|
|
17
18
|
SOL: "So11111111111111111111111111111111111111111",
|
|
@@ -22,6 +23,10 @@ var isNativeAddress = (tokenAddress) => {
|
|
|
22
23
|
const normalizedTokenAddress = tokenAddress.toLowerCase();
|
|
23
24
|
return !!tokenAddress && NATIVE_ADDRESSES.includes(normalizedTokenAddress);
|
|
24
25
|
};
|
|
26
|
+
function normalizeEvmTokenAddress(address) {
|
|
27
|
+
const lower = address.toLowerCase();
|
|
28
|
+
return lower === NATIVE_TOKEN.ETH.toLowerCase() ? zeroAddress : address;
|
|
29
|
+
}
|
|
25
30
|
|
|
26
31
|
// src/utils/chain.ts
|
|
27
32
|
import { ChainID } from "@shogun-sdk/intents-sdk";
|
|
@@ -290,10 +295,11 @@ var adaptSolanaWallet = (walletAddress, chainId, rpcUrl, signAndSendTransaction)
|
|
|
290
295
|
};
|
|
291
296
|
|
|
292
297
|
// src/wallet-adapter/evm-wallet-adapter/adapter.ts
|
|
293
|
-
import "ethers/lib/ethers.js";
|
|
298
|
+
import { utils as ethersUtils } from "ethers/lib/ethers.js";
|
|
294
299
|
import { hexValue } from "ethers/lib/utils.js";
|
|
295
300
|
import {
|
|
296
|
-
custom
|
|
301
|
+
custom,
|
|
302
|
+
publicActions
|
|
297
303
|
} from "viem";
|
|
298
304
|
function isEVMTransaction(tx) {
|
|
299
305
|
return typeof tx.from === "string";
|
|
@@ -330,6 +336,21 @@ var adaptEthersSigner = (signer, transport) => {
|
|
|
330
336
|
throw error;
|
|
331
337
|
}
|
|
332
338
|
};
|
|
339
|
+
const readContract = async ({
|
|
340
|
+
address,
|
|
341
|
+
abi,
|
|
342
|
+
functionName,
|
|
343
|
+
args = []
|
|
344
|
+
}) => {
|
|
345
|
+
const iface = new ethersUtils.Interface(abi);
|
|
346
|
+
const fnArgs = Array.isArray(args) ? args : [];
|
|
347
|
+
const data = iface.encodeFunctionData(functionName, fnArgs);
|
|
348
|
+
const provider = signer.provider;
|
|
349
|
+
if (!provider) throw new Error("Signer has no provider");
|
|
350
|
+
const result = await provider.call({ to: address, data });
|
|
351
|
+
const decoded = iface.decodeFunctionResult(functionName, result);
|
|
352
|
+
return decoded[0];
|
|
353
|
+
};
|
|
333
354
|
return {
|
|
334
355
|
vmType: "EVM" /* EVM */,
|
|
335
356
|
transport,
|
|
@@ -337,7 +358,8 @@ var adaptEthersSigner = (signer, transport) => {
|
|
|
337
358
|
address: async () => signer.getAddress(),
|
|
338
359
|
sendTransaction,
|
|
339
360
|
signTypedData,
|
|
340
|
-
switchChain
|
|
361
|
+
switchChain,
|
|
362
|
+
readContract
|
|
341
363
|
};
|
|
342
364
|
};
|
|
343
365
|
var adaptViemWallet = (wallet) => {
|
|
@@ -396,6 +418,20 @@ var adaptViemWallet = (wallet) => {
|
|
|
396
418
|
if (!addr) throw new Error("No address found");
|
|
397
419
|
return addr;
|
|
398
420
|
};
|
|
421
|
+
const readContract = async ({
|
|
422
|
+
address: address2,
|
|
423
|
+
abi,
|
|
424
|
+
functionName,
|
|
425
|
+
args = []
|
|
426
|
+
}) => {
|
|
427
|
+
const publicClient = wallet.extend(publicActions);
|
|
428
|
+
return await publicClient.readContract({
|
|
429
|
+
address: address2,
|
|
430
|
+
abi,
|
|
431
|
+
functionName,
|
|
432
|
+
args
|
|
433
|
+
});
|
|
434
|
+
};
|
|
399
435
|
return {
|
|
400
436
|
vmType: "EVM" /* EVM */,
|
|
401
437
|
transport: custom(wallet.transport),
|
|
@@ -403,17 +439,17 @@ var adaptViemWallet = (wallet) => {
|
|
|
403
439
|
address,
|
|
404
440
|
sendTransaction,
|
|
405
441
|
signTypedData,
|
|
406
|
-
switchChain
|
|
442
|
+
switchChain,
|
|
443
|
+
readContract
|
|
407
444
|
};
|
|
408
445
|
};
|
|
409
446
|
|
|
410
447
|
// src/core/executeOrder/handleEvmExecution.ts
|
|
411
448
|
import {
|
|
412
449
|
getEVMSingleChainOrderTypedData,
|
|
413
|
-
getEVMCrossChainOrderTypedData
|
|
414
|
-
PERMIT2_ADDRESS
|
|
450
|
+
getEVMCrossChainOrderTypedData
|
|
415
451
|
} from "@shogun-sdk/intents-sdk";
|
|
416
|
-
import { encodeFunctionData
|
|
452
|
+
import { encodeFunctionData as encodeFunctionData2 } from "viem";
|
|
417
453
|
|
|
418
454
|
// src/core/executeOrder/stageMessages.ts
|
|
419
455
|
var DEFAULT_STAGE_MESSAGES = {
|
|
@@ -568,6 +604,51 @@ async function handleOrderPollingResult({
|
|
|
568
604
|
};
|
|
569
605
|
}
|
|
570
606
|
|
|
607
|
+
// src/core/executeOrder/ensurePermit2Allowance.ts
|
|
608
|
+
import { encodeFunctionData, erc20Abi, maxUint256 } from "viem";
|
|
609
|
+
import { PERMIT2_ADDRESS } from "@shogun-sdk/intents-sdk";
|
|
610
|
+
async function ensurePermit2Allowance({
|
|
611
|
+
chainId,
|
|
612
|
+
tokenIn,
|
|
613
|
+
wallet,
|
|
614
|
+
accountAddress,
|
|
615
|
+
requiredAmount,
|
|
616
|
+
increaseByDelta = false
|
|
617
|
+
}) {
|
|
618
|
+
const spender = PERMIT2_ADDRESS[chainId];
|
|
619
|
+
let currentAllowance = 0n;
|
|
620
|
+
try {
|
|
621
|
+
if (!wallet.readContract) {
|
|
622
|
+
throw new Error("Wallet does not implement readContract()");
|
|
623
|
+
}
|
|
624
|
+
currentAllowance = await wallet.readContract({
|
|
625
|
+
address: tokenIn,
|
|
626
|
+
abi: erc20Abi,
|
|
627
|
+
functionName: "allowance",
|
|
628
|
+
args: [accountAddress, spender]
|
|
629
|
+
});
|
|
630
|
+
} catch (error) {
|
|
631
|
+
console.warn(`[Permit2] Failed to read allowance for ${tokenIn}`, error);
|
|
632
|
+
}
|
|
633
|
+
const approvalAmount = increaseByDelta ? currentAllowance + requiredAmount : maxUint256;
|
|
634
|
+
console.debug(
|
|
635
|
+
`[Permit2] Approving ${approvalAmount} for ${tokenIn} (current: ${currentAllowance}, required: ${requiredAmount})`
|
|
636
|
+
);
|
|
637
|
+
await wallet.sendTransaction({
|
|
638
|
+
to: tokenIn,
|
|
639
|
+
from: accountAddress,
|
|
640
|
+
data: encodeFunctionData({
|
|
641
|
+
abi: erc20Abi,
|
|
642
|
+
functionName: "approve",
|
|
643
|
+
args: [spender, approvalAmount]
|
|
644
|
+
}),
|
|
645
|
+
value: 0n
|
|
646
|
+
});
|
|
647
|
+
console.info(
|
|
648
|
+
`[Permit2] Approval transaction sent for ${tokenIn} on chain ${chainId}`
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
|
|
571
652
|
// src/core/executeOrder/handleEvmExecution.ts
|
|
572
653
|
async function handleEvmExecution({
|
|
573
654
|
recipientAddress,
|
|
@@ -582,12 +663,13 @@ async function handleEvmExecution({
|
|
|
582
663
|
const messageFor = (stage) => DEFAULT_STAGE_MESSAGES[stage];
|
|
583
664
|
await wallet.switchChain(chainId);
|
|
584
665
|
const tokenIn = normalizeNative(chainId, quote.tokenIn.address);
|
|
666
|
+
quote.tokenOut.address = normalizeEvmTokenAddress(quote.tokenOut.address);
|
|
585
667
|
const shouldWrapNative = isNativeAddress(quote.tokenIn.address);
|
|
586
668
|
update("processing", shouldWrapNative ? `${messageFor("processing")} (wrapping native token)` : messageFor("processing"));
|
|
587
669
|
if (shouldWrapNative) {
|
|
588
670
|
await wallet.sendTransaction({
|
|
589
671
|
to: tokenIn,
|
|
590
|
-
data:
|
|
672
|
+
data: encodeFunctionData2({
|
|
591
673
|
abi: [{ type: "function", name: "deposit", stateMutability: "payable", inputs: [], outputs: [] }],
|
|
592
674
|
functionName: "deposit",
|
|
593
675
|
args: []
|
|
@@ -597,15 +679,12 @@ async function handleEvmExecution({
|
|
|
597
679
|
});
|
|
598
680
|
}
|
|
599
681
|
update("processing", messageFor("approving"));
|
|
600
|
-
await
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
}),
|
|
607
|
-
value: 0n,
|
|
608
|
-
from: accountAddress
|
|
682
|
+
await ensurePermit2Allowance({
|
|
683
|
+
chainId,
|
|
684
|
+
tokenIn,
|
|
685
|
+
wallet,
|
|
686
|
+
accountAddress,
|
|
687
|
+
requiredAmount: BigInt(quote.amountIn)
|
|
609
688
|
});
|
|
610
689
|
update("processing", messageFor("approved"));
|
|
611
690
|
const destination = recipientAddress ?? accountAddress;
|
|
@@ -1125,6 +1204,7 @@ export {
|
|
|
1125
1204
|
getTokenList,
|
|
1126
1205
|
isNativeAddress,
|
|
1127
1206
|
isViemWalletClient,
|
|
1207
|
+
normalizeEvmTokenAddress,
|
|
1128
1208
|
serializeBigIntsToStrings,
|
|
1129
1209
|
useBalances,
|
|
1130
1210
|
useExecuteOrder,
|