Fulfillment Cross-Chain Request Link

This script creates and fulfills cross-chain request links.

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

const recipientAddress = '0x42A5DC31169Da17639468e7Ffa271e90Fdb5e85A';
const fromToken = '0xc2132D05D31c914a87C6611C10748AEb04B58e8F'; // USDT on Polygon
const tokenAddress = '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85'; // USDC on Optimism
const chainId = '10'; // Optimism
const fromChainId = '137'; // Polygon
const tokenAmount = '10';
const tokenDecimals = '6';
const APIKey = 'api-key';
const apiUrl = 'api-url';
const userPrivateKey = 'your-private-key';

// Initialize provider and wallet
async function getSourceChainProvider() {
    const sourceChainProvider = await ethers.getDefaultProvider(fromChainId);
    return new ethers.Wallet(userPrivateKey, sourceChainProvider); // Connect wallet to the provider
}

const userSourceChainWallet = await getSourceChainProvider();

// Function to create a request link
async function createRequestLink(): Promise<string | null> {
    try {
        const { link } = await peanut.createRequestLink({
            chainId,
            tokenAddress,
            tokenAmount,
            tokenType: EPeanutLinkType.erc20, // ERC20 token transfer
            tokenDecimals,
            recipientAddress,
            APIKey,
            apiUrl,
        });
        return link;
    } catch (error) {
        console.error('Error creating request link:', error);
        return null;
    }
}

// Function to fulfill a request link using provided parameters.
async function fulfillRequestLink(link: string) {
    try {
        // Fetch the details of the request link
        const linkDetails = await peanut.getRequestLinkDetails({
            link,
            APIKey,
            apiUrl: 'https://staging.peanut.to/api/proxy/get',
        });
        console.log('Got the link details!', linkDetails);

        // Prepare the unsigned cross-chain transaction
        const xchainUnsignedTxs = await peanut.prepareXchainRequestFulfillmentTransaction({
            fromChainId,
            senderAddress: userSourceChainWallet.address,
            fromToken,
            link,
            squidRouterUrl: getSquidRouterUrl(true, false),
            provider: userSourceChainWallet.provider,
            apiUrl,
            fromTokenDecimals: tokenDecimals,
            tokenType: EPeanutLinkType.erc20,
        });
        console.log('Computed xchain unsigned fulfillment transactions', xchainUnsignedTxs);

        // Calculate transaction fees
        const fee = await peanut.calculateCrossChainTxFee({
            unsignedTxs: xchainUnsignedTxs.unsignedTxs,
            isNativeTxValue: false,
            fromAmount: tokenAmount.toString(),
        });
        console.log('Calculated cross-chain transaction fee:', fee);

        // Sign and submit the transaction
        for (const unsignedTx of xchainUnsignedTxs.unsignedTxs) {
            const { tx, txHash } = await signAndSubmitTx({
                unsignedTx,
                structSigner: {
                    signer: userSourceChainWallet,
                    gasLimit: BigNumber.from(2_000_000),
                },
            });
            console.log('Submitted a transaction to fulfill the request link with tx hash', txHash);
            await tx.wait();
            console.log('Request link fulfillment completed!');
        }
    } catch (error) {
        console.error('Error fulfilling request link:', error);
    }
}

(async () => {
    const link = await createRequestLink();
    if (link) {
        console.log('Created request link:', link);
        // Call the fulfillRequestLink function to complete the transaction
        await fulfillRequestLink(link);
    }
})();

Last updated