Metamask Integration With Laravel Part 2[ Validate Transactions With EtherScan ]
Tags: laravel, php, metamask, etherscan
Published: 2021-02-16 20:07:15

![Screenshot 2021-02-17 at 01.24.06](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yqf56mjeji3yh53x6b2.png)
 

#Welcome to second article of Metamask integration with Laravel.

This document is related to my 1st document. which I explained about Metamask integration with Laravel.

you may read it here -> {% link https://dev.to/lathindu1/metamask-integration-with-laravel-4mng %}


Okay, Let's start second part of the article now.

#How to validate transactions which are made with Metamask

here I'm using images instead of pasting codes here, it's easy to write document.
and what a wonderful, you can copy or refer full code in repository which I'm mentioned end of the document. 😊

For the validate transactions very easy and free method is a [EtherScan](https://etherscan.io) API.

#####First of all, Let's create Etherscan account.

![Screenshot 2021-02-17 at 00.01.47](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yofgbj5gjsioy1m12yf2.png)

#####Now we need to create API key

######step 01

![Screenshot 2021-02-17 at 00.04.37](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7vb9fuvikucfqqdo2z1.png)

######step 02

![Screenshot 2021-02-17 at 00.06.18](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qcd6q9elf08slgzvt2e.png)

######step 03

![Screenshot 2021-02-17 at 00.07.27](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdqxy909ymfdqa597mcx.png)
   

Now you may copy the key and store in somewhere for use in our application.


#####Next we need to create command in Laravel for add to shedular.

![Screenshot 2021-02-16 at 23.21.27](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92x8cdrm9f529o38daw5.png)
 
 
I have created a Command called **MetamaskValidationCommand**

You may open the command file in your editor [ **My favourite is VSCode 😊** ]

App->Console->Commands->MetamaskValidationCommand.php

![Screenshot 2021-02-16 at 23.22.33](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fpb0gdmpu02s3q1d35f.png)


#How To Validate?

For validate a transaction we can use [EtherScan](https://etherscan.io) 's Free API.

![Screenshot 2021-02-17 at 00.22.38](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c93yltlca68jpzvl4uqa.png)
 

You can see number of methods in [EtherScan](https://etherscan.io) API.


![Screenshot 2021-02-17 at 00.24.09](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6li68kc9mp55v397dr7n.png)


Here we use **eth_getTransactionByHash** method only. 

![Screenshot 2021-02-17 at 00.27.53](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0613xunyrr8ihnf90o5i.png)
 

we have to send 2 parameter with this simple get request.

* API Key => We already made a one 🎉
* Transaction Id => It's we already stored in database as **txHash** 🥳


above we created a command called **MetamaskValidationCommand**

##it's time to code.🧑🏻‍💻

### code step 01
Create a function to get transaction information from [EtherScan](https://etherscan.io). inside **MetamaskValidationCommand.php**

![Screenshot 2021-02-17 at 00.38.27](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b85w3bt3obkha9wqju1g.png)
 
### code step 02

Now we need to get pending transactions from database.
here we need to add some delay to get transactions. because some time Etherium transactions may take more time to be a successful transaction. so here we get transactions which are older than 20 minuets and status = pending from database.

To call **Transaction Model** we need call modal inside our constructor.

![Screenshot 2021-02-17 at 00.42.36](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5iwbjzez2augi69x0jtb.png)
 
Now we need to create get function inside **Transaction Model**.

![Screenshot 2021-02-17 at 00.47.43](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bl0uyjt1wotwnbx8v3j.png)
 

Then we can create a function for validate transactions inside **MetamaskValidationCommand.php**

![Screenshot 2021-02-17 at 00.51.45](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xe2fc7kgvtp69ejfk99g.png)

Here we first get all the pending [older than 20min] transactions and loop them inside foreach.

then we get them one by one and send transaction id (txHash) to  [EtherScan](https://etherscan.io) by using function which we mede above ( **code step 01** ).

so our functions are ready to use.

Now we need to modify our command with custom command params.

![Screenshot 2021-02-17 at 00.58.02](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l57l1fwltpzte8uj638q.png)

finally we need to add our command to kernel.php

app => Console => kernel.php

here I'm adding schedule to run command every Minuit. You are free to change for any time range --> [References](https://laravel.com/docs/8.x/scheduling#schedule-frequency-options).

![Screenshot 2021-02-17 at 01.02.23](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezmgcp4ab4o22c1xprwd.png)


Now we are ready to test.

 01 make a transaction like I described in [Article 01](https://dev.to/lathindu1/metamask-integration-with-laravel-4mng).

![Screenshot 2021-02-17 at 01.16.27](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47tky3oerb9kf9vnlwa3.png)
 


02 Wait At least 20 Minuit (You may change the delay value inside TransactionModel.php).


03 You can see it's marked as success. if its success payment.

 ![Screenshot 2021-02-17 at 01.19.17](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzgfxznrbz9fwmjtzdth.png)


I think your transaction also succeeded without an error. any error please comment below I will help you guys to success.


#Important
In these both tutorials I have described as very basic methods. we don't write codes like that in production sites. we need to think more about security and durability. I guest you may got help from my article.

##You may Find my Fiver Gig Here.

[https://www.fiverr.com/s2/0c68721323](https://www.fiverr.com/s2/0c68721323)

here You may read article one

{% link https://dev.to/lathindu1/metamask-integration-with-laravel-4mng %}

Here I'm Adding Public GitHub Repository which will store all of my tutorials. you may clone it and see every tutorials what I will publish 🤗.

**GitHub Repository** {% github lathindu1/tutorials %}

Thank You Very much.
- Lathindu Pramuditha.
- GitHub Profile ->  https://github.com/lathindu1