Sunday, October 2, 2022
HomeProgrammingFind out how to Name Any API From a Solidity Sensible Contract,...

Find out how to Name Any API From a Solidity Sensible Contract, Half 2 | by Vansh Wassan | Sep, 2022


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

Picture by GuerrillaBuzz Crypto PR on Unsplash

That is the second a part of the tutorial the place we deploy the Requester Contract and request information from the Airnode. Take a look at the primary half right here.

In Half I, we efficiently built-in and deployed an Airnode by ChainAPI. Now we are going to code a easy Requester Contract to name and skim information from our Airnode.

Earlier than beginning, ensure you arrange the Airnode Monorepo in your system. Comply with by the Readme to put in and construct all of the dependencies and packages to have the ability to entry the Airnode CLI.

Clone the Airnode Monorepo.

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

To put in the dependencies,

$ yarn run bootstrap

To construct all of the packages,

$ yarn run construct

An Airnode is a first-party oracle that pushes off-chain API information to your on-chain contract. It makes a request to the on-chain RRP protocol contract (AirnodeRrpV0.sol) that provides the request to the occasion logs. The off-chain Airnode then accesses the occasion logs, will get the API information 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 take action, the requester must be sponsored and make the request utilizing an identical sponsor pockets.

The Requester then calls the protocol contract, which emits a blockchain occasion with the request parameters. Airnode listens to the occasions emitted by the AirnodeRrpV0 contract. In the course of the subsequent run cycle, Airnode will get the request parameters from the emitted occasion.

The Requester Contract may have two foremost capabilities, makeRequest() and fulfill(). The makeRequest() perform will name the makeFullRequest() perform of the AirnodeRrpV0.sol protocol contract which provides the request to its storage. The focused off-chain Airnode gathers the request from AirnodeRrpV0.sol’s storage and responds utilizing the fulFill() perform of AirnodeRrpV0.sol.

The makeRequest() perform expects the next parameters to make a legitimate request.

  • airnode (handle) and endpointId specify the endpoint.
  • sponsor and sponsorWallet (addresses) specify which pockets will likely be used to satisfy the request.
  • parameters specify the API and Reserved Parameters (see Airnode ABI specs for a way these are encoded) We are going to encode the parameters off-chain utilizing @airnode-abi library.

The callback to the Requester comprises two parameters:

  • requestId: First acquired when making the request and handed right here as a reference to establish the request for which the response is meant.
  • information: In case of a profitable response, that is the requested information which has been encoded and comprises a timestamp along with different response information. Decode it utilizing the perform decode() from the abi object.

To deploy the Requester Contract, we’re going to use Remix IDE. It’s a web-based IDE that enables growing, deploying and administering good contracts for EVM Appropriate Blockchains.

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

Now hit compile on the suitable aspect of the dashboard and compile the Sensible Contract.

Now we’re all set to deploy our Requester.

As we’re going to deploy the contract on Polygon Mumbai Testnet, ensure you have sufficient MATIC in your pockets to deploy the Requester after which fund the sponsorWallet later. You will get some from the Mumbai Faucet.

Head to Deploy and run Transactions and choose Injected Supplier — MetaMask possibility beneath Surroundings. Join your MetaMask. Be sure to’re on Mumbai Testnet.

The _rrpAddressis the primary airnodeRrpAddress. The RRP Contracts have already been deployed on-chain. You may test on your particular chain right here.

Fill within the _rrpAddress and click on on Deploy. Affirm the transaction in your MetaMask and look forward to it to deploy the Requester Contract.

Be sure to’re on the Polygon Mumbai Testnet

When your Contract will get deployed, head to Deploy & run transactions and click on on the dropdown on your Requester beneath Deployed Contracts.

Now choose the makeRequest dropdown to see all of the parameters you’ll want to cross so as to make a full request to the Airnode.

Right here, you’ll want to cross in your airnode(Airnode handle), endpointID, sponsor(The Requester itself),sponsorWalletand parameters so as to name the makeRequest() perform.

We are able to discover the airnode within the receipt.json beneath the output listing obtained after we deployed our Airnode.

The endpointID could be discovered beneath the config.json file.

We have to derive the sponsorWallet by the Airnode CLI command that can make the precise name. We additionally must fund it with some MATIC to cowl the gasoline price.

After you’ve setup the Airnode CLI and put in and constructed all of the dependencies and packages, run the next 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

Home 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 prolonged public key) could be present in the identical receipt.json. The sponsor-address would be the handle of the Requester contract itself (the one that you simply simply deployed).

Run the command to acquire your sponsorWallet.

Fund the sponsorWallet with some take a look at MATIC.

The parameters are required to be encoded in bytes32 earlier than you ship it. We’re going to use the @airnode-abi library for encoding the parameters off-chain after which sending it to the Requester.

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

Run the next command to get your encoded parameters :

node .srcencodeParams.js

Now you’ve got all of the parameters that you simply require to run the makeRequest perform. Populate all of the fields and click on on Transact.

Word: The sponsor right here would be the handle of the Requester Contract that you simply simply deployed.

Click on on transact, affirm the transaction on MetaMask and look forward to the transaction to finish.

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

You may want to attend for some time because the Airnode calls the fulfill() perform in AirnodeRrpV0.sol that can in flip name again the requester contract at fulfillAddress utilizing perform fulfillFunctionId to ship information.

Right here, we will see the newest Fulfill transaction.

Now return on Remix and test for requestId beneath logs for the newest transaction.

You can even discover your requestId beneath logs within the Polygon Mumbai Block Explorer.

Copy your requestId and paste it on beneath the fulfilledData technique to decode the response. Click on on name and you will notice the API response. Right here, we requested Tesla’s Inventory worth.

Now you efficiently deployed an Airnode and made a Requester Contract to learn information from it. You can even consult with this Repo for all of the code that I’ve used for this tutorial.

Thanks for studying.

Any questions? Take a look at API3’s Discord Server and drop your queries there!
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments