Network represents a network of nodes that are members of a specific Fabric channel. The Network can be used to access deployed smart contracts, and to listen for events emitted when blocks are committed to the ledger. Network instances are obtained from a Gateway using the getNetwork method.

To safely handle connection errors during eventing, it is recommended to use a checkpointer to track eventing progress. This allows eventing to be resumed with no loss or duplication of events.

Example

Chaincode events

const checkpointer = checkpointers.inMemory();

while (true) {
const events = await network.getChaincodeEvents(chaincodeName, {
checkpoint: checkpointer,
startBlock: BigInt(101), // Ignored if the checkpointer has checkpoint state
});
try {
for async (const event of events) {
// Process then checkpoint event
await checkpointer.checkpointChaincodeEvent(event)
}
} catch (err: unknown) {
// Connection error
} finally {
events.close();
}
}

Example

Block events

const checkpointer = checkpointers.inMemory();

while (true) {
const events = await network.getBlockEvents({
checkpoint: checkpointer,
startBlock: BigInt(101), // Ignored if the checkpointer has checkpoint state
});
try {
for async (const event of events) {
// Process then checkpoint block
await checkpointer.checkpointBlock(event.getHeader().getNumber())
}
} catch (err: unknown) {
// Connection error
} finally {
events.close();
}
}

Hierarchy

  • Network

Methods

  • Get block and private data events.

    Throws

    GatewayError Thrown by the iterator if the gRPC service invocation fails.

    Example

    const events = await network.getBlockAndPrivateEventsData({ startBlock: BigInt(101) });
    try {
    for async (const event of events) {
    // Process block and private data event
    }
    } finally {
    events.close();
    }

    Parameters

    Returns Promise<CloseableAsyncIterable<BlockAndPrivateData>>

    Blocks and private data protocol buffer messages. The iterator should be closed after use to complete the eventing session.

  • Get block events.

    Throws

    GatewayError Thrown by the iterator if the gRPC service invocation fails.

    Example

    const blocks = await network.getBlockEvents({ startBlock: BigInt(101) });
    try {
    for async (const block of blocks) {
    // Process block
    }
    } finally {
    blocks.close();
    }

    Parameters

    Returns Promise<CloseableAsyncIterable<Block>>

    Block protocol buffer messages. The iterator should be closed after use to complete the eventing session.

  • Get chaincode events emitted by transaction functions of a specific chaincode.

    Throws

    GatewayError Thrown by the iterator if the gRPC service invocation fails.

    Example

    const events = await network.getChaincodeEvents(chaincodeName, { startBlock: BigInt(101) });
    try {
    for async (const event of events) {
    // Process event
    }
    } finally {
    events.close();
    }

    Parameters

    • chaincodeName: string

      A chaincode name.

    • Optional options: EventsOptions

      Event listening options.

    Returns Promise<CloseableAsyncIterable<ChaincodeEvent>>

    The iterator should be closed after use to complete the eventing session.

  • Get a smart contract within the named chaincode. If no contract name is supplied, this is the default smart contract for the named chaincode.

    Parameters

    • chaincodeName: string

      Chaincode name.

    • Optional contractName: string

      Smart contract name.

    Returns Contract

  • Get filtered block events.

    Throws

    GatewayError Thrown by the iterator if the gRPC service invocation fails.

    Example

    const blocks = await network.getFilteredBlockEvents({ startBlock: BigInt(101) });
    try {
    for async (const block of blocks) {
    // Process block
    }
    } finally {
    blocks.close();
    }

    Parameters

    Returns Promise<CloseableAsyncIterable<FilteredBlock>>

    Filtered block protocol buffer messages. The iterator should be closed after use to complete the eventing session.

  • Get the name of the Fabric channel this network represents.

    Returns string

  • Create a request to receive chaincode events emitted by transaction functions of a specific chaincode. Supports off-line signing flow.

    Parameters

    • chaincodeName: string

      Chaincode name.

    • Optional options: EventsOptions

      Event listening options.

    Returns ChaincodeEventsRequest

Generated using TypeDoc