@mania-labs/mania-sdk 1.0.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/README.md +397 -0
- package/dist/index.d.mts +1116 -0
- package/dist/index.d.ts +1116 -0
- package/dist/index.js +1220 -0
- package/dist/index.mjs +1165 -0
- package/package.json +56 -0
- package/src/.claude/settings.local.json +9 -0
- package/src/abi/ManiaFactoryUpgradeable.json +2183 -0
- package/src/abi.ts +268 -0
- package/src/bondingCurve.ts +319 -0
- package/src/constants.ts +72 -0
- package/src/index.ts +71 -0
- package/src/mania.ts +652 -0
- package/src/types.ts +238 -0
- package/src/utils.ts +154 -0
package/README.md
ADDED
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
# @mania-labs/mania-sdk
|
|
2
|
+
|
|
3
|
+
Official SDK for interacting with Mania Fun - an EVM-based token launchpad using bonding curves.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Mania Protocol enables anyone to create and trade tokens on a bonding curve. When a token's bonding curve reaches the migration threshold (4 ETH), liquidity is automatically migrated to Uniswap V3, providing deep liquidity for the token.
|
|
8
|
+
|
|
9
|
+
### Key Features
|
|
10
|
+
|
|
11
|
+
- **Bonding Curve Trading**: Buy and sell tokens on a constant product AMM
|
|
12
|
+
- **Fair Launch**: Every token starts with the same bonding curve parameters
|
|
13
|
+
- **Automatic Migration**: Liquidity migrates to Uniswap V3 when threshold is reached
|
|
14
|
+
- **Creator Rewards**: Token creators earn 0.30% of all trading fees
|
|
15
|
+
|
|
16
|
+
### Important Notes
|
|
17
|
+
|
|
18
|
+
Tokens will be non-transferrable until migration. This is a critical security feature to prevent pre-initialisation of Uniswap pools at unfavourable prices. This ensures all tokens migrate with equal reserves, in a ratio of 4 ETH : 206.9M tokens.
|
|
19
|
+
|
|
20
|
+
The Mania Factory contract is upgradeable via a UUPS proxy pattern. Updates will be pushed sparingly, with significant notice for all integrators and SDK users. To stay informed about any upcoming updates, please join our Telegram channel at https://t.me/maniaFunDevelopers
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @mania-labs/mania-sdk viem
|
|
26
|
+
# or
|
|
27
|
+
yarn add @mania-labs/mania-sdk viem
|
|
28
|
+
# or
|
|
29
|
+
pnpm add @mania-labs/mania-sdk viem
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Quick Start
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { ManiaSDK } from "@mania-labs/mania-sdk";
|
|
36
|
+
import { parseEther } from "viem";
|
|
37
|
+
import { base } from "viem/chains";
|
|
38
|
+
|
|
39
|
+
// Initialize the SDK
|
|
40
|
+
const sdk = new ManiaSDK({
|
|
41
|
+
factoryAddress: "0x...", // ManiaFactory contract address
|
|
42
|
+
rpcUrl: "https://megaeth-testnet-v2.blockscout.com",
|
|
43
|
+
chainId: 6543,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Connect a wallet for transactions
|
|
47
|
+
sdk.connectWallet(process.env.PRIVATE_KEY!, base);
|
|
48
|
+
|
|
49
|
+
// Get token information
|
|
50
|
+
const tokenInfo = await sdk.getTokenInfo("0x...");
|
|
51
|
+
console.log("Current Price:", tokenInfo.currentPrice);
|
|
52
|
+
console.log("Migration Progress:", tokenInfo.migrationProgress, "%");
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
### Creating a Token
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// Create a new token
|
|
61
|
+
const result = await sdk.create({
|
|
62
|
+
name: "My Token",
|
|
63
|
+
symbol: "MTK",
|
|
64
|
+
uri: "ipfs://...", // Metadata URI
|
|
65
|
+
creator: "0x...", // Creator address (receives fees)
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
console.log("Token created:", result.tokenAddress);
|
|
69
|
+
console.log("Transaction:", result.hash);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Create and Buy in One Transaction
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
// Create token and buy in a single transaction
|
|
76
|
+
const result = await sdk.createAndBuy({
|
|
77
|
+
name: "My Token",
|
|
78
|
+
symbol: "MTK",
|
|
79
|
+
uri: "ipfs://...",
|
|
80
|
+
creator: "0x...",
|
|
81
|
+
buyAmountEth: parseEther("0.1"), // Buy with 0.1 ETH
|
|
82
|
+
minTokensOut: 0n, // Set minimum for slippage protection
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Buying Tokens
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import { parseEther } from "viem";
|
|
90
|
+
|
|
91
|
+
// Buy with manual slippage
|
|
92
|
+
const quote = await sdk.getBuyQuote(tokenAddress, parseEther("0.1"));
|
|
93
|
+
const minTokensOut = (quote * 99n) / 100n; // 1% slippage
|
|
94
|
+
|
|
95
|
+
await sdk.buy({
|
|
96
|
+
token: tokenAddress,
|
|
97
|
+
amountEth: parseEther("0.1"),
|
|
98
|
+
minTokensOut,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Or use built-in slippage calculation
|
|
102
|
+
await sdk.buyWithSlippage(
|
|
103
|
+
tokenAddress,
|
|
104
|
+
parseEther("0.1"),
|
|
105
|
+
100 // 1% slippage in basis points
|
|
106
|
+
);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Selling Tokens
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
// Sell tokens
|
|
113
|
+
const tokenAmount = parseEther("1000"); // 1000 tokens
|
|
114
|
+
const quote = await sdk.getSellQuote(tokenAddress, tokenAmount);
|
|
115
|
+
const minEthOut = (quote * 99n) / 100n; // 1% slippage
|
|
116
|
+
|
|
117
|
+
await sdk.sell({
|
|
118
|
+
token: tokenAddress,
|
|
119
|
+
amountTokens: tokenAmount,
|
|
120
|
+
minEthOut,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Or use built-in slippage calculation
|
|
124
|
+
await sdk.sellWithSlippage(tokenAddress, tokenAmount, 100);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Working with Bonding Curves
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { BondingCurve } from "@mania-labs/mania-sdk";
|
|
131
|
+
|
|
132
|
+
// Get bonding curve instance for calculations
|
|
133
|
+
const curve = await sdk.getBondingCurveInstance(tokenAddress);
|
|
134
|
+
|
|
135
|
+
// Get current state
|
|
136
|
+
const state = curve.getState();
|
|
137
|
+
console.log("Virtual Token Reserves:", state.virtualTokenReserves);
|
|
138
|
+
console.log("Virtual ETH Reserves:", state.virtualEthReserves);
|
|
139
|
+
|
|
140
|
+
// Check status
|
|
141
|
+
console.log("Is Complete:", curve.isComplete());
|
|
142
|
+
console.log("Is Migrated:", curve.isMigrated());
|
|
143
|
+
|
|
144
|
+
// Get quotes locally (no RPC call)
|
|
145
|
+
const buyQuote = curve.getBuyQuote(parseEther("0.1"));
|
|
146
|
+
console.log("Tokens Out:", buyQuote.tokensOut);
|
|
147
|
+
console.log("Fee:", buyQuote.fee);
|
|
148
|
+
console.log("Price per Token:", buyQuote.pricePerToken);
|
|
149
|
+
|
|
150
|
+
// Calculate price impact
|
|
151
|
+
const priceImpact = curve.calculateBuyPriceImpact(parseEther("1"));
|
|
152
|
+
console.log("Price Impact:", priceImpact, "%");
|
|
153
|
+
|
|
154
|
+
// Get migration progress
|
|
155
|
+
console.log("Migration Progress:", curve.getMigrationProgress(), "%");
|
|
156
|
+
console.log("ETH Until Migration:", curve.getEthUntilMigration());
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Migration to Uniswap V3
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
// Check if curve is complete
|
|
163
|
+
const isComplete = await sdk.isComplete(tokenAddress);
|
|
164
|
+
|
|
165
|
+
if (isComplete) {
|
|
166
|
+
// Migrate liquidity to Uniswap V3
|
|
167
|
+
// Note: Caller pays the migration fee (poolMigrationFee)
|
|
168
|
+
const result = await sdk.migrate({ token: tokenAddress });
|
|
169
|
+
console.log("Pool Address:", result.poolAddress);
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Watching Events
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
// Watch for new token creations
|
|
177
|
+
const unwatch = sdk.watchCreateEvents((event) => {
|
|
178
|
+
console.log("New token created:", event.mint);
|
|
179
|
+
console.log("Name:", event.name);
|
|
180
|
+
console.log("Symbol:", event.symbol);
|
|
181
|
+
console.log("Creator:", event.creator);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// Watch trades on a specific token
|
|
185
|
+
sdk.watchTradeEvents(tokenAddress, (event) => {
|
|
186
|
+
console.log(event.isBuy ? "Buy" : "Sell");
|
|
187
|
+
console.log("ETH Amount:", event.ethAmount);
|
|
188
|
+
console.log("Token Amount:", event.tokenAmount);
|
|
189
|
+
console.log("User:", event.user);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Watch for curve completions
|
|
193
|
+
sdk.watchCompleteEvents((event) => {
|
|
194
|
+
console.log("Curve complete:", event.mint);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// Watch for migrations
|
|
198
|
+
sdk.watchMigrationEvents((event) => {
|
|
199
|
+
console.log("Token migrated:", event.mint);
|
|
200
|
+
console.log("Pool:", event.pool);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Stop watching
|
|
204
|
+
unwatch();
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Reading Global State
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
const globalState = await sdk.getGlobalState();
|
|
211
|
+
|
|
212
|
+
console.log("Fee Basis Points:", globalState.feeBasisPoints); // 100 = 1%
|
|
213
|
+
console.log("Migration Enabled:", globalState.enableMigrate);
|
|
214
|
+
console.log("Pool Migration Fee:", globalState.poolMigrationFee);
|
|
215
|
+
console.log("Token Total Supply:", globalState.tokenTotalSupply);
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Utility Functions
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
import {
|
|
222
|
+
formatEthValue,
|
|
223
|
+
formatTokenAmount,
|
|
224
|
+
parseEthValue,
|
|
225
|
+
calculateWithSlippage,
|
|
226
|
+
formatPrice,
|
|
227
|
+
formatMarketCap,
|
|
228
|
+
truncateAddress,
|
|
229
|
+
bpsToPercent,
|
|
230
|
+
} from "@mania-labs/mania-sdk";
|
|
231
|
+
|
|
232
|
+
// Format values for display
|
|
233
|
+
console.log(formatEthValue(parseEther("1.234567"))); // "1.2346"
|
|
234
|
+
console.log(formatTokenAmount(parseEther("1234567890"))); // "1.23B"
|
|
235
|
+
console.log(formatPrice(1234567890123456n)); // "0.0012"
|
|
236
|
+
console.log(formatMarketCap(parseEther("1500"))); // "1.50K ETH"
|
|
237
|
+
|
|
238
|
+
// Calculate slippage
|
|
239
|
+
const minOut = calculateWithSlippage(parseEther("100"), 100); // 1% slippage
|
|
240
|
+
|
|
241
|
+
// Format addresses
|
|
242
|
+
console.log(truncateAddress("0x1234...5678")); // "0x1234...5678"
|
|
243
|
+
|
|
244
|
+
// Convert basis points
|
|
245
|
+
console.log(bpsToPercent(100)); // 1
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## Constants
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
import {
|
|
252
|
+
MIGRATION_THRESHOLD,
|
|
253
|
+
TOKENS_FOR_LP,
|
|
254
|
+
PROTOCOL_FEE_BASIS_POINTS,
|
|
255
|
+
CREATOR_FEE_BASIS_POINTS,
|
|
256
|
+
DEFAULT_SLIPPAGE_BPS,
|
|
257
|
+
UNISWAP_FEE_TIER,
|
|
258
|
+
} from "@mania-labs/mania-sdk";
|
|
259
|
+
|
|
260
|
+
// Migration threshold: 4 ETH
|
|
261
|
+
console.log(MIGRATION_THRESHOLD); // 4000000000000000000n
|
|
262
|
+
|
|
263
|
+
// Tokens allocated for LP: 206.9M
|
|
264
|
+
console.log(TOKENS_FOR_LP);
|
|
265
|
+
|
|
266
|
+
// Fee breakdown
|
|
267
|
+
console.log(PROTOCOL_FEE_BASIS_POINTS); // 70 (0.70%)
|
|
268
|
+
console.log(CREATOR_FEE_BASIS_POINTS); // 30 (0.30%)
|
|
269
|
+
|
|
270
|
+
// Default slippage: 1%
|
|
271
|
+
console.log(DEFAULT_SLIPPAGE_BPS); // 100
|
|
272
|
+
|
|
273
|
+
// Uniswap V3 fee tier: 0.3%
|
|
274
|
+
console.log(UNISWAP_FEE_TIER); // 3000
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Chain Configuration
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
import { getChainConfig, CHAIN_CONFIGS } from "@mania-labs/mania-sdk";
|
|
281
|
+
|
|
282
|
+
// Get config for a specific chain
|
|
283
|
+
const baseConfig = getChainConfig(8453);
|
|
284
|
+
console.log(baseConfig?.factoryAddress);
|
|
285
|
+
console.log(baseConfig?.wethAddress);
|
|
286
|
+
|
|
287
|
+
// Or create SDK from chain ID
|
|
288
|
+
const sdk = ManiaSDK.fromChainId(8453, "https://mainnet.base.org");
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
## Advanced Usage
|
|
292
|
+
|
|
293
|
+
### Custom Viem Clients
|
|
294
|
+
|
|
295
|
+
```typescript
|
|
296
|
+
import { createPublicClient, createWalletClient, http } from "viem";
|
|
297
|
+
import { base } from "viem/chains";
|
|
298
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
299
|
+
|
|
300
|
+
const publicClient = createPublicClient({
|
|
301
|
+
chain: base,
|
|
302
|
+
transport: http("https://mainnet.base.org"),
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
const walletClient = createWalletClient({
|
|
306
|
+
account: privateKeyToAccount("0x..."),
|
|
307
|
+
chain: base,
|
|
308
|
+
transport: http("https://mainnet.base.org"),
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
const sdk = new ManiaSDK({
|
|
312
|
+
factoryAddress: "0x...",
|
|
313
|
+
chainId: 8453,
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
sdk.setPublicClient(publicClient);
|
|
317
|
+
sdk.setWalletClient(walletClient);
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
### Direct ABI Access
|
|
321
|
+
|
|
322
|
+
```typescript
|
|
323
|
+
import { MANIA_FACTORY_ABI, ERC20_ABI } from "@mania-labs/mania-sdk";
|
|
324
|
+
import { getContract } from "viem";
|
|
325
|
+
|
|
326
|
+
// Use ABIs directly with viem
|
|
327
|
+
const contract = getContract({
|
|
328
|
+
address: factoryAddress,
|
|
329
|
+
abi: MANIA_FACTORY_ABI,
|
|
330
|
+
client: publicClient,
|
|
331
|
+
});
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
## Protocol Details
|
|
335
|
+
|
|
336
|
+
### Bonding Curve Formula
|
|
337
|
+
|
|
338
|
+
The protocol uses a constant product formula with virtual reserves:
|
|
339
|
+
|
|
340
|
+
```
|
|
341
|
+
tokensOut = (virtualTokenReserves * netEth) / (virtualEthReserves + netEth)
|
|
342
|
+
ethOut = (tokenAmount * virtualEthReserves) / (virtualTokenReserves + tokenAmount)
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### Fee Structure
|
|
346
|
+
|
|
347
|
+
- **Total Trading Fee**: 1.00% (100 basis points)
|
|
348
|
+
- Protocol Fee: 0.70%
|
|
349
|
+
- Creator Fee: 0.30%
|
|
350
|
+
|
|
351
|
+
### Migration
|
|
352
|
+
|
|
353
|
+
When `realEthReserves` reaches 4 ETH:
|
|
354
|
+
|
|
355
|
+
1. Bonding curve is marked as complete
|
|
356
|
+
2. Anyone can call `migrate()` (paying `poolMigrationFee`)
|
|
357
|
+
3. Token trading is enabled (unlocked)
|
|
358
|
+
4. Liquidity (206.9M tokens + 4 ETH) is added to Uniswap V3
|
|
359
|
+
5. LP NFT is sent to dead address (locked forever)
|
|
360
|
+
|
|
361
|
+
## TypeScript Support
|
|
362
|
+
|
|
363
|
+
This package is written in TypeScript and includes full type definitions.
|
|
364
|
+
|
|
365
|
+
```typescript
|
|
366
|
+
import type {
|
|
367
|
+
BondingCurveState,
|
|
368
|
+
GlobalState,
|
|
369
|
+
CreateTokenParams,
|
|
370
|
+
BuyParams,
|
|
371
|
+
SellParams,
|
|
372
|
+
TokenInfo,
|
|
373
|
+
TransactionResult,
|
|
374
|
+
} from "@mania-labs/mania-sdk";
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
## Error Handling
|
|
378
|
+
|
|
379
|
+
```typescript
|
|
380
|
+
try {
|
|
381
|
+
await sdk.buy({
|
|
382
|
+
token: tokenAddress,
|
|
383
|
+
amountEth: parseEther("10"), // Too much ETH
|
|
384
|
+
minTokensOut: 0n,
|
|
385
|
+
});
|
|
386
|
+
} catch (error) {
|
|
387
|
+
// Common errors:
|
|
388
|
+
// - BuyExceedsMigrationThreshold: Buy would exceed 4 ETH threshold
|
|
389
|
+
// - BondingCurveComplete: Trading has ended
|
|
390
|
+
// - TooLittleTokensReceived: Slippage exceeded
|
|
391
|
+
console.error("Transaction failed:", error);
|
|
392
|
+
}
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
## License
|
|
396
|
+
|
|
397
|
+
MIT
|