DEV Community

Cover image for Metamask Integration With Laravel Part 2[ Validate Transactions With EtherScan ]
Lathindu Pramduitha
Lathindu Pramduitha

Posted on • Updated on

Metamask Integration With Laravel Part 2[ Validate Transactions With EtherScan ]

Screenshot 2021-02-17 at 01.24.06

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 ->

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 API.

First of all, Let's create Etherscan account.

Screenshot 2021-02-17 at 00.01.47

Now we need to create API key
step 01

Screenshot 2021-02-17 at 00.04.37

step 02

Screenshot 2021-02-17 at 00.06.18

step 03

Screenshot 2021-02-17 at 00.07.27

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

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

How To Validate?

For validate a transaction we can use EtherScan 's Free API.

Screenshot 2021-02-17 at 00.22.38

You can see number of methods in EtherScan API.

Screenshot 2021-02-17 at 00.24.09

Here we use eth_getTransactionByHash method only.

Screenshot 2021-02-17 at 00.27.53

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. inside MetamaskValidationCommand.php

Screenshot 2021-02-17 at 00.38.27

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

Now we need to create get function inside Transaction Model.

Screenshot 2021-02-17 at 00.47.43

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

Screenshot 2021-02-17 at 00.51.45

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 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

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.

Screenshot 2021-02-17 at 01.02.23

Now we are ready to test.

01 make a transaction like I described in Article 01.

Screenshot 2021-02-17 at 01.16.27

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

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

here You may read article one

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


Thank You Very much.

Top comments (1)

Collapse
 
vnxz profile image
vnxz

thanks you . Can you also show how accept payment by metamask in laravel