• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

239
Views
Cómo llegar a la función Wei usando window.ethereum

Simplemente estoy tratando de acceder a una función de toWei en un sitio web front-end en JavaScript.

Desde que eliminamos nuestro window.web3, MetaMask ya no vuelve a cargar automáticamente la página en los cambios de cadena/red.

(Fuente: https://docs.metamask.io/guide/provider-migration.html#summary-of-breaking-changes )

Así que estoy usando window.ethereum que ha estado bien hasta ahora. Pero no puedo encontrar, incluso con una considerable búsqueda en Google, un ejemplo directo del uso de window.ethereum para acceder a una .toWei(..) .

Por ejemplo, me gustaría algo como:

 var weiVal = window.ethereum.toWei(String(eth_float), 'ether');

Pero esta llamada de función en particular no existe. ¿Cómo puedo hacer esto?

almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

La función toWei() era parte del módulo web3 eliminado y ya no forma parte de la extensión del navegador MetaMask.

Actualmente 0 resultados en código: https://github.com/MetaMask/metamask-extension/search?q=toWei


Sin embargo, puede importar el paquete web3 JS a su interfaz y usar la función desde aquí.

 <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script> <script> const web3 = new Web3(); const weiAmount = web3.utils.toWei("1", "ether"); console.log(weiAmount); </script>

Fuente del paquete minificado: el repositorio oficial de web3.js - https://github.com/ChainSafe/web3.js

almost 3 years ago · Juan Pablo Isaza Report

0

Convertir de ethereum a wei es solo una multiplicación por 10^-18 (y viceversa por 10^18). Aquí hay una demostración:

 function e2w() { val = document.getElementById("numberInput").value; console.log("Wei: " + val * 10 ** 18); } function w2e() { val = document.getElementById("numberInput").value; console.log("Ethereum: " + val * 10 ** -18); }
 <form> <label for="numberInput">Enter value</label><br> <input type="number" step="any" id="numberInput"><br> <button onclick="e2w()">Eth --> Wei</button> <button onclick="w2e()">Wei --> Eth</button> </form>

Una solución más general para la conversión de unidades entre todas las unidades de Ethereum aquí:

 BigNumber.config({ DECIMAL_PLACES: 50 }); const units = { "wei": 1, "kwei": 10 ** 3, "mwei": 10 ** 6, "gwei": 10 ** 9, "microether": 10 ** 12, "milliether": 10 ** 15, "ether": 10 ** 18, } function unitConverter() { fromUnit = document.getElementById("from").value; toUnit = document.getElementById("to").value; inputNumber = document.getElementById("inputNumber").value; result = new BigNumber(inputNumber).times(units[fromUnit]).div(units[toUnit]).toString(10); document.getElementById("outputNumber").innerHTML = result; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/9.0.1/bignumber.min.js"></script> <label for="from">from:</label> <select id="from" name="fromUnits" onchange="unitConverter()"> <option value="wei">Wei</option> <option value="kwei">Kwei (babbage)</option> <option value="mwei">Mwei (lovelace)</option> <option value="gwei">Gwei (shannon)</option> <option value="microether">microether (szabo)</option> <option value="milliether">milliether (finney)</option> <option value="ether">Ether</option> </select> <p> <label for="to">to:</label> <select id="to" name="toUnits" onchange="unitConverter()"> <option value="wei">Wei</option> <option value="kwei">Kwei (babbage)</option> <option value="mwei">Mwei (lovelace)</option> <option value="gwei">Gwei (shannon)</option> <option value="microether">microether (szabo)</option> <option value="milliether">milliether (finney)</option> <option value="ether">Ether</option> </select> <p> <label>Input</label> <input id="inputNumber" type="number" placeholder="Input:" value=1 oninput="unitConverter()" onchange="unitConverter()"> <p> <p>Output: <span id="outputNumber">1</span></p>

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error