Skip to main content

getOperation

Retrieve a specific operation by its unique ID. This is a query method that doesn’t modify state.

Method Signature

getOperation: ActorMethod<[bigint], [] | [Operation]>

Parameters

ParameterTypeRequiredDescription
idbigintYesThe unique operation ID to retrieve

Response

Returns an optional Operation:
  • [] - Operation not found
  • [Operation] - Operation found
interface Operation {
  time: Time; // bigint timestamp
  typeof: OperationType;
}

OperationType

Operations can be one of several types:
type OperationType = 
  | { access: { user: string } }
  | { token: { tokenid: TokenID, deltas: Array<Delta> } }
  | { trade: TradeOperation }
  | { other: { data: Metadata, name: string } }
  | { mint: { tokenid: TokenID, data: Metadata } }
  | { transaction: TransactionOperation }
For detailed type definitions, see the getOperations documentation.

Example Usage

Basic Operation Lookup

const operationId = 12345n;
const result = await actor.getOperation(operationId);

if (result.length === 0) {
  console.log("Operation not found");
} else {
  const operation = result[0];
  console.log("Operation found:", operation);
  console.log("Timestamp:", operation.time);
  console.log("Type:", operation.typeof);
}

Check Operation Type

const result = await actor.getOperation(12345n);

if (result.length > 0) {
  const operation = result[0];
  
  if ('trade' in operation.typeof) {
    const trade = operation.typeof.trade;
    console.log(`Trade: ${trade.user} ${trade.typeof.buy ? 'bought' : 'sold'} ${trade.amount_token} tokens`);
  } else if ('mint' in operation.typeof) {
    const mint = operation.typeof.mint;
    console.log(`Mint: Token ${mint.tokenid} was minted`);
  } else if ('transaction' in operation.typeof) {
    const tx = operation.typeof.transaction;
    console.log(`Transaction: ${tx.description} - ${tx.amount} tokens`);
  }
}

Batch Operation Lookup

async function getMultipleOperations(ids: bigint[]) {
  const promises = ids.map(id => actor.getOperation(id));
  const results = await Promise.all(promises);
  
  const operations = results
    .map((result, index) => ({
      id: ids[index],
      operation: result.length > 0 ? result[0] : null
    }))
    .filter(item => item.operation !== null);
    
  return operations;
}

// Usage
const operationIds = [100n, 101n, 102n];
const operations = await getMultipleOperations(operationIds);

Format Operation Timestamp

function formatOperationTime(operation: Operation): string {
  // Convert nanoseconds to milliseconds for JavaScript Date
  const milliseconds = Number(operation.time / 1_000_000n);
  const date = new Date(milliseconds);
  return date.toISOString();
}

const result = await actor.getOperation(12345n);
if (result.length > 0) {
  const operation = result[0];
  console.log("Operation time:", formatOperationTime(operation));
}

Query Method

This is a query method, meaning:
  • It doesn’t modify canister state
  • It’s faster and cheaper to call
  • Results are not certified (for certified data, use update calls)
  • Can be called without authentication in some cases
I