Contract State

Know what changed in any Stacks contract, when, and who changed it.

Every map and variable write from block 1, with the transaction that made it.

One key, one call, for any transaction:

$curl "$SECONDLAYER_API_URL/v1/index/events?event_type=nested_contract_call&tx_id=0xf53d86108ccd4a7d7418101a86e4030ebe8c9ff942d47888987d5f6bac7c279b" \
-H "Authorization: Bearer sk-sl_…"
200response.jsonjson
{
"events": [
{
"cursor": "1230200:13",
"block_height": 1230200,
"block_time": "2025-05-21T09:56:51.000Z",
"tx_id": "0xf53d86108ccd4a7d7418101a86e4030ebe8c9ff942d47888987d5f6bac7c279b",
"tx_index": 0,
"event_index": 13,
"event_type": "nested_contract_call",
"contract_id": "SP26BVHEKMZSAKZ4PZ5SFPZVMRHDKH99D2Z38TK1Y.kiki-token",
"sender": "SP1E5PQRC5Y4WWEY1BP80254TGR2HDCSN5NF0VSFR",
"caller": "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-wkiki",
"function_name": "transfer",
"function_args": ["0x0100000000000000000000009062fc6a00", "0x05165c5b5f0c2f89ce3bc15d9001149a860516b3352d", "0x0616402da2c079e5d31d58b9cfc7286d1b1eb2f7834e0f616d6d2d7661756c742d76322d3031", "0x09"],
"raw_result": "0x0703"
}
]
}

That ALEX swap ran 22 calls between contracts and wrote two maps; the response shows one. A transaction id is enough.

Five event types cover what a contract stores and every call it makes. Values are Clarity 0x hex. Every type filters by contract, transaction and height; the rest are below. Filters: List events.

event_typeTells youExtra filters
nested_contract_callWho really uses a contract, including apps that route through another one, and calls that failed and were handledfunctionName, caller, sender
var_setA setting or counter changedvarName
map_setAn entry changedmap
map_insertA new position, listing or balancemap
map_deleteAn entry was removedmap

Who made the call

Every call between contracts carries two principals.

FieldMeans
contract_idThe contract called
callerWho made the call: Clarity's contract-caller
sendertx-sender at that moment. as-contract swaps it for the contract that used it

At the ALEX drain, block 1,508,068, a vault called a fake token, which moved the vault's real tokens.

RowsCallcallersender
21Vault calls the fake token's transfer-fixedvaultvault
14, 16, 18, 20Fake token moves aBTC, sBTC, ALEX and sUSDTfake tokenvault

An unfamiliar caller with a vault as sender means that contract is moving the vault's tokens. The vault's own call looks normal alone.

Walk one map's writes to know what any key held after any transaction.

for await (const write of index.events.walk({
eventType: "map_set",
contractId: "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.amm-vault-v2-01",
map: "reserve",
fromHeight: 0,
})) {
console.log(write.block_height, write.decoded.key, write.decoded.value);
}
// 1230200 SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-wkiki 2122910416441083

No print carries that write, and a node can't say which transaction set a key. For storage keys rather than named maps, read state writes.

State Writes

Every key a block wrote, in node order.

const { state_writes, next_cursor } = await sl.index.stateWrites.list({
blockHeight: 8054704,
contractId: "SP1.vault", // only keys under this contract
txContext: true, // join the writing transaction
});
FieldMeans
cursor<block_height>:<ordinal>
block_height, ordinalThe block, and the write's position in node order
tx_indexnull for block-level writes
keyThe chain's storage key
value_hexThe new value
  • GET /v1/index/state-writes; block_height reads one block, not with a height range.
  • Self-hosted rows start where your node first sent the state_writes observer key.

The same rows stream with clock: "vm" through the checkpointed consumer.

await sl.streams.events.consume({
clock: "vm",
types: ["map_set", "var_set"],
fromCursor: await db.loadCheckpoint(),
onBatch: async (rows, ctx) => {
await db.save(rows, ctx.cursor);
return ctx.cursor;
},
onReorg: (reorg) => db.deleteFromHeight(reorg.fork_point_height),
});

A webhook fires on the same types once the block lands, whenever a vault's reserves change:

import { trigger } from "@secondlayer/sdk";
await sl.webhooks.create({
name: "vault-reserves",
url: "https://your.app/hooks/reserves",
triggers: [
trigger.mapSet({
contractId: "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.amm-vault-v2-01",
map: "reserve",
}),
],
});

A subgraph takes map_set and var_set as sources; built from stored values, anyone can rebuild its rows.

The node patch is public; history is a six-week replay from block 1.

CoveredNot covered
Values a contract stores, and calls between contractsEvery intermediate value in the code
Transactions that landedAnything before it lands: it warns, it does not prevent. A transaction that fails outright leaves no rows
History from block 1, and who made each changeSomething new about one key's current value: a node could always read that
Stored values, checkable against the block's state rootCalls between contracts: not committed on chain, so not provable

Reads are metered like event rows (Keys and Billing), so scope by contract and map. Reading contract state and testing on it use the meters. A hosted webhook or subgraph on contract state needs the Contract state plan: $999 a month, with $100 of usage included. Upgrade from the credits page or the pricing page.

When the plan ends, hosted webhooks on contract state pause and hosted subgraphs on contract state stop. Nothing restarts on its own: resubscribe, then resume each webhook and redeploy each subgraph.

Hosted mainnet has rows from block 1; a self-hosted instance only from the block its node first recorded them.

Edit on GitHub

Quick Start

Copy this prompt into your agent.

Set up Secondlayer Index in this project. It reads Stacks chain data, decoded, from block 1.

1. Install `@secondlayer/sdk` with the project's package manager. If a release-age policy blocks the install, stop and ask me instead of changing it.
2. Ask me for a Secondlayer API key (`sk-sl_...`, from https://www.secondlayer.tools/signin) and put it in `.env` as `SECONDLAYER_API_KEY`. Never commit it. If I self-host, use `INSTANCE_TOKEN` and my instance URL instead. Until I give you one, read with a free trial key: `curl -X POST https://api.secondlayer.tools/api/public/trial-key` returns `{ key }`, read-only for Streams and Index, good for 24 hours and 100,000 rows. Keep it out of git too.
3. Add a note to `AGENTS.md`, creating it if needed: before using Secondlayer, follow the secondlayer skill if it's installed, and read https://www.secondlayer.tools/llms.txt. Before writing code, read https://www.secondlayer.tools/docs/index.md. Any docs page opens as Markdown when you add `.md` to its URL.
4. If you support skills, run: `bunx skills add ryanwaits/secondlayer --skill secondlayer -y`