@ringssol/sdk 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +52 -2
- package/dist/api.d.ts +15 -0
- package/dist/api.js +43 -0
- package/dist/api.js.map +1 -0
- package/dist/bridge.d.ts +89 -0
- package/dist/bridge.js +115 -0
- package/dist/bridge.js.map +1 -0
- package/dist/chain.d.ts +49 -0
- package/dist/chain.js +205 -0
- package/dist/chain.js.map +1 -0
- package/dist/codec.d.ts +5 -0
- package/dist/codec.js +43 -0
- package/dist/codec.js.map +1 -0
- package/dist/data.d.ts +31 -0
- package/dist/data.js +30 -0
- package/dist/data.js.map +1 -0
- package/dist/dex.d.ts +104 -0
- package/dist/dex.js +162 -0
- package/dist/dex.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/keys.d.ts +20 -0
- package/dist/keys.js +47 -0
- package/dist/keys.js.map +1 -0
- package/dist/launch.d.ts +80 -0
- package/dist/launch.js +92 -0
- package/dist/launch.js.map +1 -0
- package/dist/rings.d.ts +48 -0
- package/dist/rings.js +115 -0
- package/dist/rings.js.map +1 -0
- package/package.json +44 -4
- package/src/api.ts +44 -0
- package/src/bridge.ts +175 -0
- package/src/chain.ts +266 -0
- package/src/codec.ts +40 -0
- package/src/data.ts +42 -0
- package/src/dex.ts +250 -0
- package/src/index.ts +7 -0
- package/src/keys.ts +57 -0
- package/src/launch.ts +148 -0
- package/src/rings.ts +133 -0
package/dist/codec.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
2
|
+
export const utf8 = (s) => new TextEncoder().encode(s);
|
|
3
|
+
export const b64 = (bytes) => Buffer.from(bytes).toString("base64");
|
|
4
|
+
export const fromB64 = (s) => new Uint8Array(Buffer.from(s, "base64"));
|
|
5
|
+
export function base58(bytes) {
|
|
6
|
+
let x = 0n;
|
|
7
|
+
for (const b of bytes)
|
|
8
|
+
x = (x << 8n) | BigInt(b);
|
|
9
|
+
let s = "";
|
|
10
|
+
while (x > 0n) {
|
|
11
|
+
s = B58[Number(x % 58n)] + s;
|
|
12
|
+
x /= 58n;
|
|
13
|
+
}
|
|
14
|
+
for (const b of bytes) {
|
|
15
|
+
if (b !== 0)
|
|
16
|
+
break;
|
|
17
|
+
s = "1" + s;
|
|
18
|
+
}
|
|
19
|
+
return s;
|
|
20
|
+
}
|
|
21
|
+
export function fromBase58(s) {
|
|
22
|
+
if (s.length > 512)
|
|
23
|
+
throw new Error("invalid base58");
|
|
24
|
+
let x = 0n;
|
|
25
|
+
for (const c of s) {
|
|
26
|
+
const i = B58.indexOf(c);
|
|
27
|
+
if (i < 0)
|
|
28
|
+
throw new Error("invalid base58");
|
|
29
|
+
x = x * 58n + BigInt(i);
|
|
30
|
+
}
|
|
31
|
+
const bytes = [];
|
|
32
|
+
while (x > 0n) {
|
|
33
|
+
bytes.unshift(Number(x & 0xffn));
|
|
34
|
+
x >>= 8n;
|
|
35
|
+
}
|
|
36
|
+
for (const c of s) {
|
|
37
|
+
if (c !== "1")
|
|
38
|
+
break;
|
|
39
|
+
bytes.unshift(0);
|
|
40
|
+
}
|
|
41
|
+
return Uint8Array.from(bytes);
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=codec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,MAAM,GAAG,GAAG,4DAA4D,CAAC;AAEzE,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAC/D,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,KAAiB,EAAE,EAAE,CAAC,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1B,CAAC;IACD,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC;QACd,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC;QACjC,CAAC,KAAK,EAAE,CAAC;IACX,CAAC;IACD,KAAK,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QAClB,IAAI,CAAC,KAAK,GAAG;YAAE,MAAM;QACrB,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IACnB,CAAC;IACD,OAAO,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC"}
|
package/dist/data.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface LaunchCoin {
|
|
2
|
+
mint: string;
|
|
3
|
+
name: string;
|
|
4
|
+
symbol: string;
|
|
5
|
+
[field: string]: unknown;
|
|
6
|
+
}
|
|
7
|
+
/** Rings Launch's board: every coin launched there, with its pair and market data. */
|
|
8
|
+
export declare function board(opts?: {
|
|
9
|
+
api?: string;
|
|
10
|
+
}): Promise<{
|
|
11
|
+
tokens: LaunchCoin[];
|
|
12
|
+
pairs: unknown[];
|
|
13
|
+
usd: Record<string, number | null>;
|
|
14
|
+
feeBps: number;
|
|
15
|
+
}>;
|
|
16
|
+
/** One launched coin. */
|
|
17
|
+
export declare function coin(mint: string, opts?: {
|
|
18
|
+
api?: string;
|
|
19
|
+
}): Promise<Record<string, unknown>>;
|
|
20
|
+
/** Open blocks on the OTC desk. The sellers are never named. */
|
|
21
|
+
export declare function blocks(opts?: {
|
|
22
|
+
api?: string;
|
|
23
|
+
}): Promise<Record<string, unknown>>;
|
|
24
|
+
/** Open buy requests on the OTC desk. */
|
|
25
|
+
export declare function requests(opts?: {
|
|
26
|
+
api?: string;
|
|
27
|
+
}): Promise<Record<string, unknown>>;
|
|
28
|
+
/** What Rings has earned, by product. */
|
|
29
|
+
export declare function revenue(opts?: {
|
|
30
|
+
api?: string;
|
|
31
|
+
}): Promise<Record<string, unknown>>;
|
package/dist/data.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Public Rings data: no account, no keys.
|
|
2
|
+
const BASE = "https://rings.run";
|
|
3
|
+
async function get(path, base = BASE) {
|
|
4
|
+
const res = await fetch(base + path);
|
|
5
|
+
const data = (await res.json().catch(() => ({})));
|
|
6
|
+
if (!res.ok)
|
|
7
|
+
throw new Error(data.error ?? `Rings answered ${res.status}`);
|
|
8
|
+
return data;
|
|
9
|
+
}
|
|
10
|
+
/** Rings Launch's board: every coin launched there, with its pair and market data. */
|
|
11
|
+
export async function board(opts = {}) {
|
|
12
|
+
return get("/api/launch/tokens", opts.api);
|
|
13
|
+
}
|
|
14
|
+
/** One launched coin. */
|
|
15
|
+
export async function coin(mint, opts = {}) {
|
|
16
|
+
return get(`/api/launch/tokens/${mint}`, opts.api);
|
|
17
|
+
}
|
|
18
|
+
/** Open blocks on the OTC desk. The sellers are never named. */
|
|
19
|
+
export async function blocks(opts = {}) {
|
|
20
|
+
return get("/api/blocks", opts.api);
|
|
21
|
+
}
|
|
22
|
+
/** Open buy requests on the OTC desk. */
|
|
23
|
+
export async function requests(opts = {}) {
|
|
24
|
+
return get("/api/requests", opts.api);
|
|
25
|
+
}
|
|
26
|
+
/** What Rings has earned, by product. */
|
|
27
|
+
export async function revenue(opts = {}) {
|
|
28
|
+
return get("/api/revenue", opts.api);
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=data.js.map
|
package/dist/data.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data.js","sourceRoot":"","sources":["../src/data.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAE1C,MAAM,IAAI,GAAG,mBAAmB,CAAC;AAEjC,KAAK,UAAU,GAAG,CAAI,IAAY,EAAE,IAAI,GAAG,IAAI;IAC7C,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,IAAI,GAAG,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
|
|
4
|
+
sell: string;
|
|
5
|
+
buy: string;
|
|
6
|
+
amount: bigint;
|
|
7
|
+
/** What the route gives at the quoted price, before Rings' fee on a sale. 0n on devnet, which has no market. */
|
|
8
|
+
out: bigint;
|
|
9
|
+
/** The least the swap may give: below it, Rings refunds instead. */
|
|
10
|
+
minOut: bigint;
|
|
11
|
+
slippageBps: number;
|
|
12
|
+
route: string[];
|
|
13
|
+
/** Token accounts the route opens. Their rent is part of `costs` and comes back to the private balance. */
|
|
14
|
+
accounts: number;
|
|
15
|
+
deliver: "private" | "wallet";
|
|
16
|
+
/** For deliver "wallet": the address the coins go to. */
|
|
17
|
+
to?: string;
|
|
18
|
+
/** Rings' network costs in lamports, paid with the swap. */
|
|
19
|
+
costs: bigint;
|
|
20
|
+
/** Rings' fee in lamports, paid with the swap; null on a sale, where it comes out of the SOL the sale gives. */
|
|
21
|
+
fee: bigint | null;
|
|
22
|
+
feeBps: number;
|
|
23
|
+
}
|
|
24
|
+
export interface SwapState {
|
|
25
|
+
id: string;
|
|
26
|
+
state: string;
|
|
27
|
+
error: string | null;
|
|
28
|
+
/** What the swap gave, and what reached you once Rings' fee on a sale is taken. */
|
|
29
|
+
out: bigint | null;
|
|
30
|
+
received: bigint | null;
|
|
31
|
+
swapSignature: string | null;
|
|
32
|
+
deliverSignature: string | null;
|
|
33
|
+
}
|
|
34
|
+
export type OrderKind = "limit" | "stop" | "dca";
|
|
35
|
+
export interface OrderInput {
|
|
36
|
+
kind: OrderKind;
|
|
37
|
+
sell: string;
|
|
38
|
+
buy: string;
|
|
39
|
+
amount: bigint;
|
|
40
|
+
/** limit: the least the whole amount must give. stop: sell once it would give this much or less. */
|
|
41
|
+
targetOut?: bigint;
|
|
42
|
+
/** dca: how many rounds, and seconds between them. */
|
|
43
|
+
rounds?: number;
|
|
44
|
+
every?: number;
|
|
45
|
+
/** limit and stop: seconds the order stays open. */
|
|
46
|
+
expiresIn?: number;
|
|
47
|
+
slippageBps?: number;
|
|
48
|
+
deliver?: "private" | "wallet";
|
|
49
|
+
to?: string;
|
|
50
|
+
}
|
|
51
|
+
export interface Order {
|
|
52
|
+
id: string;
|
|
53
|
+
kind: OrderKind;
|
|
54
|
+
/** open, closing, then closed; an order never paid for ends unpaid. */
|
|
55
|
+
state: string;
|
|
56
|
+
/** Once closing: filled, cancelled, expired or failed. */
|
|
57
|
+
outcome: string | null;
|
|
58
|
+
inputMint: string;
|
|
59
|
+
outputMint: string;
|
|
60
|
+
amount: string;
|
|
61
|
+
remaining: string;
|
|
62
|
+
targetOut: string | null;
|
|
63
|
+
filledIn: string | null;
|
|
64
|
+
filledOut: string | null;
|
|
65
|
+
expiresAt: string | null;
|
|
66
|
+
error: string | null;
|
|
67
|
+
[field: string]: unknown;
|
|
68
|
+
}
|
|
69
|
+
/** What the dex needs from an account: where refunds go, what it holds, and paying Rings privately. */
|
|
70
|
+
export interface Payer {
|
|
71
|
+
address: string;
|
|
72
|
+
wallet: string;
|
|
73
|
+
holds(mint: string): Promise<bigint>;
|
|
74
|
+
pay(to: string, mint: string, amount: bigint): Promise<Sent>;
|
|
75
|
+
}
|
|
76
|
+
export declare class Dex {
|
|
77
|
+
private readonly api;
|
|
78
|
+
private readonly network;
|
|
79
|
+
private readonly payer;
|
|
80
|
+
constructor(api: Api, network: Network, payer: Payer);
|
|
81
|
+
quote(input: {
|
|
82
|
+
sell: string;
|
|
83
|
+
buy: string;
|
|
84
|
+
amount: bigint;
|
|
85
|
+
slippageBps?: number;
|
|
86
|
+
deliver?: "private" | "wallet";
|
|
87
|
+
to?: string;
|
|
88
|
+
}): Promise<Quote>;
|
|
89
|
+
/** Pays for the swap privately and waits until Rings has delivered (or refunded) it. */
|
|
90
|
+
swap(q: Quote, opts?: {
|
|
91
|
+
timeoutMs?: number;
|
|
92
|
+
}): Promise<SwapState>;
|
|
93
|
+
swapStatus(id: string): Promise<SwapState>;
|
|
94
|
+
wait(id: string, timeoutMs?: number): Promise<SwapState>;
|
|
95
|
+
/** Places a limit, stop or DCA order, paid privately up front. What it doesn't use comes back to the private balance. */
|
|
96
|
+
order(o: OrderInput): Promise<Order>;
|
|
97
|
+
/** This account's orders until they close. A closed order no longer names its account: keep its id to look it up. */
|
|
98
|
+
orders(): Promise<Order[]>;
|
|
99
|
+
orderStatus(id: string): Promise<Order>;
|
|
100
|
+
/** Closes an open order; what it holds comes back to the private balance. */
|
|
101
|
+
cancel(id: string): Promise<void>;
|
|
102
|
+
private payAll;
|
|
103
|
+
private tell;
|
|
104
|
+
}
|
package/dist/dex.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { SOL } from "./chain.js";
|
|
2
|
+
const JUPITER = "https://lite-api.jup.ag/swap/v1/quote";
|
|
3
|
+
const WSOL = "So11111111111111111111111111111111111111112";
|
|
4
|
+
const DONE = ["done", "delivered", "refunded", "failed"];
|
|
5
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
6
|
+
export class Dex {
|
|
7
|
+
api;
|
|
8
|
+
network;
|
|
9
|
+
payer;
|
|
10
|
+
constructor(api, network, payer) {
|
|
11
|
+
this.api = api;
|
|
12
|
+
this.network = network;
|
|
13
|
+
this.payer = payer;
|
|
14
|
+
}
|
|
15
|
+
async quote(input) {
|
|
16
|
+
const slippageBps = input.slippageBps ?? 50;
|
|
17
|
+
const deliver = input.deliver ?? "private";
|
|
18
|
+
const route = this.network === "mainnet" ? await jupiterQuote(input.sell, input.buy, input.amount, slippageBps) : { out: 0n, minOut: 1n, route: ["devnet"], accounts: 2 };
|
|
19
|
+
const qs = new URLSearchParams({ in: feed(input.sell), out: feed(input.buy), deliver, accounts: String(route.accounts), amount: String(input.amount), minOut: String(route.minOut) });
|
|
20
|
+
if (deliver === "wallet")
|
|
21
|
+
qs.set("wallet", input.to ?? this.payer.wallet);
|
|
22
|
+
const c = await this.api.get(`/api/dex/costs?${qs}`);
|
|
23
|
+
if (!c.price)
|
|
24
|
+
throw new Error(c.error ?? "Rings couldn't price this swap");
|
|
25
|
+
if (c.feeError)
|
|
26
|
+
throw new Error(c.feeError);
|
|
27
|
+
return { sell: input.sell, buy: input.buy, amount: input.amount, slippageBps, deliver, to: input.to, ...route, costs: BigInt(c.price), fee: c.fee ? BigInt(c.fee) : null, feeBps: c.feeBps ?? 0 };
|
|
28
|
+
}
|
|
29
|
+
/** Pays for the swap privately and waits until Rings has delivered (or refunded) it. */
|
|
30
|
+
async swap(q, opts = {}) {
|
|
31
|
+
const made = await this.api.post("/api/dex/swaps", {
|
|
32
|
+
inputMint: feed(q.sell),
|
|
33
|
+
outputMint: feed(q.buy),
|
|
34
|
+
amount: q.amount,
|
|
35
|
+
minOut: q.minOut,
|
|
36
|
+
slippageBps: q.slippageBps,
|
|
37
|
+
routeAccounts: q.accounts,
|
|
38
|
+
deliver: q.deliver,
|
|
39
|
+
wallet: q.deliver === "wallet" ? (q.to ?? this.payer.wallet) : undefined,
|
|
40
|
+
deliverTo: this.payer.address,
|
|
41
|
+
});
|
|
42
|
+
checkAsked(made, q);
|
|
43
|
+
const paid = await this.payAll(made);
|
|
44
|
+
await this.tell(`/api/dex/swaps/${made.id}`, paid);
|
|
45
|
+
return this.wait(made.id, opts.timeoutMs);
|
|
46
|
+
}
|
|
47
|
+
async swapStatus(id) {
|
|
48
|
+
const s = await this.api.get(`/api/dex/swaps/${id}`);
|
|
49
|
+
const big = (v) => (v == null ? null : BigInt(v));
|
|
50
|
+
return { id, state: s.state, error: s.error, out: big(s.outAmount), received: big(s.received), swapSignature: s.swapSig, deliverSignature: s.deliverSig };
|
|
51
|
+
}
|
|
52
|
+
async wait(id, timeoutMs = 300_000) {
|
|
53
|
+
const end = Date.now() + timeoutMs;
|
|
54
|
+
for (;;) {
|
|
55
|
+
const s = await this.swapStatus(id);
|
|
56
|
+
if (DONE.includes(s.state) || Date.now() > end)
|
|
57
|
+
return s;
|
|
58
|
+
await sleep(3000);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Places a limit, stop or DCA order, paid privately up front. What it doesn't use comes back to the private balance. */
|
|
62
|
+
async order(o) {
|
|
63
|
+
const deliver = o.deliver ?? "private";
|
|
64
|
+
const made = await this.api.post("/api/dex/orders", {
|
|
65
|
+
kind: o.kind,
|
|
66
|
+
inputMint: feed(o.sell),
|
|
67
|
+
outputMint: feed(o.buy),
|
|
68
|
+
amount: o.amount,
|
|
69
|
+
targetOut: o.targetOut,
|
|
70
|
+
slippageBps: o.slippageBps ?? 100,
|
|
71
|
+
rounds: o.rounds,
|
|
72
|
+
intervalS: o.every,
|
|
73
|
+
expiresS: o.expiresIn,
|
|
74
|
+
routeAccounts: 2,
|
|
75
|
+
deliver,
|
|
76
|
+
wallet: deliver === "wallet" ? (o.to ?? this.payer.wallet) : undefined,
|
|
77
|
+
deliverTo: this.payer.address,
|
|
78
|
+
});
|
|
79
|
+
const coin = made.payments.filter((p) => p.mint !== SOL).reduce((t, p) => t + BigInt(p.amount), 0n);
|
|
80
|
+
if (o.sell !== SOL && coin !== o.amount)
|
|
81
|
+
throw new Error("Rings asked for an unexpected payment. Nothing was paid.");
|
|
82
|
+
const paid = await this.payAll(made);
|
|
83
|
+
await this.tell(`/api/dex/orders/${made.id}`, paid);
|
|
84
|
+
return this.orderStatus(made.id);
|
|
85
|
+
}
|
|
86
|
+
/** This account's orders until they close. A closed order no longer names its account: keep its id to look it up. */
|
|
87
|
+
async orders() {
|
|
88
|
+
return (await this.api.get("/api/dex/orders")).orders;
|
|
89
|
+
}
|
|
90
|
+
async orderStatus(id) {
|
|
91
|
+
return this.api.get(`/api/dex/orders/${id}`);
|
|
92
|
+
}
|
|
93
|
+
/** Closes an open order; what it holds comes back to the private balance. */
|
|
94
|
+
async cancel(id) {
|
|
95
|
+
await this.api.post(`/api/dex/orders/${id}`, { action: "cancel" });
|
|
96
|
+
}
|
|
97
|
+
// Every coin is checked first: a shortfall found after the first payment would leave that payment with Rings until
|
|
98
|
+
// the swap is refunded. Then SOL first, so a failed coin payment strands only the small SOL part.
|
|
99
|
+
async payAll(made) {
|
|
100
|
+
const want = new Map();
|
|
101
|
+
for (const p of made.payments)
|
|
102
|
+
want.set(p.mint, (want.get(p.mint) ?? 0n) + BigInt(p.amount));
|
|
103
|
+
for (const [mint, amount] of want) {
|
|
104
|
+
const have = await this.payer.holds(mint);
|
|
105
|
+
if (have < amount)
|
|
106
|
+
throw new Error(`The private balance holds ${have} of ${mint}, the payment needs ${amount}. Nothing was paid.`);
|
|
107
|
+
}
|
|
108
|
+
const paid = [];
|
|
109
|
+
for (const p of [...made.payments].sort((a, b) => Number(b.mint === SOL) - Number(a.mint === SOL))) {
|
|
110
|
+
const sent = await this.payer.pay(made.payTo, p.mint, BigInt(p.amount));
|
|
111
|
+
paid.push({ signature: sent.signature, slot: String(sent.slot) });
|
|
112
|
+
}
|
|
113
|
+
return paid;
|
|
114
|
+
}
|
|
115
|
+
// Rings answers 409 until its node has seen the payments.
|
|
116
|
+
async tell(path, payments) {
|
|
117
|
+
for (let i = 1;; i++) {
|
|
118
|
+
try {
|
|
119
|
+
return await this.api.post(path, { action: "paid", payments });
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
if (e.status !== 409 || i >= 30)
|
|
123
|
+
throw e;
|
|
124
|
+
await sleep(1500);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const feed = (mint) => (mint === SOL ? WSOL : mint);
|
|
130
|
+
async function jupiterQuote(sell, buy, amount, slippageBps) {
|
|
131
|
+
const res = await fetch(`${JUPITER}?inputMint=${feed(sell)}&outputMint=${feed(buy)}&amount=${amount}&slippageBps=${slippageBps}&restrictIntermediateTokens=false`);
|
|
132
|
+
const q = (await res.json().catch(() => ({})));
|
|
133
|
+
if (!q.outAmount || !q.otherAmountThreshold)
|
|
134
|
+
throw new Error(q.error ? `No route: ${q.error}` : "No route for this swap right now");
|
|
135
|
+
const hops = (q.routePlan ?? []).map((p) => p.swapInfo ?? {});
|
|
136
|
+
// Every token account the route touches, but the one sold from, is opened for the swap and closed after.
|
|
137
|
+
const mints = new Set(hops.flatMap((h) => [h.inputMint, h.outputMint]).filter((m) => Boolean(m)));
|
|
138
|
+
if (sell !== SOL)
|
|
139
|
+
mints.delete(sell);
|
|
140
|
+
return {
|
|
141
|
+
out: BigInt(q.outAmount),
|
|
142
|
+
minOut: BigInt(q.otherAmountThreshold),
|
|
143
|
+
route: [...new Set(hops.map((h) => h.label).filter((l) => Boolean(l)))],
|
|
144
|
+
accounts: Math.min(8, Math.max(1, mints.size)),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/** Rings may ask only for the coin sold, exactly, and SOL for its costs and fee as quoted, with a little room for costs. */
|
|
148
|
+
function checkAsked(made, q) {
|
|
149
|
+
const total = new Map();
|
|
150
|
+
for (const p of made.payments)
|
|
151
|
+
total.set(p.mint, (total.get(p.mint) ?? 0n) + BigInt(p.amount));
|
|
152
|
+
const fee = made.fee ? BigInt(made.fee) : 0n;
|
|
153
|
+
const quoted = q.fee ?? 0n;
|
|
154
|
+
// A two-coin swap's fee is priced again when it is made: half again of room for the market moving.
|
|
155
|
+
const room = q.costs + q.costs / 4n + 1000000n + fee;
|
|
156
|
+
const sol = total.get(SOL) ?? 0n;
|
|
157
|
+
const ok = fee <= quoted + quoted / 2n + 10000n &&
|
|
158
|
+
(q.sell === SOL ? total.size === 1 && sol > q.amount && sol <= q.amount + room : total.size === 2 && total.get(q.sell) === q.amount && sol > 0n && sol <= room);
|
|
159
|
+
if (!ok)
|
|
160
|
+
throw new Error("Rings asked for more than the quote. Nothing was paid.");
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=dex.js.map
|
package/dist/dex.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dex.js","sourceRoot":"","sources":["../src/dex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AAEjC,MAAM,OAAO,GAAG,uCAAuC,CAAC;AACxD,MAAM,IA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GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC7C,MAAM,MAAM,GAAG,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC;IAC3B,mGAAmG;IACnG,MAAM,IAAI,GAAG,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,GAAG,EAAE,GAAG,QAAU,GAAG,GAAG,CAAC;IACvD,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;IACjC,MAAM,EAAE,GACN,GAAG,IAAI,MAAM,GAAG,MAAM,GAAG,EAAE,GAAG,MAAO;QACrC,CAAC,CAAC,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,IAAI,GAAG,GAAG,EAAE,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC;IAClK,IAAI,CAAC,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;AACrF,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { Rings, type ConnectOptions, type Holding } from "./rings.js";
|
|
2
|
+
export { RingsError } from "./api.js";
|
|
3
|
+
export { SOL, type Network, type Sent } from "./chain.js";
|
|
4
|
+
export type { Order, OrderInput, OrderKind, Quote, SwapState } from "./dex.js";
|
|
5
|
+
export type { CreatorRewards, LaunchInput, LaunchState, Shield } from "./launch.js";
|
|
6
|
+
export type { BridgeIn, BridgeInInput, BridgeOutInput, BridgeQuote, BridgeState } from "./bridge.js";
|
|
7
|
+
export * as data from "./data.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAqC,MAAM,YAAY,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,EAAE,GAAG,EAA2B,MAAM,YAAY,CAAC;AAI1D,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC"}
|
package/dist/keys.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare function keyMessage(domain: string, wallet: string): string;
|
|
2
|
+
export declare function loginMessage(f: {
|
|
3
|
+
domain: string;
|
|
4
|
+
wallet: string;
|
|
5
|
+
nonce: string;
|
|
6
|
+
issuedAt: string;
|
|
7
|
+
x25519: string;
|
|
8
|
+
ed25519: string;
|
|
9
|
+
account: string;
|
|
10
|
+
}): string;
|
|
11
|
+
export interface AccountKeys {
|
|
12
|
+
/** Seed of the key that owns the private balance. */
|
|
13
|
+
account: Uint8Array;
|
|
14
|
+
x25519Public: Uint8Array;
|
|
15
|
+
edPublic: Uint8Array;
|
|
16
|
+
}
|
|
17
|
+
/** The account's keys from the wallet's signature over keyMessage. */
|
|
18
|
+
export declare function accountKeys(wallet: string, signature: Uint8Array): AccountKeys;
|
|
19
|
+
/** The site whose keys a host uses: the Rings sites all share rings.run's. */
|
|
20
|
+
export declare const keyHostOf: (host: string) => string;
|
package/dist/keys.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ed25519, x25519 } from "@noble/curves/ed25519";
|
|
2
|
+
import { hkdf } from "@noble/hashes/hkdf";
|
|
3
|
+
import { sha256 } from "@noble/hashes/sha2";
|
|
4
|
+
import { utf8 } from "./codec.js";
|
|
5
|
+
// Both messages must match the ones rings.run asks wallets to sign, byte for byte: the same wallet then opens the same
|
|
6
|
+
// Rings account and private balance here and on the site.
|
|
7
|
+
export function keyMessage(domain, wallet) {
|
|
8
|
+
return [
|
|
9
|
+
"Rings key v1",
|
|
10
|
+
"",
|
|
11
|
+
`Domain: ${domain}`,
|
|
12
|
+
`Wallet: ${wallet}`,
|
|
13
|
+
"",
|
|
14
|
+
"Sign to unlock your Rings keys on this device.",
|
|
15
|
+
"This signature stays in your browser and moves no funds.",
|
|
16
|
+
"Never sign this message on any other site.",
|
|
17
|
+
].join("\n");
|
|
18
|
+
}
|
|
19
|
+
export function loginMessage(f) {
|
|
20
|
+
return [
|
|
21
|
+
`${f.domain} wants you to sign in to Rings with your Solana account:`,
|
|
22
|
+
f.wallet,
|
|
23
|
+
"",
|
|
24
|
+
"Sign in to Rings. This moves no funds and costs nothing.",
|
|
25
|
+
"",
|
|
26
|
+
`Nonce: ${f.nonce}`,
|
|
27
|
+
`Issued At: ${f.issuedAt}`,
|
|
28
|
+
`Messaging key: ${f.x25519}`,
|
|
29
|
+
`Message signing key: ${f.ed25519}`,
|
|
30
|
+
`Account key: ${f.account}`,
|
|
31
|
+
].join("\n");
|
|
32
|
+
}
|
|
33
|
+
/** The account's keys from the wallet's signature over keyMessage. */
|
|
34
|
+
export function accountKeys(wallet, signature) {
|
|
35
|
+
if (signature.length !== 64)
|
|
36
|
+
throw new Error("expected a 64-byte Ed25519 signature");
|
|
37
|
+
const root = hkdf(sha256, signature, utf8("rings/root/v1"), utf8(wallet), 32);
|
|
38
|
+
const expand = (info) => hkdf(sha256, root, undefined, utf8(info), 32);
|
|
39
|
+
return {
|
|
40
|
+
account: expand("rings/account/v1"),
|
|
41
|
+
x25519Public: x25519.getPublicKey(expand("rings/msg/x25519/v1")),
|
|
42
|
+
edPublic: ed25519.getPublicKey(expand("rings/msg/ed25519/v1")),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** The site whose keys a host uses: the Rings sites all share rings.run's. */
|
|
46
|
+
export const keyHostOf = (host) => host.toLowerCase().replace(/^(otc\.|launch\.|dex\.|terminal\.)/, "");
|
|
47
|
+
//# sourceMappingURL=keys.js.map
|
package/dist/keys.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAElC,uHAAuH;AACvH,0DAA0D;AAE1D,MAAM,UAAU,UAAU,CAAC,MAAc,EAAE,MAAc;IACvD,OAAO;QACL,cAAc;QACd,EAAE;QACF,WAAW,MAAM,EAAE;QACnB,WAAW,MAAM,EAAE;QACnB,EAAE;QACF,gDAAgD;QAChD,0DAA0D;QAC1D,4CAA4C;KAC7C,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,CAAwH;IACnJ,OAAO;QACL,GAAG,CAAC,CAAC,MAAM,0DAA0D;QACrE,CAAC,CAAC,MAAM;QACR,EAAE;QACF,0DAA0D;QAC1D,EAAE;QACF,UAAU,CAAC,CAAC,KAAK,EAAE;QACnB,cAAc,CAAC,CAAC,QAAQ,EAAE;QAC1B,kBAAkB,CAAC,CAAC,MAAM,EAAE;QAC5B,wBAAwB,CAAC,CAAC,OAAO,EAAE;QACnC,gBAAgB,CAAC,CAAC,OAAO,EAAE;KAC5B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC;AASD,sEAAsE;AACtE,MAAM,UAAU,WAAW,CAAC,MAAc,EAAE,SAAqB;IAC/D,IAAI,SAAS,CAAC,MAAM,KAAK,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;IACrF,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,CAAC,eAAe,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;IAC9E,MAAM,MAAM,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;IAC/E,OAAO;QACL,OAAO,EAAE,MAAM,CAAC,kBAAkB,CAAC;QACnC,YAAY,EAAE,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAAC;QAChE,QAAQ,EAAE,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,sBAAsB,CAAC,CAAC;KAC/D,CAAC;AACJ,CAAC;AAED,8EAA8E;AAC9E,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,oCAAoC,EAAE,EAAE,CAAC,CAAC"}
|
package/dist/launch.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Api } from "./api.js";
|
|
2
|
+
import type { Payer } from "./dex.js";
|
|
3
|
+
export interface LaunchInput {
|
|
4
|
+
name: string;
|
|
5
|
+
/** Letters and numbers, 10 at most. Its case is kept. */
|
|
6
|
+
symbol: string;
|
|
7
|
+
description?: string;
|
|
8
|
+
website?: string;
|
|
9
|
+
x?: string;
|
|
10
|
+
telegram?: string;
|
|
11
|
+
/** PNG, JPG or WebP, 1 MB at most. */
|
|
12
|
+
image: Uint8Array;
|
|
13
|
+
/** The coin it trades against, by ticker: SOL (the default), USDC, RINGS or another Rings Launch pair. */
|
|
14
|
+
pair?: string;
|
|
15
|
+
venue?: "meteora" | "raydium" | "pump";
|
|
16
|
+
/** Meteora and LaunchLab: "liquidity" starts the curve deeper. */
|
|
17
|
+
mode?: "standard" | "liquidity";
|
|
18
|
+
/** SOL to buy the coin with as it launches; the coins go to the private balance. */
|
|
19
|
+
firstBuy?: bigint;
|
|
20
|
+
/** Shares of Rings' trading fee, in whole percents as basis points, together at most 5000: the coin's holders, its creator, and buyback-and-burn of the coin. */
|
|
21
|
+
holdersBps?: number;
|
|
22
|
+
creatorBps?: number;
|
|
23
|
+
burnBps?: number;
|
|
24
|
+
/** Launch even though another coin shields this ticker or name, paying the shield fee. */
|
|
25
|
+
overShield?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface LaunchState {
|
|
28
|
+
id: string;
|
|
29
|
+
/** quoted, paid, then launched and done; or failed, or refunded. */
|
|
30
|
+
state: string;
|
|
31
|
+
error: string | null;
|
|
32
|
+
mint: string;
|
|
33
|
+
/** The first buy's coins, delivered to the private balance. */
|
|
34
|
+
tokens: bigint | null;
|
|
35
|
+
launchSignature: string | null;
|
|
36
|
+
}
|
|
37
|
+
export interface Shield {
|
|
38
|
+
/** The coin that shields the name or ticker now, if any; `until` in Unix seconds, null while its launch is under way. */
|
|
39
|
+
shield: {
|
|
40
|
+
mint: string;
|
|
41
|
+
symbol: string;
|
|
42
|
+
name: string;
|
|
43
|
+
venue: string;
|
|
44
|
+
until: number | null;
|
|
45
|
+
matched: string;
|
|
46
|
+
} | null;
|
|
47
|
+
/** What launching over a shield costs, in lamports, all of it to the $RINGS flywheel. */
|
|
48
|
+
fee: bigint;
|
|
49
|
+
hours: number;
|
|
50
|
+
}
|
|
51
|
+
export interface CreatorRewards {
|
|
52
|
+
isCreator: boolean;
|
|
53
|
+
claimable?: bigint;
|
|
54
|
+
claiming?: bigint;
|
|
55
|
+
claimed?: bigint;
|
|
56
|
+
minClaim?: bigint;
|
|
57
|
+
[field: string]: unknown;
|
|
58
|
+
}
|
|
59
|
+
export declare class Launch {
|
|
60
|
+
private readonly api;
|
|
61
|
+
private readonly payer;
|
|
62
|
+
constructor(api: Api, payer: Payer);
|
|
63
|
+
/** Whether a coin launched on Rings in the last few days shields this name or ticker, and the fee to launch over it. */
|
|
64
|
+
shield(name: string, symbol: string): Promise<Shield>;
|
|
65
|
+
/** Launches a coin, paid privately, and waits until it trades. Rings launches it from a one-time key: nothing on chain names you. */
|
|
66
|
+
create(input: LaunchInput, opts?: {
|
|
67
|
+
timeoutMs?: number;
|
|
68
|
+
}): Promise<LaunchState>;
|
|
69
|
+
status(id: string): Promise<LaunchState>;
|
|
70
|
+
wait(id: string, timeoutMs?: number): Promise<LaunchState>;
|
|
71
|
+
/** Before the coin exists, a paid launch can be called off: the payment comes back to the private balance. */
|
|
72
|
+
refund(id: string): Promise<void>;
|
|
73
|
+
/** The creator's share of a coin's fees, when this account launched it. Anyone else is only told it isn't theirs. */
|
|
74
|
+
rewards(mint: string): Promise<CreatorRewards>;
|
|
75
|
+
/** Asks for the creator's share; Rings pays it into the private balance kept at launch, within minutes. */
|
|
76
|
+
claim(mint: string): Promise<{
|
|
77
|
+
id: string;
|
|
78
|
+
lamports: bigint;
|
|
79
|
+
}>;
|
|
80
|
+
}
|