使用 window.ethereum 获取 toWei 函数
Getting toWei function using window.ethereum
我只是想在 JavaScript.
的前端网站上访问 toWei
utils 函数
Since we removed our window.web3, MetaMask no longer automatically reloads the page on chain/network changes.
(Source: https://docs.metamask.io/guide/provider-migration.html#summary-of-breaking-changes)
所以我使用的 window.ethereum
目前为止还不错。但我无法找到使用 window.ethereum
访问 .toWei(..)
函数的直接示例。
例如,我想要这样的东西:
var weiVal = window.ethereum.toWei(String(eth_float), 'ether');
但是这个特定的函数调用不存在。我该怎么做?
toWei()
函数是已删除的 web3
模块的一部分,不再是 MetaMask 浏览器扩展的一部分。
当前代码中有 0 个结果:https://github.com/MetaMask/metamask-extension/search?q=toWei
但是,您可以将 web3 JS 包导入您的前端并使用此处的功能。
<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>
压缩包来源:官方 web3.js repo - https://github.com/ChainSafe/web3.js
从 ethereum 转换为 wei 只是乘以 10^-18(反之亦然乘以 10^18)。这是一个演示:
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>
所有Ethereum units单位转换的更通用的解决方案在这里:
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>
我只是想在 JavaScript.
的前端网站上访问toWei
utils 函数
Since we removed our window.web3, MetaMask no longer automatically reloads the page on chain/network changes.
(Source: https://docs.metamask.io/guide/provider-migration.html#summary-of-breaking-changes)
所以我使用的 window.ethereum
目前为止还不错。但我无法找到使用 window.ethereum
访问 .toWei(..)
函数的直接示例。
例如,我想要这样的东西:
var weiVal = window.ethereum.toWei(String(eth_float), 'ether');
但是这个特定的函数调用不存在。我该怎么做?
toWei()
函数是已删除的 web3
模块的一部分,不再是 MetaMask 浏览器扩展的一部分。
当前代码中有 0 个结果:https://github.com/MetaMask/metamask-extension/search?q=toWei
但是,您可以将 web3 JS 包导入您的前端并使用此处的功能。
<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>
压缩包来源:官方 web3.js repo - https://github.com/ChainSafe/web3.js
从 ethereum 转换为 wei 只是乘以 10^-18(反之亦然乘以 10^18)。这是一个演示:
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>
所有Ethereum units单位转换的更通用的解决方案在这里:
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>