This script fulfills request links and completes them.

When receiving a link (after creation) it can be fulfilled with the same pair of token chain (or cross-chain), you will need your peanut api key (get one here!) and the following:

Simple example:

Copy

import peanut from '@squirrel-labs/peanut-sdk';
import { BigNumber, ethers } from 'ethers';

const userPrivateKey = 'SENDER-PRIVATE-KEY';
const APIKey = 'YOUR-API-KEY-HERE';
const link = 'PREVIOUSLY-CREATED-LINK'
//const apiUrl = 'OPTIONAL-DIFFERENT-API-URL';

// Initialize provider and wallet
function getWallet(chainId: number, privateKey: string): ethers.Wallet  {
    return new ethers.Wallet(privateKey, ethers.getDefaultProvider(chainId)); // Connect wallet to the provider
}

// Function to fulfill a request link
async function fulfillRequestLink(link: string) {
    try {
        // Get the details of the request link
        const linkDetails = await peanut.getRequestLinkDetails({
            link,
            APIKey,
            //apiUrl, //optional different apiUrl
        });
        console.log('Got the link details!', linkDetails);

        // Prepare the unsigned transaction for fulfilling the request link
        const { unsignedTx } = peanut.prepareRequestLinkFulfillmentTransaction({
            recipientAddress: linkDetails.recipientAddress!,
            tokenAddress: linkDetails.tokenAddress,
            tokenAmount: linkDetails.tokenAmount,
            tokenDecimals: linkDetails.tokenDecimals,
            tokenType: peanut.interfaces.EPeanutLinkType.erc20,
        });
        console.log('Prepared unsigned transaction', unsignedTx);

        // Sign and submit the transaction
        const signer = getWallet(Number(linkDetails.chainId), userPrivateKey)
        const { tx, txHash } = await peanut.signAndSubmitTx({
            unsignedTx,
            structSigner: {
                signer,
                gasLimit: BigNumber.from(2_000_000),
            },
        });
        console.log('Submitted transaction with tx hash', txHash);
        await tx.wait();
        console.log('Request link fulfillment completed!');

        await peanut.submitRequestLinkFulfillment({
          chainId: linkDetails.chainId,
          hash: txHash,
          payerAddress: signer.address,
	        link,
          amountUsd: '',
          //apiUrl, // optional differnt apiUrl
        })

    } catch (error) {
        console.error('Error fulfilling request link:', error);
    }
}

(async () => {
    // Fulfill the request link
    await fulfillRequestLink(link);
})();