four-flap-meme-sdk 1.5.71 → 1.5.72
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/flap/portal.d.ts
CHANGED
|
@@ -95,6 +95,20 @@ export type TokenStateV5 = {
|
|
|
95
95
|
nativeToQuoteSwapEnabled: boolean;
|
|
96
96
|
extensionID: `0x${string}`;
|
|
97
97
|
};
|
|
98
|
+
/**
|
|
99
|
+
* TokenStateV7 - 包含毕业相关信息(pool, progress, lpFeeProfile)
|
|
100
|
+
* 用于 XLayer 等支持 V7 的链
|
|
101
|
+
*/
|
|
102
|
+
export type TokenStateV7 = TokenStateV5 & {
|
|
103
|
+
taxRate: bigint;
|
|
104
|
+
pool: string;
|
|
105
|
+
progress: bigint;
|
|
106
|
+
lpFeeProfile: number;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* 将 lpFeeProfile 转换为 V3 fee 值
|
|
110
|
+
*/
|
|
111
|
+
export declare function lpFeeProfileToV3Fee(profile: number): number;
|
|
98
112
|
/**
|
|
99
113
|
* Flap Portal 支持的链
|
|
100
114
|
*/
|
|
@@ -176,6 +190,23 @@ export declare class FlapPortal {
|
|
|
176
190
|
getTokenV3(token: string): Promise<TokenStateV3>;
|
|
177
191
|
getTokenV4(token: string): Promise<TokenStateV4>;
|
|
178
192
|
getTokenV5(token: string): Promise<TokenStateV5>;
|
|
193
|
+
/**
|
|
194
|
+
* 获取代币状态 V7(包含毕业相关信息)
|
|
195
|
+
* 注意:仅部分链支持 V7(如 XLayer v5.7+),不支持的链会 revert
|
|
196
|
+
*/
|
|
197
|
+
getTokenV7(token: string): Promise<TokenStateV7>;
|
|
198
|
+
/**
|
|
199
|
+
* 计算距离毕业还需多少 OKB/ETH(含手续费)
|
|
200
|
+
* @param state TokenStateV5 或 TokenStateV7
|
|
201
|
+
* @returns 毕业相关信息
|
|
202
|
+
*/
|
|
203
|
+
computeRemainingToGraduation(state: TokenStateV5 | TokenStateV7): {
|
|
204
|
+
remainingOkb: string;
|
|
205
|
+
graduationReserve: string;
|
|
206
|
+
currentReserve: string;
|
|
207
|
+
isGraduated: boolean;
|
|
208
|
+
progressPercent: string;
|
|
209
|
+
};
|
|
179
210
|
computePriceAndProgress(state: TokenStateV5): {
|
|
180
211
|
price: string;
|
|
181
212
|
progress: string;
|
package/dist/flap/portal.js
CHANGED
|
@@ -64,6 +64,8 @@ const PORTAL_ABI = [
|
|
|
64
64
|
'function getTokenV3(address token) external view returns (uint8,uint256,uint256,uint256,uint8,uint256,uint256,address,bool)',
|
|
65
65
|
'function getTokenV4(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,address,bool,bytes32))',
|
|
66
66
|
'function getTokenV5(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,uint256,uint256,address,bool,bytes32))',
|
|
67
|
+
// V7: 包含 taxRate, pool, progress, lpFeeProfile
|
|
68
|
+
'function getTokenV7(address token) external view returns ((uint8,uint256,uint256,uint256,uint8,uint256,uint256,uint256,uint256,address,bool,bytes32,uint256,address,uint256,uint8))',
|
|
67
69
|
// 报价方法
|
|
68
70
|
'function previewBuy(address token, uint256 ethAmount) external view returns (uint256)',
|
|
69
71
|
'function previewSell(address token, uint256 tokenAmount) external view returns (uint256)',
|
|
@@ -88,6 +90,17 @@ const PORTAL_ABI = [
|
|
|
88
90
|
'function version() external pure returns (string)',
|
|
89
91
|
'function getLocks(address token) external view returns (uint256[])'
|
|
90
92
|
];
|
|
93
|
+
/**
|
|
94
|
+
* 将 lpFeeProfile 转换为 V3 fee 值
|
|
95
|
+
*/
|
|
96
|
+
export function lpFeeProfileToV3Fee(profile) {
|
|
97
|
+
switch (profile) {
|
|
98
|
+
case V3LPFeeProfile.LP_FEE_PROFILE_STANDARD: return 2500;
|
|
99
|
+
case V3LPFeeProfile.LP_FEE_PROFILE_LOW: return 100;
|
|
100
|
+
case V3LPFeeProfile.LP_FEE_PROFILE_HIGH: return 10000;
|
|
101
|
+
default: return 2500; // 默认 STANDARD
|
|
102
|
+
}
|
|
103
|
+
}
|
|
91
104
|
export class FlapPortal {
|
|
92
105
|
constructor(cfg) {
|
|
93
106
|
this.rpcUrl = cfg.rpcUrl;
|
|
@@ -144,6 +157,66 @@ export class FlapPortal {
|
|
|
144
157
|
extensionID: raw[11],
|
|
145
158
|
};
|
|
146
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* 获取代币状态 V7(包含毕业相关信息)
|
|
162
|
+
* 注意:仅部分链支持 V7(如 XLayer v5.7+),不支持的链会 revert
|
|
163
|
+
*/
|
|
164
|
+
async getTokenV7(token) {
|
|
165
|
+
const raw = await this.contract.getTokenV7(token);
|
|
166
|
+
return {
|
|
167
|
+
status: raw[0],
|
|
168
|
+
reserve: raw[1],
|
|
169
|
+
circulatingSupply: raw[2],
|
|
170
|
+
price: raw[3],
|
|
171
|
+
tokenVersion: raw[4],
|
|
172
|
+
r: raw[5],
|
|
173
|
+
h: raw[6],
|
|
174
|
+
k: raw[7],
|
|
175
|
+
dexSupplyThresh: raw[8],
|
|
176
|
+
quoteTokenAddress: raw[9],
|
|
177
|
+
nativeToQuoteSwapEnabled: raw[10],
|
|
178
|
+
extensionID: raw[11],
|
|
179
|
+
taxRate: raw[12],
|
|
180
|
+
pool: raw[13],
|
|
181
|
+
progress: raw[14],
|
|
182
|
+
lpFeeProfile: Number(raw[15]),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* 计算距离毕业还需多少 OKB/ETH(含手续费)
|
|
187
|
+
* @param state TokenStateV5 或 TokenStateV7
|
|
188
|
+
* @returns 毕业相关信息
|
|
189
|
+
*/
|
|
190
|
+
computeRemainingToGraduation(state) {
|
|
191
|
+
const r = Number(formatEther(state.r));
|
|
192
|
+
const h = Number(formatEther(state.h));
|
|
193
|
+
const k = Number(formatEther(state.k));
|
|
194
|
+
const curve = new CDPV2(r, h, k);
|
|
195
|
+
const currentReserve = formatEther(state.reserve);
|
|
196
|
+
const dexSupplyThresh = formatEther(state.dexSupplyThresh);
|
|
197
|
+
const circulatingSupply = formatEther(state.circulatingSupply);
|
|
198
|
+
// 计算毕业时的储备
|
|
199
|
+
const graduationReserve = curve.estimateReserve(dexSupplyThresh);
|
|
200
|
+
const graduationReserveNum = parseFloat(graduationReserve.toString());
|
|
201
|
+
const currentReserveNum = parseFloat(currentReserve);
|
|
202
|
+
// 是否已毕业 (status == 4 表示 DEX)
|
|
203
|
+
const isGraduated = state.status === 4;
|
|
204
|
+
// 还需多少(净值)
|
|
205
|
+
const remainingNet = Math.max(0, graduationReserveNum - currentReserveNum);
|
|
206
|
+
// 含手续费(1% buy fee)
|
|
207
|
+
const remainingWithFee = remainingNet / (1 - this.buyFeeRate);
|
|
208
|
+
// 计算进度
|
|
209
|
+
const progressPercent = isGraduated
|
|
210
|
+
? '100.00'
|
|
211
|
+
: (currentReserveNum / graduationReserveNum * 100).toFixed(2);
|
|
212
|
+
return {
|
|
213
|
+
remainingOkb: remainingWithFee.toFixed(6),
|
|
214
|
+
graduationReserve: graduationReserveNum.toFixed(6),
|
|
215
|
+
currentReserve: currentReserveNum.toFixed(6),
|
|
216
|
+
isGraduated,
|
|
217
|
+
progressPercent,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
147
220
|
// 价格/进度
|
|
148
221
|
computePriceAndProgress(state) {
|
|
149
222
|
// 使用合约返回的实时价格,而不是重新计算
|
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export { TM1, type FourChainV1 } from './contracts/tm1.js';
|
|
|
11
11
|
export { TM2, type FourChainV2 } from './contracts/tm2.js';
|
|
12
12
|
export { Helper3, Helper3Writer } from './contracts/helper3.js';
|
|
13
13
|
export { CDPV2 } from './flap/curve.js';
|
|
14
|
-
export { FlapPortal, FlapPortalWriter, type FlapChain, type PortalConfig, type TokenStateV2, type TokenStateV3, type TokenStateV4, type TokenStateV5, type QuoteExactInputParams, type ExactInputParams, type ExactInputV3Params, type NewTokenV3Params, type NewTokenV4Params, TokenStatus, TokenVersion, DexThreshType, MigratorType, V3LPFeeProfile, DEXId } from './flap/portal.js';
|
|
14
|
+
export { FlapPortal, FlapPortalWriter, type FlapChain, type PortalConfig, type TokenStateV2, type TokenStateV3, type TokenStateV4, type TokenStateV5, type TokenStateV7, type QuoteExactInputParams, type ExactInputParams, type ExactInputV3Params, type NewTokenV3Params, type NewTokenV4Params, TokenStatus, TokenVersion, DexThreshType, MigratorType, V3LPFeeProfile, lpFeeProfileToV3Fee, DEXId } from './flap/portal.js';
|
|
15
15
|
export { uploadTokenMeta, type TokenMetaInput } from './flap/ipfs.js';
|
|
16
16
|
export { buildPermitPiggybackAuto } from './flap/permit.js';
|
|
17
17
|
export { predictVanityTokenAddressByChain, findSaltEndingByChain } from './flap/vanity.js';
|
package/dist/index.js
CHANGED
|
@@ -30,7 +30,7 @@ export { TM1 } from './contracts/tm1.js';
|
|
|
30
30
|
export { TM2 } from './contracts/tm2.js';
|
|
31
31
|
export { Helper3, Helper3Writer } from './contracts/helper3.js';
|
|
32
32
|
export { CDPV2 } from './flap/curve.js';
|
|
33
|
-
export { FlapPortal, FlapPortalWriter, TokenStatus, TokenVersion, DexThreshType, MigratorType, V3LPFeeProfile, DEXId } from './flap/portal.js';
|
|
33
|
+
export { FlapPortal, FlapPortalWriter, TokenStatus, TokenVersion, DexThreshType, MigratorType, V3LPFeeProfile, lpFeeProfileToV3Fee, DEXId } from './flap/portal.js';
|
|
34
34
|
export { uploadTokenMeta } from './flap/ipfs.js';
|
|
35
35
|
export { buildPermitPiggybackAuto } from './flap/permit.js';
|
|
36
36
|
export { predictVanityTokenAddressByChain, findSaltEndingByChain } from './flap/vanity.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "four-flap-meme-sdk",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.72",
|
|
4
4
|
"description": "SDK for Flap bonding curve and four.meme TokenManager",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -36,4 +36,4 @@
|
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"typescript": "^5.6.3"
|
|
38
38
|
}
|
|
39
|
-
}
|
|
39
|
+
}
|
|
@@ -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
|
-
}
|