Skip to content

R1 public calculation reference

R1 gives applications, developers and Agents a common way to calculate asset-rule fees, fee destinations and buyback plans. Supply the same inputs to this JavaScript reference or the frozen Solidity reference and compare the results locally.

This folder contains the complete frozen AIBTCInscription release, its ABI, English and Chinese rule text, build context, bytecode, hashes and 68 golden cases. The JavaScript interface uses native BigInt and the current T/C parameters.

Run locally

Use Node.js 22+. No installation is needed. From the repository root:

node reference/r1/example.mjs
node reference/r1/check-vectors.mjs
node --test reference/r1/r1.test.mjs

You can also copy this whole r1 folder into another project. From that folder:

node example.mjs
node check-vectors.mjs
node --test r1.test.mjs

The example, checker and tests run offline. They use only Node built-ins and files in this folder.

Files

Resource Open
Frozen Solidity reference AIBTCInscription.sol · embedded rule text
Complete rule text English · Chinese
Contract interface ABI
Expected inputs and results 68 golden cases
Release identity Build manifest · deployment manifest · file hashes
Reproducible compiler context Standard input · recorded output · metadata
Frozen bytecode Creation · runtime
Local JavaScript Pure calculation interface · runnable example
Local verification Artifact and vector checker · tests

Fee calculation and routing

The reference calculates fees on the supplied trade amount. The base fee is 0.5%. The selected rule fee applies when the asset was already graduated at the start of the trade. The reference rounds up the total fee and base fee separately, then takes their difference as the rule fee. The selected destination directs the rule fee to the creator or to buyback and locking.

Interface

All integer inputs must be JavaScript bigint values from 0n through UINT256_MAX. The stage input must be a JavaScript boolean. Convert external decimal strings explicitly with BigInt before calling; numbers and strings are rejected by these functions.

Function Returns
isValidRule(T, C) Whether the T/C combination is valid
ceilBps(amount, bps) The integer ceiling of amount × bps / 10000
quoteUserBuy(requestedTotal, executedTotal, T, postAtTradeStart) platformFee, ruleFee, totalFee, principal, totalDebit
quoteUserSell(G, T, postAtTradeStart) platformFee, ruleFee, totalFee, net
routeRuleFee(ruleFee, C) creatorAmount, buybackAmount
planBuyback(V, N) shouldExecute, spend, nextSequence, platformFee, ruleFee

T is 0 or a multiple of 10 through 500. C0 assigns the whole rule fee to the buyback route; C10000 assigns it to the creator. T0 requires C0. This choice applies to the rule fee, while the platform fee remains separate.

postAtTradeStart is supplied by the caller. BUY requires a requested total of at least 1,000 sats and a positive executed total within that request. After graduation, those totals must match. SELL requires gross output G ≥ 1000; the resulting net can be below 1,000.

planBuyback accepts a supplied available balance V and current event sequence N. Below 1,000 sats it returns an inactive plan with zero fields, including when N is UINT256_MAX. At the threshold it plans one purchase using all of V and returns N + 1; that addition rejects overflow only when the plan needs it.

Rejected calls throw R1Error with a code: InvalidUint256, InvalidBoolean, InvalidBasisPoints, InvalidTax, InvalidBuy, MinimumGross, InvalidDestination or ArithmeticOverflow. These are local JavaScript error labels; ArithmeticOverflow corresponds to the contract's checked N + 1.

Frozen release and verification

The reference address is 0xed976e7b95d099c60318ef848c73d584ee5eb08a.

The same address is deployed on Ethereum, BNB Chain and Base. The Ethereum and Base explorers show the source as verified (Exact Match): Etherscan · Basescan; BNB Chain: BscScan.

The English rule text has SHA-256 3c0b91af6cbfce5ea8eb8c6d1174c117ff34233695f480f5e6c9b3db7ddaa73b.

check-vectors.mjs checks the 17-file release, its SHA-256 file digests, embedded text, source/build identities and all 68 golden cases, including 18 rejected calls. The tests add explicit rounding boundaries, integer conservation, type/range rejection and finite generated cases with a fixed seed. These checks compare the JavaScript implementation with frozen expected results.

The original bytes in artifacts/ are preserved. standard-input.json contains both Solidity sources and the exact compiler settings; standard-output.json contains the recorded output. build-manifest.json pins compiler 0.8.36+commit.8a079791.Emscripten.clang, compiler SHA-256 and settings identity. To reproduce compilation, pass the unchanged standard input to that exact pinned compiler and compare its ABI and creation/runtime bytecode with the release. The compiler is not bundled, and the local checker does not recompile Solidity.

The ABI is the authoritative Solidity interface. The build and deployment manifests retain their original compatibility field names, including statuteTextSha256; the published rule text is inscription.en.txt.

Keep this release and existing asset rules fixed. A future rule version needs its own reference identity and explicit asset parameters. Code is MIT; the permission notice is included in LICENSE.

Execution boundary

R1 supplies public calculations. Applications supply actual parameters, stage, balances and execution evidence, and handle pricing, trades and custody. A quote, fee destination or buyback plan reports a calculation; it does not transfer funds, complete a trade or lock assets. The 15 reserved topics, including Agent, are documentation entries rather than executable R1 business functions. The examples require no wallet, RPC connection, database, signing or broadcasting.