Loading...
Loading...
Compare original and translation side by side
bun run clarity-patterns/clarity-patterns.ts <subcommand> [options]bun run clarity-patterns/clarity-patterns.ts <subcommand> [options]list [--category <category>]coderegistrytemplatestestingget --name <pattern-name>template --name <template-name>list [--category <category>]coderegistrytemplatestestingget --name <pattern-name>template --name <template-name>(define-public (transfer (amount uint) (to principal))
(begin
(asserts! (is-eq tx-sender owner) ERR_UNAUTHORIZED)
(try! (ft-transfer? TOKEN amount tx-sender to))
(ok true)))try!asserts!(define-public (transfer (amount uint) (to principal))
(begin
(asserts! (is-eq tx-sender owner) ERR_UNAUTHORIZED)
(try! (ft-transfer? TOKEN amount tx-sender to))
(ok true)))try!asserts!(print {
notification: "contract-event",
payload: {
amount: amount,
sender: tx-sender,
recipient: to
}
})notificationpayload(print {
notification: "contract-event",
payload: {
amount: amount,
sender: tx-sender,
recipient: to
}
})notificationpayload(match (contract-call? .other fn args)
success (ok success)
error (err ERR_EXTERNAL_CALL_FAILED))(match (contract-call? .other fn args)
success (ok success)
error (err ERR_EXTERNAL_CALL_FAILED))(define-constant STATUS_ACTIVE (pow u2 u0)) ;; 1
(define-constant STATUS_PAID (pow u2 u1)) ;; 2
(define-constant STATUS_VERIFIED (pow u2 u2)) ;; 4
;; Pack multiple flags: (+ STATUS_ACTIVE STATUS_PAID) → u3
;; Check flag: (> (bit-and status STATUS_ACTIVE) u0)
;; Set flag: (var-set status (bit-or (var-get status) NEW_FLAG))
;; Clear flag: (var-set status (bit-and (var-get status) (bit-not FLAG)))(define-constant STATUS_ACTIVE (pow u2 u0)) ;; 1
(define-constant STATUS_PAID (pow u2 u1)) ;; 2
(define-constant STATUS_VERIFIED (pow u2 u2)) ;; 4
;; 打包多个标志:(+ STATUS_ACTIVE STATUS_PAID) → u3
;; 检查标志:(> (bit-and status STATUS_ACTIVE) u0)
;; 设置标志:(var-set status (bit-or (var-get status) NEW_FLAG))
;; 清除标志:(var-set status (bit-and (var-get status) (bit-not FLAG)))(define-private (send-maybe
(recipient {to: principal, ustx: uint})
(prior (response bool uint)))
(match prior
ok-result (let (
(to (get to recipient))
(ustx (get ustx recipient)))
(try! (stx-transfer? ustx tx-sender to))
(ok true))
err-result (err err-result)))
(define-public (send-many (recipients (list 200 {to: principal, ustx: uint})))
(fold send-maybe recipients (ok true)))(define-private (send-maybe
(recipient {to: principal, ustx: uint})
(prior (response bool uint)))
(match prior
ok-result (let (
(to (get to recipient))
(ustx (get ustx recipient)))
(try! (stx-transfer? ustx tx-sender to))
(ok true))
err-result (err err-result)))
(define-public (send-many (recipients (list 200 {to: principal, ustx: uint})))
(fold send-maybe recipients (ok true)))(define-map Parents uint {name: (string-ascii 32), lastChildId: uint})
(define-map Children {parentId: uint, id: uint} uint)
(define-read-only (get-child (parentId uint) (childId uint))
(map-get? Children {parentId: parentId, id: childId}))
(define-private (is-some? (x (optional uint)))
(is-some x))
(define-read-only (get-children (parentId uint) (shift uint))
(filter is-some?
(list
(get-child parentId (+ shift u1))
(get-child parentId (+ shift u2))
(get-child parentId (+ shift u3))
;; ... up to page size
)))(define-map Parents uint {name: (string-ascii 32), lastChildId: uint})
(define-map Children {parentId: uint, id: uint} uint)
(define-read-only (get-child (parentId uint) (childId uint))
(map-get? Children {parentId: parentId, id: childId}))
(define-private (is-some? (x (optional uint)))
(is-some x))
(define-read-only (get-children (parentId uint) (shift uint))
(filter is-some?
(list
(get-child parentId (+ shift u1))
(get-child parentId (+ shift u2))
(get-child parentId (+ shift u3))
;; ... 直至分页大小
)))(define-map Allowed {contract: principal, type: uint} bool)
;; Check in function
(asserts! (default-to false (map-get? Allowed {contract: contract, type: type}))
ERR_NOT_ALLOWED)
;; Batch update
(define-public (set-allowed-list (items (list 100 {token: principal, enabled: bool})))
(ok (map set-iter items (ok true))))(define-map Allowed {contract: principal, type: uint} bool)
;; 在函数中检查
(asserts! (default-to false (map-get? Allowed {contract: contract, type: type}))
ERR_NOT_ALLOWED)
;; 批量更新
(define-public (set-allowed-list (items (list 100 {token: principal, enabled: bool})))
(ok (map set-iter items (ok true))))(define-map TrustedTraits principal bool)
;; In functions accepting traits
(asserts! (default-to false (map-get? TrustedTraits (contract-of t)))
ERR_UNTRUSTED)(define-map TrustedTraits principal bool)
;; 在接受Trait的函数中
(asserts! (default-to false (map-get? TrustedTraits (contract-of t)))
ERR_UNTRUSTED)(define-constant DELAY u21000) ;; ~146 days in BTC blocks
(define-data-var activation-block uint u0)
;; Set on deploy or init
(var-set activation-block (+ burn-block-height DELAY))
(define-read-only (is-active?)
(>= burn-block-height (var-get activation-block)))(define-constant DELAY u21000) ;; ~146天(按BTC区块计算)
(define-data-var activation-block uint u0)
;; 在部署或初始化时设置
(var-set activation-block (+ burn-block-height DELAY))
(define-read-only (is-active?)
(>= burn-block-height (var-get activation-block)))(define-data-var last-action-block uint u0)
(define-public (rate-limited-action)
(begin
(asserts! (> burn-block-height (var-get last-action-block)) ERR_RATE_LIMIT)
(var-set last-action-block burn-block-height)
;; ... action
(ok true)))(define-data-var last-action-block uint u0)
(define-public (rate-limited-action)
(begin
(asserts! (> burn-block-height (var-get last-action-block)) ERR_RATE_LIMIT)
(var-set last-action-block burn-block-height)
;; ... 执行操作
(ok true)))at-block(define-map Proposals uint {
votesFor: uint,
votesAgainst: uint,
status: uint,
liquidTokens: uint,
blockHash: (buff 32)
})
;; Get voting power at proposal creation
(define-read-only (get-vote-power (proposal-id uint) (voter principal))
(let ((proposal (unwrap! (map-get? Proposals proposal-id) u0)))
(at-block (get blockHash proposal)
(contract-call? .token get-balance voter))))
;; Quorum check: (>= (/ (* total-votes u100) liquid-supply) QUORUM_PERCENT)at-block(define-map Proposals uint {
votesFor: uint,
votesAgainst: uint,
status: uint,
liquidTokens: uint,
blockHash: (buff 32)
})
;; 获取提案创建时的投票权
(define-read-only (get-vote-power (proposal-id uint) (voter principal))
(let ((proposal (unwrap! (map-get? Proposals proposal-id) u0)))
(at-block (get blockHash proposal)
(contract-call? .token get-balance voter))))
;; 法定人数检查:(>= (/ (* total-votes u100) liquid-supply) QUORUM_PERCENT)(define-constant SCALE (pow u10 u8)) ;; 8 decimal places
;; Multiply then divide to preserve precision
(define-read-only (calculate-share (amount uint) (percentage uint))
(/ (* amount percentage) SCALE))
;; Convert to/from scaled values
(define-read-only (to-scaled (amount uint))
(* amount SCALE))
(define-read-only (from-scaled (amount uint))
(/ amount SCALE))(define-constant SCALE (pow u10 u8)) ;; 8位小数
;; 先乘后除以保留精度
(define-read-only (calculate-share (amount uint) (percentage uint))
(/ (* amount percentage) SCALE))
;; 在缩放值与原值间转换
(define-read-only (to-scaled (amount uint))
(* amount SCALE))
(define-read-only (from-scaled (amount uint))
(/ amount SCALE))as-contract(define-public (withdraw (amount uint) (recipient principal))
(begin
(asserts! (is-authorized tx-sender) ERR_UNAUTHORIZED)
(as-contract (stx-transfer? amount (as-contract tx-sender) recipient))))as-contracttx-sendercontract-calleras-contract(define-public (withdraw (amount uint) (recipient principal))
(begin
(asserts! (is-authorized tx-sender) ERR_UNAUTHORIZED)
(as-contract (stx-transfer? amount (as-contract tx-sender) recipient))))as-contracttx-sendercontract-caller| Call Path | contract-caller | tx-sender |
|---|---|---|
| user -> target | user | user |
| user -> proxy -> target | proxy | user |
| user -> proxy (as-contract) -> target | proxy | proxy |
contract-callercontract-callertx-sender| 调用路径 | contract-caller | tx-sender |
|---|---|---|
| 用户 -> 目标合约 | 用户 | 用户 |
| 用户 -> 代理合约 -> 目标合约 | 代理合约 | 用户 |
| 用户 -> 代理合约(as-contract) -> 目标合约 | 代理合约 | 代理合约 |
contract-callercontract-callertx-sender(as-contract
(with-stx u1000000) ;; Allow 1 STX
(with-ft .token TOKEN u500) ;; Allow 500 fungible tokens
(with-nft .nft-contract NFT (list u1 u2 u3)) ;; Allow specific NFT IDs
;; ... body
)
;; DANGER: Avoid unless necessary
(with-all-assets-unsafe)(as-contract
(with-stx u1000000) ;; 允许1个STX
(with-ft .token TOKEN u500) ;; 允许500个 fungible token
(with-nft .nft-contract NFT (list u1 u2 u3)) ;; 允许特定NFT ID
;; ... 函数体
)
;; 危险:非必要请勿使用
(with-all-assets-unsafe);; Proposal state
(define-map Intents uint {
participants: (list 20 principal),
accepts: uint, ;; Bitmask of who accepted
status: uint, ;; 0=pending, 1=ready, 2=executed, 3=cancelled
expiry: uint,
payload: (buff 256)
})
;; Accept via signature verification
(define-public (accept (intent-id uint) (signature (buff 65)))
(let (
(intent (unwrap! (map-get? Intents intent-id) ERR_NOT_FOUND))
(msg-hash (sha256 (concat (int-to-ascii intent-id) (get payload intent))))
(signer (try! (secp256k1-recover? msg-hash signature))))
;; Verify signer is participant, update accepts bitmask
(ok true)));; 提案状态
(define-map Intents uint {
participants: (list 20 principal),
accepts: uint, ;; 已接受者的位掩码
status: uint, ;; 0=待处理,1=就绪,2=已执行,3=已取消
expiry: uint,
payload: (buff 256)
})
;; 通过签名验证接受提案
(define-public (accept (intent-id uint) (signature (buff 65)))
(let (
(intent (unwrap! (map-get? Intents intent-id) ERR_NOT_FOUND))
(msg-hash (sha256 (concat (int-to-ascii intent-id) (get payload intent))))
(signer (try! (secp256k1-recover? msg-hash signature))))
;; 验证签名者为参与者,更新接受位掩码
(ok true)));; Full snapshot — comprehensive (use for high-value records)
(define-private (capture-snapshot)
{
stacksBlock: stacks-block-height,
burnBlock: burn-block-height,
tenure: tenure-height,
blockTime: stacks-block-time,
chainId: chain-id,
txSender: tx-sender,
contractCaller: contract-caller,
txSponsor: tx-sponsor?,
stacksBlockHash: (get-stacks-block-info? id-header-hash (- stacks-block-height u1)),
burnBlockHash: (get-burn-block-info? header-hash (- burn-block-height u1))
}
)
;; Standard snapshot — balanced cost
;; {stacksBlock, burnBlock, blockTime, txSender}
;; Minimal snapshot — cheapest
;; {stacksBlock, burnBlock};; 完整快照——全面记录(用于高价值记录)
(define-private (capture-snapshot)
{
stacksBlock: stacks-block-height,
burnBlock: burn-block-height,
tenure: tenure-height,
blockTime: stacks-block-time,
chainId: chain-id,
txSender: tx-sender,
contractCaller: contract-caller,
txSponsor: tx-sponsor?,
stacksBlockHash: (get-stacks-block-info? id-header-hash (- stacks-block-height u1)),
burnBlockHash: (get-burn-block-info? header-hash (- burn-block-height u1))
}
)
;; 标准快照——平衡成本
;; {stacksBlock, burnBlock, blockTime, txSender}
;; 最小快照——成本最低
;; {stacksBlock, burnBlock}(define-map Registry
principal
{
stacksBlock: uint,
burnBlock: uint,
count: uint
}
)
(map-get? Registry address)
(map-set Registry tx-sender {...})(define-map Registry
principal
{
stacksBlock: uint,
burnBlock: uint,
count: uint
}
)
(map-get? Registry address)
(map-set Registry tx-sender {...})(define-map Registry
(buff 32)
{
attestor: principal,
stacksBlock: uint
}
)
;; First attestor wins
(asserts! (is-none (map-get? Registry hash)) ERR_ALREADY_EXISTS)
(map-set Registry hash {...})(define-map Registry
(buff 32)
{
attestor: principal,
stacksBlock: uint
}
)
;; 先写入者胜出
(asserts! (is-none (map-get? Registry hash)) ERR_ALREADY_EXISTS)
(map-set Registry hash {...})(define-map Registry
{entity: principal, action: uint}
{stacksBlock: uint}
)
(map-get? Registry {entity: address, action: action-id})(define-map Registry
{entity: principal, action: uint}
{stacksBlock: uint}
)
(map-get? Registry {entity: address, action: action-id});; Primary: hash -> data
(define-map Attestations (buff 32) {...})
;; Secondary: address + index -> hash
(define-map AttestorIndex
{attestor: principal, index: uint}
(buff 32)
)
;; Counter for next index
(define-map AttestorCount principal uint)
;; On insert:
(let ((idx (default-to u0 (map-get? AttestorCount attestor))))
(map-set AttestorIndex {attestor: attestor, index: idx} hash)
(map-set AttestorCount attestor (+ idx u1)))
;; Enumerate:
(define-read-only (get-attestor-hash-at (attestor principal) (index uint))
(map-get? AttestorIndex {attestor: attestor, index: index}));; 主键:哈希 -> 数据
(define-map Attestations (buff 32) {...})
;; 二级索引:地址 + 索引 -> 哈希
(define-map AttestorIndex
{attestor: principal, index: uint}
(buff 32)
)
;; 下一个索引的计数器
(define-map AttestorCount principal uint)
;; 插入时:
(let ((idx (default-to u0 (map-get? AttestorCount attestor))))
(map-set AttestorIndex {attestor: attestor, index: idx} hash)
(map-set AttestorCount attestor (+ idx u1)))
;; 枚举:
(define-read-only (get-attestor-hash-at (attestor principal) (index uint))
(map-get? AttestorIndex {attestor: attestor, index: index}))(define-data-var totalEntries uint u0)
(define-data-var uniqueAddresses uint u0)
;; On new entry:
(var-set totalEntries (+ (var-get totalEntries) u1))
(if isNewAddress
(var-set uniqueAddresses (+ (var-get uniqueAddresses) u1))
true)
;; Read stats:
(define-read-only (get-stats)
{
totalEntries: (var-get totalEntries),
uniqueAddresses: (var-get uniqueAddresses)
}
)(define-data-var totalEntries uint u0)
(define-data-var uniqueAddresses uint u0)
;; 新增记录时:
(var-set totalEntries (+ (var-get totalEntries) u1))
(if isNewAddress
(var-set uniqueAddresses (+ (var-get uniqueAddresses) u1))
true)
;; 读取统计数据:
(define-read-only (get-stats)
{
totalEntries: (var-get totalEntries),
uniqueAddresses: (var-get uniqueAddresses)
}
)(define-public (attest (key (buff 32)))
(begin
(asserts! (is-none (map-get? Registry key)) ERR_ALREADY_EXISTS)
(map-set Registry key {...})
(ok true)))(define-public (check-in)
(begin
(map-set Registry tx-sender {...})
(ok true)))(define-map History
{address: principal, index: uint}
{...snapshot...}
)
(define-public (record)
(let ((idx (default-to u0 (map-get? HistoryCount tx-sender))))
(map-set History {address: tx-sender, index: idx} {...})
(map-set HistoryCount tx-sender (+ idx u1))
(ok idx)))(define-public (attest (key (buff 32)))
(begin
(asserts! (is-none (map-get? Registry key)) ERR_ALREADY_EXISTS)
(map-set Registry key {...})
(ok true)))(define-public (check-in)
(begin
(map-set Registry tx-sender {...})
(ok true)))(define-map History
{address: principal, index: uint}
{...snapshot...}
)
(define-public (record)
(let ((idx (default-to u0 (map-get? HistoryCount tx-sender))))
(map-set History {address: tx-sender, index: idx} {...})
(map-set HistoryCount tx-sender (+ idx u1))
(ok idx)))(define-public (register)
(ok (map-set Registry tx-sender {...})))(define-public (update (data (buff 64)))
(begin
(asserts! (is-some (map-get? Registry tx-sender)) ERR_NOT_REGISTERED)
(map-set Registry tx-sender {...})
(ok true)))(define-data-var admin principal CONTRACT_OWNER)
(define-public (register-address (address principal))
(begin
(asserts! (is-eq tx-sender (var-get admin)) ERR_UNAUTHORIZED)
(map-set Registry address {...})
(ok true)))(define-public (register)
(ok (map-set Registry tx-sender {...})))(define-public (update (data (buff 64)))
(begin
(asserts! (is-some (map-get? Registry tx-sender)) ERR_NOT_REGISTERED)
(map-set Registry tx-sender {...})
(ok true)))(define-data-var admin principal CONTRACT_OWNER)
(define-public (register-address (address principal))
(begin
(asserts! (is-eq tx-sender (var-get admin)) ERR_UNAUTHORIZED)
(map-set Registry address {...})
(ok true)))Stxer (Historical Simulation) — Mainnet fork, pre-deployment validation
RV (Property-Based Fuzzing) — Invariants, edge cases, battle-grade
Vitest + Clarinet SDK — Integration tests, TypeScript
Clarunit — Unit tests in Clarity itselfStxer(历史模拟) — 主网分叉,部署前验证
RV(基于属性的模糊测试) — 不变量、边缘情况、生产级测试
Vitest + Clarinet SDK — 集成测试、TypeScript
Clarunit — 纯Clarity编写的单元测试| Tool | Use When | Skip When |
|---|---|---|
| Clarinet SDK | Standard testing, CI/CD, type-safe | - |
| Clarunit | Testing Clarity logic in Clarity, simple assertions | Complex multi-account flows |
| RV | Treasuries, DAOs, high-value contracts, finding edge cases | Simple contracts, time pressure |
| Stxer | Pre-mainnet validation, governance simulations | Early development, testnet-only |
| 工具 | 适用场景 | 不适用场景 |
|---|---|---|
| Clarinet SDK | 标准测试、CI/CD、类型安全 | - |
| Clarunit | 用Clarity测试Clarity逻辑、简单断言 | 复杂多账户流程 |
| RV | 金库、DAO、高价值合约、寻找边缘情况 | 简单合约、时间紧张的开发 |
| Stxer | 主网部署前验证、治理模拟 | 早期开发、仅测试网部署 |
import { defineConfig } from "vitest/config";
import { vitestSetupFilePath, getClarinetVitestsArgv } from "@hirosystems/clarinet-sdk/vitest";
export default defineConfig({
test: {
environment: "clarinet",
singleThread: true,
setupFiles: [vitestSetupFilePath],
environmentOptions: {
clarinet: getClarinetVitestsArgv(),
},
},
});import { defineConfig } from "vitest/config";
import { vitestSetupFilePath, getClarinetVitestsArgv } from "@hirosystems/clarinet-sdk/vitest";
export default defineConfig({
test: {
environment: "clarinet",
singleThread: true,
setupFiles: [vitestSetupFilePath],
environmentOptions: {
clarinet: getClarinetVitestsArgv(),
},
},
});import { Cl } from "@stacks/transactions";
import { describe, expect, it } from "vitest";
describe("my-contract", function () {
it("transfers tokens correctly", function () {
// ARRANGE
const deployer = simnet.deployer;
const wallet1 = simnet.getAccounts().get("wallet_1")!;
const amount = 100;
// ACT
const result = simnet.callPublicFn(
"my-contract",
"transfer",
[Cl.uint(amount), Cl.principal(wallet1)],
deployer
);
// ASSERT
expect(result.result).toBeOk(Cl.bool(true));
});
});import { Cl } from "@stacks/transactions";
import { describe, expect, it } from "vitest";
describe("my-contract", function () {
it("transfers tokens correctly", function () {
// 准备(ARRANGE)
const deployer = simnet.deployer;
const wallet1 = simnet.getAccounts().get("wallet_1")!;
const amount = 100;
// 执行(ACT)
const result = simnet.callPublicFn(
"my-contract",
"transfer",
[Cl.uint(amount), Cl.principal(wallet1)],
deployer
);
// 断言(ASSERT)
expect(result.result).toBeOk(Cl.bool(true));
});
});beforeAllbeforeEachsingleThread: truecvToValue()cvToJSON()beforeAllbeforeEachsingleThread: truecvToValue()cvToJSON()import { Cl, cvToValue, cvToJSON } from "@stacks/transactions";
Cl.uint(100) // uint
Cl.int(-50) // int
Cl.bool(true) // bool
Cl.principal("SP123...") // principal
Cl.contractPrincipal("SP123", "name") // contract principal
Cl.stringAscii("hello") // (string-ascii N)
Cl.stringUtf8("hello") // (string-utf8 N)
Cl.bufferFromHex("deadbeef") // (buff N)
Cl.tuple({ amount: Cl.uint(100) }) // tuple
Cl.list([Cl.uint(1), Cl.uint(2)]) // list
Cl.some(Cl.uint(100)) // (some value)
Cl.none() // noneimport { Cl, cvToValue, cvToJSON } from "@stacks/transactions";
Cl.uint(100) // uint类型
Cl.int(-50) // int类型
Cl.bool(true) // bool类型
Cl.principal("SP123...") // 主体
Cl.contractPrincipal("SP123", "name") // 合约主体
Cl.stringAscii("hello") // (string-ascii N)
Cl.stringUtf8("hello") // (string-utf8 N)
Cl.bufferFromHex("deadbeef") // (buff N)
Cl.tuple({ amount: Cl.uint(100) }) // 元组
Cl.list([Cl.uint(1), Cl.uint(2)]) // 列表
Cl.some(Cl.uint(100)) // (some value)
Cl.none() // noneexpect(result.result).toBeOk(Cl.uint(100));
expect(result.result).toBeErr(Cl.uint(1));
expect(result.result).toBeBool(true);
expect(result.result).toBeUint(100);
expect(result.result).toBePrincipal("SP123...");expect(result.result).toBeOk(Cl.uint(100));
expect(result.result).toBeErr(Cl.uint(1));
expect(result.result).toBeBool(true);
expect(result.result).toBeUint(100);
expect(result.result).toBePrincipal("SP123...");;; Property: loan amount always increases correctly
(define-public (test-borrow (amount uint))
(if (is-eq amount u0)
(ok false) ;; Discard invalid input
(let ((initial (get-loan tx-sender)))
(try! (borrow amount))
(asserts! (is-eq (get-loan tx-sender) (+ initial amount))
(err u999))
(ok true))))
;; Invariant: total supply never exceeds cap
(define-read-only (invariant-supply-capped)
(<= (var-get total-supply) MAX_SUPPLY))npx rv . my-contract testnpx rv . my-contract invariant;; 属性:贷款金额始终正确增加
(define-public (test-borrow (amount uint))
(if (is-eq amount u0)
(ok false) ;; 丢弃无效输入
(let ((initial (get-loan tx-sender)))
(try! (borrow amount))
(asserts! (is-eq (get-loan tx-sender) (+ initial amount))
(err u999))
(ok true))))
;; 不变量:总供应量绝不超过上限
(define-read-only (invariant-supply-capped)
(<= (var-get total-supply) MAX_SUPPLY))npx rv . my-contract testnpx rv . my-contract invariant;; @name Multiplication works correctly
(define-public (test-multiply)
(begin
(asserts! (is-eq u8 (contract-call? .math multiply u2 u4))
(err "2 * 4 should equal 8"))
(ok true)))tests/my-contract_test.clartest-;; @name 乘法功能正常
(define-public (test-multiply)
(begin
(asserts! (is-eq u8 (contract-call? .math multiply u2 u4))
(err "2 * 4 应等于 8"))
(ok true)))tests/my-contract_test.clartest-my-project/
├── Clarinet.toml
├── vitest.config.js
├── package.json
├── contracts/
│ ├── my-contract.clar
│ └── my-contract.tests.clar # RV tests
├── tests/
│ ├── my-contract.test.ts # Vitest
│ ├── my-contract_test.clar # Clarunit
│ └── clarunit.test.ts # Clarunit runner
└── simulations/
└── my-contract-stxer.ts # Stxermy-project/
├── Clarinet.toml
├── vitest.config.js
├── package.json
├── contracts/
│ ├── my-contract.clar
│ └── my-contract.tests.clar # RV测试
├── tests/
│ ├── my-contract.test.ts # Vitest测试
│ ├── my-contract_test.clar # Clarunit测试
│ └── clarunit.test.ts # Clarunit运行器
└── simulations/
└── my-contract-stxer.ts # Stxer模拟{
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
"test:rv": "npx rv . my-contract test",
"test:rv:invariant": "npx rv . my-contract invariant",
"test:stxer": "npx tsx simulations/my-contract-stxer.ts"
}
}{
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
"test:rv": "npx rv . my-contract test",
"test:rv:invariant": "npx rv . my-contract invariant",
"test:stxer": "npx tsx simulations/my-contract-stxer.ts"
}
}| Template | File | Description |
|---|---|---|
| templates/heartbeat-registry.md | Agent heartbeat with full chain context, address enumeration, liveness checks |
| templates/proof-of-existence.md | Document timestamping with SIP-018 signatures, first-write-wins, attestor index |
| templates/registry-minimal.md | Minimal registry combining snapshot + stats + events |
| 模板 | 文件 | 描述 |
|---|---|---|
| templates/heartbeat-registry.md | 带有完整链上上下文、地址枚举、活跃度检查的Agent心跳注册器 |
| templates/proof-of-existence.md | 带有SIP-018签名、先写入者胜出规则、证明者索引的文档时间戳服务 |
| templates/registry-minimal.md | 集成快照、统计和事件的最小化注册器 |
| Category | Block Limit | Read-Only Limit |
|---|---|---|
| Runtime | 5,000,000,000 | 1,000,000,000 |
| Read count | 15,000 | 30 |
| Read bytes | 100,000,000 | 100,000 |
| Write count | 15,000 | 0 |
| Write bytes | 15,000,000 | 0 |
| 分类 | 区块限制 | 只读限制 |
|---|---|---|
| 运行时 | 5,000,000,000 | 1,000,000,000 |
| 读取次数 | 15,000 | 30 |
| 读取字节数 | 100,000,000 | 100,000 |
| 写入次数 | 15,000 | 0 |
| 写入字节数 | 15,000,000 | 0 |
letletstacks-block-heightblock-heighttx-sendercontract-callertry!asserts!(response ok err){notification: "event-name", payload: {...}}::get_costsstacks-block-heightblock-heighttx-sendercontract-callertry!asserts!(response ok err){notification: "event-name", payload: {...}}::get_costs