Misar Docs
MisarMailMisar.BlogMisarReachMisarPostMisar.DevMisar PlatformMisar IdentityMisar Posts API
Sdks

Solidity Contracts

Misar.Dev Solidity oracle contracts for on-chain, verifiable API usage billing.

Unlike the other entries in this section, the Solidity package is not a client SDK for the Misar.Dev REST API. It is a set of smart contracts (@misar/dev-contracts) that record Misar.Dev API usage on-chain for verifiable billing.

To call the Misar.Dev API (projects, templates, chat, usage, etc.), use a client SDK such as TypeScript, Swift, Dart, or C#. The Solidity contracts are for on-chain usage attestation only.

Installation

The contracts are developed and tested with Hardhat:

npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox

The contract imports OpenZeppelin cryptography utilities:

npm install @openzeppelin/contracts

Compile and test:

npx hardhat compile
npx hardhat test

MisarDevUsageOracle

A contract that stores signed, replay-protected API usage records on-chain (Solidity ^0.8.24). Usage events are signed off-chain by a trusted oracleSigner and submitted on-chain, where the signature is verified before the record is accepted.

Constructor

constructor(address _oracleSigner)

Sets owner = msg.sender and the trusted oracleSigner. Reverts with ZeroAddress if the signer is the zero address.

Functions

FunctionAccessDescription
recordUsage(bytes32 projectHash, bytes32 requestId, uint256 tokens, uint256 costMicroUsd, uint256 timestamp, bytes signature)publicRecords a usage entry. Verifies the ECDSA signature against oracleSigner; reverts ReplayDetected if requestId was already recorded, or InvalidSignature on mismatch. Emits UsageRecorded and updates cumulativeTokens / cumulativeCostMicroUsd for the project.
verifyUsage(bytes32 requestId) → boolviewReturns whether a usage record exists for the given request ID.
setOracleSigner(address newSigner)onlyOwnerRotates the trusted signer. Emits OracleSignerUpdated.
transferOwnership(address newOwner)onlyOwnerTransfers contract ownership. Emits OwnershipTransferred.

Public state

MemberTypeDescription
owneraddressContract owner.
oracleSigneraddressAddress whose signature authorizes usage records.
usageRecords(bytes32)UsageRecordPer-request usage record (projectHash, tokens, costMicroUsd, timestamp, exists).
cumulativeTokens(bytes32)uint256Running token total per projectHash.
cumulativeCostMicroUsd(bytes32)uint256Running cost total (micro-USD) per projectHash.

Events

UsageRecorded(bytes32 indexed projectHash, bytes32 indexed requestId, uint256 tokens, uint256 costMicroUsd, uint256 timestamp), OracleSignerUpdated(address indexed previous, address indexed next), OwnershipTransferred(address indexed previous, address indexed next).

Error handling

The contract uses custom errors (gas-efficient reverts): Unauthorized, InvalidSignature, ReplayDetected, and ZeroAddress.