four-flap-meme-sdk 1.3.21 → 1.3.23

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.
@@ -7,8 +7,9 @@ import { getErc20DecimalsMerkle as _getErc20DecimalsMerkle, generateHopWallets a
7
7
  // BSC 链常量
8
8
  const BSC_PANCAKE_V2_ROUTER = '0x10ED43C718714eb63d5aA57B78B54704E256024E';
9
9
  const BSC_WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
10
- // Monad 链常量(如果有 DEX)
11
- const MONAD_WMON = '0x760AfE86e5de5fa0Ee542fc7B7B713e1c5425701';
10
+ // Monad 链常量
11
+ const MONAD_PANCAKE_V2_ROUTER = '0xb1bc24c34e88f7d43d5923034e3a14b24daacff9';
12
+ const MONAD_WMON = '0x3bd359c1119da7da1d913d1c4d2b7c461115433a';
12
13
  const ROUTER_ABI = [
13
14
  'function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)'
14
15
  ];
@@ -30,6 +31,12 @@ async function getTokenToNativeQuote(provider, tokenAddress, tokenAmount, chainI
30
31
  const amounts = await router.getAmountsOut(tokenAmount, [tokenAddress, BSC_WBNB]);
31
32
  return amounts[1];
32
33
  }
34
+ if (chainId === 143) {
35
+ // ✅ Monad: 使用 PancakeSwap V2 Router
36
+ const router = new Contract(MONAD_PANCAKE_V2_ROUTER, ROUTER_ABI, provider);
37
+ const amounts = await router.getAmountsOut(tokenAmount, [tokenAddress, MONAD_WMON]);
38
+ return amounts[1];
39
+ }
33
40
  // 其他链暂不支持报价,返回 0
34
41
  return 0n;
35
42
  }
@@ -11,6 +11,9 @@ const MULTICALL3_ABI = [
11
11
  // BSC 链常量
12
12
  const BSC_PANCAKE_V2_ROUTER = '0x10ED43C718714eb63d5aA57B78B54704E256024E';
13
13
  const BSC_WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
14
+ // ✅ Monad 链常量
15
+ const MONAD_PANCAKE_V2_ROUTER = '0xb1bc24c34e88f7d43d5923034e3a14b24daacff9';
16
+ const MONAD_WMON = '0x3bd359c1119da7da1d913d1c4d2b7c461115433a';
14
17
  const ROUTER_ABI = [
15
18
  'function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)'
16
19
  ];
@@ -19,7 +22,7 @@ const ROUTER_ABI = [
19
22
  * @param provider - Provider 实例
20
23
  * @param tokenAddress - ERC20 代币地址
21
24
  * @param tokenAmount - 代币数量(wei)
22
- * @param chainId - 链 ID(56=BSC)
25
+ * @param chainId - 链 ID(56=BSC, 143=Monad
23
26
  * @returns 等值的原生代币数量(wei),失败返回 0n
24
27
  */
25
28
  async function getTokenToNativeQuote(provider, tokenAddress, tokenAmount, chainId) {
@@ -32,6 +35,12 @@ async function getTokenToNativeQuote(provider, tokenAddress, tokenAmount, chainI
32
35
  const amounts = await router.getAmountsOut(tokenAmount, [tokenAddress, BSC_WBNB]);
33
36
  return amounts[1];
34
37
  }
38
+ if (chainId === 143) {
39
+ // ✅ Monad: 使用 PancakeSwap V2 Router
40
+ const router = new Contract(MONAD_PANCAKE_V2_ROUTER, ROUTER_ABI, provider);
41
+ const amounts = await router.getAmountsOut(tokenAmount, [tokenAddress, MONAD_WMON]);
42
+ return amounts[1];
43
+ }
35
44
  // 其他链暂不支持报价,返回 0
36
45
  return 0n;
37
46
  }
@@ -30,6 +30,9 @@ const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
30
30
  // BSC 链常量
31
31
  const BSC_PANCAKE_V2_ROUTER = '0x10ED43C718714eb63d5aA57B78B54704E256024E';
32
32
  const BSC_WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
33
+ // ✅ Monad 链常量
34
+ const MONAD_PANCAKE_V2_ROUTER = '0xb1bc24c34e88f7d43d5923034e3a14b24daacff9';
35
+ const MONAD_WMON = '0x3bd359c1119da7da1d913d1c4d2b7c461115433a';
33
36
  const ROUTER_ABI = [
34
37
  'function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)'
35
38
  ];
@@ -38,7 +41,7 @@ const ROUTER_ABI = [
38
41
  * @param provider - Provider 实例
39
42
  * @param tokenAddress - ERC20 代币地址
40
43
  * @param tokenAmount - 代币数量(wei)
41
- * @param chainId - 链 ID(56=BSC)
44
+ * @param chainId - 链 ID(56=BSC, 143=Monad
42
45
  * @returns 等值的原生代币数量(wei),失败返回 0n
43
46
  */
44
47
  async function getTokenToNativeQuote(provider, tokenAddress, tokenAmount, chainId) {
@@ -51,6 +54,12 @@ async function getTokenToNativeQuote(provider, tokenAddress, tokenAmount, chainI
51
54
  const amounts = await router.getAmountsOut(tokenAmount, [tokenAddress, BSC_WBNB]);
52
55
  return amounts[1];
53
56
  }
57
+ if (chainId === 143) {
58
+ // ✅ Monad: 使用 PancakeSwap V2 Router
59
+ const router = new Contract(MONAD_PANCAKE_V2_ROUTER, ROUTER_ABI, provider);
60
+ const amounts = await router.getAmountsOut(tokenAmount, [tokenAddress, MONAD_WMON]);
61
+ return amounts[1];
62
+ }
54
63
  // 其他链暂不支持报价,返回 0
55
64
  return 0n;
56
65
  }
@@ -36,6 +36,9 @@ const APPROVE_INTERFACE = new ethers.Interface(['function approve(address,uint25
36
36
  // BSC 链常量
37
37
  const BSC_PANCAKE_V2_ROUTER = '0x10ED43C718714eb63d5aA57B78B54704E256024E';
38
38
  const BSC_WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
39
+ // ✅ Monad 链常量
40
+ const MONAD_PANCAKE_V2_ROUTER = '0xb1bc24c34e88f7d43d5923034e3a14b24daacff9';
41
+ const MONAD_WMON = '0x3bd359c1119da7da1d913d1c4d2b7c461115433a';
39
42
  const ROUTER_ABI = [
40
43
  'function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)'
41
44
  ];
@@ -44,7 +47,7 @@ const ROUTER_ABI = [
44
47
  * @param provider - Provider 实例
45
48
  * @param tokenAddress - ERC20 代币地址
46
49
  * @param tokenAmount - 代币数量(wei)
47
- * @param chainId - 链 ID(56=BSC)
50
+ * @param chainId - 链 ID(56=BSC, 143=Monad
48
51
  * @returns 等值的原生代币数量(wei),失败返回 0n
49
52
  */
50
53
  async function getTokenToNativeQuote(provider, tokenAddress, tokenAmount, chainId) {
@@ -57,6 +60,12 @@ async function getTokenToNativeQuote(provider, tokenAddress, tokenAmount, chainI
57
60
  const amounts = await router.getAmountsOut(tokenAmount, [tokenAddress, BSC_WBNB]);
58
61
  return amounts[1];
59
62
  }
63
+ if (chainId === 143) {
64
+ // ✅ Monad: 使用 PancakeSwap V2 Router
65
+ const router = new Contract(MONAD_PANCAKE_V2_ROUTER, ROUTER_ABI, provider);
66
+ const amounts = await router.getAmountsOut(tokenAmount, [tokenAddress, MONAD_WMON]);
67
+ return amounts[1];
68
+ }
60
69
  // 其他链暂不支持报价,返回 0
61
70
  return 0n;
62
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "1.3.21",
3
+ "version": "1.3.23",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,16 +0,0 @@
1
- /**
2
- * ECDH + AES-GCM 加密工具(浏览器兼容)
3
- * 用于将签名交易用服务器公钥加密
4
- */
5
- /**
6
- * 用服务器公钥加密签名交易(ECDH + AES-GCM)
7
- *
8
- * @param signedTransactions 签名后的交易数组
9
- * @param publicKeyBase64 服务器提供的公钥(Base64 格式)
10
- * @returns JSON 字符串 {e: 临时公钥, i: IV, d: 密文}
11
- */
12
- export declare function encryptWithPublicKey(signedTransactions: string[], publicKeyBase64: string): Promise<string>;
13
- /**
14
- * 验证公钥格式(Base64)
15
- */
16
- export declare function validatePublicKey(publicKeyBase64: string): boolean;
@@ -1,146 +0,0 @@
1
- /**
2
- * ECDH + AES-GCM 加密工具(浏览器兼容)
3
- * 用于将签名交易用服务器公钥加密
4
- */
5
- /**
6
- * 获取全局 crypto 对象(最简单直接的方式)
7
- */
8
- function getCryptoAPI() {
9
- // 尝试所有可能的全局对象,优先浏览器环境
10
- const cryptoObj = (typeof window !== 'undefined' && window.crypto) ||
11
- (typeof self !== 'undefined' && self.crypto) ||
12
- (typeof global !== 'undefined' && global.crypto) ||
13
- (typeof globalThis !== 'undefined' && globalThis.crypto);
14
- if (!cryptoObj) {
15
- const env = typeof window !== 'undefined' ? 'Browser' : 'Node.js';
16
- const protocol = typeof location !== 'undefined' ? location.protocol : 'unknown';
17
- throw new Error(`❌ Crypto API 不可用。环境: ${env}, 协议: ${protocol}. ` +
18
- '请确保在 HTTPS 或 localhost 下运行');
19
- }
20
- return cryptoObj;
21
- }
22
- /**
23
- * 获取 SubtleCrypto(用于加密操作)
24
- */
25
- function getSubtleCrypto() {
26
- const crypto = getCryptoAPI();
27
- if (!crypto.subtle) {
28
- const protocol = typeof location !== 'undefined' ? location.protocol : 'unknown';
29
- const hostname = typeof location !== 'undefined' ? location.hostname : 'unknown';
30
- throw new Error(`❌ SubtleCrypto API 不可用。协议: ${protocol}, 主机: ${hostname}. ` +
31
- '请确保:1) 使用 HTTPS (或 localhost);2) 浏览器支持 Web Crypto API;' +
32
- '3) 不在无痕/隐私浏览模式下');
33
- }
34
- return crypto.subtle;
35
- }
36
- /**
37
- * Base64 转 ArrayBuffer(优先使用浏览器 API)
38
- */
39
- function base64ToArrayBuffer(base64) {
40
- // 浏览器环境(优先)
41
- if (typeof atob !== 'undefined') {
42
- const binaryString = atob(base64);
43
- const bytes = new Uint8Array(binaryString.length);
44
- for (let i = 0; i < binaryString.length; i++) {
45
- bytes[i] = binaryString.charCodeAt(i);
46
- }
47
- return bytes.buffer;
48
- }
49
- // Node.js 环境(fallback)
50
- if (typeof Buffer !== 'undefined') {
51
- return Buffer.from(base64, 'base64').buffer;
52
- }
53
- throw new Error('❌ Base64 解码不可用');
54
- }
55
- /**
56
- * ArrayBuffer 转 Base64(优先使用浏览器 API)
57
- */
58
- function arrayBufferToBase64(buffer) {
59
- // 浏览器环境(优先)
60
- if (typeof btoa !== 'undefined') {
61
- const bytes = new Uint8Array(buffer);
62
- let binary = '';
63
- for (let i = 0; i < bytes.length; i++) {
64
- binary += String.fromCharCode(bytes[i]);
65
- }
66
- return btoa(binary);
67
- }
68
- // Node.js 环境(fallback)
69
- if (typeof Buffer !== 'undefined') {
70
- return Buffer.from(buffer).toString('base64');
71
- }
72
- throw new Error('❌ Base64 编码不可用');
73
- }
74
- /**
75
- * 生成随机 Hex 字符串
76
- */
77
- function randomHex(length) {
78
- const crypto = getCryptoAPI();
79
- const array = new Uint8Array(length);
80
- crypto.getRandomValues(array);
81
- return Array.from(array)
82
- .map(b => b.toString(16).padStart(2, '0'))
83
- .join('');
84
- }
85
- /**
86
- * 用服务器公钥加密签名交易(ECDH + AES-GCM)
87
- *
88
- * @param signedTransactions 签名后的交易数组
89
- * @param publicKeyBase64 服务器提供的公钥(Base64 格式)
90
- * @returns JSON 字符串 {e: 临时公钥, i: IV, d: 密文}
91
- */
92
- export async function encryptWithPublicKey(signedTransactions, publicKeyBase64) {
93
- try {
94
- // 0. 获取 SubtleCrypto 和 Crypto API
95
- const subtle = getSubtleCrypto();
96
- const crypto = getCryptoAPI();
97
- // 1. 准备数据
98
- const payload = {
99
- signedTransactions,
100
- timestamp: Date.now(),
101
- nonce: randomHex(8)
102
- };
103
- const plaintext = JSON.stringify(payload);
104
- // 2. 生成临时 ECDH 密钥对
105
- const ephemeralKeyPair = await subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, true, ['deriveKey']);
106
- // 3. 导入服务器公钥
107
- const publicKeyBuffer = base64ToArrayBuffer(publicKeyBase64);
108
- const publicKey = await subtle.importKey('raw', publicKeyBuffer, { name: 'ECDH', namedCurve: 'P-256' }, false, []);
109
- // 4. 派生共享密钥(AES-256)
110
- const sharedKey = await subtle.deriveKey({ name: 'ECDH', public: publicKey }, ephemeralKeyPair.privateKey, { name: 'AES-GCM', length: 256 }, false, ['encrypt']);
111
- // 5. AES-GCM 加密
112
- const iv = crypto.getRandomValues(new Uint8Array(12));
113
- const encrypted = await subtle.encrypt({ name: 'AES-GCM', iv }, sharedKey, new TextEncoder().encode(plaintext));
114
- // 6. 导出临时公钥
115
- const ephemeralPublicKeyRaw = await subtle.exportKey('raw', ephemeralKeyPair.publicKey);
116
- // 7. 返回加密包(JSON 格式)
117
- return JSON.stringify({
118
- e: arrayBufferToBase64(ephemeralPublicKeyRaw), // 临时公钥
119
- i: arrayBufferToBase64(iv.buffer), // IV
120
- d: arrayBufferToBase64(encrypted) // 密文
121
- });
122
- }
123
- catch (error) {
124
- throw new Error(`加密失败: ${error?.message || String(error)}`);
125
- }
126
- }
127
- /**
128
- * 验证公钥格式(Base64)
129
- */
130
- export function validatePublicKey(publicKeyBase64) {
131
- try {
132
- if (!publicKeyBase64)
133
- return false;
134
- // Base64 字符集验证
135
- if (!/^[A-Za-z0-9+/=]+$/.test(publicKeyBase64))
136
- return false;
137
- // ECDH P-256 公钥固定长度 65 字节(未压缩)
138
- // Base64 编码后约 88 字符
139
- if (publicKeyBase64.length < 80 || publicKeyBase64.length > 100)
140
- return false;
141
- return true;
142
- }
143
- catch {
144
- return false;
145
- }
146
- }