Here’s an example article explaining how to send transactions from the Metamask Portfolio using Backend Node.js with Metamask API:
Metamask Transaction, Shipment with Backend PHP and Node.js
In the instant article, we will look to the crese a sample API off your Node.js backends, you will be backended by your Metamask’s portfolio.
Prerequisite:
- Install Node.js and Metamask on your machine.
- Create a local PHP Development Environment (eg Laravel or Express).
- Create an account with a Metamask to test API.
Project Structure:
`bash
Metamask-Aapi/
app.php
configuration/
data base.php
Publicly/
controls/controllers/
index.php
Services/
Transactionsservice.php
Routes/
api.php
celler/celery/
Autolad.php
App.js (Backend PHP File)
`pp
Usage Illudate Support Facades DB;
// contact the data base
$ Connection = DB :: Connect ('MySQL',
'Host' => 'Localhost',
'User' => 'your_username',
'Password' => 'your_password',
]);
// Define a new transaction service
Class Transaction Service {
Public Function Creattransction ($ Data) {
// Validate input data
if (! ISSET ($ data ['to'])) ||! isst ($ data ['sum'])) {
the return false;
}
try {
// Complete them
$ reply = db :: insertion ('transctions',
'user_id' => author ()-> id ( ),
'wall_id' => author ()-> id ( ),
'sum' => floatval ($ Data ['sum']),
'tx_hash' => hash ('Sha256', Uniqid ())
]);
If ($emmy) {
the account of thrue;
} Else
throw a new expection ("Transction Failed");
}
} catch (exception $ e) {
the return false;
}
}
}
// Create a new copy of translationsservice
$ transaction service = new transactions serve ();
// Define a transaction feature free
SENDTRANSAction function ($ to, $m amunt) {
$ DATA =
'to' => $ to,
'Sum' => $
];
If (! $ Transctionsservice-> Creat Transaction ($ Data)) {
the return false;
}
// Return True to Indicate a Succcesful transaction
the account of thrue;
}
Index.php (control fillet)
`pp
Usage Illudate http request;
if
$ to = request :: input ('to');
$ amont = request :: Login ('sum');
If (! ISSET ($ to)) {
Return Response ()-> JSON (['Error' => 'The Invalid For Adddress']);
}
If (! ISSET ($ sum)) {
Return Response ()-> JSON (['error' => 'Invalid amunt']));
}
// Send are transaction using the SendTrunsction feature
$ response = Sendransction ($ to, $m amunt);
Return Response ()-> JSON ($ Reply);
api.php (route file)
`pp
Usage Illudate http request;
Using the Plication Http Controllers Transactionsservice;
// Define a new rote to send transactions
Route :: Post ('/Senddrunsction',
'Name' => 'SENDTRANSuction',
'use' => function (request $ charge) {
Return a Transportation Service :: Sendransction ($ Request-> Input ('To'), $ Request-> Input ('Sum'));
},
'Methods' => ['Post']
]);
Explanation:
- In the file
App.php
welfare a new transaction service survice the Laravel IOC container
- The
Createtransaction's Method Validates input data and executes are the data base.
- In the "Senddransction functionality, we retrieved the address and quantity off the Metamas Portfolio to Request.
- Finally, instigating the fille off the rote
/api/sensation
, weigh a new rote-to-send transactions weighed in the Laravel rote.