Coset SDK
Data Read

Data Read

After you created a Coset instance (see Getting Started), you can read the latest oracle data from a Coset node.

coset.read()

Use read() to fetch oracle data plus update metadata.

import { Coset, Networks, PaymentToken } from "@coset-dev/sdk";
 
const coset = new Coset(
  Networks.MANTLE_TESTNET,
  PaymentToken.USDC,
  "0xabc...", // Oracle Address
  process.env.PRIVATE_KEY as `0x${string}`
);
 
const result = await coset.read();
 
// Oracle payload (shape depends on the oracle / provider)
console.log(result.data);
 
// Optional metadata helpers
console.log(result.lastUpdateFormatted);
console.log(result.isUpdateRecommended);

Strict vs non-strict reads

read() accepts a single boolean parameter:

  • coset.read(false) (default) calls the node path get-data-without-check.
  • coset.read(true) calls the node path get-data.
const nonStrict = await coset.read(false);
const strict = await coset.read(true);

Important: error behavior

Unlike many SDK methods, Coset.read() does not reject on failure.

If a node call fails, it typically resolves with whatever error object was caught (often shaped like { message: string, data?: any }).

const res = await coset.read();
 
if (res.message) {
    // Handle error case
    console.error("Read failed:", res.message);
} else {
    console.log("Read OK:", res.data);
}

Returned shape (IRead)

read() resolves to an IRead object:

  • data: oracle response payload.
  • lastUpdateTimestamp / lastUpdateFormatted: when the oracle was last updated on-chain.
  • recommendedUpdateDuration: recommended update interval (seconds).
  • isUpdateRecommended: true when the data is considered stale (based on metadata).

coset.strictRead() (optional)

If you want a “read, and attempt an update as a fallback” flow, you can use Coset.strictRead().

Notes:

  • It first attempts read(true).
  • Only if read(true) throws (rejects), it performs a paid update() and returns { data } from the update result.
  • Because read(true) often returns errors instead of throwing, the update fallback may be unreachable for some failure modes.