Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A tutorial on using Infura Filecoin API

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the "tutorial on the use of Infura Filecoin API". In the daily operation, I believe that many people have doubts about the use of Infura Filecoin API. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "tutorial on the use of Infura Filecoin API"! Next, please follow the editor to study!

Infura is famous for providing free ethernet web3 API. With the popularity of Filecoin, Infura has timely launched Filecoin's free API, which makes it convenient for developers to develop Filecoin applications without building Filecoin nodes. In this tutorial, we will learn how to use Filecoin API provided by Infura to create a simple Node.js program to interface with Filecoin blockchain networks, such as verifying Filecoin addresses and querying Filecoin address balances.

If your main development language is PHP or Golang, you can use the following development package: Filecoin.php | Filecoin.go

1 、 Infura API

Before you can visit Infura API, you first need to register on the Infura website. This is a free API, just sign up for email:

Go to infura.io and log in.

Select Filecoin in the sidebar and click create New Project.

Enter a project name, for example: Filecoin-Get Started, and click create.

Now, you should be able to see Project ID and Project Secret. Write down these two fields and we'll use them later.

The result looks like this:

Next, let's create the project.

2. Create a Filecoin docking project

For simplicity, let's create a new directory for the project and a file containing the boilerplate code:

First create a new project directory and enter it:

~ $mkdir ~ / Code/filecoin-wallet-checker-paired $cd ~ / Code/filecoin-wallet-checker

Next, create the file index.js in the filecoin-wallet-checker directory and add the following initial code:

Let request = require ('request') / / Call the Infura API and check that the address is valid.request (options, (error, response, body) = > {if (error) {console.error (' Error:', error)} else {console.log ('Response:', body)}})

Finally, don't forget to add the request package to the project, because we used it in the above code:

~ / filecoin-wallet-checker$ npm install request >... > + request@2.88.2 > added 47 packages from 58 contributors and audited 47 packages in 1.594s >.

Now that we have set up a project, we can start writing JS programs that interface with Filecoin block chains!

3. Filecoin docking JS code writing

We have established the project directory and prepared the index.js file. Now create a basic Filecoin API call to enrich the program.

In retrospect, this is our boilerplate code as follows:

Let request = require ('request') / / Call the Infura API and check that the address is valid.request (options, (error, response, body) = > {if (error) {console.error (' Error:', error)} else {console.log ('Response:', body)}})

Create an object named options below the comment, which we will use to declare the details of the request request:

/ / Call the Infura API and check that the address is valid.let options = {}

Set the url, method, and headers parameters in the options object:

/ / Call the Infura API and check that the address is valid.let options = {url: 'https://filecoin.infura.io', method:' post', headers: {'content-type':' application/json'}}

It is easy to understand the meaning of the above parameters, so I will not repeat them. The two parameters we are more interested in are body and auth.

Now add the auth parameter object:

/ / Call the Infura API and check that the address is valid.let options = {url: 'https://filecoin.infura.io', method:' post', headers: {'content-type':' application/json'}, auth: {}}

Infura API will use the contents of the auth parameter to validate the request.

Add your Infura project ID and project key to the user and pass fields:

/ / Call the Infura API and check that the address is valid.let options = {url: 'https://filecoin.infura.io', method:' post', headers: {'content-type':' application/json'}, auth: {user: '1nO7B.. pass:' bda4a...'}

Enter the Infura project ID in the user field, and then enter the Infura project password in the pass field. You can go to infura.io/dashboard/filecoin, select a specific project and go to the Settings tab to find this information.

The last parameter we need to add to the options object is body. This object is used to declare the ID to be used by the API endpoint, the version of JSON RPC, the method to be called, and so on. Set id to 0 and jsonrpc to 2.0:

/ / Call the Infura API and check that the address is valid.let options = {url: 'https://filecoin.infura.io', method:' post', headers: {'content-type':' application/json'}, auth: {user: '1nO7B.. pass:' bda4a...'}, body: `{"jsonrpc": "2.0", "id": 0}`}

Finally, set the method to be called as Filecoin.ChainHead:

/ / Call the Infura API and check that the address is valid.let options = {url: 'https://filecoin.infura.io', method:' post', headers: {'content-type':' application/json'}, auth: {user: '1nO7B.. auth, pass:' bda4a...'}, body: `{"jsonrpc": "2.0", "id": 0 "method": "Filecoin.ChainHead"} `}

The Filecoin.ChainHead method returns the current chain header data of the Filecoin block chain. Although this is not the final method we are going to use, it is a good way to test whether our js program is working properly.

4. Test the running of the Filecoin docking program

Our js program has achieved the basic function of docking Filecoin block chain, now run a test to make sure it works properly!

In the project directory, use node to execute the docking program:

Node index.js > Post successful: response: {"jsonrpc": "2.0", "result": {"Cids": [{"/": "bafy2bzaceamdit67mnlyozufeaptmhmti6dv.

Excellent! Infura API received our request and sent us the latest link information. But we are not interested in the link head data, we want to get information about the address!

5. Verify whether the Filecoin address is valid

Now let's use Infura's Filecoin API to verify that the specified string is a valid Filecoin address.

Within the body parameter of the options object, change method to Filecoin.WalletValidateAddress:

/ / Call the Infura API and check that the address is valid.let options = {url: 'https://filecoin.infura.io', method:' post', headers: {'content-type':' application/json'}, auth: {user: '1nO7B.. auth, pass:' bda4a...'}, body: `{"jsonrpc": "2.0", "id": 0 "method": "Filecoin.WalletValidateAddress"} `}

If we run the program now, Infura API returns an error because the WalletValidateAddress method requires at least one string as a parameter.

Add an array named params to the body object:

/ / Call the Infura API and check that the address is valid.let options = {url: 'https://filecoin.infura.io', method:' post', headers: {'content-type':' application/json'}, auth: {user: '1nO7B.. auth, pass:' bda4a...'}, body: `{"jsonrpc": "2.0", "id": 0 "method": "Filecoin.WalletValidateAddress", "params": [""],} `}

In the params array, add the address to check:

/ / Call the Infura API and check that the address is valid.let options = {url: 'https://filecoin.infura.io', method:' post', headers: {'content-type':' application/json'}, auth: {user: '1nO7B.. auth, pass:' bda4a...'}, body: `{"jsonrpc": "2.0", "id": 0 "method": "Filecoin.WalletValidateAddress", "params": ["f1ydrwynitbbfs5ckb7c3qna5cu25la2agmapkchi"],} `}

This example uses f1ydrwynitbbfs5ckb7c3qna5cu25la2agmapkchi as the string to validate.

Let's rerun the js program to see the response:

Node index.js > Response: {"jsonrpc": "2.0", "result": "f1ydrwynitbbfs5ckb7c3qna5cu25la2agmapkchi", "id": 0}

Wow! The presence of an address in result means that our address is valid. If we send an invalid address, we will get the following information:

Response: {"jsonrpc": "2.0"," id ": 0," error ": {" code ": 1," message ":" invalid address payload "} 6. Check the balance of Filecoin address.

In the previous example, our JS program can check whether the given string is a valid Filecoin address. Now let's implement the function of checking the balance of Filecoin addresses.

The only change we need to make is to change the request method to WalletBalance:

/ / Call the Infura API and check that the address is valid.let options = {url: 'https://filecoin.infura.io', method:' post', headers: {'content-type':' application/json'}, auth: {user: '1nO7B.. auth, pass:' bda4a...'}, body: `{"jsonrpc": "2.0", "id": 0 "method": "Filecoin.WalletBalance", "params": ["f1ydrwynitbbfs5ckb7c3qna5cu25la2agmapkchi"]} `}

Infura API will let us know the balance:

Node index.js > ADDRESS: {"jsonrpc": "2.0", "result": "7182015146934547358774", "id": 0}

The balance unit in the result returned by Infura API is attoFIL. If the requested address has no balance, the response from Infura will be blank.

7. Complete Filecoin docking JS program code

So far, our js program code for docking Filecoin is as follows, you can continue to improve on this basis:

Let request = require ('request') / / Call the Infura API and check that the address is valid.let options = {url:' https://filecoin.infura.io', method: 'post', headers: {' content-type': 'application/json'}, auth: {user:' 1nO7B.., pass: 'bda4a...'}, body: `{"jsonrpc": "2.0", "id": 0 "method": "Filecoin.WalletBalance", "params": ["f3tfhgkbq6h65fqhumadd7wvogx3bbhgm3ifg6mk6hq35ob3fex2uei7hfbo2nwqkzudjzjidmshxpvo3ja4iq"]} `} request (options, (error, response, body) = > {if (error) {console.error ('Error:', error)} else {console.log ('Response:', body)}}) The study on the "tutorial on the use of Infura Filecoin API" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report