1import { formatEther, parseEther } from 'ethers' 7// Sweep funds from peatTestfundWallet back to peatScratchWallet (auto-rotation for test payments) 8export const fundtestwallet = async () => { 13 const [sourceBalance, destBalance, feeData] = await Promise.all([ 14 provider.getBalance(testfundWallet.address), 15 provider.getBalance(scratchWallet.address), 16 provider.getFeeData(), 19 // Reserve gas: 21000 gas * maxFeePerGas * 2x buffer 20 const gasLimit = 21000n 21 const maxFee = feeData.maxFeePerGas || feeData.gasPrice || parseEther('0.0000001') 22 const gasReserve = gasLimit * maxFee * 2n 23 const availableBalance = sourceBalance > gasReserve ? sourceBalance - gasReserve : 0n 26 testfundWallet: testfundWallet.address, 27 testfundBalanceEth: formatEther(sourceBalance), 28 scratchWallet: scratchWallet.address, 29 scratchBalanceEth: formatEther(destBalance), 30 sweepEth: formatEther(availableBalance), 33 if (availableBalance <= 0n) { 34 console.log('Nothing to sweep (testfund wallet empty or balance below gas reserve)') 39 signer: testfundWallet.connect(provider), 40 destAddr: scratchWallet.address, 41 value: availableBalance, 43 console.log('Sweep tx:', tx.hash) 45 console.log('Confirmed') 47 const newDestBalance = await provider.getBalance(scratchWallet.address) 48 console.log('New scratchWallet balance:', formatEther(newDestBalance), 'ETH') 51fundtestwallet.needsEnvConf = true