Monitoring An Ethereum Address for Transaction Confirmation in PHP
Tower An Ethereum Address and Friends of Confirmations Fore, You’ll Need to Usse The [Ethers.js] (https: /ethers.io/docs/docs/en/v5) The Ethereum blockchain. In this article, We’ll explore how to achieve this using php.
Setting up ethers.js in PHP
First, Install the Ethers.js' Packing Globly Esting Composer:
Bash
Comperer Require Ethers-JS
RE
Theen, create a new PHP File (E.G.,Ethereum_Monitor.php) and add the following code:
PHP
use ethers \ walets \ wallet;
use ethers \ Provids \ http provider;
use Ethers \ Provids \ Websocet provider;
Class Ethereum monitor {
Private $ Walletaddress;
private $ providerurl;
Public Function __Construct ($ Walletaddress, $ providerurl) {
$ thhis-> walladdress = $ walletaddress;
$ thhis-> providurl = $ providerurl;
}
Public Function Getchainid () {
// ethers.js automatically sets the chain id to 1 for us
Values 1;
}
Public Functioning WaitFortransaction Confirmation ($ Address, $ Amount) {
While (True) {
Try {
// Check if we have a vad wall instance
$ wallet = new wallet ($ this-> walletaddress);
// get the transaction object web3.js
$ tx = $ wallet-> transction ($ addest, $ amunt);
// Get the Transuction hash and time Stamp
$ hash = $ tx-> hash;
$ time stamp = $ tx-> timestamp;
// Check if the transaction
if ($ tx-> confirmation> 0) {
The Account of Thrue; // Transaction is a contact
} Else
// if no confirmation found, wait and try
Echo "Transaction with Address $ Address has not reached confirmation:" JSon_ENCODE ($ hash). "\ n";
drag (1); // Wait for
}
} Catch (Exception $ e) {
Echo "Error occurred while waiting for transuction confirmation:" $ ee-> getmessage (). "\ n";
break;
}
}
The Return False; // Transaction with not reached any confirmations
}
}
// Example Usage:
$ Walletaddress = '0x1234569800BCDF';
$ providerurl = '
$ monitor = New Ethereum monitor ($ walletaddress, $ providerurl);
Echo "Transaction confirmation: ($ monitor-> Getchainid () == 1?" YES ":" No ")." \ n ";
RE
Inthis Example, we create an instance off theEthereum monitoring class with a watt dotress and proviard url. We’ll have you loop to check the transactions ever-soing the `waitfortransactionaction confation.
How it works
- The Ethers.js Library Automatic Sets The Chain Id to 1.
- We use the ‘walletaddress’ instance to get a vaid transaction object sing web3.js.
- We retrieved the transaction hash and timetables the transaction object.
1 otherwise, we are wait and try with the apher waiting for 1 second.
This this this implementation assumes that you have leg projected by the infurra project Set to your Ethereum Network Provid URL. You can find More Information About Ethers.js and Web3.js.