import { ethers } from 'ethers' ; import { deriveAirnodeXpub , deriveSponsorWalletAddress , requestWithdrawal , useAirnodeRrp } from '@api3/airnode-admin' ; import { getDeployedContract , getProvider , readIntegrationInfo , runAndHandleErrors , getAirnodeWallet , cliPrint , setMaxPromiseTimeout , } from '../src' ; const waitForFulfillment = async ( withdrawalRequestId ) => { const airnodeRrp = await getDeployedContract ( '@api3/airnode-protocol/contracts/rrp/AirnodeRrp.sol' ) ; const provider = getProvider ( ) ; return new Promise ( ( resolve ) => provider . once ( airnodeRrp . filters . FulfilledWithdrawal ( null , null , withdrawalRequestId ) , resolve ) ) ; } ; const makeWithdrawalRequest = async ( ) : Promise < string > => { const integrationInfo = readIntegrationInfo ( ) ; const airnodeRrp = await getDeployedContract ( '@api3/airnode-protocol/contracts/rrp/AirnodeRrp.sol' ) ; const airnodeWallet = getAirnodeWallet ( ) ; const sponsor = ethers . Wallet . fromMnemonic ( integrationInfo . mnemonic ) ; const sponsorWalletAddress = await deriveSponsorWalletAddress ( deriveAirnodeXpub ( airnodeWallet . mnemonic . phrase ) , airnodeWallet . address , sponsor . address ) ; const withdrawalRequestId = await requestWithdrawal ( useAirnodeRrp ( airnodeRrp ) , airnodeWallet . address , sponsorWalletAddress ) ; return withdrawalRequestId ; } ; const printWalletBalances = async ( ) => { const integrationInfo = readIntegrationInfo ( ) ; const airnodeWallet = getAirnodeWallet ( ) ; const sponsor = ethers . Wallet . fromMnemonic ( integrationInfo . mnemonic ) ; const sponsorWalletAddress = await deriveSponsorWalletAddress ( deriveAirnodeXpub ( airnodeWallet . mnemonic . phrase ) , airnodeWallet . address , sponsor . address ) ; const provider = getProvider ( ) ; const sponsorBalance = ethers . utils . formatEther ( await provider . getBalance ( sponsor . address ) ) ; const sponsorWalletBalance = ethers . utils . formatEther ( await provider . getBalance ( sponsorWalletAddress ) ) ; cliPrint . info ( `Sponsor balance: ${ sponsorBalance }. Sponsor wallet balance: ${ sponsorWalletBalance }` ) ; } ; const main = async ( ) => { await printWalletBalances ( ) ; cliPrint . info ( 'Making withdrawal request...' ) ; const requestId = await setMaxPromiseTimeout ( makeWithdrawalRequest ( ) , 80 * 1000 ) ; cliPrint . info ( 'Waiting for fulfillment...' ) ; await waitForFulfillment ( requestId ) ; cliPrint . info ( 'Withdrawal request fulfilled' ) ; await printWalletBalances ( ) ; } ; runAndHandleErrors ( main ) ;