How to create Debridge Solana Calldata

https://docs.dln.trade/dln-on-chain/creating-calldata-for-solana

Example: Mint Memecoin paying in SOL
  • Custom function to mint NFT through a solana program paying 0.002 SOL

export const createMintCalldata = async (recipientAddress: string, mintQuantity: string) => {
    const connection = new Connection(rpcUrl.mainnet, 'finalized');
    const keypair = Keypair.generate(); // this will be replaced by the auth_placeholder in the main ixn
    const wallet = new Wallet(keypair);

    const provider = new AnchorProvider(connection, wallet, {
        preflightCommitment: 'finalized',
    });
    setProvider(provider);

    // Load the program using the correct program ID
    const programId = new PublicKey(pg.mainnet.PROGRAM_ID); // Assume pg.PROGRAM_ID is correctly defined
    // const idl = await Program.fetchIdl(programId, provider);
    const idl = mintIdl;

    if (!idl) {
        throw new Error('IDL file not found');
    }

    const program = new Program(idl, programId, provider);

    const payer = keypair.publicKey;
    const recipient = new PublicKey(recipientAddress);
    console.log('Recipient: ', recipient.toBase58());

    const mintDecimals = 9;
    const mintAmount = Number(mintQuantity);
    const solAmount = 0.002;
    const [mint] = PublicKey.findProgramAddressSync([Buffer.from(MINT_SEED)], programId);

    console.log('Mint address: ', mint.toBase58());

    // Get the destination token account
    const destination = await getAssociatedTokenAddress(mint, recipient);

    // Program context
    const context = {
        mint,
        destination,
        payer,
        destinationOwner: recipient,
        rent: web3.SYSVAR_RENT_PUBKEY,
        systemProgram: web3.SystemProgram.programId,
        tokenProgram: TOKEN_PROGRAM_ID,
        associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
    };

    // Solana program Mint token Ixn
    const ix1 = await program.methods
        .mintTokens(
            new BN(mintAmount * 10 ** mintDecimals), // token mint quantity
            new BN(solAmount * LAMPORTS_PER_SOL), // sol amount
        )
        .accounts(context)
        .instruction();

    // Calculate expenses -> rent + program related fees/pay
    const expense1 = BigInt((solAmount + 0.001) * LAMPORTS_PER_SOL);

    let instructionsData: PlainInstructionData[] = []; // format the PublicKey and BigInt to string before pushing

    // Replace the signer with Auth_placeholder
    ix1.keys.forEach((key) => {
        if (key.isSigner) {
            key.pubkey = AUTHORITY_PLACEHOLDER;
        }
    });
    instructionsData.push({instruction: ix1, expense: expense1});

    /** Next instruction: Close Account Ixn 
    * This is to get all the SOL back to the recipient which got left after paying rent, solAmount
    * If the Program takes payment in spl tokens, use amountSubstitutions instead.
    */
    const ix2 = new TransactionInstruction({
        keys: [
            {
                pubkey: AUTHORITY_PLACEHOLDER, // authority placeholder
                isWritable: true,
                isSigner: true,
            },
            {
                pubkey: recipient,
                isWritable: true,
                isSigner: false,
            },
            {
                pubkey: new PublicKey('11111111111111111111111111111111'), // system program
                isWritable: false,
                isSigner: false,
            },
        ],
        programId: new PublicKey('exe59FS5cojZkPJVDFDV8RnXCC7wd6yoBjsUtqH7Zai'), // dln program id
        data: Buffer.from('5c61744bf772d12e', 'hex'),
    });

    instructionsData.push({instruction: ix2}); // no expense

    // Now call the createSolanaCalldata
    const calldata = await createSolanaCalldata(instructionsData); 

Last updated