import {
Connection,
Keypair,
SystemProgram,
LAMPORTS_PER_SOL,
Transaction,
} from '@solana/web3.js';
import {
Actions,
createEd25519AuthorityInfo,
fetchSwigAccount,
getCreateSwigInstruction,
getAddAuthorityInstructions,
getSignInstructions,
findSwigPda,
} from '@swig-wallet/classic';
async function demonstrateV2Features() {
const connection = new Connection('https://api.devnet.solana.com');
// Setup keypairs
const userKeypair = Keypair.generate();
const authorityKeypair = Keypair.generate();
const recipientKeypair = Keypair.generate();
// Fund the user account
const airdropSignature = await connection.requestAirdrop(
userKeypair.publicKey,
2 * LAMPORTS_PER_SOL
);
await connection.confirmTransaction(airdropSignature);
// Create a new Swig account
const swigId = Keypair.generate().publicKey;
const swigAccountAddress = findSwigPda(swigId);
const createInstructions = getCreateSwigInstruction(
userKeypair.publicKey,
swigId,
Actions.set().all().get(),
createEd25519AuthorityInfo({
id: userKeypair.publicKey,
signer: userKeypair.publicKey,
})
);
// Create the account
const createTx = new Transaction().add(...createInstructions);
createTx.feePayer = userKeypair.publicKey;
createTx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
createTx.sign(userKeypair);
await connection.sendAndConfirmTransaction(createTx, [userKeypair]);
// Fetch the created account and check version
const swig = await fetchSwigAccount(connection, swigAccountAddress);
console.log(`Created account version: ${swig.accountVersion()}`);
console.log(`Wallet address: ${(await getSwigWalletAddress(swig)).toBase58()}`);
// Add an authority with automatic program action management
const addAuthorityInstructions = await getAddAuthorityInstructions(
swig,
0, // root role
createEd25519AuthorityInfo({
id: authorityKeypair.publicKey,
signer: authorityKeypair.publicKey,
}),
Actions.set().manageAuthority().get()
);
const addAuthorityTx = new Transaction().add(...addAuthorityInstructions);
addAuthorityTx.feePayer = userKeypair.publicKey;
addAuthorityTx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
addAuthorityTx.sign(userKeypair);
await connection.sendAndConfirmTransaction(addAuthorityTx, [userKeypair]);
// Perform a transfer using automatic instruction selection
const transferInstructions = await getSignInstructions(
swig,
0, // role id
[
SystemProgram.transfer({
fromPubkey: await getSwigWalletAddress(swig), // Auto-resolves correct address
toPubkey: recipientKeypair.publicKey,
lamports: BigInt(LAMPORTS_PER_SOL / 4),
}),
]
);
const transferTx = new Transaction().add(...transferInstructions);
transferTx.feePayer = userKeypair.publicKey;
transferTx.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
transferTx.sign(userKeypair);
await connection.sendAndConfirmTransaction(transferTx, [userKeypair]);
console.log('Transfer completed successfully!');
// Check final balances
const walletBalance = await connection.getBalance(await getSwigWalletAddress(swig));
const recipientBalance = await connection.getBalance(recipientKeypair.publicKey);
console.log(`Wallet balance: ${walletBalance / LAMPORTS_PER_SOL} SOL`);
console.log(`Recipient balance: ${recipientBalance / LAMPORTS_PER_SOL} SOL`);
}
// Run the example
demonstrateV2Features().catch(console.error);