机器人 API(自动做市商)
机器人可以通过本页面使用的同一 API 成为做市商:用其 Nostr 密钥签名登录,并以签名的 NIP-69 订单发布报价。要更新价格,用相同的 d 和更新的时间戳重新发送报价。两个 hub(BTC 和 SPM)只需在本页面用同一密钥登录后连接一次。
- 暂停、恢复或取消:POST a=offer_set,参数 {id, action: pause | resume | cancel}。
- 价格为每 1 BTC(BLAKE2b)的 SPM 数,与市场偏差不超过 10 %;每笔兑换 100,000 至 10,000,000 sats BTC(新做市商在完成 10 笔前上限 500,000);最多 20 个未结报价;有效期最长 30 天。
- 事件时间须与当前时间相差 10 分钟以内。每笔兑换后从你的 hub 自动收取 1 % 佣金;若未支付,你的报价会被暂停。
- 订单也发布在 Nostr 上(kind 38383,标签 y=pyblock):其他系统可以读取。
// PyBLØCK Exchange — minimal maker bot (Node 18+ / Bun; npm i nostr-tools)
import { finalizeEvent, generateSecretKey, getPublicKey } from "nostr-tools/pure";
const API = "https://exchange.pyblock.xyz/api/exchange_swap.php";
const sk = process.env.NSEC_HEX ? Uint8Array.from(Buffer.from(process.env.NSEC_HEX, "hex")) : generateSecretKey() // use your own key: NSEC_HEX=…;
let cookie = "";
async function call(a, body) {
const r = await fetch(`${API}?a=${a}`, body === undefined ? { headers: { cookie } } : {
method: "POST", body: JSON.stringify(body),
headers: { cookie, "content-type": "application/json", origin: "https://exchange.pyblock.xyz" } });
const c = r.headers.get("set-cookie"); if (c) cookie = c.split(";")[0];
return r.json();
}
// 1. log in: sign the challenge (kind 22242) for this domain
const { challenge } = await call("challenge");
const login = finalizeEvent({ kind: 22242, created_at: Math.floor(Date.now() / 1000), content: "",
tags: [["challenge", challenge], ["domain", "exchange.pyblock.xyz"]] }, sk);
console.log("login:", (await call("login", { event: login })).ok, getPublicKey(sk).slice(0, 12));
// 2. the market, then post (or update) an offer: NIP-69 order, kind 38383. Same "d" = update in place.
const info = await call("info"), mid = (info.rates.mkt_buy + info.rates.mkt_sell) / 2;
const now = Math.floor(Date.now() / 1000);
const offer = finalizeEvent({ kind: 38383, created_at: now, content: "", tags: [
["d", "bot-sell-1"], ["k", "sell"], // sell = you sell BTC (BLAKE2b) for SPM; buy = you buy it
["f", "BTC"], ["s", "pending"], ["amt", "5000000"], ["fa", "100000", "10000000"], // total, min, max per swap (sats of BTC BLAKE2b)
["price", (mid * 1.01).toFixed(8)], // SPM per 1 BTC (BLAKE2b); within 10 % of the market
["layer", "lightning"], ["expiration", String(now + 86400)], ["y", "pyblock"], ["z", "order"] ] }, sk);
console.log("post:", JSON.stringify(await call("offer_post", { event: offer })));
// 3. your offers; pause / resume / cancel by id
const mine = await call("offers"); console.log("offers:", JSON.stringify(mine).slice(0, 200));