Estaba siguiendo esta guía rápida para construir un Dapp .
Para construir la interfaz de usuario e interactuar con el contrato inteligente, utiliza React. Existen las siguientes funciones:
async function submitBid(event) { event.preventDefault(); if (typeof window.ethereum !== 'undefined') { const contract = await initializeProvider(); try { // User inputs amount in terms of Ether, convert to Wei before sending to the contract. const wei = parseEther(amount); await contract.makeBid({ value: wei }); // Wait for the smart contract to emit the LogBid event then update component state contract.on('LogBid', (_, __) => { fetchMyBid(); fetchHighestBid(); }); } catch (e) { console.log('error making bid: ', e); } } } async function withdraw() { if (typeof window.ethereum !== 'undefined') { const contract = await initializeProvider(); // Wait for the smart contract to emit the LogWithdrawal event and update component state contract.on('LogWithdrawal', (_) => { fetchMyBid(); fetchHighestBid(); }); try { await contract.withdraw(); } catch (e) { console.log('error withdrawing fund: ', e); } } } De acuerdo con la biblioteca ethers.js , para escuchar un evento de contrato inteligente requiere provider.on(eventName, listener) , pero me pregunto por qué en este caso para el argumento del oyente usa este (_) o (_, __) , ¿qué significa esto significa tener cada uno de ellos y por qué ambos son diferentes si la función de devolución de llamada es más o menos la misma para ambas funciones, estos son eventos de contrato inteligente a considerar:
event LogBid(address indexed _highestBidder, uint256 _highestBid); event LogWithdrawal(address indexed _withdrawer, uint256 amount);La convención general para nombrar una variable _ dentro de una función lamda es indicar que no va a usar el parámetro que se pasará a lamda pero el consumidor de lamda aún espera pasar variables.
Dicho esto... javascript no requiere que hagas esto y en realidad funcionaría bien si ambos oyentes no esperaran argumentos.
Cabe señalar que el _ en
contract.on('LogBid', (_, __) => { // _ is the unused param 'highestBidder' // __ is the unused param highest bid fetchMyBid(); fetchHighestBid(); } y el _ en
contract.on('LogWithdrawal', (_) => { // _ is the address of who withdrawed // If lamda supplied another var it would hold the amount withdrawn fetchMyBid(); fetchHighestBid(); });no son del mismo valor.