🥜
Peanut Docs
  • Learn
    • 🥜What is Peanut?
    • 🏦Cashout
      • Supported geographies
    • 📩How to use Peanut Links?
      • ⚙️How do Peanut Links Work?
      • 🔒Trust assumptions
    • 📌Use cases
    • 📚Case Studies
      • 🎁Welcome Packs
      • 📘Raffles to Boost UWAs and Transactions
      • 📗Sending Testnet Tokens at Hackathons
      • 📙IRL Events Marketing
    • ⛓️Supported Chains and Tokens
    • 💰Pricing
    • 🆘Support
  • Integrate
    • Using the SDK
      • Create ClaimLinks
        • Create Link
        • Batch Create Links
        • Create Raffle Links
        • Create Multi-Token Link
        • Create NFT Link
        • Create Gasless Links
        • Create Branded Links
      • Claim
        • Claim Link
        • Claim Gasless Link
        • Claim Cross-Chain Link
        • Claim Raffle Link
        • Claim Link as Sender (Reclaiming)
      • Create Request Link
      • Pay a Request Link
      • Pay a Request Link X-Chain
      • Utils
        • Get Link Details
        • Cross-chain
          • Get Supported Destination Chains
          • Get Supported Destination Tokens
          • Get Cross-Chain Options
          • Get Cross-Chain Route
        • Raffle (Legacy)
          • Get Raffle Info
          • Get User Raffle Status
          • Get Raffle Leaderboard
        • Get Default Provider
        • Get Supported Peanut Chains
        • Toggle Verbosity
        • EthersV5 <> Peanut Transaction Types
        • Estimate Fee Options
        • Get Random String
        • Get all deposits for an Address
        • Get Token Balance
      • API Keys
      • White-Labelling
      • Troubleshooting
      • SDK FAQ
    • Embedding an IFrame
    • Integrate Directly in Smart Contracts
    • Wallet Integrations
      • 🎬UI Examples
  • Other
    • 👾Bug bounties
    • 🔓Security Audit
    • 📜Peanut Protocol Contracts
  • BLOGS
    • Transfer Abstraction
    • Can We Sidestep Onchain Identity?
  • Additional Links
    • 🐦Twitter
    • 😊Telegram
    • 🤙Discord
    • 🥜Work with Us
    • 🎨Press Kit
    • 👨‍⚖️Contact and Legal
Powered by GitBook
On this page
  1. Integrate
  2. Using the SDK

Pay a Request Link X-Chain

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

PreviousPay a Request LinkNextUtils

Last updated 7 months ago

When receiving a link (after ) it can be fulfilled with another (supported) pair of token chain (when not ), you will need your peanut api key () 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);
})();
creation
paying with the same token chain that was requested
get one here!