Skip to content

fetchFromChain

fetchFromChain(schemaUri): Promise<ISchemaDetails | null>

Retrieves schema details from the blockchain using a given schema ID. This function plays a crucial role in accessing stored schemas within a blockchain environment. It queries the blockchain to fetch the schema associated with the provided schema ID, facilitating the retrieval of schema information stored in an immutable and secure manner.

Parameters

schemaUri: schema:cord:${string}

The unique identifier of the schema, formatted as a URI string. This ID is used to locate and retrieve the schema on the blockchain, ensuring accuracy in schema retrieval.

Returns

Promise<ISchemaDetails | null>

  • A promise that resolves to the schema details (ISchemaDetails) if found on the blockchain. If the schema is not present, the promise resolves to null. This approach provides a straightforward method for accessing schema information by their unique identifiers.

The function employs a try-catch block to handle any errors during the blockchain query process. If the schema is not found or if an error occurs during fetching, appropriate exceptions are thrown to indicate the issue.

Throws

  • Thrown if the schema with the provided ID is not found on the blockchain, providing clarity in cases where the requested data is missing.

Throws

  • Thrown in case of errors during the fetching process, such as network issues or problems with querying the blockchain.

Example

async function getSchemaDetails(schemaUri: string) {
try {
const schemaDetails = await fetchFromChain(schemaUri);
if (schemaDetails) {
console.log('Fetched Schema Details:', schemaDetails);
} else {
console.log('Schema not found on the blockchain.');
}
} catch (error) {
console.error('Error fetching schema:', error);
}
}
// Example usage
getSchemaDetails('your_schema_uri');

Source

schema/src/Schema.chain.ts:346