JavaScript

Contents

Getting Started: Javascript SDK

Let's scaffold a React app and add our dependencies

Review the Solana Web3.js SDK and Solana API resources.

Instantiate the Wallet and Connection

Use the Solana docs and examples here if you need help. We're going to focus on ShdwDrive SDK in these docs, so if you need a primer on how to build a React site with Solana, we can refer you to other resources.

Solana example code:

Sample Code provided by Solana

};

Building components for various ShdwDrive operations

Let's start by instantiating the ShdwDrive connection class object. This will have all ShdwDrive methods and it implements the signing wallet within the class for all transactions.

At the simplest level, it is recommend for a React app to immediately try to load a connection to a user's ShdwDrives upon wallet connection. This can be done with the useEffect React hook.

This can be done with a NodeJS + TypeScript program as well.

Create a Storage Account

This implementation is effectively the same for both Web and Node implementations. There are three params that are required to create a storage account:

  • name: a friendly name for your storage account

  • size: The size of your storage accounts with a human readable ending containing KB, MB, or GB

  • version: can be either v1 or v2. Note - v1 is completely deprecated and you shuold only use v2 moving forward.

Get a list of Owned Storage Accounts

This implementation is effectively the same for both Web and Node implementations. The only parameter required is either v1 or v2 for the version of storage account you created in the previous step.

Full Response:

Output

Get a Specific Storage Account

This implementation is effectively the same for both Web and Node implementations. The only parameter required is either a PublicKey object or a base-58 string of the public key.

Full Response:

Output

Upload a File

The uploadFile method requires two parameters:

  • key: A PublicKey object representing the public key of the Shdw Storage Account

  • data: A file of either the File object type or ShadowFile object type

Check the intellisense popup below when hovering over the method

File objects are implemented in web browsers, and ShadowFile is a custom type we implemented in TypeScript. So either you are using File in the web, or you are scripting in TS.

Here is an example with a React Component:

React Upload Component

And a NodeJS + TypeScript implementation would look like:

TS UploadFile

Upload Multiple Files

This is a nearly identical implementation to uploadFile, except that it requires a FileList or array of ShadowFiles and an optional concurrency parameter.

Recall that the default setting is to attempt to upload 3 files concurrently. Here you can override this and specify how many files you want to try to upload based on the cores and bandwith of your infrastructure.

Delete a File

The implementation of deleteFile is the same between web and Node. There are three required parameters to delete a file:

  • key: the storage account's public key

  • url: the current URL of the file to be deleted

  • version: can be either v1 or v2

Edit a File (aka Replace a file)

The editFile method is a combo of uploadFile and deleteFile. Let's look at the params:

  • key: the Public Key of the storage account

  • url: the URL of the file that is being replaced

  • data: the file that is replacing the current file. It must have the exact same filename and extension, and it must be a File or ShadowFile object

  • version: either v1 or v2

List Storage Account Files (aka List Objects)

This is a simple implementation that only requires a public key to get the file names of a storage account.

And the response payload:

Increase Storage Account Size

This is a method to simply increase the storage limit of a storage account. It requires three params:

  • key: storage account public key

  • size: amount to increase by, must end with KB, MB, or GB

  • version: storage account version, must be v1 or v2

Reduce Storage Account Size

This is a method to decrease the storage limit of a storage account. This implementation only requires three params - the storage account key, the amount to reduce it by, and the version.

Next you'll want to claim your unused SHDW

This method allows you to reclaim the SHDW that is no longer being used. This method only requires a storage account public key and a version.

Delete a Storage Account

As the name implies, you can delete a storage account and all of its files. The storage account can still be recovered until the current epoch ends, but after that, it will be removed. This implementation only requires two params - a storage account key and a version.

Undelete the Deleted Storage Account

You can still get your storage account back if the current epoch hasn't elapsed. This implementation only requires two params - an account public key and a version.

Methods

constructor

Definition

This method is used to create a new instance of the ShdwDrive class. It accepts a web3 connection object and a web3 wallet. It returns an instance of the ShdwDrive class.

Parameters

  • connection: Connection - initialized web3 connection object

  • wallet: any - Web3 wallet

Returns

It returns an instance of the ShdwDrive class.

addStorage

Definition

addStorage is a method of the ShadowDrive class defined in index.ts at line 121. It takes three parameters: key, size, and version and returns a Promise<ShadowDriveResponse> with the confirmed transaction ID.

Parameters

  • key: PublicKey - Public Key of the existing storage to increase size on

  • size: string - Amount of storage you are requesting to add to your storage account. Should be in a string like '1KB', '1MB', '1GB'. Only KB, MB, and GB storage delineations are supported currently.

  • version: ShadowDriveVersion - ShadowDrive version (v1 or v2)

Returns

Confirmed transaction ID

cancelDeleteStorageAccount

Definition

Implementation of cancelDeleteStorageAccount defined in index.ts:135 This method is used to cancel a delete request for a Storage Account on ShdwDrive. It accepts a Public Key of the Storage Account and the ShdwDrive version (v1 or v2). It returns a Promise<{ txid: string }> containing the confirmed transaction ID of the undelete request.

Parameters

  • key: PublicKey - Publickey

Returns

Confirmed transaction ID

claimStake

Definition

This method is used to request a Stake on ShdwDrive. It accepts a PublicKey of the Storage Account and the ShdwDrive version (v1 or v2). It returns a Promise<{ txid: string }> containing the confirmed transaction ID of the claimStake request.

Parameters

  • key: PublicKey - Publickey of Storage Account

  • version: `ShadowDrive

Returns

Confirmed transaction ID

createStorageAccount

Definition

Implementation of ShdwDrive.createStorageAccount defined in index.ts:120 This method is used to create a new Storage Account on ShdwDrive. It accepts the name of the Storage Account, the size of the requested Storage Account, and the ShdwDrive version (v1 or v2). It also accepts an optional secondary owner for the Storage Account. It returns a Promise containing the created Storage Account and the transaction signature.

Parameters

  • name: string - What you want your storage account to be named. (Does not have to be unique)

  • size: string - Amount of storage you are requesting to create. Should be in a string like '1KB', '1MB', '1GB'. Only KB, MB, and GB storage delineations are supported currently.

  • version: ShadowDriveVersion - ShdwDrive version(v1 or v2)

  • owner2 (optional): PublicKey - Optional secondary owner for the storage account.

Returns

deleteFile

Definition

This method is used to delete a file on ShdwDrive. It accepts a Public Key of your Storage Account, the ShdwDrive URL of the file you are requesting to delete and the ShdwDrive version (v1 or v2). It returns a Promise containing the confirmed transaction ID of the delete request.

Parameters

  • key: PublicKey - Publickey of Storage Account

  • url: string - ShdwDrive URL of the file you are requesting to delete.

  • version: `ShdwDrive

  • Version` - ShdwDrive version (v1 or v2)

Returns

deleteStorageAccount

Definition

Implementation of ShadowDrive.deleteStorageAccount defined in index.ts:124 This method is used to delete a Storage Account on ShdwDrive. It accepts a Public Key of the Storage Account and the ShdwDrive version (v1 or v2). It returns a Promise<{ txid: string }> containing the confirmed transaction ID of the delete request.

Parameters

  • key: PublicKey - Publickey of a Storage Account

  • version: ShadowDriveVersion - ShdwDrive version (v1 or v2)

Returns

Confirmed transaction ID

editFile

Definition

This method is used to edit a file on ShdwDrive. It accepts a Public Key of your Storage Account, the URL of the existing file, the File or ShadowFile object, and the ShdwDrive version (v1 or v2). It returns a Promise containing the file location and the transaction signature.

Parameters

  • key: PublicKey - Publickey of Storage Account

  • url: string - URL of existing file

  • data: File | ShadowFile - File or ShadowFile object, file extensions should be included in the name property of ShadowFiles.

  • version: ShadowDriveVersion - ShdwDrive version (v1 or v2)

Returns

getStorageAccount

Definition

This method is used to get the details of a Storage Account on ShdwDrive. It accepts a Public Key of the Storage Account and returns a Promise containing the Storage Account details.

Parameters

  • key: PublicKey - Publickey of a Storage Account

Returns

getStorageAccounts

Definition

This method is used to get a list of all the Storage Accounts associated with the current user. It accepts a ShdwDrive version (v1 or v2). It returns a Promise<StorageAccountResponse[]> containing the list of storage accounts.

Parameters

  • version: ShadowDriveVersion - ShdwDrive version (v1 or v2)

Returns

listObjects

Definition

This method is used to list the Objects in a Storage Account on ShdwDrive. It accepts a Public Key of the Storage Account and returns a Promise containing the list of Objects in the Storage Account.

Parameters

  • storageAccount: PublicKey

Returns

makeStorageImmutable

Definition

This method is used to make a Storage Account immutable on ShdwDrive. It accepts a Public Key of the Storage Account and the ShdwDrive version (v1 or v2). It returns a Promise containing the confirmed transaction ID of the makeStorageImmutable request.

Parameters

  • key: PublicKey - Publickey of Storage Account

  • version: ShadowDriveVersion - ShdwDrive version (v1 or v2)

Returns

migrate

Definition

This method is used to migrate a Storage Account on ShdwDrive. It accepts a PublicKey of the Storage Account. It returns a Promise<{ txid: string }> containing the confirmed transaction ID of the migration request.

Parameters

  • key: PublicKey - Publickey of Storage Account

Returns

Confirmed transaction ID

redeemRent

Definition

This method is used to redeem Rent on ShdwDrive. It accepts a Public Key of the Storage Account and the Public Key of the file account to close. It returns a Promise<{ txid: string }> containing the confirmed transaction ID of the redeemRent request.

Parameters

  • key: PublicKey - Publickey of Storage Account

  • fileAccount: PublicKey - PublicKey of the file account to close

Returns

Confirmed transaction ID

reduceStorage

Definition

This method is used to reduce the storage of a Storage Account on ShdwDrive. It accepts a Public Key of the Storage Account, the amount of storage you are requesting to reduce from your storage account, and the ShdwDrive version (v1 or v2). It returns a Promise containing the confirmed transaction ID of the reduce storage request.

Parameters

  • key: PublicKey - Publickey of Storage Account

  • size: string - Amount of storage you are requesting to reduce from your storage account. Should be in a string like '1KB', '1MB', '1GB'. Only KB, MB, and GB storage delineations are supported currently.

  • version: ShadowDriveVersion - ShdwDrive version (v1 or v2)

Returns

storageConfigPDA

Definition

This exposes the PDA account in case developers have a need to display / use the data stored in the account.

Parameters

  • key: PublicKey - Publickey of Storage Account

  • data: File | ShadowFile - File or ShadowFile object, file extensions should be included in the name property of ShadowFiles.

Returns

Public Key

refreshStake

Definition

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

  • key: PublicKey - Publickey of the Storage Account

  • version: can be either v1 or v2. Note - v1 is completely deprecated and you shuold only use v2 moving forward.

Returns

topUp

Definition

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 `refreshStake` method after this.

Parameters

  • key: PublicKey - Publickey of the Storage Account

  • amount: Number - Amount of $SHDW to transfer to the stake account

Returns

uploadFile

Definition

This method is used to upload a file to ShdwDrive. It accepts a Public Key of your Storage Account and a File or ShadowFile object. The file extensions should be included in the name property of ShadowFiles. It returns a Promise containing the file location and the transaction signature.

Parameters

  • key: PublicKey - Publickey of Storage Account.

  • data: File | ShadowFile - File or ShadowFile object, file extensions should be included in the name property of ShadowFiles.

Returns

uploadMultipleFiles

Definition

This method is used to upload multiple files to a Storage Account on ShdwDrive. It accepts the Storage Account's PublicKey, a data object containing the FileList or ShadowFile array of files to upload, an optional concurrent number for the number of files to concurrently upload, and an optional callback function for every batch of files uploaded. It returns a Promise<ShadowBatchUploadResponse[]> containing the file names, locations and transaction signatures for uploaded files.

Parameters

  • key: PublicKey - Storage account PublicKey to upload the files to.

  • data: FileList | ShadowFile[] -

  • concurrent (optional): number - Number of files to concurrently upload. Default: 3

  • callback (optional): Function - Callback function for every batch of files uploaded. A number will be passed into the callback like callback(num) indicating the number of files that were confirmed in that specific batch.

Returns

userInfo

Definition

userInfo: PublicKey

Example - Using POST API requests via the Javascript SDK to make an account immutable with solana transaction signing

Help improve this resource or provide us with feedback.

Last updated