Alphanet JavaScript SDKs

On this page, we are outlining the two JavaScript SDK you can use to communicate with the wallet and Alphanet. For an end-to-end tutorial, follow the README on this repository. It will guide you through deploying and interacting with the gumball machine blueprint!

Wallet SDK

The JavaScript wallet SDK allows you to communicate with the Alphanet wallet browser extension. It helps you fetch the current account’s address and send transaction manifests to the wallet to be signed and submitted to the network.

To have access to this SDK, you should install it in your project’s dependencies by running this command: npm install @radixdlt/alphanet-walletextension-sdk.

You can then import and initialize the SDK like so:

import Sdk, { ManifestBuilder } from '@radixdlt/alphanet-walletextension-sdk';

// Initialize the SDK
const sdk = Sdk()

Examples

Here is an example of using the Wallet SDK to fetch the current account’s address:

const result = await sdk.request({
    accountAddresses: {},
})

if (result.isErr()) {
    throw result.error
}

const { accountAddresses } = result.value

To send a transaction manifest to the wallet, you can use the sendTransaction method like so:

let manifest = new ManifestBuilder()
    .callMethod("[account_address]", "lock_fee", ['Decimal("100")'])
    .withdrawFromAccountByAmount("[account_address]", 10, "resource_tdx_a_1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzqegh4k9")
    .callMethod("[account_address]", "deposit_batch", ['Expression("ENTIRE_WORKTOP")'])
    .build()
    .toString();

const transactionHash = await sdk
  .sendTransaction(manifest)
  .map((response) => response.transactionHash)

If the transaction manifest is valid and the wallet is able to sign and submit it, it will give you back the transaction’s hash. You should then use the Gateway SDK to get the transaction receipt. You can find a full example where the two SDKs work together here.

Gateway SDK

The Gateway SDK is what you will use to communicate with the Gateway API. This SDK was generated with OpenAPI and allows you to call all the API endpoints in JavaScript. You can find its source code here For a full description of each endpoint, please refer to this page. It allows you to fetch the receipt of a submitted transaction, the state of components, data about resources and much more!

To add this SDK as a dependency to your project, you should run the following command: npm install @radixdlt/alphanet-gateway-api-v0-sdk. You can then import the Gateway SDK by adding this line to your JavaScript file:

import { StateApi, TransactionApi, StatusApi } from '@radixdlt/alphanet-gateway-api-v0-sdk'

const transactionApi = new TransactionApi()
const stateApi = new StateApi()
const statusApi = new StatusApi()

Examples

We are going to cover a couple of examples so you can get a grasp on the workflow of using this SDK. For the first example, we are going to show how to fetch the balance of a particular component:

// Fetch the state of a component
const account_state = await stateApi.stateComponentPost({
  v0StateComponentRequest: { component_address: accountAddress }
})

let account_vault = account_state.owned_vaults.find(vault => vault.resource_amount.resource_address == "[resource_address]")
let amount = account_vault.resource_amount.amount_attos / Math.pow(10,18)

Similarly, you can get information about a resource manager like this:

let data = await stateApi.stateResourcePost({
  v0StateResourceRequest: {resource_address: "resource_tdx_a_1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzqegh4k9"}
});

console.log(data);

For the last example, we will see how to retrieve the current epoch:

let data = await stateApi.stateEpochPost();
console.log(data);