The DinaraChannels contract
0x98C00995Fb264305Dd1065Ed3E7B62aC7003B733View on Tempo’s explorerDeployed by contracts/script/Deploy.s.sol. Every channel, proposal and receipt lives in its storage, and it holds every deposit itself: Dinara never does.
For developers
A Solidity contract on Tempo, a TypeScript SDK, and this app. Every name below is a real export, and every claim names the file behind it.
Live on Tempo mainnet with real USDC.e. Not independently audited, so keep amounts small. Test counts come from the suites in the repository.
The contract
DinaraChannels, in Solidity with no dependencies. One contract holds every channel’s deposit, recovers the agent’s EIP-712 signature itself, and writes a receipt from measured balances.
Pulls the deposit with transferFrom after the owner’s approval, records the balance it measured, and stores the agent key, the ceiling, up to eight payees, the expiry, an optional operator and a label. A ceiling above the deposit, an expiry in the past or more than 366 days out, or a repeated payee is refused.
Adds funds, raises the ceiling or extends the expiry, and only ever widens the agent’s room. Lowering a ceiling or shortening an expiry would undercut payees who served against the old terms, so neither is possible.
The owner brings expiry forward to five minutes from now, which leaves the agent and the operator that long to end the channel with the newest state.
The operator’s, and only the operator’s. Recovers the agent’s EIP-712 signature over the state, pays every payee exactly what the agent signed, returns the rest to the owner and writes the receipt. Every amount on the receipt is measured from token balances, never copied from an argument.
Everyone else ends a channel by proposing an agent-signed state: the agent or operator before expiry, anyone after. Nothing moves yet. The proposal stays open for ten minutes.
Anyone holding an agent-signed state that owes some payee more raises the proposal while the window is open; each payee’s line becomes the larger of the two. Sequence numbers never decide it, so an agent cannot end its channel with a state that underpays.
Once the window closes, anyone pays out the proposal that stood, with the same measured payout and receipt as settle. A merge that owes more than the ceiling or the balance is scaled down pro rata, and the rest is refunded.
If nobody settles within an hour of expiry, the owner takes everything back, and the channel still ends in one receipt that says so. Never while a proposal is under dispute.
channel(id), pending(id), receipt(id) and balanceOf(id) read a channel, the proposal under dispute, how it ended and what it still holds. domainSeparator() and stateDigest(state) give the exact EIP-712 digest the agent signs.
109 Foundry tests run the contract: 4 of them fuzzed over 512 runs each, and 3 invariants checked over 128 runs of random calls, among them that the contract holds exactly what its channels hold and that a standing proposal is the merge of every state submitted. 65 tests cover the Tempo SDK, 5 of them against the compiled contract on a local chain. Every rule that moves money has a test that it holds and a test that breaking it is refused with its exact custom error.
The SDK
TypeScript on viem. Amounts are bigint base units with six decimals, like every Tempo stablecoin: 2_500n is a quarter of a cent.
Two calls from any EVM wallet: approve, then openChannel. tempoCalls builds each as {to, data} and sends nothing itself.
import {createPublicClient, createWalletClient, http} from "viem";
import {
USDC_E, TEMPO_MAINNET, tempoCalls, tempoChannelIdOf, usd,
} from "@dinara/sdk";
const chain = TEMPO_MAINNET.chain;
const transport = http();
const client = createPublicClient({chain, transport});
const wallet = createWalletClient({account: owner, chain, transport});
// 1. Let the contract pull the deposit. Tempo takes the fee in pathUSD by default.
const approve = tempoCalls.approve(USDC_E, contract, usd(100));
const approved = await wallet.sendTransaction(approve);
await client.waitForTransactionReceipt({hash: approved});
// 2. Open the channel. The contract records the balance it measured.
const open = tempoCalls.openChannel(contract, {
token: USDC_E,
deposit: usd(100),
ceiling: usd(40), // never more than the deposit
agent: agent.address, // signs vouchers; never holds money
operator, // may settle at once, or null for none
expiresAt: Math.floor(Date.now() / 1000) + 24 * 3600,
label: "Research agent",
payees: [inference, search, transcribe],
});
const opened = await wallet.sendTransaction(open);
const receipt = await client.waitForTransactionReceipt({hash: opened});
const id = tempoChannelIdOf(receipt.logs, contract); // channels count from 1TempoMeterSession never touches the chain. tempoPayingFetch answers an x402 402 with a signed voucher and refuses locally what settlement would refuse.
import {createPublicClient, http} from "viem";
import {privateKeyToAccount} from "viem/accounts";
import {
paymentResponseOf, readTempoChannel, TEMPO_MAINNET,
TempoMeterSession, tempoPayingFetch,
} from "@dinara/sdk";
const chain = TEMPO_MAINNET.chain;
const client = createPublicClient({chain, transport: http()});
const channel = await readTempoChannel(client, contract, id);
if (!channel) throw new Error("No channel with that id");
const session = new TempoMeterSession(channel, privateKeyToAccount(agentKey), {
chainId: TEMPO_MAINNET.chainId,
});
// x402 V2: the dinara requirement on eip155:4217 in a 402's PAYMENT-REQUIRED
// is paid once, with a voucher for exactly its amount, in PAYMENT-SIGNATURE.
const url = "https://api.example.com/v1/complete";
const res = await tempoPayingFetch(session, url, {method: "POST", body}, {
maxPrice: 50_000n, // refuse any quote above $0.05
});
// PAYMENT-RESPONSE: {channel, contract, seq, charged, owed,
// settlement: "deferred"}
paymentResponseOf(res)?.extra;
session.latest; // the newest signed voucher: all a settlement needsNo call to Dinara and no facilitator at request time. verifyTempoX402Payment checks the signature, channel, payee, ceiling, expiry, price, that the voucher is newer than the last and lowers no line, that the channel holds this payee’s token, and that the channel’s operator, if any, is one this payee trusts to settle.
import {createPublicClient, http} from "viem";
import {
USDC_E, readTempoChannel, readTempoPayment, TEMPO_MAINNET,
tempoSettleResponse, tempoX402PaymentRequired, verifyTempoX402Payment,
x402PaymentResponseHeaders, type TempoQuote, type TempoState,
} from "@dinara/sdk";
const chain = TEMPO_MAINNET.chain;
const client = createPublicClient({chain, transport: http()});
const quote: TempoQuote = {
payee: PAYEE_ADDRESS,
price: 2_500n, // $0.0025
asset: USDC_E, // the stablecoin this API is paid in
contract, // the DinaraChannels deployment
memo: "1k tokens",
resource: {url: "https://api.example.com/v1/complete"},
trustedOperators: [DINARA_OPERATOR],
};
// The newest state accepted for this channel, and its voucher.
let previous: TempoState | null = null;
let newest: string | null = null;
const refuse = (error?: string) => tempoX402PaymentRequired(quote, {error});
export async function POST(request: Request) {
// No payment, or a refused one: 402 with PAYMENT-REQUIRED.
const payment = readTempoPayment(request.headers); // PAYMENT-SIGNATURE
if (!payment?.ok) return refuse(payment?.reason);
const channel = await readTempoChannel(client, contract, payment.channelId);
if (!channel) return refuse("no-channel");
const check = verifyTempoX402Payment({
payment, channel, terms: quote, previous,
});
if (!check.ok) return refuse(check.reason);
newest = payment.voucher; // all you need to be paid at settlement
previous = check.voucher.state;
// PAYMENT-RESPONSE: the seq and what is owed. No transaction per call.
const paid = tempoSettleResponse({check, channel});
return Response.json(await complete(request), {
headers: x402PaymentResponseHeaders(paid),
});
}tempoCalls.settle is the operator’s alone. Without an operator, tempoCalls.proposeSettlement, challengeSettlement and finalizeSettlement take the same state through the dispute window.
import {createPublicClient, createWalletClient, http} from "viem";
import {
decodeTempoVoucher, readTempoReceipt, TEMPO_MAINNET, tempoCalls,
} from "@dinara/sdk";
const chain = TEMPO_MAINNET.chain;
const transport = http();
const client = createPublicClient({chain, transport});
const wallet = createWalletClient({account: operator, chain, transport});
const signed = decodeTempoVoucher(newestVoucher)!; // the agent's newest
const settle = tempoCalls.settle(contract, id, signed);
const settled = await wallet.sendTransaction(settle);
await client.waitForTransactionReceipt({hash: settled});
// paid, refunded and each payee's line: measured from balances
const receipt = await readTempoReceipt(client, contract, id);On the wire this is x402 V2, with Dinara as its own scheme, dinara, on network eip155:4217: the 402 carries a dinara requirement in PAYMENT-REQUIRED, the agent pays in PAYMENT-SIGNATURE, and the API answers with PAYMENT-RESPONSE. x402’s upto scheme is one payer to one merchant per channel; dinara is one ceiling across many providers, settled once.
The signed state
What the agent signs for every charge, and what settlement checks. A state is cumulative: each line is the total owed to one payee so far, so the newest state alone settles the channel and an older one can only pay less.
| Order | Field | Type | What it holds |
|---|---|---|---|
| 1 | channelId | uint256 | The channel this state belongs to. The contract numbers channels from 1 |
| 2 | seq | uint64 | How many charges have been metered so far. It never decides a challenge |
| 3 | total | uint128 | The sum of every line, in base units. Never above the ceiling |
| 4 | logHead | bytes32 | The head of the hash chain over every charge: head = keccak256(abi.encode(head, seq, payeeIndex, amount, at, keccak256(bytes(memo)))) |
| 5 | lines | uint128[] | One per payee, in the channel’s order: the total owed to that payee so far |
The domain binds every signature to one chain and one deployment, so a state signed for another chain or contract recovers to some other key and is refused. Signatures are 65 bytes, low-s, with v of 27 or 28.
// The type, exactly as the contract hashes it (STATE_TYPEHASH)
State(uint256 channelId,uint64 seq,uint128 total,bytes32 logHead,uint128[] lines)
// The domain: one per chain and deployment
{name: "Dinara", version: "1", chainId: 4217, verifyingContract: DinaraChannels}On the wire a voucher is dinaraevm1. followed by base64url JSON: the state, the agent’s signature and the charge that produced it, every integer as a decimal string. Short enough for an HTTP header, and complete enough to check without asking anyone.
On chain and off chain
Approving the deposit, opening the channel and settling it are the only transactions a channel needs. Everything between them is signed, checked and kept off-chain.
Tempo mainnet, in one contract.
approve, openChannel, and settle or reclaim. A top-up or an early close adds one each; settling without an operator is a proposal and a finalize. Every fee is paid in the stablecoin.No transaction, no fee, no wallet pop-up.
PAYMENT-REQUIRED, PAYMENT-SIGNATURE and PAYMENT-RESPONSE, in the dinara schemeDeployed
Both addresses open on Tempo’s public explorer.
Deployed by contracts/script/Deploy.s.sol. Every channel, proposal and receipt lives in its storage, and it holds every deposit itself: Dinara never does.
USDC bridged to Tempo, a TIP-20 stablecoin with six decimals. Real money. Tempo has no gas token, so network fees are paid in a stablecoin too: pathUSD unless the wallet names another fee token.
Tempo mainnet is chain 4217, at https://rpc.tempo.xyz. Dinara’s operator, the one key that may settle a channel at once when the channel names it, is 0x8AFfc4f1F51A12EF3E5048903e12Bb146A9AF350.
Not done
A build page that lists only the wins is not evidence of anything.
The contract carries a test for each rule and for the case each rule refuses. That is not the same as review by somebody with no stake in the answer.
Short enough to finish a demo, too short for a payee that checks in once a day. A production deployment would set hours. A payee also has to watch for proposals itself unless it reports its vouchers to an operator that will challenge for it.
This deployment holds real USDC.e on Tempo mainnet. While nothing is audited the app refuses a deposit over $100.00 per channel, but the contract itself has no cap: anyone calling it directly is not held to that.
Acme Inference API, Northwind Search and Relay Transcription are endpoints in this app. They quote real prices, check real vouchers and are paid real USDC.e at settlement, but their answers are canned: the point is the payment, not the model.
forge test in contracts/ for the Foundry suite, then pnpm --filter @dinara/sdk test for the SDK, which runs the compiled contract on a local anvil chain when forge build and anvil are present.DRY_RUN=1 pnpm --filter @dinara/sdk example:agent:mainnet checks this deployment on Tempo mainnet without spending: the contract and its EIP-712 domain, the operator, the fee tokens and the balances. Without DRY_RUN it runs one real channel in USDC.e: a small deposit, a few paid calls over x402, one settlement, and the receipt read back from measured balances.contracts/src/DinaraChannels.sol lists the rules that move money in one place, and docs/tempo.md is the interface the contract, the SDK and this app share.