Kit
Functions

assertIsLamports

assertIsLamports(putativeLamports): asserts putativeLamports is Lamports

Lamport values returned from the RPC API conform to the type Lamports. You can use a value of that type wherever a quantity of Lamports is expected.

Parameters

ParameterType
putativeLamportsbigint

Returns

asserts putativeLamports is Lamports

Example

From time to time you might acquire a number that you expect to be a quantity of Lamports, from an untrusted network API or user input. To assert that such an arbitrary number is usable as a quantity of Lamports, use this function.

import { assertIsLamports } from '@solana/rpc-types';
 
// Imagine a function that creates a transfer instruction when a user submits a form.
function handleSubmit() {
    // We know only that what the user typed conforms to the `number` type.
    const lamports: number = parseInt(quantityInput.value, 10);
    try {
        // If this type assertion function doesn't throw, then
        // Typescript will upcast `lamports` to `Lamports`.
        assertIsLamports(lamports);
        // At this point, `lamports` is a `Lamports` that can be used anywhere Lamports are expected.
        await transfer(fromAddress, toAddress, lamports);
    } catch (e) {
        // `lamports` turned out not to validate as a quantity of Lamports.
    }
}

On this page