@floorlaunchpad/sdk 0.6.0 → 0.8.1
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 +279 -0
- package/README.md +12 -1
- package/dist/abi/evm-event-names.js +7 -0
- package/dist/abi/evm.gen.js +81 -2
- package/dist/abi/functions.gen.js +135 -1
- package/dist/abi/solana.gen.js +64 -0
- package/dist/client/solana/index.js +25 -6
- package/dist/config/generated.js +803 -26374
- package/dist/errors.js +21 -2
- package/dist/events/types.gen.js +1 -1
- package/dist/evm/chains.js +80 -0
- package/dist/evm/floor/abi.gen.js +115 -0
- package/dist/evm/floor/calls.js +5 -0
- package/dist/evm/squid.js +21 -5
- package/dist/evm/venues.js +302 -0
- package/dist/generated/interface.gen.js +93 -44
- package/dist/index.js +7 -0
- package/dist/indexer/bodies.js +288 -5
- package/dist/indexer/client.js +17 -1
- package/dist/indexer/contract.js +141 -15
- package/dist/indexer/paths.js +80 -7
- package/dist/indexer/read.js +25 -3
- package/dist/indexer/stream.js +2 -2
- package/dist/math/v2/book.js +24 -7
- package/dist/math/v2/create.js +1 -1
- package/dist/math/v2/escrow.js +205 -0
- package/dist/math/v2/fees.js +30 -4
- package/dist/math/v2/milestones.js +1 -1
- package/dist/math/v2/office.js +110 -0
- package/dist/math/v2/receipt.js +3 -3
- package/dist/math/v2/relay.js +263 -96
- package/dist/math/v2/rows.js +17 -0
- package/dist/math/v2/team.js +42 -1
- package/dist/math/v2/trade.js +3 -1
- package/dist/reprice.js +88 -0
- package/dist/route/quote.js +16 -0
- package/dist/scaledUi.js +113 -0
- package/dist/solana/floor/accounts.js +38 -5
- package/dist/solana/floor/events.js +62 -1
- package/dist/solana/floor/instructions.js +39 -1
- package/dist/solana/floor/program.js +3 -0
- package/dist/stockRefresh.js +99 -0
- package/dist/ton/consts.generated.js +2 -2
- package/dist/ton/gas.js +3 -3
- package/dist/v2/claims.js +28 -5
- package/dist/v2/devbuy.js +28 -3
- package/dist/v2/events.js +2 -0
- package/dist/v2/evm.js +183 -5
- package/dist/v2/layouts.js +102 -2
- package/dist/v2/office-events.js +127 -0
- package/dist/v2/office-evm.js +68 -0
- package/dist/v2/office-read.js +182 -0
- package/dist/v2/office-solana.js +134 -0
- package/dist/v2/office-trade.js +73 -0
- package/dist/v2/route-alt.js +1 -0
- package/dist/v2/route.js +1 -1
- package/dist/v2/solana.js +224 -5
- package/dist/v2/solanaSettle.js +2 -0
- package/dist/v2/tx/evm.js +66 -2
- package/dist/v2/tx/solana.js +3 -3
- package/dist/v2/types.js +4 -0
- package/docs/aggregator.md +19 -0
- package/docs/cursor-pages.md +9 -3
- package/docs/ton.md +1 -1
- package/examples/floor-line.html +1 -1
- package/package.json +26 -1
- package/src/abi/evm-event-names.ts +7 -0
- package/src/abi/evm.gen.ts +81 -2
- package/src/abi/functions.gen.ts +135 -1
- package/src/abi/solana.gen.ts +64 -0
- package/src/client/solana/index.ts +25 -6
- package/src/config/generated.ts +803 -26374
- package/src/errors.ts +21 -2
- package/src/events/types.gen.ts +458 -1
- package/src/evm/chains.ts +89 -0
- package/src/evm/floor/abi.gen.ts +115 -0
- package/src/evm/floor/calls.ts +5 -0
- package/src/evm/squid.ts +22 -6
- package/src/evm/venues.ts +340 -0
- package/src/generated/interface.gen.ts +93 -44
- package/src/index.ts +7 -0
- package/src/indexer/bodies.ts +308 -5
- package/src/indexer/client.ts +31 -4
- package/src/indexer/contract.ts +148 -17
- package/src/indexer/paths.ts +139 -7
- package/src/indexer/read.ts +27 -5
- package/src/indexer/stream.ts +4 -4
- package/src/math/v2/book.ts +23 -7
- package/src/math/v2/create.ts +1 -1
- package/src/math/v2/escrow.ts +213 -0
- package/src/math/v2/fees.ts +30 -4
- package/src/math/v2/milestones.ts +1 -1
- package/src/math/v2/office.ts +114 -0
- package/src/math/v2/receipt.ts +7 -3
- package/src/math/v2/relay.ts +269 -96
- package/src/math/v2/rows.ts +17 -0
- package/src/math/v2/team.ts +42 -1
- package/src/math/v2/trade.ts +3 -1
- package/src/reprice.ts +121 -0
- package/src/route/quote.ts +16 -0
- package/src/scaledUi.ts +123 -0
- package/src/solana/floor/accounts.ts +38 -5
- package/src/solana/floor/events.ts +63 -2
- package/src/solana/floor/instructions.ts +39 -1
- package/src/solana/floor/program.ts +3 -0
- package/src/stockRefresh.ts +131 -0
- package/src/ton/consts.generated.ts +2 -2
- package/src/ton/gas.ts +3 -3
- package/src/v2/claims.ts +34 -5
- package/src/v2/devbuy.ts +38 -3
- package/src/v2/events.ts +3 -1
- package/src/v2/evm.ts +209 -5
- package/src/v2/layouts.ts +102 -2
- package/src/v2/office-events.ts +133 -0
- package/src/v2/office-evm.ts +76 -0
- package/src/v2/office-read.ts +264 -0
- package/src/v2/office-solana.ts +159 -0
- package/src/v2/office-trade.ts +96 -0
- package/src/v2/route-alt.ts +1 -0
- package/src/v2/route.ts +1 -1
- package/src/v2/solana.ts +235 -5
- package/src/v2/solanaSettle.ts +2 -0
- package/src/v2/tx/evm.ts +66 -2
- package/src/v2/tx/solana.ts +5 -3
- package/src/v2/types.ts +76 -1
package/src/reprice.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
export const REPRICE_WAD = 10n ** 18n;
|
|
4
|
+
|
|
5
|
+
export interface RepriceKnobs { minDriftBps: number; maxMoveBps: number; explainBps: number; agreeBps: number }
|
|
6
|
+
export const REPRICE_DEFAULTS: Readonly<RepriceKnobs> = Object.freeze({ minDriftBps: 200, maxMoveBps: 2500, explainBps: 1500, agreeBps: 300 });
|
|
7
|
+
|
|
8
|
+
export interface PriceSources { from: string[]; spread_bps?: number | string }
|
|
9
|
+
|
|
10
|
+
export interface RepriceEntry { enabled: boolean; decimals: number; usdCentsX64: bigint; createFee: bigint }
|
|
11
|
+
|
|
12
|
+
export interface RepriceQuote { symbol: string; address: string }
|
|
13
|
+
|
|
14
|
+
export type RepriceBasis = "small move" | "multiplier" | "two sources";
|
|
15
|
+
|
|
16
|
+
export interface RepriceUpdate {
|
|
17
|
+
symbol: string; address: string; enabled: boolean; decimals: number;
|
|
18
|
+
usd_cents_x64: string; create_fee: string;
|
|
19
|
+
from_usd: number; to_usd: number; drift_bps: number; from_fee: string;
|
|
20
|
+
multiplier: number | null; basis: RepriceBasis;
|
|
21
|
+
}
|
|
22
|
+
export interface RepriceSkipped { symbol: string; address: string; why: string }
|
|
23
|
+
|
|
24
|
+
export interface RepriceArgs extends Partial<RepriceKnobs> {
|
|
25
|
+
quotes: readonly RepriceQuote[];
|
|
26
|
+
|
|
27
|
+
prices: Readonly<Record<string, string | number | null | undefined>>;
|
|
28
|
+
|
|
29
|
+
sources?: Readonly<Record<string, PriceSources | undefined>> | null;
|
|
30
|
+
|
|
31
|
+
read: (address: string) => Promise<RepriceEntry | null>;
|
|
32
|
+
|
|
33
|
+
multiplier?: (address: string) => Promise<bigint | null>;
|
|
34
|
+
|
|
35
|
+
feeUsdCents: number | bigint;
|
|
36
|
+
|
|
37
|
+
keyOf?: (address: string) => string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function usdToCentsX64(usd: string): bigint {
|
|
41
|
+
if (!/^\d+(\.\d+)?$/.test(usd)) throw new RangeError(`usd price "${usd}" is not a decimal number`);
|
|
42
|
+
const [i, f = ""] = usd.split(".");
|
|
43
|
+
const x = ((BigInt(i! + f) * 100n) << 64n) / 10n ** BigInt(f.length);
|
|
44
|
+
if (x === 0n) throw new RangeError(`usd price "${usd}" rounds to 0`);
|
|
45
|
+
if (x >= 1n << 128n) throw new RangeError(`usd price "${usd}" overflows u128`);
|
|
46
|
+
return x;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function createFeeFor(usdCents: number | bigint, usdCentsX64: bigint, decimals: number): bigint {
|
|
50
|
+
if (usdCentsX64 <= 0n) throw new RangeError("usdCentsX64 must be positive");
|
|
51
|
+
const n = (BigInt(usdCents) * 10n ** BigInt(decimals)) << 64n;
|
|
52
|
+
return (n + usdCentsX64 - 1n) / usdCentsX64;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function moveBps(from: bigint, to: bigint): number {
|
|
56
|
+
if (from === 0n) return 0;
|
|
57
|
+
const d = to > from ? to - from : from - to;
|
|
58
|
+
return Number((d * 10_000n) / from);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const centsX64ToUsd = (x: bigint): number => Number((x * 1_000_000n) >> 64n) / 100_000_000;
|
|
62
|
+
|
|
63
|
+
export function twoSourcesAgree(src: PriceSources | null | undefined, agreeBps: number = REPRICE_DEFAULTS.agreeBps): boolean {
|
|
64
|
+
if (!src || !Array.isArray(src.from)) return false;
|
|
65
|
+
const spread = Number(src.spread_bps);
|
|
66
|
+
return src.from.includes("pyth") && src.from.includes("coingecko") && Number.isFinite(spread) && spread >= 0 && spread <= agreeBps;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function repriceRows(a: RepriceArgs): Promise<{ update: RepriceUpdate[]; skipped: RepriceSkipped[] }> {
|
|
70
|
+
const { minDriftBps, maxMoveBps, explainBps, agreeBps } = {
|
|
71
|
+
...REPRICE_DEFAULTS,
|
|
72
|
+
...Object.fromEntries(Object.entries({ minDriftBps: a.minDriftBps, maxMoveBps: a.maxMoveBps, explainBps: a.explainBps, agreeBps: a.agreeBps }).filter(([, v]) => v !== undefined)),
|
|
73
|
+
} as RepriceKnobs;
|
|
74
|
+
const keyOf = a.keyOf ?? ((x: string) => x);
|
|
75
|
+
const multiplier = a.multiplier ?? (async () => null);
|
|
76
|
+
const update: RepriceUpdate[] = []; const skipped: RepriceSkipped[] = [];
|
|
77
|
+
for (const q of a.quotes) {
|
|
78
|
+
const { symbol, address } = q;
|
|
79
|
+
const k = keyOf(address);
|
|
80
|
+
const usd = a.prices[k];
|
|
81
|
+
if (usd === undefined || usd === null) { skipped.push({ symbol, address, why: "no price now (run again later)" }); continue; }
|
|
82
|
+
let x64: bigint;
|
|
83
|
+
try { x64 = usdToCentsX64(String(usd)); } catch (e) { skipped.push({ symbol, address, why: `bad price: ${(e as Error).message}` }); continue; }
|
|
84
|
+
const e = await a.read(address);
|
|
85
|
+
if (!e) { skipped.push({ symbol, address, why: "not listed" }); continue; }
|
|
86
|
+
const fee = createFeeFor(a.feeUsdCents, x64, e.decimals);
|
|
87
|
+
const drift = moveBps(e.usdCentsX64, x64);
|
|
88
|
+
if (drift <= minDriftBps && moveBps(e.createFee, fee) <= minDriftBps) { skipped.push({ symbol, address, why: "within" }); continue; }
|
|
89
|
+
let m: bigint | null = null; let basis: RepriceBasis = "small move";
|
|
90
|
+
if (drift > maxMoveBps) {
|
|
91
|
+
m = await multiplier(address);
|
|
92
|
+
const explained = m !== null && e.usdCentsX64 > 0n && moveBps((e.usdCentsX64 * m) / REPRICE_WAD, x64) <= explainBps;
|
|
93
|
+
const src = a.sources?.[k] ?? null;
|
|
94
|
+
if (explained) basis = "multiplier";
|
|
95
|
+
else if (twoSourcesAgree(src, agreeBps)) basis = "two sources";
|
|
96
|
+
else {
|
|
97
|
+
const one = src && Array.isArray(src.from)
|
|
98
|
+
? ` (sources: ${src.from.join(" + ") || "none"}${src.spread_bps !== undefined ? `, ${(Number(src.spread_bps) / 100).toFixed(2)} % apart` : ""})`
|
|
99
|
+
: a.sources ? " (no source record)" : " (no sources file)";
|
|
100
|
+
skipped.push({ symbol, address, why: `moved ${(drift / 100).toFixed(2)} %; the multiplier ${m === null ? "(unread)" : (Number(m) / 1e18).toString()} does not explain it and Pyth + CoinGecko do not both confirm it${one}: check by hand` });
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
update.push({
|
|
105
|
+
symbol, address, enabled: e.enabled, decimals: e.decimals,
|
|
106
|
+
usd_cents_x64: x64.toString(), create_fee: fee.toString(),
|
|
107
|
+
from_usd: centsX64ToUsd(e.usdCentsX64), to_usd: centsX64ToUsd(x64), drift_bps: drift,
|
|
108
|
+
from_fee: e.createFee.toString(), multiplier: m === null ? null : Number(m) / 1e18, basis,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return { update, skipped };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function skippedTally(skipped: readonly RepriceSkipped[]): Record<string, number> {
|
|
115
|
+
const t: Record<string, number> = {};
|
|
116
|
+
for (const s of skipped) {
|
|
117
|
+
const why = s.why.startsWith("moved") ? "a big move neither the multiplier nor two sources confirm" : s.why;
|
|
118
|
+
t[why] = (t[why] ?? 0) + 1;
|
|
119
|
+
}
|
|
120
|
+
return t;
|
|
121
|
+
}
|
package/src/route/quote.ts
CHANGED
|
@@ -44,3 +44,19 @@ export interface RouteQuote {
|
|
|
44
44
|
export function routeInputs(q: RouteQuote): string[] {
|
|
45
45
|
return q.path.hops.map(hopIn);
|
|
46
46
|
}
|
|
47
|
+
|
|
48
|
+
export function bestEdgeQuoter(quoters: readonly EdgeQuoter[]): EdgeQuoter {
|
|
49
|
+
return async (req) => {
|
|
50
|
+
const answers = await Promise.all(quoters.map(async (q) => {
|
|
51
|
+
try {
|
|
52
|
+
return (await q(req)) as EdgeQuote | null;
|
|
53
|
+
} catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}));
|
|
57
|
+
let best: EdgeQuote | null = null;
|
|
58
|
+
for (const a of answers) if (a && a.outAmount > 0n && (best === null || a.outAmount > best.outAmount)) best = a;
|
|
59
|
+
if (best === null) throw new RangeError("no edge route");
|
|
60
|
+
return best;
|
|
61
|
+
};
|
|
62
|
+
}
|
package/src/scaledUi.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
export const WAD = 10n ** 18n;
|
|
4
|
+
|
|
5
|
+
export const SCALED_UI_AMOUNT_EXTENSION = 25;
|
|
6
|
+
|
|
7
|
+
export interface ScaledUiConfig {
|
|
8
|
+
multiplierWad: bigint;
|
|
9
|
+
newMultiplierWad: bigint;
|
|
10
|
+
newMultiplierEffectiveTs: bigint;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type QuoteUnit = "raw" | "displayed";
|
|
14
|
+
|
|
15
|
+
export const PARTNER_QUOTE_UNIT = {
|
|
16
|
+
coingecko: "displayed",
|
|
17
|
+
dexscreener: "raw",
|
|
18
|
+
geckoterminal: "raw",
|
|
19
|
+
defillama: "raw",
|
|
20
|
+
jupiter: "raw",
|
|
21
|
+
kyberswap: "raw",
|
|
22
|
+
velora: "raw",
|
|
23
|
+
} as const satisfies Record<string, QuoteUnit>;
|
|
24
|
+
|
|
25
|
+
export const COINGECKO_RAW_SCALED_CHAINS: ReadonlySet<string> = new Set(["base"]);
|
|
26
|
+
|
|
27
|
+
export function coinGeckoQuoteUnit(chain: string): QuoteUnit {
|
|
28
|
+
return COINGECKO_RAW_SCALED_CHAINS.has(chain) ? "raw" : PARTNER_QUOTE_UNIT.coingecko;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function f64ToWad(data: Uint8Array, off: number): bigint | null {
|
|
32
|
+
if (off < 0 || off + 8 > data.length) return null;
|
|
33
|
+
const bits = new DataView(data.buffer, data.byteOffset, data.byteLength).getBigUint64(off, true);
|
|
34
|
+
if (bits >> 63n) return null;
|
|
35
|
+
const exp = (bits >> 52n) & 0x7ffn;
|
|
36
|
+
if (exp === 0n || exp === 0x7ffn) return null;
|
|
37
|
+
const m = (bits & ((1n << 52n) - 1n)) | (1n << 52n);
|
|
38
|
+
const e = exp - 1075n;
|
|
39
|
+
const v = e >= 0n ? (m * WAD) << e : (m * WAD) >> -e;
|
|
40
|
+
return v > 0n ? v : null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function decodeScaledUiConfig(data: Uint8Array | null | undefined): ScaledUiConfig | null {
|
|
44
|
+
if (!data || data.length < 166 || data[165] !== 1) return null;
|
|
45
|
+
const dv = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
46
|
+
for (let o = 166; o + 4 <= data.length; ) {
|
|
47
|
+
const type = dv.getUint16(o, true);
|
|
48
|
+
const len = dv.getUint16(o + 2, true);
|
|
49
|
+
if (type === 0 && len === 0) break;
|
|
50
|
+
if (type === SCALED_UI_AMOUNT_EXTENSION && len >= 56 && o + 4 + 56 <= data.length) {
|
|
51
|
+
const v = o + 4;
|
|
52
|
+
const multiplierWad = f64ToWad(data, v + 32);
|
|
53
|
+
const newMultiplierWad = f64ToWad(data, v + 48);
|
|
54
|
+
if (multiplierWad === null || newMultiplierWad === null) return null;
|
|
55
|
+
return { multiplierWad, newMultiplierWad, newMultiplierEffectiveTs: dv.getBigInt64(v + 40, true) };
|
|
56
|
+
}
|
|
57
|
+
o += 4 + len;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const uiMultiplierConfig = (wad: bigint): ScaledUiConfig | null => (wad > 0n ? { multiplierWad: wad, newMultiplierWad: wad, newMultiplierEffectiveTs: 0n } : null);
|
|
63
|
+
|
|
64
|
+
export const UI_MULTIPLIER_SELECTOR = "0xa60bf13d";
|
|
65
|
+
|
|
66
|
+
export function decodeUiMultiplier(hex: string | null | undefined): ScaledUiConfig | null {
|
|
67
|
+
const m = /^0x([0-9a-fA-F]{64})/.exec(String(hex ?? ""));
|
|
68
|
+
return m === null ? null : uiMultiplierConfig(BigInt("0x" + m[1]));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const multiplierAt = (c: ScaledUiConfig, ts: bigint): bigint => (ts >= c.newMultiplierEffectiveTs ? c.newMultiplierWad : c.multiplierWad);
|
|
72
|
+
|
|
73
|
+
export type ScaledUiHistory = readonly ScaledUiConfig[];
|
|
74
|
+
|
|
75
|
+
export function multiplierIn(h: ScaledUiHistory, ts: bigint): bigint | null {
|
|
76
|
+
if (h.length === 0) return null;
|
|
77
|
+
for (const c of h) if (ts < c.newMultiplierEffectiveTs) return c.multiplierWad;
|
|
78
|
+
return h[h.length - 1]!.newMultiplierWad;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const sameConfig = (a: ScaledUiConfig, b: ScaledUiConfig): boolean =>
|
|
82
|
+
a.multiplierWad === b.multiplierWad && a.newMultiplierWad === b.newMultiplierWad && a.newMultiplierEffectiveTs === b.newMultiplierEffectiveTs;
|
|
83
|
+
|
|
84
|
+
export function noteScaledUi(h: ScaledUiHistory, c: ScaledUiConfig | null, nowS: bigint, lastReadS?: bigint): ScaledUiHistory {
|
|
85
|
+
if (c === null) return h;
|
|
86
|
+
if (h.length === 0) return [c];
|
|
87
|
+
if (sameConfig(h[h.length - 1]!, c)) return h;
|
|
88
|
+
const { multiplierWad: m, newMultiplierWad: n } = c;
|
|
89
|
+
const at = c.newMultiplierEffectiveTs;
|
|
90
|
+
const scheduled = m !== n && at > 0n;
|
|
91
|
+
|
|
92
|
+
let cut = at > 0n && at < nowS ? at : nowS;
|
|
93
|
+
|
|
94
|
+
if (lastReadS !== undefined && cut <= lastReadS && lastReadS < nowS) cut = lastReadS + 1n;
|
|
95
|
+
const kept = h.filter((x) => x.newMultiplierEffectiveTs < cut);
|
|
96
|
+
const last = kept[kept.length - 1];
|
|
97
|
+
const was = last === undefined ? h[0]!.multiplierWad : last.newMultiplierWad;
|
|
98
|
+
const out: ScaledUiConfig[] = [...kept];
|
|
99
|
+
const after = (t: bigint): bigint => (last !== undefined && t < last.newMultiplierEffectiveTs ? last.newMultiplierEffectiveTs : t);
|
|
100
|
+
if (scheduled) {
|
|
101
|
+
if (was !== m) out.push({ multiplierWad: was, newMultiplierWad: m, newMultiplierEffectiveTs: after(cut) });
|
|
102
|
+
out.push(at < cut ? { ...c, newMultiplierEffectiveTs: cut } : c);
|
|
103
|
+
} else if (was !== n) {
|
|
104
|
+
out.push({ multiplierWad: was, newMultiplierWad: n, newMultiplierEffectiveTs: after(cut) });
|
|
105
|
+
} else if (out.length === 0) {
|
|
106
|
+
out.push(c);
|
|
107
|
+
}
|
|
108
|
+
return out.length === h.length && out.every((x, i) => sameConfig(x, h[i]!)) ? h : out;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const quoteToDisplayed = (raw: bigint, wad: bigint): bigint => (raw * wad) / WAD;
|
|
112
|
+
|
|
113
|
+
export function quoteToRaw(displayed: bigint, wad: bigint): bigint {
|
|
114
|
+
if (wad <= 0n) throw new RangeError("a multiplier must be > 0");
|
|
115
|
+
return (displayed * WAD) / wad;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const usdPerRawFromDisplayed = (usdPerDisplayed: bigint, wad: bigint): bigint => (usdPerDisplayed * wad) / WAD;
|
|
119
|
+
|
|
120
|
+
export function usdPerDisplayedFromRaw(usdPerRaw: bigint, wad: bigint): bigint {
|
|
121
|
+
if (wad <= 0n) throw new RangeError("a multiplier must be > 0");
|
|
122
|
+
return (usdPerRaw * WAD) / wad;
|
|
123
|
+
}
|
|
@@ -15,7 +15,11 @@ export const SLOT_MEMBER: Layout = [
|
|
|
15
15
|
["requested", "u64"],
|
|
16
16
|
["earned", "u64"],
|
|
17
17
|
["withdrawn", "u64"],
|
|
18
|
-
|
|
18
|
+
|
|
19
|
+
["deal_fee_bps", "u16"],
|
|
20
|
+
|
|
21
|
+
["agency_bps", "u16"],
|
|
22
|
+
["reserved", "bytes:4"],
|
|
19
23
|
];
|
|
20
24
|
|
|
21
25
|
export const CONFIG: Layout = [
|
|
@@ -23,7 +27,9 @@ export const CONFIG: Layout = [
|
|
|
23
27
|
["bump", "u8"],
|
|
24
28
|
["create_paused", "u8"],
|
|
25
29
|
["next_preset_id", "u8"],
|
|
26
|
-
|
|
30
|
+
|
|
31
|
+
["deal_fee_cap_bps", "u16"],
|
|
32
|
+
["pad0", "bytes:2"],
|
|
27
33
|
["authority", "pubkey"],
|
|
28
34
|
["pending_authority", "pubkey"],
|
|
29
35
|
["timelock", "pubkey"],
|
|
@@ -90,14 +96,34 @@ export const TRADER_REF: Layout = [
|
|
|
90
96
|
["revoked", "u8"],
|
|
91
97
|
|
|
92
98
|
["pad0", "bytes:5"],
|
|
93
|
-
|
|
99
|
+
|
|
100
|
+
["og_until", "u64[3]"],
|
|
94
101
|
];
|
|
95
102
|
|
|
96
103
|
export const TRADER_LINK: Layout = [
|
|
97
104
|
["wallet", "pubkey"],
|
|
98
105
|
["tiers", "pubkey[3]"],
|
|
99
106
|
["bump", "u8"],
|
|
100
|
-
|
|
107
|
+
|
|
108
|
+
["og_until", "bytes:24"],
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
export const OG_USER: Layout = [
|
|
112
|
+
["referrer", "pubkey"],
|
|
113
|
+
["from", "i64"],
|
|
114
|
+
["until", "i64"],
|
|
115
|
+
["bump", "u8"],
|
|
116
|
+
["revoked", "u8"],
|
|
117
|
+
["pad0", "bytes:6"],
|
|
118
|
+
["reserved", "bytes:32"],
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
export const OG_COUNT: Layout = [
|
|
122
|
+
["count", "u16"],
|
|
123
|
+
["bump", "u8"],
|
|
124
|
+
["pad0", "bytes:5"],
|
|
125
|
+
["total", "u64"],
|
|
126
|
+
["reserved", "bytes:16"],
|
|
101
127
|
];
|
|
102
128
|
|
|
103
129
|
export const REF_OWED: Layout = [
|
|
@@ -209,7 +235,7 @@ export const GENESIS_ALLOC_WAGMI_CLAIMED = 8;
|
|
|
209
235
|
|
|
210
236
|
const NESTED: Record<string, Layout> = { SlotMember: SLOT_MEMBER };
|
|
211
237
|
|
|
212
|
-
export const LAYOUTS = { Config: CONFIG, QuoteEntry: QUOTE_ENTRY, TeamSlot: TEAM_SLOT, SlotMember: SLOT_MEMBER, TraderRef: TRADER_REF, TraderLink: TRADER_LINK, RefOwed: REF_OWED, GenesisRaise: GENESIS_RAISE, GenesisAllocation: GENESIS_ALLOCATION } as const;
|
|
238
|
+
export const LAYOUTS = { Config: CONFIG, QuoteEntry: QUOTE_ENTRY, TeamSlot: TEAM_SLOT, SlotMember: SLOT_MEMBER, TraderRef: TRADER_REF, TraderLink: TRADER_LINK, RefOwed: REF_OWED, OgUser: OG_USER, OgCount: OG_COUNT, GenesisRaise: GENESIS_RAISE, GenesisAllocation: GENESIS_ALLOCATION } as const;
|
|
213
239
|
|
|
214
240
|
function scalarSize(t: string): number {
|
|
215
241
|
switch (t) {
|
|
@@ -367,6 +393,13 @@ export const decodeTeamSlot = (d: Uint8Array) => decodeAccount("TeamSlot", d);
|
|
|
367
393
|
export const decodeTraderRef = (d: Uint8Array) => decodeAccount("TraderRef", d);
|
|
368
394
|
export const decodeTraderLink = (d: Uint8Array) => decodeAccount("TraderLink", d);
|
|
369
395
|
export const decodeRefOwed = (d: Uint8Array) => decodeAccount("RefOwed", d);
|
|
396
|
+
export const decodeOgUser = (d: Uint8Array) => decodeAccount("OgUser", d);
|
|
397
|
+
export const decodeOgCount = (d: Uint8Array) => decodeAccount("OgCount", d);
|
|
398
|
+
|
|
399
|
+
export function traderLinkOgUntil(ogUntil: Uint8Array): [bigint, bigint, bigint] {
|
|
400
|
+
const v = new DataView(ogUntil.buffer, ogUntil.byteOffset, ogUntil.byteLength);
|
|
401
|
+
return [v.getBigUint64(0, true), v.getBigUint64(8, true), v.getBigUint64(16, true)];
|
|
402
|
+
}
|
|
370
403
|
export const decodeGenesisRaise = (d: Uint8Array) => decodeAccount("GenesisRaise", d);
|
|
371
404
|
export const decodeGenesisAllocation = (d: Uint8Array) => decodeAccount("GenesisAllocation", d);
|
|
372
405
|
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
import { hex } from "../../codec.ts";
|
|
4
4
|
import { EVENT_IX_TAG_LE, eventDiscriminator } from "../instruction.ts";
|
|
5
|
-
import { Reader } from "./borsh.ts";
|
|
5
|
+
import { Reader, Writer } from "./borsh.ts";
|
|
6
6
|
|
|
7
|
-
type B = "pubkey" | "bool" | "u8" | "u16" | "u32" | "u64" | "i64" | "u128" | `bytes:${number}` | `u64[${number}]` | `pubkey[${number}]`;
|
|
7
|
+
type B = "pubkey" | "bool" | "u8" | "u16" | "u32" | "u64" | "i64" | "u128" | `bytes:${number}` | `u64[${number}]` | `u128[${number}]` | `i64[${number}]` | `pubkey[${number}]`;
|
|
8
8
|
type T = B | `${B}?`;
|
|
9
9
|
const S = (...f: [string, T][]): [string, T][] => f;
|
|
10
10
|
|
|
@@ -31,6 +31,12 @@ export const EVENTS: Record<string, [string, T][]> = {
|
|
|
31
31
|
TraderLinkOpened: S(["trader", "pubkey"], ["tiers", "pubkey[3]"]),
|
|
32
32
|
|
|
33
33
|
TraderRefCleared: S(["trader", "pubkey"], ["records", "u16"]),
|
|
34
|
+
|
|
35
|
+
OgUserSet: S(["referrer", "pubkey"], ["from", "i64"], ["until", "i64"], ["count", "u16"]),
|
|
36
|
+
|
|
37
|
+
OgUserRevoked: S(["referrer", "pubkey"], ["until", "i64"], ["count", "u16"]),
|
|
38
|
+
|
|
39
|
+
OgSynced: S(["referrer", "pubkey"], ["records", "u16"]),
|
|
34
40
|
CreatorBuyUnlocked: S(["launch", "pubkey"], ["creator", "pubkey"], ["tokens", "u64"], ["members", "u64[10]?"], ["paid", "u64?"]),
|
|
35
41
|
RefOwedOpened: S(["referrer", "pubkey"], ["quoteMint", "pubkey"]),
|
|
36
42
|
RefsRolled: S(["quoteMint", "pubkey"], ["traderRefs", "u16"], ["amount", "u64"]),
|
|
@@ -76,6 +82,28 @@ export const EVENTS: Record<string, [string, T][]> = {
|
|
|
76
82
|
|
|
77
83
|
TokensBurned: S(["launch", "pubkey"], ["wallet", "pubkey"], ["role", "u8"], ["place", "u8"], ["fromTeamBuy", "u64"], ["fromWallet", "u64"], ["balanceBefore", "u64"], ["owedAfter", "u64"], ["split", "u64[10]"], ["supply", "u64"], ["sOut", "u64"], ["floorAfterX64", "u128"], ["slot", "u64"], ["fromShareBought", "u64?"]),
|
|
78
84
|
|
|
85
|
+
DealFeePaid: S(["launch", "pubkey"], ["place", "u8"], ["member", "pubkey"], ["owed", "u64"], ["fee", "u64"],
|
|
86
|
+
|
|
87
|
+
["agencyPart", "u64"]),
|
|
88
|
+
PlatformCredited: S(["launch", "pubkey"], ["payer", "pubkey"], ["quoteMint", "pubkey"], ["amount", "u64"], ["memo", "bytes:32"]),
|
|
89
|
+
TeamSlotDealFee: S(["launch", "pubkey"], ["index", "u8"], ["key", "bytes:32"], ["dealFeeBps", "u16"]),
|
|
90
|
+
|
|
91
|
+
TeamSlotAgency: S(["launch", "pubkey"], ["index", "u8"], ["key", "bytes:32"], ["agency", "pubkey"], ["agencyBps", "u16"]),
|
|
92
|
+
DealFeeCapSet: S(["capBps", "u16"]),
|
|
93
|
+
|
|
94
|
+
DealEscrowOpened: S(["launch", "pubkey"], ["escrow", "pubkey"], ["launcher", "pubkey"], ["kol", "pubkey"], ["ref32", "bytes:32"], ["assetMint", "pubkey"], ["kind", "u8"], ["n", "u8"], ["targets", "u128[8]"], ["amounts", "u64[8]"], ["total", "u64"], ["expiry", "i64"]),
|
|
95
|
+
DealEscrowArmed: S(["launch", "pubkey"], ["escrow", "pubkey"], ["ref32", "bytes:32"], ["step", "u8"], ["marketCap", "u128"], ["armedAt", "i64"], ["armedSlot", "u64"]),
|
|
96
|
+
DealEscrowReleased: S(["launch", "pubkey"], ["escrow", "pubkey"], ["ref32", "bytes:32"], ["step", "u8"], ["kol", "pubkey"], ["amount", "u64"], ["fee", "u64"], ["marketCap", "u128"]),
|
|
97
|
+
DealEscrowRefunded: S(["launch", "pubkey"], ["escrow", "pubkey"], ["ref32", "bytes:32"], ["launcher", "pubkey"], ["amount", "u64"]),
|
|
98
|
+
DealEscrowCancelAgreed: S(["launch", "pubkey"], ["escrow", "pubkey"], ["ref32", "bytes:32"], ["by", "pubkey"], ["party", "u8"], ["agreed", "u8"]),
|
|
99
|
+
DealEscrowCancelled: S(["launch", "pubkey"], ["escrow", "pubkey"], ["ref32", "bytes:32"], ["launcher", "pubkey"], ["amount", "u64"]),
|
|
100
|
+
|
|
101
|
+
RefVolumeOpened: S(["launch", "pubkey"], ["referrer", "pubkey"]),
|
|
102
|
+
DealEscrowTermsV3: S(["launch", "pubkey"], ["escrow", "pubkey"], ["ref32", "bytes:32"], ["kinds", "bytes:8"], ["proofMask", "u8"], ["agency", "pubkey"], ["agencyBps", "u16"], ["volAtLock", "u128"]),
|
|
103
|
+
DealEscrowSplit: S(["launch", "pubkey"], ["escrow", "pubkey"], ["ref32", "bytes:32"], ["step", "u8"], ["kind", "u8"], ["kol", "pubkey"], ["kolPart", "u64"], ["agency", "pubkey"], ["agencyPart", "u64"], ["tiers", "pubkey[3]"], ["refs", "u64[3]"], ["feeRest", "u64"]),
|
|
104
|
+
DealEscrowProofSubmitted: S(["launch", "pubkey"], ["escrow", "pubkey"], ["ref32", "bytes:32"], ["step", "u8"], ["proofHash", "bytes:32"], ["state", "u8"], ["windowEnds", "i64"]),
|
|
105
|
+
DealEscrowProofObjected: S(["launch", "pubkey"], ["escrow", "pubkey"], ["ref32", "bytes:32"], ["step", "u8"]),
|
|
106
|
+
|
|
79
107
|
GenesisPublicStarted: S(["signer", "pubkey"], ["at", "i64"]),
|
|
80
108
|
|
|
81
109
|
GenesisEndSet: S(["signer", "pubkey"], ["oldEndsAt", "i64"], ["newEndsAt", "i64"]),
|
|
@@ -112,6 +140,8 @@ function readField(r: Reader, t: B): unknown {
|
|
|
112
140
|
const n = Number(m[2]);
|
|
113
141
|
if (m[1] === "bytes") return r.bytes(n);
|
|
114
142
|
if (m[1] === "u64") return Array.from({ length: n }, () => r.u64());
|
|
143
|
+
if (m[1] === "u128") return Array.from({ length: n }, () => r.u128());
|
|
144
|
+
if (m[1] === "i64") return Array.from({ length: n }, () => r.i64());
|
|
115
145
|
return Array.from({ length: n }, () => r.pubkey());
|
|
116
146
|
}
|
|
117
147
|
|
|
@@ -149,3 +179,34 @@ export function decodeEventCpi(ixData: Uint8Array): FloorEvent | null {
|
|
|
149
179
|
if (ixData.length < 16 || hex(ixData.slice(0, 8)) !== hex(EVENT_IX_TAG_LE)) return null;
|
|
150
180
|
return decodeEventData(ixData.slice(8));
|
|
151
181
|
}
|
|
182
|
+
|
|
183
|
+
function writeField(w: Writer, t: B, v: any): void {
|
|
184
|
+
const m = /^(\w+)[:[](\d+)\]?$/.exec(t);
|
|
185
|
+
if (m && !["u8", "u16", "u32", "u64", "u128"].includes(t)) {
|
|
186
|
+
const n = Number(m[2]);
|
|
187
|
+
if (v.length !== n) throw new RangeError(`${t}: ${v.length} values`);
|
|
188
|
+
if (m[1] === "bytes") w.bytes(v);
|
|
189
|
+
else for (const x of v) writeField(w, m[1] as B, x);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (t === "i64") w.uint(BigInt.asUintN(64, v), 8);
|
|
193
|
+
else (w as any)[t](v);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function encodeFloorEventData(e: FloorEvent): Uint8Array {
|
|
197
|
+
const sch = EVENTS[e.name];
|
|
198
|
+
if (!sch) throw new RangeError(`not a floor_amm event: ${e.name}`);
|
|
199
|
+
const w = new Writer().bytes(eventDiscriminator(e.name));
|
|
200
|
+
let ended = false;
|
|
201
|
+
for (const [f, t] of sch) {
|
|
202
|
+
const v = e.fields[f];
|
|
203
|
+
if (t.endsWith("?")) {
|
|
204
|
+
if (ended || v === null || v === undefined) ended = true;
|
|
205
|
+
else writeField(w, t.slice(0, -1) as B, v);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (ended) throw new RangeError(`${e.name}: a required field ${f} after an absent appended one`);
|
|
209
|
+
writeField(w, t as B, v);
|
|
210
|
+
}
|
|
211
|
+
return w.done();
|
|
212
|
+
}
|
|
@@ -5,7 +5,7 @@ import type { AccountMeta, Instruction } from "../instruction.ts";
|
|
|
5
5
|
import { SYSTEM_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, WSOL_MINT, associatedTokenAddress } from "../pda.ts";
|
|
6
6
|
import {
|
|
7
7
|
FLOOR_AMM_PROGRAM_ID, GENESIS_USDC_MINT, INSTRUCTIONS_SYSVAR_ID, METADATA_PROGRAM_ID, configPda, floorEventAuthorityPda, genesisAllocPda, genesisConvertBandPda, genesisMigrationVaultPda, genesisRaisePda, genesisRaiseTokenVault,
|
|
8
|
-
genesisVaultFloor, genesisVaultUsdc, launchPda, metadataPda, presetPda, quoteEntryPda, teamSlotPda, traderLinkPda,
|
|
8
|
+
genesisVaultFloor, genesisVaultUsdc, launchPda, metadataPda, ogCountPda, ogUserPda, presetPda, quoteEntryPda, teamSlotPda, traderLinkPda,
|
|
9
9
|
} from "./program.ts";
|
|
10
10
|
import { Writer } from "./borsh.ts";
|
|
11
11
|
import { CLAIM_BITS, CLAIM_KNOWN_MASKS, CLAIM_RESERVED, CLAIM_CREATOR, CLAIM_CREATOR_REF, CLAIM_TEAM_SLOT, CLAIM_TRADER_REF, CLAIM_RESERVED_HOLDER, CLAIM_EXTRA, CLAIM_TEAM_BUY, CLAIM_SHARE_BOUGHT } from "../../generated/index.ts";
|
|
@@ -31,6 +31,11 @@ export function initializeConfigInstruction(p: { upgradeAuthority: string; confi
|
|
|
31
31
|
export function transferAuthorityInstruction(p: { authority: string; config: string; newAuthority: string } & Ctx): Instruction {
|
|
32
32
|
return ix("transfer_authority", [RS(p.authority), W(p.config)], (w) => w.pubkey(p.newAuthority), p);
|
|
33
33
|
}
|
|
34
|
+
|
|
35
|
+
export function relayBookInstruction(p: { payer: string; launch: string } & Ctx): Instruction {
|
|
36
|
+
|
|
37
|
+
return { programAddress: pid(p), accounts: [RS(p.payer), W(p.launch)], data: new Writer().bytes(instructionDiscriminator("relay_book")).done() };
|
|
38
|
+
}
|
|
34
39
|
export function acceptAuthorityInstruction(p: { signer: string; config: string } & Ctx): Instruction {
|
|
35
40
|
return ix("accept_authority", [RS(p.signer), W(p.config)], none, p);
|
|
36
41
|
}
|
|
@@ -197,6 +202,17 @@ export function teamSlotAddInstruction(p: TeamSlotPayment & { key: string; kind:
|
|
|
197
202
|
return ix("team_slot_add", slotPay(p), (w) => w.pubkey(p.key).u8(p.kind).u64(p.amount).bool(p.payNow), p);
|
|
198
203
|
}
|
|
199
204
|
|
|
205
|
+
export function teamSlotAddDealInstruction(p: { creator: string; launch: string; teamSlot: string; config: string; key: string; kind: number; amount: bigint; dealFeeBps: number; rest?: AccountMeta[] } & Ctx): Instruction {
|
|
206
|
+
if (!Number.isInteger(p.dealFeeBps) || p.dealFeeBps < 0 || p.dealFeeBps > 0xffff) throw new RangeError("team_slot_add_deal: deal_fee_bps is a u16");
|
|
207
|
+
const i = ix("team_slot_add_deal", [RS(p.creator), W(p.launch), W(p.teamSlot), R(p.config)], (w) => w.pubkey(p.key).u8(p.kind).u64(p.amount).u16(p.dealFeeBps), p);
|
|
208
|
+
return withRest(i, p.rest ?? []);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function setDealFeeCapInstruction(p: { authority: string; config: string; capBps: number } & Ctx): Instruction {
|
|
212
|
+
if (!Number.isInteger(p.capBps) || p.capBps < 0 || p.capBps > 5000) throw new RangeError("set_deal_fee_cap: cap_bps in 0..=5000");
|
|
213
|
+
return ix("set_deal_fee_cap", [RS(p.authority), W(p.config)], (w) => w.u16(p.capBps), p);
|
|
214
|
+
}
|
|
215
|
+
|
|
200
216
|
export function bindHandleInstruction(p: { referralAuthority: string; config: string; launch: string; teamSlot: string; index: number; handle: Uint8Array; wallet: string } & Ctx): Instruction {
|
|
201
217
|
if (p.handle.length !== 32) throw new RangeError("a handle is 32 bytes");
|
|
202
218
|
|
|
@@ -224,6 +240,28 @@ export function openTraderLinkInstruction(p: { payer: string; referralAuthority:
|
|
|
224
240
|
);
|
|
225
241
|
}
|
|
226
242
|
|
|
243
|
+
export function setOgUserInstruction(p: { payer: string; referralAuthority: string; config?: string; referrer: string } & Ctx): Instruction {
|
|
244
|
+
return ix(
|
|
245
|
+
"set_og_user",
|
|
246
|
+
[WS(p.payer), RS(p.referralAuthority), R(p.config ?? configPda(p.programId)[0]), W(ogUserPda(p.referrer, p.programId)[0]), W(ogCountPda(p.programId)[0]), R(SYSTEM_PROGRAM_ID)],
|
|
247
|
+
(w) => w.pubkey(p.referrer),
|
|
248
|
+
p,
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function revokeOgUserInstruction(p: { authority: string; config?: string; referrer: string } & Ctx): Instruction {
|
|
253
|
+
return ix(
|
|
254
|
+
"revoke_og_user",
|
|
255
|
+
[RS(p.authority), R(p.config ?? configPda(p.programId)[0]), W(ogUserPda(p.referrer, p.programId)[0]), W(ogCountPda(p.programId)[0])],
|
|
256
|
+
(w) => w.pubkey(p.referrer),
|
|
257
|
+
p,
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function syncOgInstruction(p: { referrer: string; records: readonly string[] } & Ctx): Instruction {
|
|
262
|
+
return withRest(ix("sync_og", [R(ogUserPda(p.referrer, p.programId)[0])], (w) => w.pubkey(p.referrer), p), p.records.map((r) => W(r)));
|
|
263
|
+
}
|
|
264
|
+
|
|
227
265
|
export function openRefOwedInstruction(p: { payer: string; quoteMint: string; refOwed: string; vault: string; quoteTokenProgram: string; referrer: string } & Ctx): Instruction {
|
|
228
266
|
return ix("open_ref_owed", [WS(p.payer), R(p.quoteMint), W(p.refOwed), W(p.vault), R(p.quoteTokenProgram), R(ASSOCIATED_TOKEN_PROGRAM_ID), R(SYSTEM_PROGRAM_ID)], (w) => w.pubkey(p.referrer), p);
|
|
229
267
|
}
|
|
@@ -34,6 +34,9 @@ export const traderLinkPda = (wallet: string, p = FLOOR_AMM_PROGRAM_ID): Pda =>
|
|
|
34
34
|
export const unwrapPda = (wallet: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["unwrap", k(wallet)], p);
|
|
35
35
|
export const refOwedPda = (referrer: string, quoteMint: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["ref_owed", k(referrer), k(quoteMint)], p);
|
|
36
36
|
|
|
37
|
+
export const ogUserPda = (referrer: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["og_user", k(referrer)], p);
|
|
38
|
+
export const ogCountPda = (p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["og_count"], p);
|
|
39
|
+
|
|
37
40
|
export const genesisRaisePda = (p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["genesis_raise"], p);
|
|
38
41
|
export const genesisAllocPda = (wallet: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["genesis_alloc", k(wallet)], p);
|
|
39
42
|
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import type { RegistryPrice } from "./registryPrice.ts";
|
|
4
|
+
|
|
5
|
+
export const STOCK_REFRESH_URL = "https://floor.supply/api/quote-refresh";
|
|
6
|
+
|
|
7
|
+
export const STOCK_FRESH_S = 43_200n;
|
|
8
|
+
|
|
9
|
+
export const STOCK_WAIT_MS = 45_000;
|
|
10
|
+
export const STOCK_POLL_MS = 3_000;
|
|
11
|
+
|
|
12
|
+
export const STOCK_ASK_TIMEOUT_MS = 100_000;
|
|
13
|
+
|
|
14
|
+
export const STOCK_REFRESH_STATUSES = ["fresh", "within", "written", "cooldown", "no_price", "not_stock", "unavailable", "limited"] as const;
|
|
15
|
+
export type StockRefreshStatus = (typeof STOCK_REFRESH_STATUSES)[number];
|
|
16
|
+
|
|
17
|
+
export interface StockRefreshAnswer {
|
|
18
|
+
status: StockRefreshStatus;
|
|
19
|
+
updatedAt: bigint | null;
|
|
20
|
+
priceUsd: string | null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const FLOOR_SITE_NETWORK = {
|
|
24
|
+
solana: "sol",
|
|
25
|
+
ethereum: "eth",
|
|
26
|
+
base: "base",
|
|
27
|
+
bsc: "bsc",
|
|
28
|
+
arbitrum: "arb",
|
|
29
|
+
arc: "arc",
|
|
30
|
+
robinhood: "rh",
|
|
31
|
+
hyperevm: "hyper",
|
|
32
|
+
elysium: "elysium",
|
|
33
|
+
} as const satisfies Record<string, string>;
|
|
34
|
+
|
|
35
|
+
export function decodeStockRefreshAnswer(j: unknown): StockRefreshAnswer | null {
|
|
36
|
+
if (j === null || typeof j !== "object") return null;
|
|
37
|
+
const o = j as Record<string, unknown>;
|
|
38
|
+
if (typeof o.status !== "string" || !(STOCK_REFRESH_STATUSES as readonly string[]).includes(o.status)) return null;
|
|
39
|
+
const at = o.updatedAt;
|
|
40
|
+
const updatedAt = typeof at === "number" && Number.isSafeInteger(at) && at >= 0 ? BigInt(at) : null;
|
|
41
|
+
const priceUsd = typeof o.priceUsd === "string" && o.priceUsd.length <= 40 ? o.priceUsd : null;
|
|
42
|
+
return { status: o.status as StockRefreshStatus, updatedAt, priceUsd };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type StockRefreshFetch = (
|
|
46
|
+
url: string,
|
|
47
|
+
init: { method: "POST"; headers: Record<string, string>; body: string; signal?: AbortSignal },
|
|
48
|
+
) => Promise<{ status: number; json(): Promise<unknown> }>;
|
|
49
|
+
|
|
50
|
+
export async function askStockRefresh(p: { chain: string; quote: string; url?: string; fetch?: StockRefreshFetch; timeoutMs?: number }): Promise<StockRefreshAnswer | null> {
|
|
51
|
+
const network = (FLOOR_SITE_NETWORK as Record<string, string>)[p.chain];
|
|
52
|
+
const f = p.fetch ?? (globalThis.fetch as unknown as StockRefreshFetch | undefined);
|
|
53
|
+
if (network === undefined || typeof f !== "function") return null;
|
|
54
|
+
try {
|
|
55
|
+
const signal = typeof AbortSignal.timeout === "function" ? AbortSignal.timeout(p.timeoutMs ?? STOCK_ASK_TIMEOUT_MS) : undefined;
|
|
56
|
+
const res = await f(p.url ?? STOCK_REFRESH_URL, {
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: { "content-type": "application/json" },
|
|
59
|
+
body: JSON.stringify({ chain: network, quote: p.quote }),
|
|
60
|
+
...(signal ? { signal } : {}),
|
|
61
|
+
});
|
|
62
|
+
if (res.status !== 200) return null;
|
|
63
|
+
return decodeStockRefreshAnswer(await res.json());
|
|
64
|
+
} catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface StockRefreshOutcome {
|
|
70
|
+
entry: RegistryPrice;
|
|
71
|
+
|
|
72
|
+
status: StockRefreshStatus | "skip" | "none";
|
|
73
|
+
|
|
74
|
+
moved: boolean;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const LATE: unique symbol = Symbol("late");
|
|
78
|
+
|
|
79
|
+
async function settled<T>(f: () => Promise<T>, ms: number): Promise<T | null | typeof LATE> {
|
|
80
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
81
|
+
try {
|
|
82
|
+
const late = new Promise<typeof LATE>((r) => (timer = setTimeout(() => r(LATE), Math.max(0, ms))));
|
|
83
|
+
return await Promise.race([Promise.resolve().then(f), late]);
|
|
84
|
+
} catch {
|
|
85
|
+
return null;
|
|
86
|
+
} finally {
|
|
87
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function refreshStockPrice(p: {
|
|
92
|
+
chain: string;
|
|
93
|
+
quote: string;
|
|
94
|
+
|
|
95
|
+
entry: RegistryPrice;
|
|
96
|
+
|
|
97
|
+
readEntry: () => Promise<RegistryPrice | null>;
|
|
98
|
+
|
|
99
|
+
ask?: () => Promise<StockRefreshAnswer | null>;
|
|
100
|
+
url?: string;
|
|
101
|
+
fetch?: StockRefreshFetch;
|
|
102
|
+
|
|
103
|
+
nowMs?: () => number;
|
|
104
|
+
sleep?: (ms: number) => Promise<void>;
|
|
105
|
+
}): Promise<StockRefreshOutcome> {
|
|
106
|
+
const nowMs = p.nowMs ?? (() => Date.now());
|
|
107
|
+
const sleep = p.sleep ?? ((ms: number) => new Promise<void>((r) => setTimeout(r, ms)));
|
|
108
|
+
const given = p.entry;
|
|
109
|
+
const keep = (status: StockRefreshOutcome["status"]): StockRefreshOutcome => ({ entry: given, status, moved: false });
|
|
110
|
+
const nowS = BigInt(Math.floor(nowMs() / 1000));
|
|
111
|
+
if (nowS <= given.updatedAt + STOCK_FRESH_S) return keep("skip");
|
|
112
|
+
const ask = p.ask ?? (() => askStockRefresh({ chain: p.chain, quote: p.quote, url: p.url, fetch: p.fetch }));
|
|
113
|
+
const a = await settled(ask, STOCK_ASK_TIMEOUT_MS);
|
|
114
|
+
if (a === null || a === undefined || a === LATE) return keep("none");
|
|
115
|
+
let target: bigint | null;
|
|
116
|
+
if (a.status === "written") target = a.updatedAt;
|
|
117
|
+
else if (a.status === "cooldown") target = null;
|
|
118
|
+
else if ((a.status === "fresh" || a.status === "within") && a.updatedAt !== null && a.updatedAt > given.updatedAt) target = a.updatedAt;
|
|
119
|
+
else return keep(a.status);
|
|
120
|
+
const until = nowMs() + STOCK_WAIT_MS;
|
|
121
|
+
let seen: RegistryPrice | null = null;
|
|
122
|
+
for (let reads = 0; reads <= Math.ceil(STOCK_WAIT_MS / STOCK_POLL_MS); reads++) {
|
|
123
|
+
const e = await settled(p.readEntry, until - nowMs());
|
|
124
|
+
if (e === LATE) break;
|
|
125
|
+
if (e !== null && e !== undefined && e.usdCentsX64 > 0n && e.updatedAt > given.updatedAt && (seen === null || e.updatedAt >= seen.updatedAt)) seen = e;
|
|
126
|
+
if (seen !== null && (target === null || seen.updatedAt >= target)) break;
|
|
127
|
+
if (nowMs() >= until) break;
|
|
128
|
+
await sleep(STOCK_POLL_MS);
|
|
129
|
+
}
|
|
130
|
+
return seen === null ? keep(a.status) : { entry: seen, status: a.status, moved: true };
|
|
131
|
+
}
|