@shippedcalls/sdk 0.2.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/README.md +3 -0
- package/dist/abi.d.ts +797 -0
- package/dist/abi.js +1021 -0
- package/dist/abi.js.map +1 -1
- package/dist/client.d.ts +41 -1
- package/dist/client.js +54 -2
- package/dist/client.js.map +1 -1
- package/dist/errors.js +7 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +105 -0
- package/package.json +1 -1
- package/skill/SKILL.md +1 -0
- package/skill/references/api.md +1 -0
- package/skill/references/rounds.md +47 -0
- package/skill/scripts/calls.sh +1 -1
- package/src/abi.ts +1022 -0
- package/src/client.ts +56 -3
- package/src/errors.ts +7 -0
- package/src/index.ts +2 -2
- package/src/types.ts +72 -0
package/src/client.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { formatEther, getAddress, isAddress, parseEventLogs, type Abi, type Account, type Address, type Hash, type Transport, type TransactionReceipt } from "viem";
|
|
1
|
+
import { formatEther, getAddress, isAddress, parseEventLogs, stringToHex, type Abi, type Account, type Address, type Hash, type Transport, type TransactionReceipt } from "viem";
|
|
2
2
|
import { privateKeyToAccount } from "viem/accounts";
|
|
3
|
-
import { callAbi, factoryAbi } from "./abi.js";
|
|
3
|
+
import { callAbi, factoryAbi, roundsAbi } from "./abi.js";
|
|
4
4
|
import { hashJson } from "./canonical.js";
|
|
5
5
|
import { API_URL, CHAIN_ID, clientsFor, DEEDS, EXPLORER, FACTORY, PREVIOUS_FACTORIES, rpcEndpoints } from "./chain.js";
|
|
6
6
|
import { CallsError, describe } from "./errors.js";
|
|
7
7
|
import { callTextHash, canonicalPostUrl, normalizeHandle, parsePostUrl, postRefHash, specHash, subjectHash, ZERO_HASH } from "./hashes.js";
|
|
8
8
|
import { DISPUTE_WINDOW, MIN_BET, MIN_OPENER_BOND, odds, quoteOf, type Side } from "./odds.js";
|
|
9
|
-
import type { CallDetail, CallRow, CallStatus, CallType, Leaderboard,
|
|
9
|
+
import type { CallDetail, CallRow, CallStatus, CallType, Leaderboard, Opened, OpenInput, Player, Positions, Prepared, Proof, Quote, RecentBet, RoundsAccount, RoundsFloor, RoundSide, RoundsMarket, Settlement, ShippedAgent, Stored, TapeItem, Tx } from "./types.js";
|
|
10
10
|
|
|
11
11
|
export interface ClientOptions {
|
|
12
12
|
/** The API's origin. https://calls.shipped.bond by default. */
|
|
@@ -52,6 +52,22 @@ const deedsCoreAbi = [
|
|
|
52
52
|
] as const;
|
|
53
53
|
const BOND_OPEN = 1;
|
|
54
54
|
|
|
55
|
+
export function roundSideOf(side: unknown): RoundSide {
|
|
56
|
+
const name = String(side).toLowerCase();
|
|
57
|
+
if (name === "up" || name === "down") return name;
|
|
58
|
+
throw new CallsError("Pick a side: up or down.", "bad_side");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function marketOf(market: unknown): string {
|
|
62
|
+
const name = String(market).toUpperCase();
|
|
63
|
+
if (!/^[A-Z0-9]{2,8}$/.test(name)) throw new CallsError("A market is a symbol such as ETH.", "bad_market");
|
|
64
|
+
return name;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const marketId = (market: string) => stringToHex(market, { size: 32 });
|
|
68
|
+
/** One transaction collects at most this many rounds; the rest wait for the next claim. */
|
|
69
|
+
const CLAIM_BATCH = 40;
|
|
70
|
+
|
|
55
71
|
export function sideOf(side: unknown): Side {
|
|
56
72
|
const name = String(side).toLowerCase();
|
|
57
73
|
if (name === "holds" || name === "1") return "holds";
|
|
@@ -322,6 +338,43 @@ export function createClient(options: ClientOptions = {}) {
|
|
|
322
338
|
});
|
|
323
339
|
},
|
|
324
340
|
|
|
341
|
+
/** Five minutes, up or down. The contract's address comes from the API, so a new deployment needs no new client. */
|
|
342
|
+
rounds: {
|
|
343
|
+
/** Every market's round taking bets, the round running and its latest results, in one read. */
|
|
344
|
+
markets: () => request<RoundsFloor>("/v1/rounds"),
|
|
345
|
+
state: (market: string) => request<RoundsMarket & { open: boolean }>(`/v1/rounds/${marketOf(market)}`),
|
|
346
|
+
/** A wallet's streak, today's stake and profit, what it can collect, its daily limit and its latest rounds. */
|
|
347
|
+
history: (who?: string) => guarded(() => request<RoundsAccount>(`/v1/accounts/${walletOr(who)}/rounds`)),
|
|
348
|
+
|
|
349
|
+
/** Stakes `amount` wei on the round now taking bets. Refuses an amount over `maxStake`, and refuses while nothing is pricing rounds. */
|
|
350
|
+
async bet(market: string, side: RoundSide | string, amount: bigint, { maxStake: cap = maxStake }: { maxStake?: bigint } = {}) {
|
|
351
|
+
const name = marketOf(market);
|
|
352
|
+
const chosen = roundSideOf(side);
|
|
353
|
+
if (amount > cap) throw new CallsError(`${formatEther(amount)} ETH is over the ${formatEther(cap)} ETH limit on one bet. Raise the limit to place it.`, "over_max_stake");
|
|
354
|
+
return guarded(async () => {
|
|
355
|
+
const floor = await request<RoundsFloor>("/v1/rounds");
|
|
356
|
+
if (!floor.markets.some((m) => m.market === name)) throw new CallsError(`Rounds has no ${name} market.`, "UnknownMarket");
|
|
357
|
+
if (!floor.open) throw new CallsError("Rounds aren't taking bets right now, so nothing was sent.", "rounds_closed");
|
|
358
|
+
const receipt = await send({ address: floor.contract, abi: roundsAbi as Abi, functionName: "bet", args: [marketId(name), chosen === "up"], value: amount });
|
|
359
|
+
const placed = eventOf<{ epoch: bigint; upPool: bigint; downPool: bigint }>(receipt, roundsAbi as Abi, "Bet");
|
|
360
|
+
return { ...toTx(receipt, chainId), market: name, epoch: placed ? Number(placed.epoch) : null, side: chosen, amount: amount.toString(), pools: placed ? { up: placed.upPool.toString(), down: placed.downPool.toString() } : null };
|
|
361
|
+
});
|
|
362
|
+
},
|
|
363
|
+
|
|
364
|
+
/** Collects what this wallet is owed: every market, one market, or the epochs named. The money only ever goes to the wallet that sends this. */
|
|
365
|
+
claim: (market?: string, epochs?: readonly number[]) =>
|
|
366
|
+
guarded(async () => {
|
|
367
|
+
const only = market ? marketOf(market) : null;
|
|
368
|
+
const [floor, account] = await Promise.all([request<RoundsFloor>("/v1/rounds"), request<RoundsAccount>(`/v1/accounts/${wallet().address}/rounds`)]);
|
|
369
|
+
const owed = account.unclaimed.filter((o) => (!only || o.market === only) && (!epochs || epochs.includes(o.epoch)));
|
|
370
|
+
if (!owed.length) throw new CallsError("Nothing to claim for this wallet.", "NothingToClaim");
|
|
371
|
+
const batch = owed.slice(0, CLAIM_BATCH);
|
|
372
|
+
const receipt = await send({ address: floor.contract, abi: roundsAbi as Abi, functionName: "claim", args: [batch.map((o) => marketId(o.market)), batch.map((o) => BigInt(o.epoch))] });
|
|
373
|
+
const paid = parseEventLogs({ abi: roundsAbi as Abi, logs: receipt.logs, eventName: "Claimed" } as never) as unknown as { args: { amount: bigint } }[];
|
|
374
|
+
return { ...toTx(receipt, chainId), rounds: batch.length, left: owed.length - batch.length, amount: paid.reduce((sum, log) => sum + log.args.amount, 0n).toString() };
|
|
375
|
+
}),
|
|
376
|
+
},
|
|
377
|
+
|
|
325
378
|
/** Collects winnings or a refund once the Call is Final or Voided. The money only ever goes to the wallet that sends this. */
|
|
326
379
|
claim: (address: string) =>
|
|
327
380
|
guarded(async () => {
|
package/src/errors.ts
CHANGED
|
@@ -33,6 +33,13 @@ const REVERTS: Record<string, string> = {
|
|
|
33
33
|
IsDeedCall: "A Call on a bond settles from the bond's ruling.",
|
|
34
34
|
TransferFailed: "The payment could not be delivered to that address.",
|
|
35
35
|
InsufficientBalance: NOT_ENOUGH_ETH,
|
|
36
|
+
TooLate: "Betting has closed on this round. The next one is open.",
|
|
37
|
+
BelowMinimum: "The minimum bet is 0.001 ETH.",
|
|
38
|
+
OverStakeCap: "One wallet can stake up to 0.5 ETH in a round.",
|
|
39
|
+
OverDailyLimit: "This would take the wallet past the daily limit it set for itself.",
|
|
40
|
+
OtherSide: "This wallet is already on the other side of this round.",
|
|
41
|
+
MarketPaused: "This market isn't taking bets right now.",
|
|
42
|
+
UnknownMarket: "Rounds has no such market.",
|
|
36
43
|
};
|
|
37
44
|
|
|
38
45
|
/** One sentence for a failed read or write. RPC URLs can carry keys, so nothing from the transport is passed on. */
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { createClient, accountFromKey, bondIdOf, prepareOpen, sideOf, DEFAULT_MAX_BOND, DEFAULT_MAX_STAKE, type CallsClient, type ClientOptions } from "./client.js";
|
|
1
|
+
export { createClient, accountFromKey, bondIdOf, prepareOpen, roundSideOf, sideOf, DEFAULT_MAX_BOND, DEFAULT_MAX_STAKE, type CallsClient, type ClientOptions } from "./client.js";
|
|
2
2
|
export { API_URL, CALLED_IT, CHAIN_ID, DEEDS, EXPLORER, FACTORY, PREVIOUS_FACTORIES, RPC_URLS, rpcEndpoints } from "./chain.js";
|
|
3
|
-
export { callAbi, calledItAbi, factoryAbi } from "./abi.js";
|
|
3
|
+
export { callAbi, calledItAbi, factoryAbi, roundsAbi } from "./abi.js";
|
|
4
4
|
export { canonical, hashJson } from "./canonical.js";
|
|
5
5
|
export { CallsError } from "./errors.js";
|
|
6
6
|
export { callTextHash, canonicalPostUrl, normalizeHandle, parsePostUrl, postRefHash, specHash, subjectHash, ZERO_HASH } from "./hashes.js";
|
package/src/types.ts
CHANGED
|
@@ -303,3 +303,75 @@ export interface Positions {
|
|
|
303
303
|
week: Player["week"];
|
|
304
304
|
all: Player["all"];
|
|
305
305
|
}
|
|
306
|
+
|
|
307
|
+
export type RoundOutcome = "up" | "down" | "refunded";
|
|
308
|
+
export type RoundSide = "up" | "down";
|
|
309
|
+
|
|
310
|
+
export interface RoundPools {
|
|
311
|
+
epoch: number;
|
|
312
|
+
lockAt: number;
|
|
313
|
+
closeAt: number;
|
|
314
|
+
up: Wei;
|
|
315
|
+
down: Wei;
|
|
316
|
+
bets: number;
|
|
317
|
+
/** What a winning stake pays per unit at these pools, floored to hundredths; null while either side is empty. */
|
|
318
|
+
upPays: number | null;
|
|
319
|
+
downPays: number | null;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export interface RoundResult {
|
|
323
|
+
market: string;
|
|
324
|
+
epoch: number;
|
|
325
|
+
outcome: RoundOutcome;
|
|
326
|
+
lockPrice: string | null;
|
|
327
|
+
closePrice: string | null;
|
|
328
|
+
up: Wei;
|
|
329
|
+
down: Wei;
|
|
330
|
+
pool: Wei;
|
|
331
|
+
rake: Wei;
|
|
332
|
+
pays: number | null;
|
|
333
|
+
decidedAt: number;
|
|
334
|
+
lockAt: number;
|
|
335
|
+
closeAt: number;
|
|
336
|
+
/** The public candles anyone can open to check the two prices. */
|
|
337
|
+
candles: { lock: string; close: string };
|
|
338
|
+
txHash: Hash;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export interface RoundsMarket {
|
|
342
|
+
market: string;
|
|
343
|
+
symbol: string;
|
|
344
|
+
price: string | null;
|
|
345
|
+
betting: RoundPools & { opensAt: number; cutoffAt: number };
|
|
346
|
+
running: RoundPools & { lockPrice: string | null };
|
|
347
|
+
last: RoundResult[];
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export interface RoundsFloor {
|
|
351
|
+
now: number;
|
|
352
|
+
/** False while nothing is pricing rounds. A bet then would wait out the hour and refund, so the client refuses it. */
|
|
353
|
+
open: boolean;
|
|
354
|
+
contract: Address;
|
|
355
|
+
interval: number;
|
|
356
|
+
cutoff: number;
|
|
357
|
+
minBet: Wei;
|
|
358
|
+
maxStake: Wei;
|
|
359
|
+
rakeBps: number;
|
|
360
|
+
markets: RoundsMarket[];
|
|
361
|
+
feed: { market: string; epoch: number; wallet: Address; agent: string | null; up: boolean; amount: Wei; at: number; txHash: Hash }[];
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export interface RoundsAccount {
|
|
365
|
+
wallet: Address;
|
|
366
|
+
streak: { current: number; best: number };
|
|
367
|
+
profitToday: Wei;
|
|
368
|
+
stakedToday: Wei;
|
|
369
|
+
played: number;
|
|
370
|
+
wins: number;
|
|
371
|
+
losses: number;
|
|
372
|
+
unclaimed: { market: string; epoch: number; amount: Wei }[];
|
|
373
|
+
unclaimedTotal: Wei;
|
|
374
|
+
rounds: { market: string; epoch: number; up: boolean; stake: Wei; outcome: RoundOutcome | null; payout: Wei | null; underdog: boolean; lockAt: number; closeAt: number }[];
|
|
375
|
+
/** The daily cap this wallet set for itself on chain, or null when no endpoint answered. */
|
|
376
|
+
limit: { cap: Wei; pendingCap: Wei; pendingAt: number; spentToday: Wei } | null;
|
|
377
|
+
}
|