From 629a7917635b999f6f597a5f3634cf2e41fff0eb Mon Sep 17 00:00:00 2001 From: A91y Date: Sat, 28 Dec 2024 21:26:06 +0530 Subject: [PATCH] feat: add deposit_to_multisig function for transferring SOL or SPL tokens --- .../squads_multisig/deposit_to_multisig.ts | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/tools/squads_multisig/deposit_to_multisig.ts diff --git a/src/tools/squads_multisig/deposit_to_multisig.ts b/src/tools/squads_multisig/deposit_to_multisig.ts new file mode 100644 index 0000000..4e1d9c2 --- /dev/null +++ b/src/tools/squads_multisig/deposit_to_multisig.ts @@ -0,0 +1,91 @@ +import { SolanaAgentKit } from "../../index"; +import { PublicKey, SystemProgram, Transaction } from "@solana/web3.js"; +import { LAMPORTS_PER_SOL } from "@solana/web3.js"; +import { + getAssociatedTokenAddress, + createTransferInstruction, + getMint, + createAssociatedTokenAccountInstruction, +} from "@solana/spl-token"; +import * as multisig from "@sqds/multisig"; + +/** + * Transfer SOL or SPL tokens to a recipient + * @param agent SolanaAgentKit instance + * @param amount Amount to transfer + * @param vaultIndex Optional vault index, default is 0 + * @param mint Optional mint address for SPL tokens + * @returns Transaction signature + */ +export async function deposit_to_multisig( + agent: SolanaAgentKit, + amount: number, + vaultIndex?: number, + mint?: PublicKey, +): Promise { + try { + let tx: string; + if (!vaultIndex) { + vaultIndex = 0; + } + const createKey = agent.wallet; + const [multisigPda] = multisig.getMultisigPda({ + createKey: createKey.publicKey, + }); + const [vaultPda] = multisig.getVaultPda({ + multisigPda, + index: vaultIndex, + }); + const to = vaultPda; + if (!mint) { + // Transfer native SOL + const transaction = new Transaction().add( + SystemProgram.transfer({ + fromPubkey: agent.wallet_address, + toPubkey: to, + lamports: amount * LAMPORTS_PER_SOL, + }), + ); + + tx = await agent.connection.sendTransaction(transaction, [agent.wallet]); + } else { + // Transfer SPL token + const fromAta = await getAssociatedTokenAddress( + mint, + agent.wallet_address, + ); + let transaction = new Transaction(); + const toAta = await getAssociatedTokenAddress(mint, to, true); + const toTokenAccountInfo = await agent.connection.getAccountInfo(toAta); + // Create associated token account if it doesn't exist + if (!toTokenAccountInfo) { + transaction.add( + createAssociatedTokenAccountInstruction( + agent.wallet_address, + toAta, + to, + mint, + ), + ); + } + // Get mint info to determine decimals + const mintInfo = await getMint(agent.connection, mint); + const adjustedAmount = amount * Math.pow(10, mintInfo.decimals); + + transaction.add( + createTransferInstruction( + fromAta, + toAta, + agent.wallet_address, + adjustedAmount, + ), + ); + + tx = await agent.connection.sendTransaction(transaction, [agent.wallet]); + } + + return tx; + } catch (error: any) { + throw new Error(`Transfer failed: ${error}`); + } +}