Pay a Request Link X-Chain

You can also pay the request link with any token / chain pair

When receiving a link (after creation) it can be fulfilled with another (supported) pair of token chain (when not paying with the same token chain that was requested), you will need your peanut api key (get one here!) and the following:

  • The request link you intend to pay

  • A way of doing transactions (in this simple example we use a private key wallet)

  • The information of the token from where you wish to pay (token address, chain id and token decimals)

Simple example:

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';
const fromToken = '0xc2132D05D31c914a87C6611C10748AEb04B58e8F'; // USDT on Polygon
const fromChainId = '137'; // Polygon
const tokenDecimals = 6;

// 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);


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

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

        await peanut.submitRequestLinkFulfillment({
          chainId: linkDetails.chainId,
          hash: lastHash,
          payerAddress: senderWallet.address,
	        link,
          amountUsd: '',
          //apiUrl, // optional differnt apiUrl
        })
        
    } catch (error) {
        console.error('Error fulfilling request link:', error);
    }
}

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

Last updated