Importing Accounts

You can import state from anywhere including mainnet

Import State Directly from Mainnet

Use this endpoint to load account state directly from mainnet with only the account addresses.

Coming Soon: You will be able to upload a token mint, and update it's mint authority to whatever account you like, being able to mint from that account at will

import * as dotenv from 'dotenv';

dotenv.config();

interface RequestBody {
  accounts: string[];
}

async function setMainnetAccountState(blockchainID: string, accounts: string[]): Promise<void> {
  const mirrorApiKey = process.env.MIRROR_API_KEY;

  if (!mirrorApiKey) {
    throw new Error('MIRROR_API_KEY is not defined in the environment variables');
  }

  const requestBody: RequestBody = {
    accounts: accounts,
  };

  try {
    const response = await fetch(`https://api.mirror.ad/blockchains/${blockchainID}/accounts/mainnet`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'api_key': mirrorApiKey,
      },
      body: JSON.stringify(requestBody),
    });

    if (response.status !== 200) {
      throw new Error(`Error getting RPC URL: ${response.status}`);
    }
  } catch (error) {
    throw new Error(`Request failed: ${error.message}`);
  }
}

Directly Import State

You can also upload state directly.

For use cases where you have a subset of PDAs from say Jupiter, you can upload these direclty to your chain. We are working on providing environments with these PDAs pre-loaded, so if that would benefit you, please reach out and let us know!

interface SolanaAccount {
  address: string;
  lamports: number;
  data: Uint8Array;
  owner: string;
  executable: boolean;
  rentEpoch: number;
}

interface SetBlockchainRequest {
  address: string;
  lamports: number;
  data: string;
  owner: string;
  rentEpoch: number;
  label?: string;
  executable: boolean;
}

async function setAccounts(blockchainID: string, accounts: SolanaAccount[], label?: string): Promise<void> {
  const reqBody: SetBlockchainRequest[] = accounts.map(account => ({
    address: account.address,
    lamports: account.lamports,
    data: btoa(String.fromCharCode(...account.data)),
    owner: account.owner,
    rentEpoch: account.rentEpoch,
    label: label,
    executable: account.executable,
  }));

  const reqBytes = JSON.stringify(reqBody);

  try {
    const response = await fetch(`https://engine.mirror.ad/accounts/${blockchainID}`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
      body: reqBytes,
    });

    if (response.status !== 200) {
      const body = await response.text();
      console.error('error response', response.status, body);
      throw new Error('HTTP request failed');
    }
  } catch (error) {
    console.error('error sending request', error);
    throw new Error('HTTP request failed');
  }
}

Last updated