Skip to main content

Wallet Extended

The extended version of the wallet service utilizes Anvil's infrastructure to retreive the requested information. In order for these functions to work properly, a supported wallet must be connected. The SDK automatically handles the required wallet interaction.

Connect a wallet

To utilize those functions, it is necessary to connect a wallet beforehand.

auth()

To ensure the authenticity of a wallet address, one approach is to request user authentication at the beginning of your process. This function prompts the user to sign a dummy transaction, which is subsequently validated by our backend. Upon successful validation, the function returns the authenticated stake address associated with the wallet. At this point, you can confidently utilize the wallet address since you have proof that the user is the genuine owner.

Address comparisons

Since a stake address can be associated with multiple addresses, it is crucial to compare the stake address rather than the wallet address itself. In the event that the stake address of a wallet changes, it is necessary to repeat the authentication process.

import { anvil } from "@ada-anvil/sdk-client";

try {
await anvil.wallet.connect("eternl");
await anvil.walletExt.auth({ app: "YOUR_APP_NAME" });
// Output: {stake: 'stake1u95qwe6f8srqhuv8jt7...', app: 'YOUR_APP_NAME'}
} catch {
console.log("YOUR ERROR HANDLING");
}

getAssets()

This function retrieves the assets and their metadata owned by the specified address. If you need to retrieve the assets for the entire wallet, please use the getAccountAssets() function.

Wallet balance

If your objective is only to obtain the quantity of each asset, it is recommended to use the getBalanceByPolicies() function instead.

import { anvil } from "@ada-anvil/sdk-client";

try {
await anvil.wallet.connect("eternl");
await anvil.walletExt.getAssets({
policies: ["YOUR_POLICY"],
});
// Output: [
// {
// "id": 755013,
// "unit": "1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417416e76696c5465737431303037",
// "policyId": "1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417",
// "assetName": "AnvilTest1007",
// "metadata": { ... }
// }
// ]
} catch {
console.log("YOUR ERROR HANDLING");
}

getAccountAssets()

This function retrieves the assets and their metadata owned by all the addresses within the connected wallet.

Account balance

If your objective is only to obtain the quantity of each asset without the metadata, it is recommended to use the getAccountBalanceByPolicies() function instead.

import { anvil } from "@ada-anvil/sdk-client";

try {
await anvil.wallet.connect("eternl");
await anvil.walletExt.getAccountAssets({
policies: ["YOUR_POLICY"],
});
// Output:
// [
// {
// id: 755013,
// unit: "1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417416e76696c5465737431303037",
// policyId: "1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417",
// assetName: "AnvilTest1007",
// metadata: { ... },
// },
// ]
} catch {
console.log("YOUR ERROR HANDLING");
}

sendAssets()

This function enables you to send lovelace, FT (fungible tokens), and/or NFT (non-fungible tokens). It supports various address formats such as bech32, hexa, or ada handle addresses. Additionally, you can specify multiple destination addresses and include multiple asset types within the same transaction.

Sign & Submit

This function takes care of the user signature and automatically submits the transaction.

import { anvil } from "@ada-anvil/sdk-client";

try {
await anvil.wallet.connect("eternl");
await anvil.walletExt.sendAssets({
outputs: [{ assets: [{ unit: "YOUR_UNIT", quantity: 1 }], address: "YOUR_ADDRESS" }],
});
// Output: 'f530193a136...'
} catch {
console.log("YOUR ERROR HANDLING");
}

getBalanceByPolicies()

An alias of wallet.getBalanceByPolicies()

getAccountBalanceByPolicies()

This function is similar to getBalanceByPolicies(), but it combines the balance for all addresses associated with the current stake address.

import { anvil } from "@ada-anvil/sdk-client";

try {
await anvil.wallet.connect("eternl");
await anvil.walletExt.getAccountBalanceByPolicies();
// Output: { "cardano" : { "lovelace": 300000000 }, "1af660e4c58514a2f0ea167deca340381e55bed4aea60bc09c211417": { "the_asset_name_03": 1 } }
} catch {
console.log("YOUR ERROR HANDLING");
}

signAndSubmit()

This function initiates a prompt for the user to sign the transaction, and if successful, it will automatically submit the transaction.

import { anvil } from "@ada-anvil/sdk-client";

try {
await anvil.wallet.connect("eternl");
const sign = await anvil.walletExt.signAndSubmit({ transaction: "YOUR_TX", partialSign: true });
// Output: 'f530193a136...'

// Get the transaction status
const status = await anvil.walletExt.getTxStatus({
transaction: sign.transactionHash,
network: "preprod",
});

// or

// wait for the transaction to be confirmed (usually 2-20 minutes)
const wait = await anvil.walletExt.waitTx({
transaction: sign.transactionHash,
progressCallback: console.log, // getting tx status from the cb
});

console.log("TX confirmed", wait);
} catch {
console.log("YOUR ERROR HANDLING");
}