首先,让我提一下这是一个浏览器内项目,所以我只能使用
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
几个月前,我制作了一个 dapp,即使我从未设置过提供程序,它也能正常工作,所以我猜它使用了 MetaMask 提供的提供程序。但是,我在这里使用指南,唯一的问题是以下代码,
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
此代码用于在元掩码用户在我的 dapp 上批准元掩码后输出元掩码用户的地址,但是我收到了错误
Uncaught (in promise) TypeError: Cannot read property 'request' of undefined
Run Code Online (Sandbox Code Playgroud)
该错误只是说,如果我必须添加提供程序,我的实际目标是使用元掩码中的提供程序,并允许连接到元掩码,其余功能(例如传输或其他我知道如何处理它们),它只是当我尝试加载页面时,似乎我的问题出现了。
我没有可用的以太节点,也不打算使用nodejs,只有一个显示Metamask地址的html文件,因此使用浏览器内的web3js。
我希望这只是我没有意识到一些简单的事情,因为我似乎找不到现在不能使用 web3js 的原因。
我正在构建一个分散的应用程序,用户可以在其中将他们的加密货币钱包 (Metamask) 连接到我的网站。
They can initiate a connection request by clicking a button. On success, the wallet is connected and my website can interact with it.
Is there any way to initiate a disconnect request? Similar to a 'Log out' button. Currently, the users have to manually disconnect their wallet within Metamask settings which is not a straightforward process.
我不明白问题是什么。当我运行该应用程序时,我收到此错误:
Unhandled Runtime Error
Error: invalid address or ENS name (argument="name", value=5.050201689117535e+47, code=INVALID_ARGUMENT, version=contracts/5.5.0)
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
import {ethers} from 'ethers'
import {useEffect, useState} from 'react'
import axios from 'axios'
import Web3Modal from 'web3modal'
import { nftaddress, nftmarketaddress } from '../config'
import NFT from '../artifacts/contracts/NFT.sol/NFT.json'
import Market from '../artifacts/contracts/Market.sol/Market.json'
export default function Home() {
const [nfts, setNFts] = useState([])
const [loadingState, setLoadingState] = useState('not-loaded')
useEffect(()=> {
loadNFTs()
}, [])
async function loadNFTs() {
// what we want to load:
// ***provider, tokenContract, …Run Code Online (Sandbox Code Playgroud) 在javascript中我运行契约的方法
contract[methodName](...params, { from: myAccount }, (err, response) => {
console.log('get transaction', methodName, err, response);
if (err) return reject(err);
resolve(response);
});
Run Code Online (Sandbox Code Playgroud)
然后通过MetaMask拒绝交易.在控制台中出错
MetaMask - RPC Error: Error: MetaMask Tx Signature: User denied transaction signature.
Run Code Online (Sandbox Code Playgroud)
但我无法在我的代码中发现此错误.回调不起作用.
我怎样才能在JS中发现这个错误?
我想测试我的应用程序如何与 Metamask 交互(例如,钱包是否打开?)并创建一个灵活的测试配置,使我能够通过“安装”Metamask 接口来访问应用程序,方式与普通“登录”的方式类似(我使用 MM 有效登录)在 Assert JS (2018) 讲座视频中,例如:https ://www.youtube.com/watch ?v=5XQOK0v_YRE 有没有使用 Cypress 测试 Metamask 的参考材料或建议?Cypress 有以下用于加载 Chrome 扩展插件的文档:
module.exports = (on, config) => {
on('before:browser:launch', (browser, launchOptions) => {
// supply the path to an unpacked WebExtension
// NOTE: extensions cannot be loaded in headless Chrome
launchOptions.extensions.push('/path/to/webextension')
return launchOptions
})
}
Run Code Online (Sandbox Code Playgroud)
但除此之外没有其他帮助。一旦加载,我将如何与其交互以产生有意义的测试?我希望这应该是可能的。我看到其他人已经考虑过这些问题,我已经在 MM github 中发布了这个问题: https: //github.com/MetaMask/metamask-extension/issues/8605 thx ...
我在我的网站(Laravel 和 Vuejs)上使用元掩码登录。我安装了 Web3 并执行 getAccounts、Sign 、 getBalance 等操作。
但我想获取metamask中的所有代币(如BNB、RARI等)余额。我写了这段代码:
// accounts[0] => default wallet number
window.web3.eth.getBalance(accounts[0], (err, b) => {
if (err)
console.log(err);
let balance = {};
balance.ether = window.web3.utils.fromWei(b, "ether");
});
Run Code Online (Sandbox Code Playgroud)
但只返回 ETH 代币余额而不是所有代币。
我如何获得所有代币余额?你能帮助我吗?
我实现了一个短代码,它连接到metamask浏览器扩展并使用web3 和reactjs显示用户帐户,该代码在使用桌面浏览器时效果很好,但是当我在移动浏览器上尝试时,metamask移动版本由于非-其扩展程序在移动浏览器中的存在,像opensea这样的网络应用程序如何使用移动网络浏览器上的按钮打开metamask移动应用程序?
代码 :
export const connectWallet = async () => {
if (window.ethereum) {
try {
const addressArray = await window.ethereum.request({
method: "eth_requestAccounts",
});
const obj = {
status: " Write a message in the text-field above.",
address: addressArray[0],
};
return obj;
} catch (err) {
return {
address: "",
status: "... " + err.message,
};
}
} else {
return {
address: "",
status: (
<span>
<p>
{" "}
{" "}
<a …Run Code Online (Sandbox Code Playgroud) 我正在构建一个质押功能,并在授予访问我的令牌的权限后遇到以下错误:
“MetaMask - RPC 错误:无法设置未定义的属性(设置“loadingDefaults”)”
Solidity 合约的质押功能:
// Staking function
function depositTokens(uint _amount) public {
require(_amount > 0, 'Amount has to be > 0');
// Transfer tether tokens to this contract
tether.transferFrom(msg.sender, address(this), _amount);
// Update Staking balance
stakingBalance[msg.sender] = stakingBalance[msg.sender] + _amount;
if(!hasStaked[msg.sender]) {
stakers.push(msg.sender);
}
// Update Staking balance
isStaking[msg.sender] = true;
hasStaked[msg.sender] = true;
}
Run Code Online (Sandbox Code Playgroud)
质押前端
stakeTokens = (amount) => {
this.setState({loading: true })
this.state.tether.methods.approve(this.state.deBank._address, amount).send({from: this.state.account}).on('transactionHash', (hash) => {
this.state.deBank.methods.depositTokens(amount).send({from: this.state.account}).on('transactionHash', (hash) => {
this.setState({loading:false})
}) …Run Code Online (Sandbox Code Playgroud) 我遇到一个问题,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)