@floorlaunchpad/sdk 0.1.0 → 0.3.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/CHANGELOG.md +84 -0
- package/LICENSE +201 -60
- package/README.md +45 -24
- package/dist/abi/evm-event-names.js +12 -0
- package/dist/abi/evm.gen.js +130 -0
- package/dist/abi/functions.gen.js +144 -0
- package/dist/abi/index.js +17 -0
- package/dist/abi/solana.gen.js +128 -0
- package/dist/burn.js +21 -0
- package/dist/client/evm/index.js +191 -0
- package/dist/client/index.js +19 -0
- package/dist/client/solana/index.js +259 -0
- package/dist/client/types.js +5 -0
- package/dist/codec.js +77 -0
- package/dist/config/codeHash.js +9 -0
- package/dist/config/generated.js +746 -0
- package/dist/config/index.js +49 -0
- package/dist/config/registry.gen.js +118 -0
- package/dist/config/solana-genesis.js +7 -0
- package/dist/ed25519.js +31 -0
- package/dist/errors.js +249 -0
- package/dist/events/index.js +40 -0
- package/dist/events/types.gen.js +3 -0
- package/dist/evm/abi.js +283 -0
- package/dist/evm/balances.js +26 -0
- package/dist/evm/chains.js +144 -0
- package/dist/evm/floor/abi.gen.js +195 -0
- package/dist/evm/floor/calls.js +230 -0
- package/dist/evm/floor/create.js +49 -0
- package/dist/evm/floor/events.js +41 -0
- package/dist/evm/floor/iface.js +44 -0
- package/dist/evm/items.js +209 -0
- package/dist/evm/keccak.js +118 -0
- package/dist/evm/multicall.js +30 -0
- package/dist/evm/router.js +184 -0
- package/dist/evm/zap.js +128 -0
- package/dist/fixtures.js +53 -0
- package/dist/generated/index.js +28 -0
- package/dist/generated/interface.gen.js +146 -0
- package/dist/handle.js +22 -0
- package/dist/index.js +91 -7
- package/dist/indexer/bodies.js +473 -0
- package/dist/indexer/client.js +104 -0
- package/dist/indexer/contract.js +349 -0
- package/dist/indexer/decode.js +97 -0
- package/dist/indexer/partners.js +100 -0
- package/dist/indexer/paths.js +127 -0
- package/dist/indexer/read.js +109 -0
- package/dist/indexer/stream.js +71 -0
- package/dist/lock.js +16 -0
- package/dist/math/fixed.js +154 -0
- package/dist/math/genesis.js +39 -0
- package/dist/math/index.js +20 -0
- package/dist/math/u256.js +49 -0
- package/dist/math/v2/ask.js +162 -0
- package/dist/math/v2/book.js +607 -0
- package/dist/math/v2/circulating.js +62 -0
- package/dist/math/v2/create.js +122 -0
- package/dist/math/v2/display.js +208 -0
- package/dist/math/v2/error.js +17 -0
- package/dist/math/v2/feasibility.js +174 -0
- package/dist/math/v2/fees.js +121 -0
- package/dist/math/v2/holders.js +105 -0
- package/dist/math/v2/milestones.js +40 -0
- package/dist/math/v2/pow.js +69 -0
- package/dist/math/v2/preset.js +151 -0
- package/dist/math/v2/receipt.js +108 -0
- package/dist/math/v2/relay.js +217 -0
- package/dist/math/v2/rows.js +365 -0
- package/dist/math/v2/split.js +111 -0
- package/dist/math/v2/sqrt.js +84 -0
- package/dist/math/v2/supply.js +21 -0
- package/dist/math/v2/team.js +75 -0
- package/dist/math/v2/trade.js +126 -0
- package/dist/math/v2/wide.js +51 -0
- package/dist/quote.js +6 -0
- package/dist/registryPrice.js +23 -0
- package/dist/route/edge-jupiter.js +42 -0
- package/dist/route/edge.js +11 -0
- package/dist/route/graph.js +51 -0
- package/dist/route/path.js +75 -0
- package/dist/route/quote.js +15 -0
- package/dist/route-jupiter.js +35 -0
- package/dist/route.js +8 -0
- package/dist/sha256.js +61 -0
- package/dist/solana/alt.js +18 -0
- package/dist/solana/floor/accounts.js +390 -0
- package/dist/solana/floor/borsh.js +66 -0
- package/dist/solana/floor/create.js +65 -0
- package/dist/solana/floor/events.js +140 -0
- package/dist/solana/floor/genesis.js +91 -0
- package/dist/solana/floor/genesisList.js +68 -0
- package/dist/solana/floor/instructions.js +435 -0
- package/dist/solana/floor/metadata.js +198 -0
- package/dist/solana/floor/mint.js +206 -0
- package/dist/solana/floor/program.js +64 -0
- package/dist/solana/floor/registry.js +83 -0
- package/dist/solana/instruction.js +49 -0
- package/dist/solana/pda.js +88 -0
- package/dist/solana/tx.js +138 -0
- package/dist/v2/burnPreview.js +35 -0
- package/dist/v2/circulating.js +11 -0
- package/dist/v2/claimAllEvm.js +70 -0
- package/dist/v2/claims-evm.js +120 -0
- package/dist/v2/claims.js +218 -0
- package/dist/v2/dead.js +21 -0
- package/dist/v2/devbuy.js +37 -0
- package/dist/v2/events.js +115 -0
- package/dist/v2/evm.js +299 -0
- package/dist/v2/evmReads.js +76 -0
- package/dist/v2/genesis.js +76 -0
- package/dist/v2/layouts.js +217 -0
- package/dist/v2/market-evm.js +74 -0
- package/dist/v2/presetRowEvm.js +45 -0
- package/dist/v2/press.js +106 -0
- package/dist/v2/quoteMint.js +52 -0
- package/dist/v2/receipt-tx.js +149 -0
- package/dist/v2/route-alt.js +114 -0
- package/dist/v2/route.js +389 -0
- package/dist/v2/solana.js +391 -0
- package/dist/v2/solanaSettle.js +97 -0
- package/dist/v2/tx/evm.js +126 -0
- package/dist/v2/tx/genesis.js +47 -0
- package/dist/v2/tx/index.js +5 -0
- package/dist/v2/tx/solana.js +118 -0
- package/dist/v2/types.js +13 -0
- package/docs/aggregator.md +61 -0
- package/docs/bot.md +50 -0
- package/docs/frontend.md +29 -0
- package/docs/wallet.md +54 -0
- package/package.json +428 -32
- package/src/abi/evm-event-names.ts +12 -0
- package/src/abi/evm.gen.ts +139 -0
- package/src/abi/functions.gen.ts +154 -0
- package/src/abi/index.ts +30 -0
- package/src/abi/solana.gen.ts +138 -0
- package/src/burn.ts +37 -0
- package/src/client/evm/index.ts +199 -0
- package/src/client/index.ts +23 -0
- package/src/client/solana/index.ts +260 -0
- package/src/client/types.ts +162 -0
- package/src/codec.ts +77 -0
- package/src/config/codeHash.ts +9 -0
- package/src/config/generated.ts +746 -0
- package/src/config/index.ts +91 -0
- package/src/config/registry.gen.ts +118 -0
- package/src/config/solana-genesis.ts +7 -0
- package/src/ed25519.ts +31 -0
- package/src/errors.ts +270 -0
- package/src/events/index.ts +44 -0
- package/src/events/types.gen.ts +831 -0
- package/src/evm/abi.ts +292 -0
- package/src/evm/balances.ts +26 -0
- package/src/evm/chains.ts +167 -0
- package/src/evm/floor/abi.gen.ts +195 -0
- package/src/evm/floor/calls.ts +247 -0
- package/src/evm/floor/create.ts +68 -0
- package/src/evm/floor/events.ts +51 -0
- package/src/evm/floor/iface.ts +45 -0
- package/src/evm/items.ts +252 -0
- package/src/evm/keccak.ts +118 -0
- package/src/evm/multicall.ts +43 -0
- package/src/evm/router.ts +209 -0
- package/src/evm/zap.ts +152 -0
- package/src/fixtures.ts +53 -0
- package/src/generated/index.ts +29 -0
- package/src/generated/interface.gen.ts +153 -0
- package/src/handle.ts +22 -0
- package/src/index.ts +91 -6
- package/src/indexer/bodies.ts +528 -0
- package/src/indexer/client.ts +178 -0
- package/src/indexer/contract.ts +410 -0
- package/src/indexer/decode.ts +109 -0
- package/src/indexer/partners.ts +109 -0
- package/src/indexer/paths.ts +190 -0
- package/src/indexer/read.ts +119 -0
- package/src/indexer/stream.ts +83 -0
- package/src/lock.ts +16 -0
- package/src/math/fixed.ts +154 -0
- package/src/math/genesis.ts +39 -0
- package/src/math/index.ts +24 -0
- package/src/math/u256.ts +49 -0
- package/src/math/v2/ask.ts +218 -0
- package/src/math/v2/book.ts +660 -0
- package/src/math/v2/circulating.ts +69 -0
- package/src/math/v2/create.ts +139 -0
- package/src/math/v2/display.ts +250 -0
- package/src/math/v2/error.ts +19 -0
- package/src/math/v2/feasibility.ts +200 -0
- package/src/math/v2/fees.ts +121 -0
- package/src/math/v2/holders.ts +124 -0
- package/src/math/v2/milestones.ts +51 -0
- package/src/math/v2/pow.ts +71 -0
- package/src/math/v2/preset.ts +189 -0
- package/src/math/v2/receipt.ts +162 -0
- package/src/math/v2/relay.ts +235 -0
- package/src/math/v2/rows.ts +412 -0
- package/src/math/v2/split.ts +160 -0
- package/src/math/v2/sqrt.ts +86 -0
- package/src/math/v2/supply.ts +27 -0
- package/src/math/v2/team.ts +75 -0
- package/src/math/v2/trade.ts +145 -0
- package/src/math/v2/wide.ts +52 -0
- package/src/quote.ts +6 -0
- package/src/registryPrice.ts +28 -0
- package/src/route/edge-jupiter.ts +71 -0
- package/src/route/edge.ts +22 -0
- package/src/route/graph.ts +77 -0
- package/src/route/path.ts +84 -0
- package/src/route/quote.ts +46 -0
- package/src/route-jupiter.ts +36 -0
- package/src/route.ts +8 -0
- package/src/sha256.ts +61 -0
- package/src/solana/alt.ts +25 -0
- package/src/solana/floor/accounts.ts +393 -0
- package/src/solana/floor/borsh.ts +66 -0
- package/src/solana/floor/create.ts +89 -0
- package/src/solana/floor/events.ts +147 -0
- package/src/solana/floor/genesis.ts +111 -0
- package/src/solana/floor/genesisList.ts +75 -0
- package/src/solana/floor/instructions.ts +491 -0
- package/src/solana/floor/metadata.ts +246 -0
- package/src/solana/floor/mint.ts +242 -0
- package/src/solana/floor/program.ts +64 -0
- package/src/solana/floor/registry.ts +96 -0
- package/src/solana/instruction.ts +61 -0
- package/src/solana/pda.ts +91 -0
- package/src/solana/tx.ts +143 -0
- package/src/v2/burnPreview.ts +52 -0
- package/src/v2/circulating.ts +22 -0
- package/src/v2/claimAllEvm.ts +97 -0
- package/src/v2/claims-evm.ts +179 -0
- package/src/v2/claims.ts +288 -0
- package/src/v2/dead.ts +21 -0
- package/src/v2/devbuy.ts +55 -0
- package/src/v2/events.ts +131 -0
- package/src/v2/evm.ts +349 -0
- package/src/v2/evmReads.ts +121 -0
- package/src/v2/genesis.ts +80 -0
- package/src/v2/layouts.ts +219 -0
- package/src/v2/market-evm.ts +95 -0
- package/src/v2/presetRowEvm.ts +54 -0
- package/src/v2/press.ts +124 -0
- package/src/v2/quoteMint.ts +63 -0
- package/src/v2/receipt-tx.ts +251 -0
- package/src/v2/route-alt.ts +114 -0
- package/src/v2/route.ts +494 -0
- package/src/v2/solana.ts +439 -0
- package/src/v2/solanaSettle.ts +117 -0
- package/src/v2/tx/evm.ts +152 -0
- package/src/v2/tx/genesis.ts +66 -0
- package/src/v2/tx/index.ts +5 -0
- package/src/v2/tx/solana.ts +140 -0
- package/src/v2/types.ts +284 -0
- package/dist/adapter.d.ts +0 -28
- package/dist/adapter.d.ts.map +0 -1
- package/dist/adapter.js +0 -2
- package/dist/adapter.js.map +0 -1
- package/dist/chain.d.ts +0 -23
- package/dist/chain.d.ts.map +0 -1
- package/dist/chain.js +0 -25
- package/dist/chain.js.map +0 -1
- package/dist/client.d.ts +0 -26
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -43
- package/dist/client.js.map +0 -1
- package/dist/dex-adapter.d.ts +0 -228
- package/dist/dex-adapter.d.ts.map +0 -1
- package/dist/dex-adapter.js +0 -407
- package/dist/dex-adapter.js.map +0 -1
- package/dist/index.d.ts +0 -7
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/quotes.d.ts +0 -12
- package/dist/quotes.d.ts.map +0 -1
- package/dist/quotes.gen.d.ts +0 -8
- package/dist/quotes.gen.d.ts.map +0 -1
- package/dist/quotes.gen.js +0 -2170
- package/dist/quotes.gen.js.map +0 -1
- package/dist/quotes.js +0 -24
- package/dist/quotes.js.map +0 -1
- package/dist/types.d.ts +0 -111
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/src/adapter.ts +0 -20
- package/src/chain.ts +0 -39
- package/src/client.ts +0 -41
- package/src/dex-adapter.ts +0 -477
- package/src/quotes.gen.ts +0 -2170
- package/src/quotes.ts +0 -29
- package/src/types.ts +0 -115
package/src/v2/solana.ts
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { addressBytes } from "../codec.ts";
|
|
4
|
+
import { isOnCurve } from "../ed25519.ts";
|
|
5
|
+
import { R, RS, W, WS, instructionDiscriminator } from "../solana/instruction.ts";
|
|
6
|
+
import type { AccountMeta, Instruction } from "../solana/instruction.ts";
|
|
7
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, SYSTEM_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, WSOL_MINT, associatedTokenAddress, findProgramAddress } from "../solana/pda.ts";
|
|
8
|
+
import type { Pda } from "../solana/pda.ts";
|
|
9
|
+
import { FLOOR_AMM_ERRORS, FLOOR_AMM_PROGRAM_ID, INSTRUCTIONS_SYSVAR_ID, METADATA_PROGRAM_ID, configPda, floorEventAuthorityPda, genesisConvertBandPda, genesisRaisePda, launchPda, teamSlotPda, traderLinkPda, traderRefPda, unwrapPda } from "../solana/floor/program.ts";
|
|
10
|
+
import { Writer } from "../solana/floor/borsh.ts";
|
|
11
|
+
import { CLAIM_CREATOR, CLAIM_CREATOR_REF, CLAIM_EXTRA, CLAIM_KNOWN_WIDE, CLAIM_TEAM_BUY, CLAIM_TEAM_SLOT, CLAIM_TRADER_REF, CLAIM_SHARE_BOUGHT, MAX_NAME_LEN, MAX_SYMBOL_LEN, createMintAccounts, withdrawPlatformManyInstruction } from "../solana/floor/instructions.ts";
|
|
12
|
+
import type { ClaimLaunch, TradeAccounts } from "../solana/floor/instructions.ts";
|
|
13
|
+
import { MEMBERS_MAX, ROW_LEN } from "./layouts.ts";
|
|
14
|
+
import { freshRegistryPrice } from "../registryPrice.ts";
|
|
15
|
+
import type { RegistryPrice } from "../registryPrice.ts";
|
|
16
|
+
import { CLAIM_BITS, CLAIM_KNOWN_MASKS, PROGRAM_IDS } from "../generated/index.ts";
|
|
17
|
+
|
|
18
|
+
export const FLOOR_HOOK_PROGRAM_ID: string = PROGRAM_IDS.floorHook;
|
|
19
|
+
|
|
20
|
+
export const FLOOR_HOOK_PLACEHOLDER_ID = "824TastyTFiKGJ8HteV5NHDkqcAcGVAn4L9JT8ZSdAE4";
|
|
21
|
+
export function isFloorHookPlaceholder(id: string): boolean {
|
|
22
|
+
return id === FLOOR_HOOK_PLACEHOLDER_ID;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Ctx {
|
|
26
|
+
programId?: string;
|
|
27
|
+
hookProgramId?: string;
|
|
28
|
+
}
|
|
29
|
+
const pid = (c?: Ctx): string => c?.programId ?? FLOOR_AMM_PROGRAM_ID;
|
|
30
|
+
const hid = (c?: Ctx): string => c?.hookProgramId ?? FLOOR_HOOK_PROGRAM_ID;
|
|
31
|
+
const k = addressBytes;
|
|
32
|
+
|
|
33
|
+
export const holderBookPda = (launch: string, hook = FLOOR_HOOK_PROGRAM_ID): Pda => findProgramAddress(["holder_book", k(launch)], hook);
|
|
34
|
+
export const holderPositionPda = (launch: string, owner: string, hook = FLOOR_HOOK_PROGRAM_ID): Pda => findProgramAddress(["holder", k(launch), k(owner)], hook);
|
|
35
|
+
export const deadMarkPda = (launch: string, tokenAccount: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["dead", k(launch), k(tokenAccount)], p);
|
|
36
|
+
|
|
37
|
+
export const extraAccountMetasPda = (baseMint: string, hook = FLOOR_HOOK_PROGRAM_ID): Pda => findProgramAddress(["extra-account-metas", k(baseMint)], hook);
|
|
38
|
+
|
|
39
|
+
const V2_ERROR_NAMES = [
|
|
40
|
+
"NothingToSettle", "HolderPositionExists", "HolderPositionNotEmpty", "HolderBookMismatch", "DeadAccountNotDead",
|
|
41
|
+
"HookAccountsMissing", "RowTooManyRanges", "RowNotFeasible",
|
|
42
|
+
] as const;
|
|
43
|
+
|
|
44
|
+
export const V2_ERROR_BASE = FLOOR_AMM_ERRORS.BurnTooLarge + 1;
|
|
45
|
+
export const FLOOR_AMM_ERRORS_V2: Readonly<Record<(typeof V2_ERROR_NAMES)[number], number>> = Object.freeze(
|
|
46
|
+
Object.fromEntries(V2_ERROR_NAMES.map((n, i) => [n, V2_ERROR_BASE + i])) as Record<(typeof V2_ERROR_NAMES)[number], number>,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
export const FLOOR_HOOK_ERRORS = Object.freeze({
|
|
50
|
+
NotTransferring: 0, WrongMint: 1, WrongProgram: 2, WrongAccount: 3, PositionNotWritable: 4, AlreadyOpen: 5, NotEmpty: 6, MetasExist: 7,
|
|
51
|
+
NotLaunchSigner: 8, HookAuthoritySet: 9, Math: 10, OwnerNotImmutable: 11,
|
|
52
|
+
|
|
53
|
+
NotTheOwnersAta: 12,
|
|
54
|
+
|
|
55
|
+
OffCurveOwner: 13,
|
|
56
|
+
} as const);
|
|
57
|
+
|
|
58
|
+
export const canHoldPosition = (owner: string): boolean => isOnCurve(k(owner));
|
|
59
|
+
|
|
60
|
+
export const TRANSFER_HOOK_EXECUTE_DISCRIMINATOR = Uint8Array.from([105, 37, 101, 197, 75, 251, 102, 26]);
|
|
61
|
+
|
|
62
|
+
export function hookAccountsFor(p: { baseMint: string; srcOwner: string; dstOwner: string } & Ctx): AccountMeta[] {
|
|
63
|
+
const hook = hid(p);
|
|
64
|
+
const launch = launchPda(p.baseMint, pid(p))[0];
|
|
65
|
+
return [
|
|
66
|
+
R(extraAccountMetasPda(p.baseMint, hook)[0]),
|
|
67
|
+
R(launch),
|
|
68
|
+
W(holderBookPda(launch, hook)[0]),
|
|
69
|
+
W(holderPositionPda(launch, p.srcOwner, hook)[0]),
|
|
70
|
+
W(holderPositionPda(launch, p.dstOwner, hook)[0]),
|
|
71
|
+
R(hook),
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function transferCheckedWithHookInstruction(p: { source: string; baseMint: string; destination: string; authority: string; srcOwner?: string; dstOwner: string; amount: bigint; decimals: number } & Ctx): Instruction {
|
|
76
|
+
return {
|
|
77
|
+
programAddress: TOKEN_2022_PROGRAM_ID,
|
|
78
|
+
accounts: [W(p.source), R(p.baseMint), W(p.destination), RS(p.authority), ...hookAccountsFor({ baseMint: p.baseMint, srcOwner: p.srcOwner ?? p.authority, dstOwner: p.dstOwner, programId: p.programId, hookProgramId: p.hookProgramId })],
|
|
79
|
+
data: new Writer().u8(12).u64(p.amount).u8(p.decimals).done(),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export const HOOK_IX_RESYNC = new TextEncoder().encode("resync__");
|
|
84
|
+
|
|
85
|
+
export function resyncInstruction(p: { baseMint: string; owner: string; ownerBase?: string } & Ctx): Instruction {
|
|
86
|
+
const hook = hid(p);
|
|
87
|
+
const launch = launchPda(p.baseMint, pid(p))[0];
|
|
88
|
+
return {
|
|
89
|
+
programAddress: hook,
|
|
90
|
+
accounts: [W(holderBookPda(launch, hook)[0]), W(holderPositionPda(launch, p.owner, hook)[0]), R(p.ownerBase ?? associatedTokenAddress(p.owner, p.baseMint, TOKEN_2022_PROGRAM_ID))],
|
|
91
|
+
data: HOOK_IX_RESYNC.slice(),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function burnCheckedInstruction(p: { account: string; baseMint: string; owner: string; amount: bigint; decimals: number }): Instruction {
|
|
96
|
+
if (p.amount <= 0n) throw new RangeError("burn: the amount must be > 0");
|
|
97
|
+
return { programAddress: TOKEN_2022_PROGRAM_ID, accounts: [W(p.account), W(p.baseMint), RS(p.owner)], data: new Writer().u8(15).u64(p.amount).u8(p.decimals).done() };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const evPair = (c?: Ctx): AccountMeta[] => [R(floorEventAuthorityPda(pid(c))[0]), R(pid(c))];
|
|
101
|
+
|
|
102
|
+
const hookProg = (c?: Ctx): AccountMeta => R(hid(c));
|
|
103
|
+
const opt = (a: string | null | undefined, c: Ctx | undefined, role: (x: string) => AccountMeta): AccountMeta => (a ? role(a) : R(pid(c)));
|
|
104
|
+
function ix(name: string, accounts: AccountMeta[], args: (w: Writer) => Writer, c?: Ctx, events = true): Instruction {
|
|
105
|
+
return { programAddress: pid(c), accounts: [...accounts, ...(events ? evPair(c) : [])], data: args(new Writer().bytes(instructionDiscriminator(name))).done() };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function creditBackingInstructionV2(p: { donor: string; launch: string; baseMint: string; quoteMint: string; donorQuote: string; quoteVault: string; quoteTokenProgram: string; amount: bigint; quoteIsLaunch?: boolean } & Ctx): Instruction {
|
|
109
|
+
if (p.amount <= 0n) throw new RangeError("credit_backing: the amount must be > 0");
|
|
110
|
+
const base = ix("credit_backing", [RS(p.donor), W(p.launch), R(p.baseMint), R(p.quoteMint), W(p.donorQuote), W(p.quoteVault), R(p.quoteTokenProgram)], (w) => w.u64(p.amount), p);
|
|
111
|
+
if (!p.quoteIsLaunch) return base;
|
|
112
|
+
return { ...base, accounts: [...base.accounts, ...hookAccountsFor({ baseMint: p.quoteMint, srcOwner: p.donor, dstOwner: p.launch, programId: p.programId, hookProgramId: p.hookProgramId })] };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export const SYNC_DEAD_MAX_PAIRS = 8;
|
|
116
|
+
|
|
117
|
+
export function syncDeadInstruction(p: { payer: string; launch: string; baseMint: string; deadTokenAccounts: readonly string[] } & Ctx): Instruction {
|
|
118
|
+
const n = p.deadTokenAccounts.length;
|
|
119
|
+
if (n < 1 || n > SYNC_DEAD_MAX_PAIRS) throw new RangeError(`sync_dead takes 1..=${SYNC_DEAD_MAX_PAIRS} dead token accounts`);
|
|
120
|
+
const base = ix("sync_dead", [WS(p.payer), W(p.launch), R(p.baseMint), R(SYSTEM_PROGRAM_ID)], (w) => w, p);
|
|
121
|
+
const pairs = p.deadTokenAccounts.flatMap((a) => [R(a), W(deadMarkPda(p.launch, a, pid(p))[0])]);
|
|
122
|
+
return { ...base, accounts: [...base.accounts, ...pairs] };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function openHolderPositionInstruction(p: { owner: string; launch: string; ownerBase: string } & Ctx): Instruction {
|
|
126
|
+
if (!canHoldPosition(p.owner)) throw new RangeError("open_holder_position: the owner is off the ed25519 curve (OffCurveOwner 13): a PDA or vault holds no position");
|
|
127
|
+
const hook = hid(p);
|
|
128
|
+
return ix("open_holder_position", [WS(p.owner), R(p.launch), W(holderBookPda(p.launch, hook)[0]), W(holderPositionPda(p.launch, p.owner, hook)[0]), R(p.ownerBase), R(SYSTEM_PROGRAM_ID), hookProg(p)], (w) => w, p);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function holderAccountWarning(p: { owner: string; baseMint: string; accounts: readonly { address: string; amount: bigint }[] }): { idle: { address: string; amount: bigint }[]; idleAmount: bigint; message: string | null } {
|
|
132
|
+
const ata = associatedTokenAddress(p.owner, p.baseMint, TOKEN_2022_PROGRAM_ID);
|
|
133
|
+
const idle = p.accounts.filter((a) => a.address !== ata && a.amount > 0n);
|
|
134
|
+
const idleAmount = idle.reduce((s, a) => s + a.amount, 0n);
|
|
135
|
+
return {
|
|
136
|
+
idle,
|
|
137
|
+
idleAmount,
|
|
138
|
+
message: idle.length
|
|
139
|
+
? "Only your main token account earns Holder Fees and Holder Milestones. Tokens in another account of this wallet earn nothing until they move back."
|
|
140
|
+
: null,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function closeHolderPositionInstruction(p: { owner: string; launch: string; ownerBase: string; quoteVault: string; ownerQuote: string; quoteMint: string; quoteTokenProgram: string; quoteIsLaunch?: boolean } & Ctx): Instruction {
|
|
145
|
+
const hook = hid(p);
|
|
146
|
+
|
|
147
|
+
const base = ix(
|
|
148
|
+
"close_holder_position",
|
|
149
|
+
[WS(p.owner), W(p.launch), W(holderBookPda(p.launch, hook)[0]), W(holderPositionPda(p.launch, p.owner, hook)[0]), R(p.ownerBase), W(p.quoteVault), W(p.ownerQuote), R(p.quoteMint), R(p.quoteTokenProgram), hookProg(p)],
|
|
150
|
+
(w) => w,
|
|
151
|
+
p,
|
|
152
|
+
);
|
|
153
|
+
if (!p.quoteIsLaunch) return base;
|
|
154
|
+
return { ...base, accounts: [...base.accounts, ...hookAccountsFor({ baseMint: p.quoteMint, srcOwner: p.launch, dstOwner: p.owner, programId: p.programId, hookProgramId: p.hookProgramId })] };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export const PRESET_CHUNK_MAX = 900;
|
|
158
|
+
|
|
159
|
+
export function writePresetChunkInstruction(p: { authority: string; config: string; preset: string; id: number; offset: number; data: Uint8Array } & Ctx): Instruction {
|
|
160
|
+
if (p.offset < 0 || p.data.length === 0 || p.offset + p.data.length > ROW_LEN) throw new RangeError(`a chunk lies inside the ${ROW_LEN}-byte row`);
|
|
161
|
+
return ix("write_preset_chunk", [WS(p.authority), R(p.config), W(p.preset), R(SYSTEM_PROGRAM_ID)], (w) => w.u8(p.id).u16(p.offset).u32(p.data.length).bytes(p.data), p, false);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function presetV2Instruction(p: { name: "add_preset" | "update_preset" | "add_genesis_preset"; authority: string; config: string; preset: string; id: number } & Ctx): Instruction {
|
|
165
|
+
|
|
166
|
+
const accts = p.name === "update_preset" ? [RS(p.authority), R(p.config), W(p.preset)] : [WS(p.authority), W(p.config), W(p.preset), R(SYSTEM_PROGRAM_ID)];
|
|
167
|
+
return ix(p.name, accts, (w) => w.u8(p.id), p);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function presetV2Instructions(p: { name: "add_preset" | "update_preset" | "add_genesis_preset"; authority: string; config: string; preset: string; id: number; row: Uint8Array; chunk?: number } & Ctx): Instruction[] {
|
|
171
|
+
if (p.row.length !== ROW_LEN) throw new RangeError(`a v2 row is ${ROW_LEN} bytes`);
|
|
172
|
+
const n = p.chunk ?? PRESET_CHUNK_MAX;
|
|
173
|
+
if (n < 1) throw new RangeError("chunk must be > 0");
|
|
174
|
+
const out: Instruction[] = [];
|
|
175
|
+
for (let o = 0; o < ROW_LEN; o += n) out.push(writePresetChunkInstruction({ ...p, offset: o, data: p.row.slice(o, Math.min(o + n, ROW_LEN)) }));
|
|
176
|
+
out.push(presetV2Instruction(p));
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export type TeamShareBuyBaseV2 = { member: string; launch: string; baseMint: string; quoteMint: string; tokenVault: string; quoteVault: string; teamSlot?: string; memberBase: string } & Ctx;
|
|
181
|
+
export function teamShareBuyInstructionV2(p: TeamShareBuyBaseV2 & { amountIn: bigint; minOut: bigint }): Instruction {
|
|
182
|
+
const base = ix(
|
|
183
|
+
"team_share_buy",
|
|
184
|
+
[WS(p.member), W(p.launch), R(p.baseMint), R(p.quoteMint), W(p.tokenVault), R(p.quoteVault), R(p.teamSlot ?? teamSlotPda(p.launch, pid(p))[0]), W(p.memberBase), R(TOKEN_2022_PROGRAM_ID)],
|
|
185
|
+
(w) => w.u64(p.amountIn).u64(minOut(p.minOut, "minOut")),
|
|
186
|
+
p,
|
|
187
|
+
);
|
|
188
|
+
return { ...base, accounts: [...base.accounts, ...hookAccountsFor({ baseMint: p.baseMint, srcOwner: p.launch, dstOwner: p.member, programId: p.programId, hookProgramId: p.hookProgramId })] };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const REM_HOOK_AT = 5;
|
|
192
|
+
|
|
193
|
+
export interface TradeAccountsV2 extends TradeAccounts {
|
|
194
|
+
|
|
195
|
+
baseOwner?: string;
|
|
196
|
+
|
|
197
|
+
quoteIsLaunch?: boolean;
|
|
198
|
+
|
|
199
|
+
quoteOwner?: string;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export const REM_QUOTE_HOOK_AT = 11;
|
|
203
|
+
|
|
204
|
+
export const REM_REF_POSITION_AT = 17;
|
|
205
|
+
|
|
206
|
+
function tradeMetas(a: TradeAccountsV2, c?: Ctx): AccountMeta[] {
|
|
207
|
+
return [
|
|
208
|
+
WS(a.trader), W(a.launch), R(a.baseMint), R(a.quoteMint), W(a.tokenVault), W(a.quoteVault), W(a.traderBase), W(a.traderQuote), R(TOKEN_2022_PROGRAM_ID), R(a.quoteTokenProgram),
|
|
209
|
+
opt(a.traderRef, c, W), opt(a.escrow, c, W),
|
|
210
|
+
|
|
211
|
+
];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function remMetas(a: TradeAccountsV2, isBuy: boolean, c?: Ctx): AccountMeta[] {
|
|
215
|
+
const rem: AccountMeta[] = [];
|
|
216
|
+
if (a.user) {
|
|
217
|
+
const wallet = a.linkWallet ?? a.user;
|
|
218
|
+
rem.push(WS(a.user), R(traderLinkPda(wallet, c?.programId)[0]), R(SYSTEM_PROGRAM_ID), R(ASSOCIATED_TOKEN_PROGRAM_ID));
|
|
219
|
+
if (a.recipient && a.recipient !== wallet) rem.push(R(traderLinkPda(a.recipient, c?.programId)[0]));
|
|
220
|
+
}
|
|
221
|
+
while (rem.length < REM_HOOK_AT) rem.push(R(pid(c)));
|
|
222
|
+
const holder = a.baseOwner ?? a.trader;
|
|
223
|
+
|
|
224
|
+
const [srcOwner, dstOwner] = isBuy ? [a.launch, holder] : [holder, a.launch];
|
|
225
|
+
const out = [...rem, ...hookAccountsFor({ baseMint: a.baseMint, srcOwner, dstOwner, programId: c?.programId, hookProgramId: c?.hookProgramId })];
|
|
226
|
+
if (!a.quoteIsLaunch) return out;
|
|
227
|
+
|
|
228
|
+
const payer = a.quoteOwner ?? a.trader;
|
|
229
|
+
const [qSrc, qDst] = isBuy ? [payer, a.launch] : [a.launch, payer];
|
|
230
|
+
const q = [...out, ...hookAccountsFor({ baseMint: a.quoteMint, srcOwner: qSrc, dstOwner: qDst, programId: c?.programId, hookProgramId: c?.hookProgramId })];
|
|
231
|
+
|
|
232
|
+
if (!a.traderRef || !a.escrow) return q;
|
|
233
|
+
return [...q, W(holderPositionPda(launchPda(a.quoteMint, pid(c))[0], a.traderRef, hid(c))[0])];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const minOut = (v: bigint, what: string): bigint => {
|
|
237
|
+
if (v <= 0n) throw new RangeError(`${what} must be > 0 (the NO_MINIMUM rule)`);
|
|
238
|
+
return v;
|
|
239
|
+
};
|
|
240
|
+
function trade(name: string, a: TradeAccountsV2 & Ctx, isBuy: boolean, x: bigint, m: bigint, what: string): Instruction {
|
|
241
|
+
const base = ix(name, tradeMetas(a, a), (w) => w.u64(x).u64(minOut(m, what)), a);
|
|
242
|
+
return { ...base, accounts: [...base.accounts, ...remMetas(a, isBuy, a)] };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export type BuyArgsV2 = TradeAccountsV2 & Ctx & { amountIn: bigint; minOut: bigint };
|
|
246
|
+
export type SellArgsV2 = TradeAccountsV2 & Ctx & { tokensIn: bigint; minOut: bigint };
|
|
247
|
+
export const buyInstructionV2 = (p: BuyArgsV2): Instruction => trade("buy", p, true, p.amountIn, p.minOut, "minOut");
|
|
248
|
+
export const buyFromBalanceInstructionV2 = (p: TradeAccountsV2 & Ctx & { keep: bigint; minOut: bigint }): Instruction => trade("buy_from_balance", p, true, p.keep, p.minOut, "minOut");
|
|
249
|
+
|
|
250
|
+
export const sellInstructionV2 = (p: SellArgsV2): Instruction => trade("sell", p, false, p.tokensIn, p.minOut, "minOut");
|
|
251
|
+
export const sellFromBalanceInstructionV2 = (p: TradeAccountsV2 & Ctx & { keep: bigint; minOut: bigint }): Instruction => trade("sell_from_balance", p, false, p.keep, p.minOut, "minOut");
|
|
252
|
+
|
|
253
|
+
export function buyWithPositionInstructions(p: BuyArgsV2 & { hasPosition: boolean }): Instruction[] {
|
|
254
|
+
const buy = buyInstructionV2(p);
|
|
255
|
+
if (p.hasPosition) return [buy];
|
|
256
|
+
const owner = p.baseOwner ?? p.trader;
|
|
257
|
+
if (owner !== p.trader) return [buy];
|
|
258
|
+
if (!canHoldPosition(owner)) return [buy];
|
|
259
|
+
return [buy, openHolderPositionInstruction({ owner, launch: p.launch, ownerBase: p.traderBase, programId: p.programId, hookProgramId: p.hookProgramId })];
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export interface CreateArgsV2 {
|
|
263
|
+
name: string;
|
|
264
|
+
symbol: string;
|
|
265
|
+
|
|
266
|
+
creatorBuy?: bigint;
|
|
267
|
+
creatorBuyMinTokens?: bigint;
|
|
268
|
+
quoteUsdCentsX64: bigint;
|
|
269
|
+
|
|
270
|
+
teamSlotRateBps?: number;
|
|
271
|
+
|
|
272
|
+
mintNonce: bigint;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export function createLaunchInstructionV2(p: {
|
|
276
|
+
creator: string; config: string; preset: string; quote: string; quoteMint: string; baseMint?: string; launch?: string; tokenVault?: string;
|
|
277
|
+
quoteVault?: string; creatorQuote: string; metadata?: string;
|
|
278
|
+
|
|
279
|
+
creatorRef?: string | null; creatorLink?: string; quoteTokenProgram: string; args: CreateArgsV2;
|
|
280
|
+
metadataProgram?: string;
|
|
281
|
+
|
|
282
|
+
quoteIsLaunch?: boolean;
|
|
283
|
+
|
|
284
|
+
teamBuyMax?: bigint;
|
|
285
|
+
|
|
286
|
+
teamSlotMaxBps: number;
|
|
287
|
+
|
|
288
|
+
registry?: { entry: RegistryPrice; now: bigint };
|
|
289
|
+
} & Ctx): Instruction {
|
|
290
|
+
const enc = new TextEncoder();
|
|
291
|
+
if (p.registry && p.args.quoteUsdCentsX64 === 0n) freshRegistryPrice(p.registry.entry, p.registry.now);
|
|
292
|
+
if (enc.encode(p.args.name).length > MAX_NAME_LEN || enc.encode(p.args.symbol).length > MAX_SYMBOL_LEN) throw new RangeError("name ≤ 32, symbol ≤ 10 bytes");
|
|
293
|
+
const buy = p.args.creatorBuy ?? 0n;
|
|
294
|
+
const minTok = p.args.creatorBuyMinTokens ?? 0n;
|
|
295
|
+
if (buy < 0n || minTok < 0n) throw new RangeError("the Team Buy and its minimum are ≥ 0");
|
|
296
|
+
if (buy === 0n && minTok > 0n) throw new RangeError("a minimum without a Team Buy");
|
|
297
|
+
if (p.teamBuyMax !== undefined && buy > p.teamBuyMax) throw new RangeError(`the Team Buy ${buy} is over the row's share ${p.teamBuyMax} (CreatorBuyTooLarge)`);
|
|
298
|
+
const rate = p.teamSlotMaxBps;
|
|
299
|
+
if (!Number.isInteger(rate) || rate < 0 || rate > 0xffff) throw new RangeError(`the row's Team Slot max is a u16: ${rate}`);
|
|
300
|
+
if (p.args.teamSlotRateBps !== undefined && p.args.teamSlotRateBps !== rate) {
|
|
301
|
+
throw new RangeError(`the Team Slot opens at the row's max ${rate}, never ${p.args.teamSlotRateBps} (TeamSlotRateOverMax)`);
|
|
302
|
+
}
|
|
303
|
+
const prog = pid(p);
|
|
304
|
+
const m = createMintAccounts({ ...p, programId: prog });
|
|
305
|
+
const base = ix(
|
|
306
|
+
"create_launch",
|
|
307
|
+
[
|
|
308
|
+
WS(p.creator), W(p.config), R(p.preset), W(p.quote), R(p.quoteMint), W(m.baseMint), W(m.launch), W(m.tokenVault), W(m.quoteVault), W(p.creatorQuote),
|
|
309
|
+
W(m.metadata), R(p.metadataProgram ?? METADATA_PROGRAM_ID), R(p.creatorRef ?? traderRefPda(p.creator, p.quoteMint, prog)[0]),
|
|
310
|
+
R(p.creatorLink ?? traderLinkPda(p.creator, prog)[0]), R(TOKEN_2022_PROGRAM_ID), R(p.quoteTokenProgram), R(ASSOCIATED_TOKEN_PROGRAM_ID),
|
|
311
|
+
R(SYSTEM_PROGRAM_ID), R(INSTRUCTIONS_SYSVAR_ID), W(teamSlotPda(m.launch, prog)[0]),
|
|
312
|
+
W(holderBookPda(m.launch, hid(p))[0]), W(extraAccountMetasPda(m.baseMint, hid(p))[0]), hookProg(p),
|
|
313
|
+
],
|
|
314
|
+
(w) => w.string(p.args.name).string(p.args.symbol).u64(buy).u64(minTok).u128(p.args.quoteUsdCentsX64).u16(rate).u64(m.mintNonce),
|
|
315
|
+
p,
|
|
316
|
+
);
|
|
317
|
+
if (!p.quoteIsLaunch) return base;
|
|
318
|
+
const quoteHook = hookAccountsFor({ baseMint: p.quoteMint, srcOwner: p.creator, dstOwner: m.launch, programId: p.programId, hookProgramId: p.hookProgramId });
|
|
319
|
+
return { ...base, accounts: [...base.accounts, ...quoteHook] };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const vaultPayoutHook = (baseMint: string, launch: string, wallet: string, c?: Ctx): AccountMeta[] =>
|
|
323
|
+
hookAccountsFor({ baseMint, srcOwner: launch, dstOwner: wallet, programId: c?.programId, hookProgramId: c?.hookProgramId });
|
|
324
|
+
|
|
325
|
+
export function unlockCreatorBuyInstructionV2(p: { sender: string; launch: string; creator: string; baseMint: string; tokenVault: string; destination?: string } & Ctx): Instruction {
|
|
326
|
+
const dest = p.destination ?? associatedTokenAddress(p.creator, p.baseMint, TOKEN_2022_PROGRAM_ID);
|
|
327
|
+
const base = ix(
|
|
328
|
+
"unlock_creator_buy",
|
|
329
|
+
[WS(p.sender), W(p.launch), R(p.creator), R(p.baseMint), W(p.tokenVault), W(dest), R(TOKEN_2022_PROGRAM_ID), R(ASSOCIATED_TOKEN_PROGRAM_ID), R(SYSTEM_PROGRAM_ID)],
|
|
330
|
+
(w) => w,
|
|
331
|
+
p,
|
|
332
|
+
);
|
|
333
|
+
return { ...base, accounts: [...base.accounts, ...vaultPayoutHook(p.baseMint, p.launch, p.creator, p)] };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function burnTokensInstructionV2(p: { holder: string; launch: string; teamSlot?: string | null; baseMint: string; tokenVault: string; place: number; teamAmount: bigint; walletAmount: bigint } & Ctx): Instruction {
|
|
337
|
+
if (!Number.isInteger(p.place) || p.place < 0 || p.place >= MEMBERS_MAX) throw new RangeError(`burn_tokens: place is 0..${MEMBERS_MAX}`);
|
|
338
|
+
if (p.teamAmount < 0n || p.walletAmount < 0n || (p.teamAmount === 0n && p.walletAmount === 0n)) throw new RangeError("burn_tokens: nothing to burn");
|
|
339
|
+
const hook = hid(p);
|
|
340
|
+
return ix(
|
|
341
|
+
"burn_tokens",
|
|
342
|
+
[
|
|
343
|
+
RS(p.holder), W(p.launch), R(p.teamSlot ?? p.launch), W(p.baseMint), W(p.tokenVault), W(associatedTokenAddress(p.holder, p.baseMint, TOKEN_2022_PROGRAM_ID)), R(TOKEN_2022_PROGRAM_ID),
|
|
344
|
+
W(holderBookPda(p.launch, hook)[0]), W(holderPositionPda(p.launch, p.holder, hook)[0]), hookProg(p),
|
|
345
|
+
],
|
|
346
|
+
(w) => w.u8(p.place).u64(p.teamAmount).u64(p.walletAmount),
|
|
347
|
+
p,
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export function quoteHookBlock(quoteMint: string, receiver: string, payers: readonly string[], p: Ctx = {}): AccountMeta[] {
|
|
352
|
+
const hook = hid(p);
|
|
353
|
+
const ql = launchPda(quoteMint, pid(p))[0];
|
|
354
|
+
return [
|
|
355
|
+
R(extraAccountMetasPda(quoteMint, hook)[0]),
|
|
356
|
+
R(ql),
|
|
357
|
+
W(holderBookPda(ql, hook)[0]),
|
|
358
|
+
W(holderPositionPda(ql, receiver, hook)[0]),
|
|
359
|
+
R(hook),
|
|
360
|
+
...payers.map((x) => W(holderPositionPda(ql, x, hook)[0])),
|
|
361
|
+
];
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export function withdrawPlatformManyInstructionV2(p: Parameters<typeof withdrawPlatformManyInstruction>[0] & { quoteIsLaunch?: boolean; treasury?: string }): Instruction {
|
|
365
|
+
if (!p.quoteIsLaunch || p.launches.length === 0) return withdrawPlatformManyInstruction(p);
|
|
366
|
+
if (!p.treasury) throw new RangeError("withdraw_platform_many: a launch-token quote needs the treasury (its position receives)");
|
|
367
|
+
return withdrawPlatformManyInstruction({ ...p, quoteHook: quoteHookBlock(p.quoteMint, p.treasury, p.launches.map((l) => l.launch), p) });
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export const CLAIM_HOLDER = CLAIM_BITS.solana.holder;
|
|
371
|
+
|
|
372
|
+
const CLAIM_QUOTE_BITS = CLAIM_CREATOR | CLAIM_CREATOR_REF | CLAIM_TEAM_SLOT | CLAIM_TRADER_REF;
|
|
373
|
+
|
|
374
|
+
export const CLAIM_KNOWN_WIDE_V2 = CLAIM_KNOWN_MASKS.solana.v2;
|
|
375
|
+
|
|
376
|
+
export interface ClaimLaunchV2 extends ClaimLaunch {
|
|
377
|
+
|
|
378
|
+
holderOwner?: string;
|
|
379
|
+
|
|
380
|
+
holderBase?: string;
|
|
381
|
+
|
|
382
|
+
baseMint?: string;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export function claimInstructionV2(p: {
|
|
386
|
+
sender: string; wallet: string; quoteMint: string; quoteTokenProgram: string; destination: string; config: string;
|
|
387
|
+
refOwed: string | null; refOwedVault: string | null; types: number; launches: readonly ClaimLaunchV2[]; unwrap?: boolean;
|
|
388
|
+
quoteIsLaunch?: boolean;
|
|
389
|
+
} & Ctx): Instruction {
|
|
390
|
+
if (p.unwrap && p.quoteIsLaunch) throw new RangeError("unwrap: a launch-token quote is never wSOL");
|
|
391
|
+
if (p.unwrap && (p.quoteMint !== WSOL_MINT || p.quoteTokenProgram !== TOKEN_PROGRAM_ID)) throw new RangeError("unwrap: only a wSOL claim");
|
|
392
|
+
if (!Number.isInteger(p.types) || p.types <= 0 || (p.types & ~CLAIM_KNOWN_WIDE_V2) !== 0) throw new RangeError("UnknownClaimType: bits 1, 2, 4, 8, 128, 256, 512 (16, 32 and 64 are reserved)");
|
|
393
|
+
if (p.launches.length > 255) throw new RangeError("at most 255 launches a claim");
|
|
394
|
+
const tok = (p.types & (CLAIM_TEAM_BUY | CLAIM_SHARE_BOUGHT)) !== 0;
|
|
395
|
+
if (tok && p.launches.some((l) => !l.teamBuy)) throw new RangeError("bits 128 / 256: every launch needs its teamBuy accounts");
|
|
396
|
+
const holder = (p.types & CLAIM_HOLDER) !== 0;
|
|
397
|
+
const wide = p.types > 0xff;
|
|
398
|
+
const prog = pid(p);
|
|
399
|
+
const base = ix(
|
|
400
|
+
wide ? "claim_wide" : "claim",
|
|
401
|
+
[WS(p.sender), p.unwrap ? W(p.wallet) : R(p.wallet), R(p.quoteMint), R(p.quoteTokenProgram), W(p.destination), R(p.config), opt(p.refOwed, p, W), opt(p.refOwedVault, p, W), R(SYSTEM_PROGRAM_ID), R(ASSOCIATED_TOKEN_PROGRAM_ID)],
|
|
402
|
+
(w) => (wide ? w.pubkey(p.wallet).u16(p.types) : w.pubkey(p.wallet).u8(p.types)).u8(p.launches.length),
|
|
403
|
+
p,
|
|
404
|
+
);
|
|
405
|
+
const rest: AccountMeta[] = [];
|
|
406
|
+
for (const l of p.launches) {
|
|
407
|
+
rest.push(W(l.launch));
|
|
408
|
+
if (holder) {
|
|
409
|
+
const owner = l.holderOwner ?? p.wallet;
|
|
410
|
+
const mint = l.baseMint ?? l.teamBuy?.baseMint;
|
|
411
|
+
if (!l.holderBase && !mint) throw new RangeError("bit 512: every launch needs its holderBase or baseMint (Take's owner_base)");
|
|
412
|
+
rest.push(W(holderBookPda(l.launch, hid(p))[0]), W(holderPositionPda(l.launch, owner, hid(p))[0]), R(l.holderBase ?? associatedTokenAddress(owner, mint!, TOKEN_2022_PROGRAM_ID)));
|
|
413
|
+
}
|
|
414
|
+
rest.push(W(l.quoteVault), W(l.teamSlot ?? l.launch));
|
|
415
|
+
if (tok) rest.push(R(l.teamBuy!.baseMint), W(l.teamBuy!.tokenVault), W(l.teamBuy!.walletBase), ...vaultPayoutHook(l.teamBuy!.baseMint, l.launch, p.wallet, p));
|
|
416
|
+
}
|
|
417
|
+
if (tok) rest.push(R(TOKEN_2022_PROGRAM_ID));
|
|
418
|
+
if (holder) rest.push(hookProg(p));
|
|
419
|
+
if (p.unwrap) rest.push(W(unwrapPda(p.wallet, prog)[0]));
|
|
420
|
+
if (p.quoteIsLaunch && (p.types & (CLAIM_QUOTE_BITS | CLAIM_HOLDER)) !== 0) {
|
|
421
|
+
const payers = p.launches.map((l) => l.launch);
|
|
422
|
+
if ((p.types & CLAIM_TRADER_REF) !== 0 && p.refOwed) payers.push(p.refOwed);
|
|
423
|
+
rest.push(...quoteHookBlock(p.quoteMint, p.wallet, payers, p));
|
|
424
|
+
}
|
|
425
|
+
return { ...base, accounts: [...base.accounts, ...rest] };
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
export function setGenesisConvertBandInstruction(p: { authority: string; solPxMin: bigint; solPxMax: bigint; usdcPxMin: bigint; usdcPxMax: bigint; config?: string; genesisRaise?: string; convertBand?: string } & Ctx): Instruction {
|
|
429
|
+
for (const v of [p.solPxMin, p.solPxMax, p.usdcPxMin, p.usdcPxMax]) if (v <= 0n || v >> 128n !== 0n) throw new RangeError("set_genesis_convert_band: each price is a u128 > 0");
|
|
430
|
+
if (p.solPxMin > p.solPxMax || p.usdcPxMin > p.usdcPxMax) throw new RangeError("set_genesis_convert_band: min ≤ max");
|
|
431
|
+
const g = pid(p);
|
|
432
|
+
return ix(
|
|
433
|
+
"set_genesis_convert_band",
|
|
434
|
+
[WS(p.authority), R(p.config ?? configPda(g)[0]), R(p.genesisRaise ?? genesisRaisePda(g)[0]), W(p.convertBand ?? genesisConvertBandPda(g)[0]), R(SYSTEM_PROGRAM_ID)],
|
|
435
|
+
(w) => w.u128(p.solPxMin).u128(p.solPxMax).u128(p.usdcPxMin).u128(p.usdcPxMax),
|
|
436
|
+
p,
|
|
437
|
+
false,
|
|
438
|
+
);
|
|
439
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { RANGES_MAX, SOL_BUY_WALK_STEPS, cloneBook } from "../math/v2/book.ts";
|
|
4
|
+
import type { Book, Range } from "../math/v2/book.ts";
|
|
5
|
+
import type { Supply } from "../math/v2/supply.ts";
|
|
6
|
+
import type { SplitRow } from "../math/v2/split.ts";
|
|
7
|
+
import { buy } from "../math/v2/trade.ts";
|
|
8
|
+
import type { BookBuy } from "../math/v2/trade.ts";
|
|
9
|
+
import { MAX_BANDS, V2_VERSION } from "./layouts.ts";
|
|
10
|
+
import type { LaunchV2Account } from "./types.ts";
|
|
11
|
+
import { quoteBuyNetInV2, quoteReceivedV2 } from "./quoteMint.ts";
|
|
12
|
+
import type { QuoteTransferFeeV2 } from "./quoteMint.ts";
|
|
13
|
+
|
|
14
|
+
export const MATH_TOKEN_DECIMALS = 6;
|
|
15
|
+
|
|
16
|
+
export const SOLANA_TOKEN_DECIMALS = 6;
|
|
17
|
+
|
|
18
|
+
const DECIMALS_MAX = 24;
|
|
19
|
+
|
|
20
|
+
function scaleOf(decimals: number): bigint {
|
|
21
|
+
if (!Number.isInteger(decimals) || decimals < MATH_TOKEN_DECIMALS || decimals > DECIMALS_MAX) {
|
|
22
|
+
throw new RangeError(`token decimals ${decimals}: ${MATH_TOKEN_DECIMALS} ..= ${DECIMALS_MAX}`);
|
|
23
|
+
}
|
|
24
|
+
return 10n ** BigInt(decimals - MATH_TOKEN_DECIMALS);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function tokensToMathV2(chain: bigint, decimals: number): bigint {
|
|
28
|
+
if (chain < 0n) throw new RangeError(`tokens ${chain} < 0`);
|
|
29
|
+
return chain / scaleOf(decimals);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function tokensToChainV2(math: bigint, decimals: number): bigint {
|
|
33
|
+
if (math < 0n) throw new RangeError(`tokens ${math} < 0`);
|
|
34
|
+
return math * scaleOf(decimals);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface FeeCurveV2 {
|
|
38
|
+
feePpm: number[];
|
|
39
|
+
feeStartPpm: number[];
|
|
40
|
+
sqrtFeeA: bigint;
|
|
41
|
+
sqrtFeeB: bigint;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface SolanaSettleInputsV2 {
|
|
45
|
+
book: Book;
|
|
46
|
+
row: SplitRow;
|
|
47
|
+
sup: Supply;
|
|
48
|
+
|
|
49
|
+
fee: FeeCurveV2;
|
|
50
|
+
|
|
51
|
+
msCapPpm: number[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const n = (x: number | bigint): number => Number(x);
|
|
55
|
+
|
|
56
|
+
function liveSupplyV2(stored: bigint, mintSupply?: bigint): bigint {
|
|
57
|
+
if (mintSupply === undefined) return stored;
|
|
58
|
+
if (mintSupply < 0n) throw new RangeError(`mint supply ${mintSupply} < 0`);
|
|
59
|
+
return mintSupply < stored ? mintSupply : stored;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function solanaSettleInputsV2(l: LaunchV2Account, opts: { mintSupply?: bigint } = {}): SolanaSettleInputsV2 {
|
|
63
|
+
if (l.version !== V2_VERSION) throw new RangeError(`LaunchV2 version ${l.version}, want ${V2_VERSION}`);
|
|
64
|
+
if (l.decimals !== SOLANA_TOKEN_DECIMALS) throw new RangeError(`LaunchV2 decimals ${l.decimals}: a Solana launch token has ${SOLANA_TOKEN_DECIMALS}`);
|
|
65
|
+
const ranges: Range[] = Array.from({ length: RANGES_MAX }, (_, i) => ({
|
|
66
|
+
sqrtA: l.rngSqrtA[i], sqrtB: l.rngSqrtB[i], tokens: l.rngTokens[i], askLeft: l.rngAskLeft[i], back: l.rngBack[i],
|
|
67
|
+
place: l.rngPlace[i], thetaBps: n(l.rngThetaBps[i]), psiBps: n(l.rngPsiBps[i]),
|
|
68
|
+
kind: n(l.rngKind[i]), class: n(l.rngClass[i]), msCapPpm: n(l.rngMsCapPpm[i]),
|
|
69
|
+
}));
|
|
70
|
+
const book: Book = {
|
|
71
|
+
sqrtP: l.sqrtP, sqrtF: l.sqrtF, sqrtLMax: l.sqrtLMax, floor: l.floor, floorTokens: l.floorTokens,
|
|
72
|
+
nBands: l.nBands,
|
|
73
|
+
bandEdge: [...l.bandEdge], bandMoney: [...l.bandMoney], bandTokens: [...l.bandTokens],
|
|
74
|
+
nC: l.nC, bandF: [...l.bandF], bandFClaim: [...l.bandFClaim], ckAtF: l.ckAtF, ckFloorTokens: l.ckFloorTokens,
|
|
75
|
+
feePpm: l.feePpm.map(n), feeStartPpm: l.feeStartPpm.map(n), sqrtFeeA: l.sqrtFeeA, sqrtFeeB: l.sqrtFeeB,
|
|
76
|
+
graduated: l.graduated !== 0, nRanges: l.nRanges, ranges,
|
|
77
|
+
|
|
78
|
+
...(l.ownFees !== 0 && { ownFees: true }),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const row: SplitRow = {
|
|
82
|
+
phiBps: l.phiBps, ckBps: l.ckBps, wcBps: l.wcBps, cShareBps: 5_000,
|
|
83
|
+
msClass: l.msClass, platformMsBps: (l.flags & 1) !== 0 ? l.platformMsBps : 0, floorMsBps: (l.flags & 2) !== 0 ? l.floorMsBps : 0,
|
|
84
|
+
};
|
|
85
|
+
return {
|
|
86
|
+
book, row, sup: { live: liveSupplyV2(l.supply, opts.mintSupply), dead: l.deadTokens },
|
|
87
|
+
fee: { feePpm: [...book.feePpm], feeStartPpm: [...book.feeStartPpm], sqrtFeeA: book.sqrtFeeA, sqrtFeeB: book.sqrtFeeB },
|
|
88
|
+
msCapPpm: ranges.map((r) => r.msCapPpm),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const bookFromLaunchV2 = (l: LaunchV2Account): Book => solanaSettleInputsV2(l).book;
|
|
93
|
+
|
|
94
|
+
export function withFeeCurveV2<T extends { book: Book }>(inputs: T, curve: FeeCurveV2): T {
|
|
95
|
+
if (curve.feePpm.length !== 5 || curve.feeStartPpm.length !== 5) throw new RangeError("a fee curve has 5 legs");
|
|
96
|
+
const book = cloneBook(inputs.book);
|
|
97
|
+
book.feePpm = [...curve.feePpm];
|
|
98
|
+
book.feeStartPpm = [...curve.feeStartPpm];
|
|
99
|
+
book.sqrtFeeA = curve.sqrtFeeA;
|
|
100
|
+
book.sqrtFeeB = curve.sqrtFeeB;
|
|
101
|
+
return { ...inputs, book };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function quoteBuySolanaV2(
|
|
105
|
+
l: LaunchV2Account,
|
|
106
|
+
gross: bigint,
|
|
107
|
+
opts: { minTokens?: bigint; quoteTransferFee?: { fee: QuoteTransferFeeV2 | null; epoch: bigint } | null; mintSupply?: bigint } = {},
|
|
108
|
+
): { buy: BookBuy; after: Book; withheld: bigint; refundReceived: bigint } {
|
|
109
|
+
const s = solanaSettleInputsV2(l, { mintSupply: opts.mintSupply });
|
|
110
|
+
const after = cloneBook(s.book);
|
|
111
|
+
|
|
112
|
+
const tf = opts.quoteTransferFee;
|
|
113
|
+
const net = tf ? quoteBuyNetInV2(tf.fee, tf.epoch, gross) : gross;
|
|
114
|
+
const r = buy(after, s.row, s.sup, net, opts.minTokens ?? 0n, { walkSteps: SOL_BUY_WALK_STEPS });
|
|
115
|
+
const refundReceived = tf ? quoteReceivedV2(tf.fee, tf.epoch, r.refund) : r.refund;
|
|
116
|
+
return { buy: r, after, withheld: gross - net, refundReceived };
|
|
117
|
+
}
|