SECRET OF CSS

How to Call Any API From a Solidity Smart Contract, Part 2 | by Vansh Wassan | Sep, 2022


Making and deploying a Requester contract to request data from the Airnode

0*crNHRAgjWh9LoIFX 300w
Photo by GuerrillaBuzz Crypto PR on Unsplash

This is the second part of the tutorial where we deploy the Requester Contract and request data from the Airnode. Check out the first part here.

In Part I, we successfully integrated and deployed an Airnode through ChainAPI. Now we will code a simple Requester Contract to call and read data from our Airnode.

Before starting, make sure you set up the Airnode Monorepo on your system. Follow through the Readme to install and build all the dependencies and packages to be able to access the Airnode CLI.

Clone the Airnode Monorepo.

$ git clone https://github.com/api3dao/airnode.git .

To install the dependencies,

$ yarn run bootstrap

To build all the packages,

$ yarn run build

An Airnode is a first-party oracle that pushes off-chain API data to your on-chain contract. It makes a request to the on-chain RRP protocol contract (AirnodeRrpV0.sol) that adds the request to the event logs. The off-chain Airnode then accesses the event logs, gets the API data and performs a callback to the requester.

https://docs.api3.org/airnode/v0.8/grp-developers/
https://docs.api3.org/airnode/v0.8/grp-developers/

A Requester is a contract that triggers an Airnode request. To do so, the requester needs to be sponsored and make the request using a matching sponsor wallet.

The Requester then calls the protocol contract, which emits a blockchain event with the request parameters. Airnode listens to the events emitted by the AirnodeRrpV0 contract. During the next run cycle, Airnode gets the request parameters from the emitted event.

The Requester Contract will have two main functions, makeRequest() and fulfill(). The makeRequest() function will call the makeFullRequest() function of the AirnodeRrpV0.sol protocol contract which adds the request to its storage. The targeted off-chain Airnode gathers the request from AirnodeRrpV0.sol’s storage and responds using the fulFill() function of AirnodeRrpV0.sol.

The makeRequest() function expects the following parameters to make a valid request.

  • airnode (address) and endpointId specify the endpoint.
  • sponsor and sponsorWallet (addresses) specify which wallet will be used to fulfill the request.
  • parameters specify the API and Reserved Parameters (see Airnode ABI specifications for how these are encoded) We will encode the parameters off-chain using @airnode-abi library.

The callback to the Requester contains two parameters:

  • requestId: First acquired when making the request and passed here as a reference to identify the request for which the response is intended.
  • data: In case of a successful response, this is the requested data which has been encoded and contains a timestamp in addition to other response data. Decode it using the function decode() from the abi object.

To deploy the Requester Contract, we are going to use Remix IDE. It’s an online IDE that allows developing, deploying and administering smart contracts for EVM Compatible Blockchains.

Make a contract and paste in the Requester.sol code.

Now hit compile on the right side of the dashboard and compile the Smart Contract.

1*A0SBboW fTyAC45ws65O2Q

Now we are all set to deploy our Requester.

As we are going to deploy the contract on Polygon Mumbai Testnet, make sure you have enough MATIC in your wallet to deploy the Requester and then fund the sponsorWallet later. You can get some from the Mumbai Faucet.

Head to Deploy and run Transactions and select Injected Provider — MetaMask option under Environment. Connect your MetaMask. Make sure you’re on Mumbai Testnet.

1*h34LOQkc5WIAt5l cWbJ A

The _rrpAddressis the main airnodeRrpAddress. The RRP Contracts have already been deployed on-chain. You can check for your specific chain here.

Fill in the _rrpAddress and click on Deploy. Confirm the transaction on your MetaMask and wait for it to deploy the Requester Contract.

1*WMB8sP2JDG6XF4XrrTFWog
Make sure you’re on the Polygon Mumbai Testnet

When your Contract gets deployed, head to Deploy & run transactions and click on the dropdown for your Requester under Deployed Contracts.

Now select the makeRequest dropdown to see all the parameters you need to pass in order to make a full request to the Airnode.

1*3uaFU21

Here, you need to pass in your airnode(Airnode address), endpointID, sponsor(The Requester itself),sponsorWalletand parameters in order to call the makeRequest() function.

We can find the airnode in the receipt.json under the output directory obtained when we deployed our Airnode.

1*

The endpointID can be found under the config.json file.

1*ha0EzXURgRHKFK74vnLvPA

We need to derive the sponsorWallet through the Airnode CLI command that will make the actual call. We also need to fund it with some MATIC to cover the gas cost.

After you’ve setup the Airnode CLI and installed and built all the dependencies and packages, run the following command to derive your sponsorWallet :

Linux:

npx @api3/airnode-admin derive-sponsor-wallet-address \
--airnode-xpub xpub6CUGRUo... \
--airnode-address 0xe1...dF05s \
--sponsor-address 0xF4...dDyu9

Windows:

npx @api3/airnode-admin derive-sponsor-wallet-address ^
--airnode-xpub xpub6CUGRUo... ^
--airnode-address 0xe1...dF05s ^
--sponsor-address 0xF4...dDyu9

Your airnode-addressand airnode-xpub (The Airnode’s extended public key) can be found in the same receipt.json. The sponsor-address will be the address of the Requester contract itself (the one that you just deployed).

1*Tl 4usi XHOZSH65lrdJew

Run the command to obtain your sponsorWallet.

1*cMWyikM8lIAxGk6YEGtd g

Fund the sponsorWallet with some test MATIC.

The parameters are required to be encoded in bytes32 before you send it. We are going to use the @airnode-abi library for encoding the parameters off-chain and then sending it to the Requester.

You can set it up by cloning this tutorial’s repository.

Run the following command to get your encoded parameters :

node .\src\encodeParams.js

Now you have all the parameters that you require to run the makeRequest function. Populate all the fields and click on Transact.

Note: The sponsor here will be the address of the Requester Contract that you just deployed.

1*buQhbSCE pq9hAibHbIOmA

Click on transact, confirm the transaction on MetaMask and wait for the transaction to complete.

Now you can head over to https://mumbai.polygonscan.com and check your sponsorWallet for any new transactions.

You might need to wait for a while as the Airnode calls the fulfill() function in AirnodeRrpV0.sol that will in turn call back the requester contract at fulfillAddress using function fulfillFunctionId to deliver data.

1*NnNht40SsIYsKEkNW45DvA

Here, we can see the latest Fulfill transaction.

Now go back on Remix and check for requestId under logs for the latest transaction.

1*Km2hDmwINDqShb1PlnXXug

You can also find your requestId under logs in the Polygon Mumbai Block Explorer.

1*Ula12KPb0Ykl1NjhhD3z A

Copy your requestId and paste it on under the fulfilledData method to decode the response. Click on call and you will see the API response. Here, we requested Tesla’s Stock price.

1*dgVH5M1HlJb AAwfTao9Hw
1*AZ3OgEd9QKtVnPql1hifXA

Now you successfully deployed an Airnode and made a Requester Contract to read data from it. You can also refer to this Repo for all the code that I’ve used for this tutorial.

Thanks for reading.

Any questions? Check out API3’s Discord Server and drop your queries there!



News Credit

%d bloggers like this: