four-flap-meme-sdk 4.0.3 → 4.0.4

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.
Files changed (38) hide show
  1. package/dist/abis/openfour.d.ts +94 -0
  2. package/dist/abis/openfour.js +63 -0
  3. package/dist/bundle/four-meme/core/create-openfour.d.ts +23 -0
  4. package/dist/bundle/four-meme/core/create-openfour.js +118 -0
  5. package/dist/bundle/four-meme/core/index.d.ts +2 -0
  6. package/dist/bundle/four-meme/core/index.js +1 -0
  7. package/dist/bundle/four-meme/private/buy-quote.d.ts +35 -0
  8. package/dist/bundle/four-meme/private/buy-quote.js +70 -0
  9. package/dist/bundle/four-meme/private/buy.js +82 -87
  10. package/dist/bundle/four-meme/private/index.d.ts +2 -0
  11. package/dist/bundle/four-meme/private/index.js +1 -0
  12. package/dist/bundle/four-meme/private/openfour-buy.d.ts +10 -0
  13. package/dist/bundle/four-meme/private/openfour-buy.js +65 -0
  14. package/dist/bundle/four-meme/types/index.d.ts +7 -0
  15. package/dist/chains/bsc/constants.d.ts +1 -0
  16. package/dist/core/clients/four.d.ts +10 -1
  17. package/dist/core/clients/four.js +66 -0
  18. package/dist/core/constants/addresses.d.ts +2 -0
  19. package/dist/core/constants/addresses.js +2 -0
  20. package/dist/domains/four/tax-token.d.ts +2 -0
  21. package/dist/domains/four/tax-token.js +13 -4
  22. package/dist/domains/four/tax-token.test.d.ts +1 -0
  23. package/dist/domains/four/tax-token.test.js +89 -0
  24. package/dist/domains/openfour/create-arg.d.ts +13 -0
  25. package/dist/domains/openfour/create-arg.js +78 -0
  26. package/dist/domains/openfour/index.d.ts +4 -0
  27. package/dist/domains/openfour/index.js +3 -0
  28. package/dist/domains/openfour/registry.d.ts +4 -0
  29. package/dist/domains/openfour/registry.js +44 -0
  30. package/dist/domains/openfour/schema.d.ts +20 -0
  31. package/dist/domains/openfour/schema.js +88 -0
  32. package/dist/domains/openfour/types.d.ts +76 -0
  33. package/dist/domains/openfour/types.js +1 -0
  34. package/dist/exports/root-bundle-and-tooling.d.ts +4 -2
  35. package/dist/exports/root-bundle-and-tooling.js +2 -2
  36. package/dist/exports/root-foundations.d.ts +1 -0
  37. package/dist/exports/root-foundations.js +1 -0
  38. package/package.json +1 -1
@@ -0,0 +1,94 @@
1
+ /**
2
+ * OpenFour 最小 ABI:Registry 发现、schema 读取、createToken / buyByBudget
3
+ */
4
+ export declare const OPENFOUR_REGISTRY_ABI: string[];
5
+ export declare const OPENFOUR_CORE_ABI: string[];
6
+ export declare const OPENFOUR_TOOLS_ABI: ({
7
+ type: string;
8
+ name: string;
9
+ stateMutability: string;
10
+ inputs: never[];
11
+ outputs: {
12
+ name: string;
13
+ type: string;
14
+ components: readonly [{
15
+ readonly name: "name";
16
+ readonly type: "string";
17
+ }, {
18
+ readonly name: "abiType";
19
+ readonly type: "string";
20
+ }, {
21
+ readonly name: "decimals";
22
+ readonly type: "uint8";
23
+ }, {
24
+ readonly name: "optional";
25
+ readonly type: "bool";
26
+ }, {
27
+ readonly name: "title";
28
+ readonly type: "string";
29
+ }, {
30
+ readonly name: "defaultValue";
31
+ readonly type: "string";
32
+ }, {
33
+ readonly name: "hint";
34
+ readonly type: "string";
35
+ }, {
36
+ readonly name: "minValue";
37
+ readonly type: "string";
38
+ }, {
39
+ readonly name: "maxValue";
40
+ readonly type: "string";
41
+ }];
42
+ }[];
43
+ } | {
44
+ type: string;
45
+ name: string;
46
+ stateMutability: string;
47
+ inputs: {
48
+ name: string;
49
+ type: string;
50
+ }[];
51
+ outputs: {
52
+ name: string;
53
+ type: string;
54
+ components: readonly [{
55
+ readonly name: "kind";
56
+ readonly type: "string";
57
+ }, {
58
+ readonly name: "version";
59
+ readonly type: "uint8";
60
+ }, {
61
+ readonly name: "params";
62
+ readonly type: "tuple[]";
63
+ readonly components: readonly [{
64
+ readonly name: "name";
65
+ readonly type: "string";
66
+ }, {
67
+ readonly name: "abiType";
68
+ readonly type: "string";
69
+ }, {
70
+ readonly name: "decimals";
71
+ readonly type: "uint8";
72
+ }, {
73
+ readonly name: "optional";
74
+ readonly type: "bool";
75
+ }, {
76
+ readonly name: "title";
77
+ readonly type: "string";
78
+ }, {
79
+ readonly name: "defaultValue";
80
+ readonly type: "string";
81
+ }, {
82
+ readonly name: "hint";
83
+ readonly type: "string";
84
+ }, {
85
+ readonly name: "minValue";
86
+ readonly type: "string";
87
+ }, {
88
+ readonly name: "maxValue";
89
+ readonly type: "string";
90
+ }];
91
+ }];
92
+ }[];
93
+ })[];
94
+ export declare const OPENFOUR_WETH_ABI: string[];
@@ -0,0 +1,63 @@
1
+ /**
2
+ * OpenFour 最小 ABI:Registry 发现、schema 读取、createToken / buyByBudget
3
+ */
4
+ const PARAM_COMPONENTS = [
5
+ { name: 'name', type: 'string' },
6
+ { name: 'abiType', type: 'string' },
7
+ { name: 'decimals', type: 'uint8' },
8
+ { name: 'optional', type: 'bool' },
9
+ { name: 'title', type: 'string' },
10
+ { name: 'defaultValue', type: 'string' },
11
+ { name: 'hint', type: 'string' },
12
+ { name: 'minValue', type: 'string' },
13
+ { name: 'maxValue', type: 'string' },
14
+ ];
15
+ const MODULE_SCHEMA_COMPONENTS = [
16
+ { name: 'kind', type: 'string' },
17
+ { name: 'version', type: 'uint8' },
18
+ { name: 'params', type: 'tuple[]', components: PARAM_COMPONENTS },
19
+ ];
20
+ export const OPENFOUR_REGISTRY_ABI = [
21
+ 'function openFourCore() view returns (address)',
22
+ 'function openFourTool() view returns (address)',
23
+ 'function isPresetActive(uint256 presetId) view returns (bool)',
24
+ 'function isPresetCreateEnabled(uint256 presetId) view returns (bool)',
25
+ ];
26
+ export const OPENFOUR_CORE_ABI = [
27
+ 'function createToken(bytes createArgs, bytes signature) payable returns (address token)',
28
+ 'function wrappedNative() view returns (address)',
29
+ 'function feeRouter() view returns (address)',
30
+ 'function zapRouter() view returns (address)',
31
+ 'function buyByBudget(address token, uint256 maxPayAmount, uint256 minAmountOut, uint256 options, bytes proof) payable',
32
+ 'function buy(address token, uint256 amount, uint256 maxPayAmount, uint256 options, bytes proof) payable',
33
+ 'event TokenCreated(address indexed token, address indexed creator, uint256 indexed presetId)',
34
+ ];
35
+ export const OPENFOUR_TOOLS_ABI = [
36
+ {
37
+ type: 'function',
38
+ name: 'getTokenBaseSchema',
39
+ stateMutability: 'pure',
40
+ inputs: [],
41
+ outputs: [{ name: '', type: 'tuple[]', components: PARAM_COMPONENTS }],
42
+ },
43
+ {
44
+ type: 'function',
45
+ name: 'getPresetEncodeSchemas',
46
+ stateMutability: 'view',
47
+ inputs: [{ name: 'presetId', type: 'uint256' }],
48
+ outputs: [
49
+ { name: 'tokenSchema', type: 'tuple', components: MODULE_SCHEMA_COMPONENTS },
50
+ { name: 'vaultSchema', type: 'tuple', components: MODULE_SCHEMA_COMPONENTS },
51
+ { name: 'curveSchema', type: 'tuple', components: MODULE_SCHEMA_COMPONENTS },
52
+ { name: 'tradeSchema', type: 'tuple', components: MODULE_SCHEMA_COMPONENTS },
53
+ { name: 'migrateSchema', type: 'tuple', components: MODULE_SCHEMA_COMPONENTS },
54
+ { name: 'customDataSchema', type: 'tuple', components: MODULE_SCHEMA_COMPONENTS },
55
+ ],
56
+ },
57
+ ];
58
+ export const OPENFOUR_WETH_ABI = [
59
+ 'function deposit() payable',
60
+ 'function approve(address spender, uint256 amount) returns (bool)',
61
+ 'function allowance(address owner, address spender) view returns (uint256)',
62
+ 'function decimals() view returns (uint8)',
63
+ ];
@@ -0,0 +1,23 @@
1
+ import type { FourCreateWithBundleBuyMerkleResult, FourSignConfig } from '../types/index.js';
2
+ import { type OpenFourInitParams, type OpenFourTemplateConfig } from '../../../domains/openfour/index.js';
3
+ export type OpenFourCreateSignParams = {
4
+ privateKeys: string[];
5
+ tokenInfo: {
6
+ name: string;
7
+ symbol: string;
8
+ description: string;
9
+ imageUrl?: string;
10
+ imageFile?: Blob;
11
+ webUrl?: string;
12
+ twitterUrl?: string;
13
+ telegramUrl?: string;
14
+ preSale?: string;
15
+ feePlan?: boolean;
16
+ templateId: number | string;
17
+ quoteSymbol: string;
18
+ templateConfig: OpenFourTemplateConfig;
19
+ initParams: OpenFourInitParams;
20
+ };
21
+ config: FourSignConfig;
22
+ };
23
+ export declare function createOpenFourTokenMerkle(params: OpenFourCreateSignParams): Promise<FourCreateWithBundleBuyMerkleResult>;
@@ -0,0 +1,118 @@
1
+ /**
2
+ * OpenFour 发币:业务 API 拿 createArg + signature,再签 OpenFourCore.createToken
3
+ */
4
+ import { Contract, Wallet } from 'ethers';
5
+ import { getGasLimit } from '../../../core/gas/bundle-gas.js';
6
+ import { NonceManager, getOptimizedGasPrice } from '../../../utils/bundle-helpers.js';
7
+ import { ZERO_ADDRESS } from '../../../core/constants/index.js';
8
+ import { FourClient } from '../../../core/clients/four.js';
9
+ import { getTxType, getGasPriceConfig, getBribeAmount } from '../../config-helpers.js';
10
+ import { signBribeTransaction } from '../../../core/sign-context-helpers.js';
11
+ import { createChainContext, loginFourClient, resolveTokenImage } from '../core-helpers.js';
12
+ import { OPENFOUR_CORE_ABI } from '../../../abis/openfour.js';
13
+ import { computeOpenFourCreateValueWei, decodeOpenFourCreateArg, isOpenFourPresaleNative, loadOpenFourAddresses, } from '../../../domains/openfour/index.js';
14
+ import { signQuoteApproveIfNeeded } from './create-token-quote.js';
15
+ function toRaisedToken(cfg) {
16
+ return {
17
+ symbol: cfg.symbol,
18
+ nativeSymbol: cfg.symbol,
19
+ symbolAddress: cfg.symbolAddress,
20
+ deployCost: cfg.createFee || '0',
21
+ buyFee: '0',
22
+ sellFee: '0',
23
+ minTradeFee: '0',
24
+ b0Amount: '0',
25
+ totalBAmount: cfg.raisedAmount,
26
+ totalAmount: cfg.totalSupply,
27
+ logoUrl: '',
28
+ tradeLevel: [],
29
+ status: 'PUBLISH',
30
+ buyTokenLink: '',
31
+ reservedNumber: 0,
32
+ saleRate: '0.8',
33
+ networkCode: 'BSC',
34
+ platform: 'MEME',
35
+ };
36
+ }
37
+ export async function createOpenFourTokenMerkle(params) {
38
+ const { privateKeys, tokenInfo, config } = params;
39
+ if (privateKeys.length !== 1) {
40
+ throw new Error('OpenFour 发币只支持 1 个 Dev 钱包');
41
+ }
42
+ const rpcUrl = config.rpcUrl || 'https://bsc-dataseed.binance.org';
43
+ const { provider, chainId } = createChainContext(rpcUrl);
44
+ const devWallet = new Wallet(privateKeys[0], provider);
45
+ const fourClient = new FourClient({ baseUrl: config.fourApiUrl });
46
+ const accessToken = await loginFourClient(devWallet, fourClient);
47
+ const imgUrl = await resolveTokenImage(fourClient, tokenInfo, accessToken);
48
+ const preSale = String(tokenInfo.preSale || '0').trim() || '0';
49
+ const cfg = tokenInfo.templateConfig;
50
+ const body = {
51
+ templateId: tokenInfo.templateId,
52
+ name: tokenInfo.name,
53
+ shortName: tokenInfo.symbol,
54
+ symbol: cfg.symbol,
55
+ desc: tokenInfo.description,
56
+ imgUrl,
57
+ webUrl: tokenInfo.webUrl,
58
+ twitterUrl: tokenInfo.twitterUrl,
59
+ telegramUrl: tokenInfo.telegramUrl,
60
+ presaleQuote: preSale,
61
+ feePlan: Boolean(tokenInfo.feePlan),
62
+ initParams: tokenInfo.initParams,
63
+ saleAmount: cfg.saleAmount,
64
+ totalSupply: cfg.totalSupply,
65
+ raisedAmount: cfg.raisedAmount,
66
+ };
67
+ const createResp = await fourClient.createOpenFourToken(accessToken, body);
68
+ const addrs = await loadOpenFourAddresses(provider);
69
+ const decoded = decodeOpenFourCreateArg(createResp.createArg);
70
+ const valueWei = computeOpenFourCreateValueWei(decoded, addrs.wrappedNative);
71
+ const gasPrice = await getOptimizedGasPrice(provider, getGasPriceConfig(config));
72
+ const nonceManager = new NonceManager(provider);
73
+ const txType = getTxType(config);
74
+ const signedTxs = [];
75
+ const bribeAmount = getBribeAmount(config);
76
+ if (bribeAmount > 0n) {
77
+ const bribeNonce = await nonceManager.getNextNonce(devWallet);
78
+ signedTxs.push(await signBribeTransaction(devWallet, bribeAmount, bribeNonce, gasPrice, chainId, txType));
79
+ }
80
+ const nativePresale = isOpenFourPresaleNative({
81
+ presaleQuote: decoded.presaleQuote,
82
+ quoteAsset: decoded.quoteAsset,
83
+ wrappedNative: addrs.wrappedNative,
84
+ });
85
+ if (!nativePresale && decoded.presaleQuote > 0n) {
86
+ const approveTx = await signQuoteApproveIfNeeded({
87
+ raisedToken: toRaisedToken(cfg),
88
+ preSale,
89
+ wallet: devWallet,
90
+ spender: addrs.core,
91
+ getNonce: () => nonceManager.getNextNonce(devWallet),
92
+ gasPrice,
93
+ chainId,
94
+ txType,
95
+ });
96
+ if (approveTx)
97
+ signedTxs.push(approveTx);
98
+ }
99
+ const core = new Contract(addrs.core, OPENFOUR_CORE_ABI, devWallet);
100
+ const unsigned = await core.createToken.populateTransaction(createResp.createArg, createResp.signature, {
101
+ value: valueWei,
102
+ });
103
+ signedTxs.push(await devWallet.signTransaction({
104
+ ...unsigned,
105
+ from: devWallet.address,
106
+ nonce: await nonceManager.getNextNonce(devWallet),
107
+ gasLimit: getGasLimit(config, 2_000_000),
108
+ gasPrice,
109
+ chainId,
110
+ type: txType,
111
+ value: valueWei,
112
+ }));
113
+ nonceManager.clearTemp();
114
+ return {
115
+ signedTransactions: signedTxs,
116
+ tokenAddress: ZERO_ADDRESS,
117
+ };
118
+ }
@@ -10,5 +10,7 @@
10
10
  * 对外 export 符号与路径保持不变。
11
11
  */
12
12
  export { createTokenWithBundleBuyMerkle } from './create-token.js';
13
+ export { createOpenFourTokenMerkle } from './create-openfour.js';
14
+ export type { OpenFourCreateSignParams } from './create-openfour.js';
13
15
  export { batchBuyWithBundleMerkle } from './buy-flow.js';
14
16
  export { batchSellWithBundleMerkle } from './sell-flow.js';
@@ -10,5 +10,6 @@
10
10
  * 对外 export 符号与路径保持不变。
11
11
  */
12
12
  export { createTokenWithBundleBuyMerkle } from './create-token.js';
13
+ export { createOpenFourTokenMerkle } from './create-openfour.js';
13
14
  export { batchBuyWithBundleMerkle } from './buy-flow.js';
14
15
  export { batchSellWithBundleMerkle } from './sell-flow.js';
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Four TokenManager 买入付款:BNB 走 msg.value;ERC20 计价走 approve + funds
3
+ */
4
+ import { Wallet, type JsonRpcProvider } from 'ethers';
5
+ import { NonceManager } from '../../../utils/bundle-helpers.js';
6
+ export type FourBuyQuoteInput = {
7
+ /** ERC20 计价地址;不传则按 BNB */
8
+ quoteTokenAddress?: string;
9
+ quoteDecimals?: number;
10
+ };
11
+ export type FourBuyPayment = {
12
+ fundsWei: bigint;
13
+ valueWei: bigint;
14
+ quoteAddress?: string;
15
+ approvalWei: bigint;
16
+ };
17
+ export declare function isFourErc20BuyQuote(quoteTokenAddress?: string): boolean;
18
+ export declare function resolveQuoteDecimals(provider: JsonRpcProvider, quote: FourBuyQuoteInput): Promise<number>;
19
+ export declare function resolveFourBuyPayment(params: {
20
+ provider: JsonRpcProvider;
21
+ tokenAddress: string;
22
+ funds: string;
23
+ quote?: FourBuyQuoteInput;
24
+ }): Promise<FourBuyPayment>;
25
+ export declare function signQuoteApproveTxs(params: {
26
+ wallets: Wallet[];
27
+ quoteAddress: string;
28
+ spender: string;
29
+ amounts: bigint[];
30
+ nonceManager: NonceManager;
31
+ gasPrice: bigint;
32
+ chainId: number;
33
+ txType: number;
34
+ provider: JsonRpcProvider;
35
+ }): Promise<string[]>;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Four TokenManager 买入付款:BNB 走 msg.value;ERC20 计价走 approve + funds
3
+ */
4
+ import { Contract, parseEther, parseUnits } from 'ethers';
5
+ import { ADDRESSES, ZERO_ADDRESS } from '../../../core/constants/index.js';
6
+ import { ERC20_ABI } from '../../../abis/common.js';
7
+ import { batchCheckAllowances } from '../../../core/erc20/index.js';
8
+ const HELPER_TRY_BUY_ABI = [
9
+ 'function tryBuy(address token, uint256 amount, uint256 funds) view returns (address tokenManager, address quote, uint256 estimatedAmount, uint256 estimatedCost, uint256 estimatedFee, uint256 amountMsgValue, uint256 amountApproval, uint256 amountFunds)',
10
+ ];
11
+ export function isFourErc20BuyQuote(quoteTokenAddress) {
12
+ if (!quoteTokenAddress)
13
+ return false;
14
+ return quoteTokenAddress.toLowerCase() !== ZERO_ADDRESS.toLowerCase();
15
+ }
16
+ export async function resolveQuoteDecimals(provider, quote) {
17
+ if (quote.quoteDecimals != null)
18
+ return quote.quoteDecimals;
19
+ if (!isFourErc20BuyQuote(quote.quoteTokenAddress))
20
+ return 18;
21
+ const token = new Contract(quote.quoteTokenAddress, ERC20_ABI, provider);
22
+ return Number(await token.decimals());
23
+ }
24
+ export async function resolveFourBuyPayment(params) {
25
+ const quote = params.quote ?? {};
26
+ if (!isFourErc20BuyQuote(quote.quoteTokenAddress)) {
27
+ const fundsWei = parseEther(params.funds);
28
+ return { fundsWei, valueWei: fundsWei, approvalWei: 0n };
29
+ }
30
+ const decimals = await resolveQuoteDecimals(params.provider, quote);
31
+ const fundsWei = parseUnits(params.funds, decimals);
32
+ const quoteAddress = quote.quoteTokenAddress;
33
+ try {
34
+ const helper = new Contract(ADDRESSES.BSC.TokenManagerHelper3, HELPER_TRY_BUY_ABI, params.provider);
35
+ const preview = await helper.tryBuy(params.tokenAddress, 0n, fundsWei);
36
+ const amountFunds = preview.amountFunds > 0n ? preview.amountFunds : fundsWei;
37
+ return {
38
+ fundsWei: amountFunds,
39
+ valueWei: preview.amountMsgValue,
40
+ quoteAddress: preview.quote || quoteAddress,
41
+ approvalWei: preview.amountApproval,
42
+ };
43
+ }
44
+ catch {
45
+ return { fundsWei, valueWei: 0n, quoteAddress, approvalWei: fundsWei };
46
+ }
47
+ }
48
+ export async function signQuoteApproveTxs(params) {
49
+ const { wallets, quoteAddress, spender, amounts, nonceManager, gasPrice, chainId, txType, provider } = params;
50
+ if (wallets.length === 0)
51
+ return [];
52
+ const allowances = await batchCheckAllowances(provider, quoteAddress, wallets.map((w) => w.address), spender);
53
+ const signed = [];
54
+ for (let i = 0; i < wallets.length; i++) {
55
+ if (allowances[i] >= amounts[i])
56
+ continue;
57
+ const token = new Contract(quoteAddress, ERC20_ABI, wallets[i]);
58
+ const unsigned = await token.approve.populateTransaction(spender, amounts[i]);
59
+ signed.push(await wallets[i].signTransaction({
60
+ ...unsigned,
61
+ from: wallets[i].address,
62
+ nonce: await nonceManager.getNextNonce(wallets[i]),
63
+ gasLimit: 80000n,
64
+ gasPrice,
65
+ chainId,
66
+ type: txType,
67
+ }));
68
+ }
69
+ return signed;
70
+ }