import { ethers } from 'ethers' ; import { deriveAirnodeXpub , deriveSponsorWalletAddress } from '@api3/airnode-admin' ; import { getDeployedContract , getProvider , readIntegrationInfo , runAndHandleErrors , getAirnodeWallet , readConfig , cliPrint , setMaxPromiseTimeout , } from '../src' ; const waitForFulfillment = async ( requestId ) => { const airnodeRrp = await getDeployedContract ( '@api3/airnode-protocol/contracts/rrp/AirnodeRrp.sol' ) ; const provider = getProvider ( ) ; const fulfilled = new Promise ( ( resolve ) => provider . once ( airnodeRrp . filters . FulfilledRequest ( null , requestId ) , resolve ) ) ; const failed = new Promise ( ( resolve ) => provider . once ( airnodeRrp . filters . FailedRequest ( null , requestId ) , resolve ) ) . then ( ( rawRequestFailedLog ) => { const log = airnodeRrp . interface . parseLog ( rawRequestFailedLog as ethers . Event ) ; throw new Error ( `Request failed. Reason:\n${ log . args . errorMessage }` ) ; } ) ; await Promise . race ( [ fulfilled , failed ] ) ; } ; const makeRequest = async ( ) : Promise < string > => { const integrationInfo = readIntegrationInfo ( ) ; const requester = await getDeployedContract ( `contracts/${ integrationInfo . integration }/Requester.sol` ) ; const airnodeRrp = await getDeployedContract ( '@api3/airnode-protocol/contracts/rrp/AirnodeRrp.sol' ) ; const airnodeWallet = getAirnodeWallet ( ) ; const sponsor = ethers . Wallet . fromMnemonic ( integrationInfo . mnemonic ) ; const endpointId = readConfig ( ) . triggers . rrp [ 0 ] . endpointId ; const sponsorWalletAddress = await deriveSponsorWalletAddress ( deriveAirnodeXpub ( airnodeWallet . mnemonic . phrase ) , airnodeWallet . address , sponsor . address ) ; const { getEncodedParameters } = await import ( `../integrations/${ integrationInfo . integration }/request-utils.ts` ) ; const receipt = await requester . makeRequest ( airnodeWallet . address , endpointId , sponsor . address , sponsorWalletAddress , await getEncodedParameters ( ) ) ; return new Promise ( ( resolve ) => getProvider ( ) . once ( receipt . hash , ( tx ) => { const parsedLog = airnodeRrp . interface . parseLog ( tx . logs [ 0 ] ) ; resolve ( parsedLog . args . requestId ) ; } ) ) ; } ; const main = async ( ) => { cliPrint . info ( 'Making request...' ) ; const requestId = await makeRequest ( ) ; cliPrint . info ( 'Waiting for fulfillment...' ) ; await setMaxPromiseTimeout ( waitForFulfillment ( requestId ) , 80 * 1000 ) ; cliPrint . info ( 'Request fulfilled' ) ; const integrationInfo = readIntegrationInfo ( ) ; const { printResponse } = await import ( `../integrations/${ integrationInfo . integration }/request-utils.ts` ) ; await printResponse ( requestId ) ; } ; runAndHandleErrors ( main ) ;