我遇到一个问题,Metamask 的移动浏览器根本没有将 window.ethereum 注入我的网站。这非常令人困惑,因为我所做的一切都与我在以前的网站中所做的一样,效果很好。
if (window.ethereum) {
this.handleEthereum();
} else {
window.addEventListener('ethereum#initialized', this.handleEthereum, {
once: true,
});
}
Run Code Online (Sandbox Code Playgroud)
为了确认 window.ethereum 从未被注入,我也只是将其输出到屏幕上,并且它无限期地保持未定义状态。
当然,使用桌面浏览器一切都可以正常工作。这只是 Metamask 移动浏览器的问题。
我尝试将值从 0.025 以太更改为 1 以太,然后也显示相同的错误。
另外,我尝试过像 1/8 这样的有理数仍然不起作用。
查看了一些答案,但他们没有解决错误。
我在其他项目中有相同的代码并且它在那里工作。
我收到的错误
Uncaught (in promise) Error: invalid BigNumber value (argument="value", value={"value":"25000000000000000"}, code=INVALID_ARGUMENT, version=bignumber/5.5.0)
Could not get the stack frames of error: TypeError: Cannot read properties of null (reading 'length')
Run Code Online (Sandbox Code Playgroud)
这是我的上市价格代码
uint256 listingPrice = 0.025 ether ; // Here ether is denoting the MATIC
function getListingPrice() public view returns (uint256) {
return listingPrice;
}
Run Code Online (Sandbox Code Playgroud)
这是在 UI 中获取值的代码
async function putItem(url) {
const web3Modal = new Web3Modal();
const connection = await web3Modal.connect(); …Run Code Online (Sandbox Code Playgroud) 我想获取用户在 Metamask 中添加的所有帐户。我尝试了所有 web3.js 方法来获取帐户,但我总是只得到一个帐户,并且始终是当前选择的帐户。
根据 web3.js 文档,web3.eth.getAccounts()应该返回该节点控制的所有帐户。但是,我得到了一个仅包含当前选定数组的数组。不用说,我在 Metamask 中创建了多个帐户。
我已将 MetaMask 与 Angular 集成,在从代码中打开 MetaMask 扩展时出现此错误。
以下是我打开扩展的代码。我在单击打开 MetaMask 时调用此函数,它给出了该错误
MetaMask - RPC 错误:权限请求已挂起;请稍等。{code:-32002, message:“权限请求已待处理;请稍候。”}
ethEnabled(template?): any
{
if ((window as any).web3)
{
(window as any).web3 = new Web3((window as any).web3.currentProvider);
(window as any).ethereum.enable();
console.log((window as any).ethereum.enable());
}
else
{
this.modalRef = this.modalService.show(template);
}
}
Run Code Online (Sandbox Code Playgroud) 我刚刚开始探索区块链技术,并于前几天制作了我的第一个智能合约。接下来,我尝试为智能合约创建一个前端,但在使用 web3.js 将我的 Angular 应用程序连接到 Metamask 时遇到困难。
具体来说,我遇到一个问题,当我尝试为我的 Angular 应用程序提供服务时,它给出了以下错误:
错误:./node_modules/eth-lib/lib/bytes.js 找不到模块:错误:无法解析“C:\Users\profile\Desktop\Coding\EthSmartContractProject\Frontend\node_modules\eth-lib”中的“加密” \lib'
错误:./node_modules/eth-lib/lib/bytes.js 找不到模块:错误:无法解析“C:\Users\profile\Desktop\Coding\EthSmartContractProject\Frontend\node_modules\eth-lib”中的“流” \lib'
这是我的 Blockchain.service.ts,我在其中尝试处理角度应用程序中的所有区块链相关任务:
import { Injectable } from '@angular/core';
import Web3 from "web3";
declare let window:any;
@Injectable({
providedIn: 'root'
})
export class ContractService {
web3: any;
accounts: Array<String>;
async loadWeb3() {
if (window.ethereum) {
window.web3 = new Web3(window.ethereum);
await window.ethereum.enable;
} else if (window.web3) {
window.web3 = new Web3(window.web3.currentProvider);
} else {
window.alert('Non-Ethereum browser detected. You Should consider using MetaMask!');
}
}
}
Run Code Online (Sandbox Code Playgroud)
重现步骤:
我正在使用 web3 使用以下方法连接到元掩码
await window.ethereum.request({ method: 'eth_requestAccounts' });
var address = await window.ethereum.request({ method: 'eth_accounts' });
Run Code Online (Sandbox Code Playgroud)
但是当我刷新屏幕时它仍然显示为已连接,我无法在 web3 中找到任何方法将其与 pancackeswap 等网站断开连接
我正在使用 web3-react,但我不知道如何在浏览器刷新时保持与 MetaMask 钱包的连接持续。
这是代码:
// define the injectedConnectors
const injectedConnector = new InjectedConnector({
supportedChainIds: [
1, // Mainet
3, // Ropsten
4, // Rinkeby
5, // Goerli
42, // Kovan
],
})
const { chainId, account, activate, active } = useWeb3React()
// activate the wallet
activate(injectedConnector)
console.log(account)
// all good.
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切正常,我激活了我的 MetaMask 钱包,并且正确记录了帐户,并且 active 变量是一个布尔值,会更改为 true。
问题是,当我刷新页面时,活动变为 false,并且我失去了 UI 与 MetaMask 钱包之间的连接。当然,将活动保存到浏览器中不会改变任何内容,因为连接依赖于活动布尔值。
文档缺乏此类信息。
我正在制作一个需要 MetaMask 身份验证的 Chrome 扩展。
我开始将其开发为 Web 应用程序,但作为 chrome 扩展,它无法检测 MetaMask...
这是我现在的网络应用程序代码:
function toggleButton() {
const loginButton = document.getElementById("login-button");
if (!window.ethereum) {
loginButton.innerText = "Install MetaMask";
loginButton.addEventListener("click", () => {
window.open("https://metamask.io");
});
return false;
}
loginButton.addEventListener("click", loginWithMetaMask);
}
async function loginWithMetaMask() {
const accounts = await window.ethereum.request({ method: "eth_requestAccounts" }).catch((e) => {
console.error(e.message);
return;
});
if (!accounts) {
return;
}
const userWallet = document.getElementById("user-wallet");
userWallet.innerText = accounts[0];
}
window.addEventListener("DOMContentLoaded", toggleButton);
Run Code Online (Sandbox Code Playgroud)
有没有办法将 Web 应用程序迁移到 Chrome 扩展程序?
ensure_signed的函数包含一个名为类型的pallets/template/lib.rs参数。我一直在研究各种项目,我看到的都是在做的事情。originOrginFor<>ensure_signed(origin)?
我的问题是我实际上应该如何从该原始值中获取诸如公钥或钱包地址之类的东西。我相信某些东西一定是内部Signed(AccountId)变体。但在几个示例和教程的测试文件中,我看到的只是它们只是通过了Origin::Signed(1)。我该怎么处理那个1?
我在这些区块链方面刚刚起步,所以任何类型的知识都受到高度赞赏
metamask ×10
ethereum ×5
blockchain ×3
web3js ×3
angular ×2
javascript ×2
reactjs ×2
android ×1
angular10 ×1
ethers.js ×1
kotlin ×1
rust ×1
solidity ×1
substrate ×1
web3-react ×1