Repository navigation
Expand file tree
/
Copy pathlaunch.cell
More file actions
70 lines (63 loc) · 4.04 KB
/
Copy pathlaunch.cell
File metadata and controls
70 lines (63 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module cellscript::launch
use cellscript::fungible_token::{Token, MintAuthority}
use cellscript::amm_pool::{Pool, LPReceipt}
const U64_MAX: u64 = 18446744073709551615
const MAX_FEE_RATE_BPS: u16 = 1000
action launch_token(symbol: [u8; 8], max_supply: u64, initial_mint: u64, pool_seed_amount: u64, pool_paired_token: Token, fee_rate_bps: u16, creator: Address, distribution: [(Address, u64); 4]) -> (auth: MintAuthority, dist0: Token, dist1: Token, dist2: Token, dist3: Token, pool: Pool, lp_receipt: LPReceipt, change: Token) {
verification
require initial_mint <= max_supply, "initial exceeds max"
require pool_seed_amount > 0, "zero pool seed"
require pool_paired_token.amount > 0, "zero paired seed"
require symbol != pool_paired_token.symbol, "same token"
require fee_rate_bps <= MAX_FEE_RATE_BPS, "fee too high"
require pool_seed_amount <= initial_mint, "pool seed exceeds mint"
require distribution[1].1 <= U64_MAX - distribution[0].1, "distribution overflow"
let dist01 = distribution[0].1 + distribution[1].1
require distribution[2].1 <= U64_MAX - dist01, "distribution overflow"
let dist012 = dist01 + distribution[2].1
require distribution[3].1 <= U64_MAX - dist012, "distribution overflow"
let dist_total = dist012 + distribution[3].1
require pool_seed_amount <= U64_MAX - dist_total, "allocation overflow"
require dist_total + pool_seed_amount <= initial_mint, "allocation exceeds mint"
create auth = MintAuthority { token_symbol: symbol, max_supply, minted: initial_mint } with_lock(creator)
create dist0 = Token { amount: distribution[0].1, symbol } with_lock(distribution[0].0)
create dist1 = Token { amount: distribution[1].1, symbol } with_lock(distribution[1].0)
create dist2 = Token { amount: distribution[2].1, symbol } with_lock(distribution[2].0)
create dist3 = Token { amount: distribution[3].1, symbol } with_lock(distribution[3].0)
require pool_seed_amount <= U64_MAX / pool_paired_token.amount, "liquidity overflow"
require pool_seed_amount * pool_paired_token.amount < U64_MAX, "liquidity root overflow"
let initial_lp = isqrt(pool_seed_amount * pool_paired_token.amount)
require initial_lp > 0, "zero lp supply"
consume pool_paired_token
create pool = Pool { token_a_type: dist0.type_hash(), token_b_type: pool_paired_token.type_hash(), token_a_symbol: symbol, token_b_symbol: pool_paired_token.symbol, reserve_a: pool_seed_amount, reserve_b: pool_paired_token.amount, total_lp: initial_lp, fee_rate_bps }
create lp_receipt = LPReceipt { pool_id: pool.type_hash(), lp_amount: initial_lp, provider: creator } with_lock(creator)
let remaining = initial_mint - dist_total - pool_seed_amount
create change = Token { amount: remaining, symbol } with_lock(creator)
}
fn isqrt(n: u64) -> u64 {
if n == 0 {
return 0
}
let mut x = n
let mut y = (x + 1) / 2
while y < x {
x = y
y = (x + n / x) / 2
}
x
}
action bootstrap_token(symbol: [u8; 8], max_supply: u64, initial_mint: u64, creator: Address, recipients: [(Address, u64); 2]) -> (auth: MintAuthority, rec0: Token, rec1: Token, change: Token) {
verification
require initial_mint <= max_supply, "initial exceeds max"
let mut total_distributed: u64 = 0
for (_, amount) in recipients {
require amount <= U64_MAX - total_distributed, "distribution overflow"
total_distributed = total_distributed + amount
}
require total_distributed <= initial_mint, "distribution exceeds mint"
create auth = MintAuthority { token_symbol: symbol, max_supply, minted: initial_mint } with_lock(creator)
create rec0 = Token { amount: recipients[0].1, symbol } with_lock(recipients[0].0)
create rec1 = Token { amount: recipients[1].1, symbol } with_lock(recipients[1].0)
let remaining = initial_mint - total_distributed
create change = Token { amount: remaining, symbol } with_lock(creator)
}