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-toolboxThe contract imports OpenZeppelin cryptography utilities:
npm install @openzeppelin/contractsCompile and test:
npx hardhat compile
npx hardhat testMisarDevUsageOracle
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
| Function | Access | Description |
|---|---|---|
recordUsage(bytes32 projectHash, bytes32 requestId, uint256 tokens, uint256 costMicroUsd, uint256 timestamp, bytes signature) | public | Records 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) → bool | view | Returns whether a usage record exists for the given request ID. |
setOracleSigner(address newSigner) | onlyOwner | Rotates the trusted signer. Emits OracleSignerUpdated. |
transferOwnership(address newOwner) | onlyOwner | Transfers contract ownership. Emits OwnershipTransferred. |
Public state
| Member | Type | Description |
|---|---|---|
owner | address | Contract owner. |
oracleSigner | address | Address whose signature authorizes usage records. |
usageRecords(bytes32) | UsageRecord | Per-request usage record (projectHash, tokens, costMicroUsd, timestamp, exists). |
cumulativeTokens(bytes32) | uint256 | Running token total per projectHash. |
cumulativeCostMicroUsd(bytes32) | uint256 | Running 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.