Rust
shdwDrive v1.5 is no longer maintained. Please migrate to v2 and consult the new developer guide for instructions.
Contents
get_storage_account_size (new)
make_storage_immutable (updated)
reduce_storage (updated)
refresh_stake (new)
top_up (new)
Install
The Rust SDK is available on crates.io and Rust SDK Github
Run the following Cargo command in your project directory:
cargo add shadow-drive-sdk
Or add the following line to your Cargo.toml
shadow-drive-sdk = "0.6.1"
You can find more examples on our Github
Example - Upload Multiple Files to ShdwDrive Using Rust
This Rust code example demonstrates how to upload multiple files to a ShdwDrive using the shadow_drive_rust library. It initializes a tracing subscriber, reads a keypair from a file, creates a ShdwDrive client, derives the storage account public key, reads files from a directory, creates a vector of ShadowFile structs for upload, and finally uploads the files to the ShdwDrive.
Methods
add_immutable_storage
add_immutable_storageDefinition
Adds storage capacity to the specified immutable StorageAccount. This will fail if the StorageAccount is not immutable.
Parameters
storage_account_key- The public key of the immutableStorageAccount.size- The additional amount of storage you want to add. E.g if you have an existing StorageAccount with 1MB of storage but you need 2MB total, size should equal 1MB. When specifying size, only KB, MB, and GB storage units are currently supported.
Example of add_immutable_storage
add_immutable_storageResponse from add_immutable_storage
add_immutable_storageadd_storage
add_storageDefinition
Adds storage capacity to the specified StorageAccount.
Parameters
storage_account_key- The public key of the StorageAccount.size- The additional amount of storage you want to add. E.g if you have an existing StorageAccount with 1MB of storage but you need 2MB total, size should equal 1MB. When specifying size, only KB, MB, and GB storage units are currently supported.
Example of add_storage
add_storageResponse from add_storage
add_storagecancel_delete_storage_account
cancel_delete_storage_accountDefinition
Unmarks a StorageAccount for deletion from the ShdwDrive. To prevent deletion, this method must be called before the end of the Solana epoch in which delete_storage_account is called.
Parameters
storage_account_key- The public key of theStorageAccountthat you want to unmark for deletion.
Example of cancel_delete_storage_account
cancel_delete_storage_accountResponse from cancel_delete_storage_account
cancel_delete_storage_accountclaim_stake
claim_stakeDefinition
Claims any available stake as a result of the reduce_storage command. After reducing storage amount, users must wait until the end of the epoch to successfully claim their stake.
Parameters
storage_account_key- The public key of the StorageAccount that you want to claim excess stake from.
Example of claim_stake
claim_stakeResponse from claim_stake
claim_stakecreate_storage_account
create_storage_accountDefinition
Creates a StorageAccount on the ShdwDrive. StorageAccounts can hold multiple files and are paid for using the SHDW token.
Parameters
name- The name of theStorageAccount. Does not need to be unique.size- The amount of storage theStorageAccountshould be initialized with. When specifying size, only KB, MB, and GB storage units are currently supported.
Example of create_storage_account
create_storage_accountAn example use case for this method can be found in the same github repository
Response from create_storage_account
create_storage_accountdelete_file
delete_fileDefinition
Marks a file for deletion from the ShdwDrive. Files marked for deletion are deleted at the end of each Solana epoch. Marking a file for deletion can be undone with cancel_delete_file, but this must be done before the end of the Solana epoch.
Parameters
storage_account_key- The public key of theStorageAccountthat contains the file.url- The ShdwDrive url of the file you want to mark for deletion.
Example of delete_file
delete_fileAn example use case for this method can be found in the same github repository
delete_storage_account
delete_storage_accountDefinition
This function marks a StorageAccount for deletion from the ShdwDrive. If an account is marked for deletion, all files within the account will be deleted as well. Any stake remaining in the StorageAccount will be refunded to the creator. Accounts marked for deletion are deleted at the end of each Solana epoch.
Parameters
storage_account_key- The public key of the StorageAccount that you want to mark for deletion.
Response from delete_storage_account
delete_storage_accountThis method returns success if it can successfully mark the account for deletion and refund any remaining stake in the account before the end of the current Solana epoch.
Example of delete_storage_account
delete_storage_accountAn example use case for this method can be found in the same github repository on line 71.
edit_file
edit_fileDefinition
Replace an existing file on the ShdwDrive with the given updated file.
Parameters
storage_account_key- The public key of theStorageAccountthat contains the file.url- The ShdwDrive url of the file you want to replace.data- The updatedShadowFile.
Example of edit_file
edit_fileResponse from edit_file
edit_fileExamples found in repository
File: examples/end_to_end.rs, Line 53
get_object_data
get_object_dataRetrieve object data
get_storage_account
get_storage_accountDefinition
Returns the StorageAccount associated with the pubkey provided by a user.
Parameters
key- The public key of theStorageAccount.
Example of get_storage_account
get_storage_accountResponse for V1 StorageAccount from get_storage_account
get_storage_accountResponse for V2 StorageAccount from get_storage_account
get_storage_accountget_storage_accounts
get_storage_accountsDefinition
Returns all StorageAccounts associated with the public key provided by a user.
Parameters
owner- The public key that is the owner of all the returnedStorageAccounts.
Example of get_storage_accounts
get_storage_accountsResponse for V1 StorageAccount from get_storage_accounts
get_storage_accountsResponse for V2 StorageAccount from get_storage_accounts
get_storage_accountsget_storage_account_size
get_storage_account_sizeDefinition
This method is used to get the amount of storage currently used by a given storage account.
Parameters
storage_account_key- The public key of theStorageAccountthat owns the files.
Example of get_storage_account_size
get_storage_account_sizeResponse from get_storage_account_size
get_storage_account_sizelist_objects
list_objectsDefinition
Gets a list of all files associated with a StorageAccount. The output contains all of the file names as strings.
Parameters
storage_account_key- The public key of theStorageAccountthat owns the files.
Example of list_objects
list_objectsResponse from list_objects
list_objectsNote: The response is a vector containing all of the file names as strings.
make_storage_immutable
make_storage_immutableDefinition
Permanently locks a StorageAccount and all contained files. After a StorageAccount has been locked, a user will no longer be able to delete/edit files, add/reduce storage amount, or delete the StorageAccount.
Parameters
storage_account_key- The public key of theStorageAccountthat will be made immutable.
Example of make_storage_immutable
make_storage_immutableResponse from make_storage_immutable
make_storage_immutablemigrate
migrateDefinition
Migrates a v1 StorageAccount to v2. This requires two separate transactions to reuse the original pubkey. To minimize chance of failure, it is recommended to call this method with a commitment level of Finalized.
Parameters
storage_account_key- The public key of the StorageAccount to be migrated.
Example of migrate
migrateResponse from migrate
migratemigrate_step_1
migrate_step_1Definition
First transaction step that migrates a v1 StorageAccount to v2. Consists of copying the existing account’s data into an intermediate account, and deleting the v1 StorageAccount.
migrate_step_2
migrate_step_2Definition
Second transaction step that migrates a v1 StorageAccount to v2. Consists of recreating the StorageAccount using the original pubkey, and deleting the intermediate account.
new
newDefinition
Creates a new ShadowDriveClient from the given Signer and URL.
Parameters
wallet- ASignerthat for signs all transactions generated by the client. Typically this is a user’s keypair.rpc_url- An HTTP URL of a Solana RPC provider. The underlying Solana RPC client is configured with 120s timeout and a commitment level of confirmed.
To customize RpcClient settings see new_with_rpc.
Example of new
newExamples found in repository
examples/end_to_end.rs (line 19)
new_with_rpc
new_with_rpcDefinition
Creates a new ShadowDriveClient from the given Signer and RpcClient.
Parameters
wallet- ASignerthat for signs all transactions generated by the client. Typically this is a user’s keypair.rpc_client- A SolanaRpcClientthat handles sending transactions and reading accounts from the blockchain. Providing theRpcClientallows customization of timeout and commitment level.
Example of new_with_rpc
new_with_rpcredeem_rent
redeem_rentDefinition
Reclaims the Solana rent from any on-chain file accounts. Older versions of the ShdwDrive used to create accounts for uploaded files.
Parameters
storage_account_key- The public key of the StorageAccount that contained the deleted file.file_account_key- The public key of the File account to be closed.
Example of redeem_rent
redeem_rentResponse from redeem_rent
redeem_rentreduce_storage
reduce_storageDefinition
Reduces the amount of total storage available for the given StorageAccount.
Parameters
storage_account_key- The public key of theStorageAccountwhose storage will be reduced.size- The amount of storage you want to remove. E.g if you have an existingStorageAccountwith 3MB of storage but you want 2MB total, size should equal 1MB. When specifying size, only KB, MB, and GB storage units are currently supported.
Example of reduce_storage
reduce_storageResponse from reduce_storage
reduce_storagerefresh_stake
refresh_stakeDefinition
This method is used to update your storage account's stake amount. It is required to call this method after calling the `topUp` method in order for your stage account to update properly.
Parameters
storage_account_key:PublicKey- Publickey of the Storage Account
Example of refresh_stake
refresh_stakeResponse from refresh_stake
refresh_stakestore_files
store_filesDefinition
Stores files in the specified StorageAccount.
Parameters
storage_account_key- The public key of theStorageAccount.data- Vector ofShadowFileobjects representing the files that will be stored.
Example of store_files
store_filesResponse from store_files
store_filestop_up
top_upDefinition
This method is used to top up a storage account's $SHDW balance to cover any necessary fees, like mutable storage fees which are collected every epoch. It is necessary to call the `refresh_stake` method after this.
Parameters
key:PublicKey- Publickey of the Storage Accountamount:u64- Amount of $SHDW to transfer to the stake account
Example of top_up
top_upResponse from top_up
top_upExample - ShdwDrive Client: Creating and Managing Storage Accounts using Rust
Example - Cancel Storage Account Deletion using rust
Example - Claim Stake using rust
Example - Uploading and Deleting Files with rust SDK
Example - Delete Storage Account using rust
Example - Tests
Example - Creating and Migrating a Storage Account using ShadowDriveClient in Rust
Example - Redeem Rent for Storage using Rust
Example - Uploading Multiple Files to a storage account in rust
Last updated