import { GlamClient, stringToChars, StateAccountType, WSOL} from '@glamsystems/glam-sdk';import { PublicKey } from '@solana/web3.js';
2
Initialize the vault
const glamClient = new GlamClient();// Transaction optionsconst txOptions = { maxFeeLamports: 10_000, useMaxFee: true, simulate: true,};// Create the vaultconst txSig = await glamClient.state.initialize( { name: stringToChars("My First Vault"), enabled: true, accountType: StateAccountType.VAULT, baseAssetMint: WSOL, // Use wrapped SOL as base asset }, txOptions);console.log('Transaction signature:', txSig);console.log('Vault address:', glamClient.vaultPda.toBase58());console.log('Vault state:', glamClient.statePda.toBase58());
3
Understanding the result
After creating the vault, you’ll receive:
Transaction signature - Confirming the vault creation
Vault address (vaultPda) - The main vault account
Vault state (statePda) - The state/configuration account
Save these addresses as you’ll need them for future operations.
The stringToChars utility converts your vault name to the format expected by the protocol. Vault names are stored as fixed-length character arrays on-chain.
If you want to interact with a vault you created previously, you need to set the state PDA:
import { PublicKey } from '@solana/web3.js';const glamClient = new GlamClient();// Set the state PDA for an existing vaultconst vaultStatePda = new PublicKey('YOUR_VAULT_STATE_ADDRESS');glamClient.statePda = vaultStatePda;// Now you can perform operations on this vaultconst amount = new BN(1 * LAMPORTS_PER_SOL);await glamClient.vault.depositSol(amount, true, txOptions);
You need to discover the vault state PDA from the vault address. See the examples for a helper function that does this.