Using the Data Anchor
You can use the Data Anchor's Rust client or CLI tool to post data on-chain and retrieve it from the ledger or our indexer service.
Prerequisite Setup
If your codebase is in Rust, please use the Rust client. Otherwise, we also provide a command line utility.
Install the client directly:
cargo add data-anchor-clientOr add the latest version in your Cargo.toml:
[dependencies]
data-anchor-client = "0.1.x"- Install - solana-cliby following the official documentation.
curl --proto '=https' --tlsv1.2 -sSfL https://solana-install.solana.workers.dev | bash- Configure - solana-clito use your preferred RPC and network e.g.- solana config set --url https://api.devnet.solana.comfor devnet.
- Download - data-anchor.
curl -sSf https://data-anchor.termina.technology/install.sh | sh- Run - data-anchorto see all the available commands and their options.
Create Namespace
A namespace represents a data collection, and only your keypair is allowed to write data to it. To create a sample namespace called nitro on Solana devnet:
let blober_client = BloberClient::builder()
    .payer(payer)
    .program_id(program_id)
    .indexer_from_url(&indexer_url)
    .await?
    .build_with_config(config)
    .await?;- The - payeris an- Arc<Keypair>that points to the Solana keypair to use with the client
- The - program_idis the address of the on-chain blober program to interact with
- The - indexer_urlis an optional parameter if using our indexer service
- The - configis a- solana_cli_config::Configobject that sets the transaction RPC details
data-anchor \
    --namespace "nitro" \
    blober initializeIf you no longer need the data commitment, close the namespace and reclaim the rent from the on-chain account:
data-anchor \
    --namespace "nitro" \
    blober closeUpload Data
To upload data, store it in a file and point the CLI to that file:
let transaction_outcomes = blober_client
    .upload_blob(data, fee, blober_id, timeout)
    .await?;- The - datais a slice of bytes (- &[u8])
- The - feeis a fee strategy that determines the priority fee
- The - blober_idis the blober PDA, or namespace, the data should be uploaded to
- [Optional] The - timeoutspecifies how long the client should wait before stopping a data upload attempt (if unset, the client will retry indefinitely)
data-anchor \
    --namespace "nitro" \
    blob upload \
    --data-path "./data.txt"Alternatively, you can directly pipe the data into the command or pass it in as a hex string via the --data param:
cat "./data.txt" | data-anchor --namespace "nitro" blob uploaddata-anchor \
    --namespace "nitro" \
    blob upload \
    --data "<some-hex-string>"After the upload is complete, you'll see output similar to the following:
Slot: 3323, Signatures: [2Vkwid2ZTman9zvGq2Tp7P2S5CUWJ2fsZSQzGzaoFkDMAgS9xNccCvNe7PJuHrXNotsVu3BoJAsRa9jdfbZraXvS, faRmYWXPQUDJcFpqffJVE49f5aMSCLYnqp1xH3DZc3SM2Uayc7jReRfR6LjNkFxeuviSJTXMTtSAmAL9tAppwyK, 5XsiKe95nk9GmkceXQkEWapAuAcEQFFYEqMfh5kyeszSxjXepSyDbgzEXmzoQniWMdWvv6mVm5Qbyh9e1i8hHF7K, 2nk2Fj2xwM7oRfsqbmDBNqYwPCLGxHcGUujBo7napJgavMSWFEQ6C9wYmLCkKcuaetBs89vtMbtYzEKaKLKjasKd, 43zzTdgoZBR3sDphuPQTZQHZdT4Ms976bRiY8jguHPZbNPibY3k4EVnrRGKCbUy97i1RzsdMRXkYyv2KJZp9MQZE], Success: trueFetch Data from Ledger
Use the signatures from the previous step to retrieve the original data from Solana:
let blob = blober_client
    .get_ledger_blobs_from_signatures(blober, signatures)
    .await?;Alternatively, set the slot number and number of lookback slots to fetch historical data.
let blobs = blober_client
    .get_ledger_blobs(slot, blober, lookback_slots)
    .await?;data-anchor \
    --namespace "nitro" \
    blob fetch 2Vkwid2ZTman9zvGq2Tp7P2S5CUWJ2fsZSQzGzaoFkDMAgS9xNccCvNe7PJuHrXNotsVu3BoJAsRa9jdfbZraXvS faRmYWXPQUDJcFpqffJVE49f5aMSCLYnqp1xH3DZc3SM2Uayc7jReRfR6LjNkFxeuviSJTXMTtSAmAL9tAppwyK 5XsiKe95nk9GmkceXQkEWapAuAcEQFFYEqMfh5kyeszSxjXepSyDbgzEXmzoQniWMdWvv6mVm5Qbyh9e1i8hHF7K 2nk2Fj2xwM7oRfsqbmDBNqYwPCLGxHcGUujBo7napJgavMSWFEQ6C9wYmLCkKcuaetBs89vtMbtYzEKaKLKjasKd 43zzTdgoZBR3sDphuPQTZQHZdT4Ms976bRiY8jguHPZbNPibY3k4EVnrRGKCbUy97i1RzsdMRXkYyv2KJZp9MQZEThis returns hex encoded bytes, so remember to hex decode it to get the data back in its original form.
Indexer Services
If you need the data to be stored for a longer time period than the ledger's lifetime of three days, you may want to use our indexer service. Follow on the steps in Indexing Data to walk through an end-to-end guide of different ways to index data you've uploaded.
Last updated
