@aurora-is-near/intents-swap-widget-evm 7.1.0
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/index.d.ts +3 -0
- package/dist/index.js +75 -0
- package/dist/index.js.map +1 -0
- package/dist/makeTransfer.d.ts +3 -0
- package/dist/plugin.d.ts +2 -0
- package/dist/types.d.ts +5 -0
- package/package.json +38 -0
- package/src/index.ts +3 -0
- package/src/makeTransfer.ts +142 -0
- package/src/plugin.ts +7 -0
- package/src/types.ts +6 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { createWalletClient as l, custom as w, encodeFunctionData as f, erc20Abi as m } from "viem";
|
|
2
|
+
import * as E from "viem/chains";
|
|
3
|
+
const d = (t) => /^0x[a-fA-F0-9]{40}$/.test(t), h = (t) => Object.values(E).find(
|
|
4
|
+
(e) => !!e && typeof e == "object" && "id" in e && typeof e.id == "number" && e.id === t
|
|
5
|
+
), I = (t, e) => {
|
|
6
|
+
const o = h(t)?.blockExplorers?.default?.url;
|
|
7
|
+
return o ? `${o}/tx/${e}` : "";
|
|
8
|
+
}, v = async (t, e) => {
|
|
9
|
+
const r = await e.request({ method: "eth_chainId" });
|
|
10
|
+
if (parseInt(r, 16) !== t)
|
|
11
|
+
try {
|
|
12
|
+
await e.request({
|
|
13
|
+
method: "wallet_switchEthereumChain",
|
|
14
|
+
params: [{ chainId: `0x${t.toString(16)}` }]
|
|
15
|
+
});
|
|
16
|
+
} catch (n) {
|
|
17
|
+
throw n && typeof n == "object" && "code" in n && n.code === 4902 ? new Error(`Chain ${t} is not available.`) : new Error(
|
|
18
|
+
`Please switch to the correct network (Chain ID: ${t}) in your wallet`
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
}, C = async (t, {
|
|
22
|
+
provider: e,
|
|
23
|
+
getTransactionLink: r = I
|
|
24
|
+
}) => {
|
|
25
|
+
const o = typeof e == "function" ? await e() : e;
|
|
26
|
+
if (!d(t.address))
|
|
27
|
+
throw new Error(`Invalid EVM address: ${t.address}`);
|
|
28
|
+
if (!t.evmChainId)
|
|
29
|
+
throw new Error("EVM chain ID is required for EVM transfers.");
|
|
30
|
+
if (!o)
|
|
31
|
+
throw new Error("No injected Ethereum wallet found.");
|
|
32
|
+
await v(t.evmChainId, o);
|
|
33
|
+
const n = l({
|
|
34
|
+
transport: w(o)
|
|
35
|
+
});
|
|
36
|
+
let [a] = await n.getAddresses();
|
|
37
|
+
if (a || ([a] = await n.requestAddresses()), !a)
|
|
38
|
+
throw new Error("No EVM account found in the injected wallet.");
|
|
39
|
+
const i = h(t.evmChainId) ?? null;
|
|
40
|
+
if (t.isNativeEvmTokenTransfer) {
|
|
41
|
+
const c = await n.sendTransaction({
|
|
42
|
+
account: a,
|
|
43
|
+
to: t.address,
|
|
44
|
+
value: BigInt(t.amount),
|
|
45
|
+
chain: i
|
|
46
|
+
});
|
|
47
|
+
return {
|
|
48
|
+
hash: c,
|
|
49
|
+
transactionLink: r(t.evmChainId, c)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (!t.tokenAddress || !d(t.tokenAddress))
|
|
53
|
+
throw new Error(`Invalid EVM token address: ${t.tokenAddress}`);
|
|
54
|
+
const u = f({
|
|
55
|
+
abi: m,
|
|
56
|
+
functionName: "transfer",
|
|
57
|
+
args: [t.address, BigInt(t.amount)]
|
|
58
|
+
}), s = await n.sendTransaction({
|
|
59
|
+
account: a,
|
|
60
|
+
to: t.tokenAddress,
|
|
61
|
+
data: u,
|
|
62
|
+
chain: i
|
|
63
|
+
});
|
|
64
|
+
return {
|
|
65
|
+
hash: s,
|
|
66
|
+
transactionLink: r(t.evmChainId, s)
|
|
67
|
+
};
|
|
68
|
+
}, p = {
|
|
69
|
+
makeTransfer: C
|
|
70
|
+
};
|
|
71
|
+
export {
|
|
72
|
+
p as evm,
|
|
73
|
+
C as makeTransfer
|
|
74
|
+
};
|
|
75
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/makeTransfer.ts","../src/plugin.ts"],"sourcesContent":["import {\n type Chain,\n createWalletClient,\n custom,\n encodeFunctionData,\n erc20Abi,\n} from 'viem';\nimport * as viemChains from 'viem/chains';\nimport type { Eip1193Provider } from 'ethers';\nimport type {\n MakeTransferArgs,\n TransferResult,\n} from '@aurora-is-near/intents-swap-widget';\n\nimport type { MakeTransferOptions } from './types';\n\nconst isEvmAddress = (a: string): boolean => /^0x[a-fA-F0-9]{40}$/.test(a);\n\nconst findViemChain = (id: number): Chain | undefined =>\n (Object.values(viemChains) as unknown[]).find(\n (c): c is Chain =>\n !!c &&\n typeof c === 'object' &&\n 'id' in c &&\n typeof (c as { id: unknown }).id === 'number' &&\n (c as Chain).id === id,\n );\n\nconst defaultTransactionLink = (chainId: number, hash: string): string => {\n const chain = findViemChain(chainId);\n const explorerUrl = chain?.blockExplorers?.default?.url;\n\n return explorerUrl ? `${explorerUrl}/tx/${hash}` : '';\n};\n\nconst switchEthereumChain = async (\n targetChainId: number,\n provider: Eip1193Provider,\n): Promise<void> => {\n const currentChainIdHex = await provider.request({ method: 'eth_chainId' });\n const currentChainId = parseInt(currentChainIdHex as string, 16);\n\n if (currentChainId === targetChainId) {\n return;\n }\n\n try {\n await provider.request({\n method: 'wallet_switchEthereumChain',\n params: [{ chainId: `0x${targetChainId.toString(16)}` }],\n });\n } catch (error: unknown) {\n if (\n error &&\n typeof error === 'object' &&\n 'code' in error &&\n (error as { code: unknown }).code === 4902\n ) {\n throw new Error(`Chain ${targetChainId} is not available.`);\n }\n\n throw new Error(\n `Please switch to the correct network (Chain ID: ${targetChainId}) in your wallet`,\n );\n }\n};\n\nexport const makeTransfer = async (\n args: MakeTransferArgs,\n {\n provider,\n getTransactionLink = defaultTransactionLink,\n }: MakeTransferOptions,\n): Promise<TransferResult> => {\n const resolved = typeof provider === 'function' ? await provider() : provider;\n\n if (!isEvmAddress(args.address)) {\n throw new Error(`Invalid EVM address: ${args.address}`);\n }\n\n if (!args.evmChainId) {\n throw new Error('EVM chain ID is required for EVM transfers.');\n }\n\n if (!resolved) {\n throw new Error('No injected Ethereum wallet found.');\n }\n\n await switchEthereumChain(args.evmChainId, resolved);\n\n const walletClient = createWalletClient({\n transport: custom(resolved as Parameters<typeof custom>[0]),\n });\n\n let [from] = await walletClient.getAddresses();\n\n if (!from) {\n [from] = await walletClient.requestAddresses();\n }\n\n if (!from) {\n throw new Error('No EVM account found in the injected wallet.');\n }\n\n const chain = findViemChain(args.evmChainId) ?? null;\n\n if (args.isNativeEvmTokenTransfer) {\n const hash = await walletClient.sendTransaction({\n account: from,\n to: args.address as `0x${string}`,\n value: BigInt(args.amount),\n chain,\n });\n\n return {\n hash,\n transactionLink: getTransactionLink(args.evmChainId, hash),\n };\n }\n\n if (!args.tokenAddress || !isEvmAddress(args.tokenAddress)) {\n throw new Error(`Invalid EVM token address: ${args.tokenAddress}`);\n }\n\n const data = encodeFunctionData({\n abi: erc20Abi,\n functionName: 'transfer',\n args: [args.address as `0x${string}`, BigInt(args.amount)],\n });\n\n const hash = await walletClient.sendTransaction({\n account: from,\n to: args.tokenAddress as `0x${string}`,\n data,\n chain,\n });\n\n return {\n hash,\n transactionLink: getTransactionLink(args.evmChainId, hash),\n };\n};\n","import type { EvmNetworkPlugin } from '@aurora-is-near/intents-swap-widget';\n\nimport { makeTransfer } from './makeTransfer';\n\nexport const evm: EvmNetworkPlugin = {\n makeTransfer,\n};\n"],"names":["isEvmAddress","a","findViemChain","id","viemChains","c","defaultTransactionLink","chainId","hash","explorerUrl","switchEthereumChain","targetChainId","provider","currentChainIdHex","error","makeTransfer","args","getTransactionLink","resolved","walletClient","createWalletClient","custom","from","chain","data","encodeFunctionData","erc20Abi","evm"],"mappings":";;AAgBA,MAAMA,IAAe,CAACC,MAAuB,sBAAsB,KAAKA,CAAC,GAEnEC,IAAgB,CAACC,MACpB,OAAO,OA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AED,MAAI,CAACI,CAAI,IAAI,MAAMH,EAAa,aAAA;AAMhC,MAJKG,MACH,CAACA,CAAI,IAAI,MAAMH,EAAa,iBAAA,IAG1B,CAACG;AACH,UAAM,IAAI,MAAM,8CAA8C;AAGhE,QAAMC,IAAQrB,EAAcc,EAAK,UAAU,KAAK;AAEhD,MAAIA,EAAK,0BAA0B;AACjC,UAAMR,IAAO,MAAMW,EAAa,gBAAgB;AAAA,MAC9C,SAASG;AAAA,MACT,IAAIN,EAAK;AAAA,MACT,OAAO,OAAOA,EAAK,MAAM;AAAA,MACzB,OAAAO;AAAA,IAAA,CACD;AAED,WAAO;AAAA,MACL,MAAAf;AAAAA,MACA,iBAAiBS,EAAmBD,EAAK,YAAYR,CAAI;AAAA,IAAA;AAAA,EAE7D;AAEA,MAAI,CAACQ,EAAK,gBAAgB,CAAChB,EAAagB,EAAK,YAAY;AACvD,UAAM,IAAI,MAAM,8BAA8BA,EAAK,YAAY,EAAE;AAGnE,QAAMQ,IAAOC,EAAmB;AAAA,IAC9B,KAAKC;AAAA,IACL,cAAc;AAAA,IACd,MAAM,CAACV,EAAK,SAA0B,OAAOA,EAAK,MAAM,CAAC;AAAA,EAAA,CAC1D,GAEKR,IAAO,MAAMW,EAAa,gBAAgB;AAAA,IAC9C,SAASG;AAAA,IACT,IAAIN,EAAK;AAAA,IACT,MAAAQ;AAAA,IACA,OAAAD;AAAA,EAAA,CACD;AAED,SAAO;AAAA,IACL,MAAAf;AAAA,IACA,iBAAiBS,EAAmBD,EAAK,YAAYR,CAAI;AAAA,EAAA;AAE7D,GCzIamB,IAAwB;AAAA,EACnC,cAAAZ;AACF;"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { MakeTransferArgs, TransferResult } from '@aurora-is-near/intents-swap-widget';
|
|
2
|
+
import { MakeTransferOptions } from './types';
|
|
3
|
+
export declare const makeTransfer: (args: MakeTransferArgs, { provider, getTransactionLink, }: MakeTransferOptions) => Promise<TransferResult>;
|
package/dist/plugin.d.ts
ADDED
package/dist/types.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aurora-is-near/intents-swap-widget-evm",
|
|
3
|
+
"version": "7.1.0",
|
|
4
|
+
"description": "EVM connector for the Intents swap widget",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"typecheck": "tsc --noEmit",
|
|
12
|
+
"lint": "eslint . --ext .js,.ts,.tsx",
|
|
13
|
+
"clean": "rimraf dist",
|
|
14
|
+
"prebuild": "yarn clean",
|
|
15
|
+
"build": "vite build"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public",
|
|
19
|
+
"registry": "https://registry.npmjs.org/"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src",
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@aurora-is-near/intents-swap-widget": "^7.1.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@aurora-is-near/intents-swap-widget": "^7.1.0"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"ethers": "^6.15.0",
|
|
36
|
+
"viem": "^2.38.3"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Chain,
|
|
3
|
+
createWalletClient,
|
|
4
|
+
custom,
|
|
5
|
+
encodeFunctionData,
|
|
6
|
+
erc20Abi,
|
|
7
|
+
} from 'viem';
|
|
8
|
+
import * as viemChains from 'viem/chains';
|
|
9
|
+
import type { Eip1193Provider } from 'ethers';
|
|
10
|
+
import type {
|
|
11
|
+
MakeTransferArgs,
|
|
12
|
+
TransferResult,
|
|
13
|
+
} from '@aurora-is-near/intents-swap-widget';
|
|
14
|
+
|
|
15
|
+
import type { MakeTransferOptions } from './types';
|
|
16
|
+
|
|
17
|
+
const isEvmAddress = (a: string): boolean => /^0x[a-fA-F0-9]{40}$/.test(a);
|
|
18
|
+
|
|
19
|
+
const findViemChain = (id: number): Chain | undefined =>
|
|
20
|
+
(Object.values(viemChains) as unknown[]).find(
|
|
21
|
+
(c): c is Chain =>
|
|
22
|
+
!!c &&
|
|
23
|
+
typeof c === 'object' &&
|
|
24
|
+
'id' in c &&
|
|
25
|
+
typeof (c as { id: unknown }).id === 'number' &&
|
|
26
|
+
(c as Chain).id === id,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const defaultTransactionLink = (chainId: number, hash: string): string => {
|
|
30
|
+
const chain = findViemChain(chainId);
|
|
31
|
+
const explorerUrl = chain?.blockExplorers?.default?.url;
|
|
32
|
+
|
|
33
|
+
return explorerUrl ? `${explorerUrl}/tx/${hash}` : '';
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const switchEthereumChain = async (
|
|
37
|
+
targetChainId: number,
|
|
38
|
+
provider: Eip1193Provider,
|
|
39
|
+
): Promise<void> => {
|
|
40
|
+
const currentChainIdHex = await provider.request({ method: 'eth_chainId' });
|
|
41
|
+
const currentChainId = parseInt(currentChainIdHex as string, 16);
|
|
42
|
+
|
|
43
|
+
if (currentChainId === targetChainId) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
await provider.request({
|
|
49
|
+
method: 'wallet_switchEthereumChain',
|
|
50
|
+
params: [{ chainId: `0x${targetChainId.toString(16)}` }],
|
|
51
|
+
});
|
|
52
|
+
} catch (error: unknown) {
|
|
53
|
+
if (
|
|
54
|
+
error &&
|
|
55
|
+
typeof error === 'object' &&
|
|
56
|
+
'code' in error &&
|
|
57
|
+
(error as { code: unknown }).code === 4902
|
|
58
|
+
) {
|
|
59
|
+
throw new Error(`Chain ${targetChainId} is not available.`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
throw new Error(
|
|
63
|
+
`Please switch to the correct network (Chain ID: ${targetChainId}) in your wallet`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const makeTransfer = async (
|
|
69
|
+
args: MakeTransferArgs,
|
|
70
|
+
{
|
|
71
|
+
provider,
|
|
72
|
+
getTransactionLink = defaultTransactionLink,
|
|
73
|
+
}: MakeTransferOptions,
|
|
74
|
+
): Promise<TransferResult> => {
|
|
75
|
+
const resolved = typeof provider === 'function' ? await provider() : provider;
|
|
76
|
+
|
|
77
|
+
if (!isEvmAddress(args.address)) {
|
|
78
|
+
throw new Error(`Invalid EVM address: ${args.address}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!args.evmChainId) {
|
|
82
|
+
throw new Error('EVM chain ID is required for EVM transfers.');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!resolved) {
|
|
86
|
+
throw new Error('No injected Ethereum wallet found.');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
await switchEthereumChain(args.evmChainId, resolved);
|
|
90
|
+
|
|
91
|
+
const walletClient = createWalletClient({
|
|
92
|
+
transport: custom(resolved as Parameters<typeof custom>[0]),
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
let [from] = await walletClient.getAddresses();
|
|
96
|
+
|
|
97
|
+
if (!from) {
|
|
98
|
+
[from] = await walletClient.requestAddresses();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!from) {
|
|
102
|
+
throw new Error('No EVM account found in the injected wallet.');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const chain = findViemChain(args.evmChainId) ?? null;
|
|
106
|
+
|
|
107
|
+
if (args.isNativeEvmTokenTransfer) {
|
|
108
|
+
const hash = await walletClient.sendTransaction({
|
|
109
|
+
account: from,
|
|
110
|
+
to: args.address as `0x${string}`,
|
|
111
|
+
value: BigInt(args.amount),
|
|
112
|
+
chain,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
hash,
|
|
117
|
+
transactionLink: getTransactionLink(args.evmChainId, hash),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!args.tokenAddress || !isEvmAddress(args.tokenAddress)) {
|
|
122
|
+
throw new Error(`Invalid EVM token address: ${args.tokenAddress}`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const data = encodeFunctionData({
|
|
126
|
+
abi: erc20Abi,
|
|
127
|
+
functionName: 'transfer',
|
|
128
|
+
args: [args.address as `0x${string}`, BigInt(args.amount)],
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const hash = await walletClient.sendTransaction({
|
|
132
|
+
account: from,
|
|
133
|
+
to: args.tokenAddress as `0x${string}`,
|
|
134
|
+
data,
|
|
135
|
+
chain,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
hash,
|
|
140
|
+
transactionLink: getTransactionLink(args.evmChainId, hash),
|
|
141
|
+
};
|
|
142
|
+
};
|
package/src/plugin.ts
ADDED