Here’s the primer of the state, the indiabrising, how to send the transaction from the Metamask Koshka, Uspolza Backend Node.js with API METAMASK:
Metamask transaction, leaving with Backend PHP and node.js
In this state, we dissatisfied with the simple API on the background of your node.js, which allows you to send transaction from the Koshka Metamask to the other Koshelki.
PROPOSE:
- Install node.js and metamask on your computer.
- Install the local midst for PHP (for example, laravel or Express).
- Submit the coded record with Metamask to check the API.
Project structure:
`Bash
Metamask-API/
app.php
Configuration/
Database.php
Public/
Controllers/
Index.php
SUGHTS/
Transactionservice.php
routes/
api.php
Sales/
Autoload.php
`
App.js (Becand -PHP)
`PHP
Using the consecration \ luster \ facades \ db;
// plunge to the database
$ connection = DB :: Connect ('MySQL', [
'Host' => 'Localhost',
'User' => 'Your_username',
'Password' => 'Your_Password',
]);
// Remove the new transaction service
Class TransactionService {
PUBLIC FUNCTION CREATETRANCTION ($ DATA) {
// Find the entry data
IF (! Isset ($ Data ['To']) ||! Isset ($ Data ['Summent'])) {
Return the bed;
}
ask {
// Punish transaction
$ result = DB :: INSERT ('transaction', [
'User_id' => Auth ()-> ID (),
'Wallet_id' => Auth ()-> ID (),
'Summ' => Floatval ($ Data ['Summ']),
'tx_hash' => Hash ('Sha256', Uniqid ()) // Creation Unique tx_hash for transaction
]);
If ($ result) {
Returned Isstin;
} still {
Baby new exceptions ("transaction is not successful");
}
} Catch (Exception $ E) {
Return the bed;
}
}
}
// Create a new copy transaction service
$ Transactionservice = New Transactionservice ();
// Decrease function of the transaction from the Metamask transaction
Function Sendtransaction ($ to, $ UMM) {
$ Data = [
'to' => $ to,
'summ' => $ sums
];
IF (! $ transactionservice-> Createtransaction ($ Data)) {
Return the bed;
}
// Returned the isstin to indicate a pre -transaction
Returned Isstin;
}
Index.php (Control File)
`PHP
Using the consecration \ http \ requestion;
// Imperial Entry Published from Metamask
$ to = Request :: Input ('To');
$ Summe = Request :: Input ('Sumber');
if (! Isset ($ to)) {
Return Response ()-> JSON (['error' => 'Week for Adres']);
}
IF (! Isset ($ Summ)) {
Return Response ()-> JSON (['error' => 'Unded sum']);
}
// Display transaction with SendTransaction Function
$ Response = Sendtransaction ($ to, $ UMMOR);
Return Response ()-> JSON ($ Response);
api.php (route file)
`PHP
Using the consecration \ http \ requestion;
Using the implementation \ http \ controllers \ transactionservice;
// Remove new route for transaction output
ROUTE :: POST ('/Sendtransaction', [
'NAME' => 'Sendtransaction',
'UPOLES' => FUNCTION (requested $ REQUEST) {
Return Transactionservice :: Sendtransaction ($ Request-> Input ('To'), $ Request-> Input ('Sumber'));
}
'Methods' => ['post']
]);
Enlightenment:
- In the file
App.php
we will defect a new transaction service, use Konteiner Laravel’s IOC (ELAFENT ORM).
- The method
Createtransaction
runs the entrance and the transaction in the database.
- In the function of
Sendtransaction
we make the devices of the Metamask Koshka and the summary of the parameters. Zatem we call the “Createransaction” method of our transaction service for transaction.
- Again, in the route file
/API/Sendtransaction
We will raise a new route for transaction transaction from the Laravel route.