Ethereum: How would one monitor an address for a transaction and 1 confirmation in PHP?

Monitoring of an Ethereum address for the transaction confirmation in PHP

In order to monitor an Ethereum address and follow the number of confirmations for a transaction, interact with the Ethereum blockchain. In this article we will examine how this can be achieved with PHP.

Set up by Ethers.js in php

Ethereum: How would one monitor an address for a transaction and 1 confirmation in PHP?

First install the global package “Ethers.Js” with composer:

`Bash

Composer needs Ether-Js

Then create a new PHP file (e.g. Ethereum_Monitor.php) and add the following code:

`PHP

Use Ethers \ Wallets \ Wallet;

Use Ethers \ Providers \ httpprovider;

Use Ethers \ Providers \ Websocket Provider;

Class Etheremonitor {

Private $ Walletdress;

private $ providerurl;

Public function __Construct ($ wallet address, $ providerurl)

$ this-> Walletaddress = $ Walletaddress;

$ this-> providerurl = $ providerurl;

}

Public function Getchainid () {

// Ethers.js automatically sets the chains -ID for us on 1

Return 1;

}

Public function WAITFORTRANSACTIONCONFIRFIRTation ($ address, $ amount) {

While (true) {

attempt {

// Check whether we have a valid item pocket body

$ wallet = New Wallet ($ this-> Walletdadress);

// Collect the transaction object with Web3.js

$ tx = $ wallet-> transaction ($ address, $ are);

// Get the transactionhash and the time stamp

$ hash = $ tx-> hash;

$ TIMESTAMP = $ TX-> TIMESTAMP;

// Check whether the transaction is confirmed

if ($ tx-> confirmation> 0) {

return; // transaction has at least one confirmation

} differently {

// If no confirmation is found, wait and try again

Echo “Transaction with the address $ address has not confirmed:”. Json_encode ($ hash). “\ N”;

Sleep (1); // Wait 1 second before checking again

}

} Catch (exception $ e) {

Echo “error occurred while I was waiting for the transaction confirmation:”. $ e-> Getmessage (). “\ N”;

break;

}

}

return false; // transaction has not reached confirmations

}

}

// Example use:

$ Walletadress = ‘0x1234567890abcdef’;

$ providerURL = ‘

$ monitor = new Etheremonitor ($ Walletdressdress, $ providerURL);

Echo “Transaction confirmation:”. ($ monitor-> Getchainid () == 1? ‘Yes’: ‘No’). “\ N”;

`

In this example, we create an instance of the “Etheremonitor” class with our item of items and the URL of the providers. We then use loop for a while to check every second after transaction confirmations according to the “WaitfortransactionConfirmation” method.

How it works

  • The Ether.js library automatically defines the chains -id to 1.

  • We use the “Walletaddress” instance to a valid transaction object with Web3.Js. to obtain.

  • We pick up the transaction -hash and the time stamp from the transaction object.

  • If there is a confirmation of the transaction (i.e. “TX-> Confirmation> 0”), we return True. Otherwise we wait and try again after we have waited for 1 second.

Note that this implementation assumes that you will be set up via an Infura project with your URL of your Ethereum network providers. You can find more information in the official documentation: <

Ethereum Does Wallet Work


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *