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:
The request link you intend to pay
A way of doing transactions (in this simple example we use a private key wallet)
Simple example:
import peanut from'@squirrel-labs/peanut-sdk';import { BigNumber, ethers } from'ethers';constuserPrivateKey='SENDER-PRIVATE-KEY';constAPIKey='YOUR-API-KEY-HERE';constlink='PREVIOUSLY-CREATED-LINK'//const apiUrl = 'OPTIONAL-DIFFERENT-API-URL';// Initialize provider and walletfunctiongetWallet(chainId:number, privateKey:string):ethers.Wallet {returnnewethers.Wallet(privateKey,ethers.getDefaultProvider(chainId)); // Connect wallet to the provider}// Function to fulfill a request linkasyncfunctionfulfillRequestLink(link:string) {try {// Get the details of the request linkconstlinkDetails=awaitpeanut.getRequestLinkDetails({ link, APIKey,//apiUrl, //optional different apiUrl });console.log('Got the link details!', linkDetails);// Prepare the unsigned transaction for fulfilling the request linkconst { 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 transactionconstsigner=getWallet(Number(linkDetails.chainId), userPrivateKey)const { tx,txHash } =awaitpeanut.signAndSubmitTx({ unsignedTx, structSigner: { signer, gasLimit:BigNumber.from(2_000_000), }, });console.log('Submitted transaction with tx hash', txHash);awaittx.wait();console.log('Request link fulfillment completed!');awaitpeanut.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 linkawaitfulfillRequestLink(link);})();