Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

278
Visualizações
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?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

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

about 4 years ago · Juan Pablo Isaza Relatório

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>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda