在过去的几天里,我一直在尝试在以太坊测试网 Rinkeby 上发送一笔交易,无论我将 Gas 增加多少,都会不断收到此错误。
“未处理的拒绝错误:返回错误:内在气体太低”
我发送的数据是:
"0x7b22416e7377657273223a5b7b225175657374696f6e223a2231222c22416e73776572223a2234227d2c7b225175657374696f6e223a2232222c22416e73776572223a2234227d2c7b225175657374696f6e223a2233222c22416e73776572223a2234227d2c7b225175657374696f6e223a2234222c22416e73776572223a2234227d2c7b225175657374696f6e223a2235222c22416e73776572223a2234227d2c7b225175657374696f6e223a2236222c22416e73776572223a2234227d5d7d"
Run Code Online (Sandbox Code Playgroud)
转换为十六进制后。
我在下面添加了我的代码。
"0x7b22416e7377657273223a5b7b225175657374696f6e223a2231222c22416e73776572223a2234227d2c7b225175657374696f6e223a2232222c22416e73776572223a2234227d2c7b225175657374696f6e223a2233222c22416e73776572223a2234227d2c7b225175657374696f6e223a2234222c22416e73776572223a2234227d2c7b225175657374696f6e223a2235222c22416e73776572223a2234227d2c7b225175657374696f6e223a2236222c22416e73776572223a2234227d5d7d"
Run Code Online (Sandbox Code Playgroud) 我想获取用户在 Metamask 中添加的所有帐户。我尝试了所有 web3.js 方法来获取帐户,但我总是只得到一个帐户,并且始终是当前选择的帐户。
根据 web3.js 文档,web3.eth.getAccounts()应该返回该节点控制的所有帐户。但是,我得到了一个仅包含当前选定数组的数组。不用说,我在 Metamask 中创建了多个帐户。
我需要在以太坊中使用 web3js 计算连续交易的随机数,但getTransactionCount不返回待处理的交易。
有没有办法使用 web3js 获取所有交易,包括待处理和已完成的交易?如果不是web3js,还有其他方法吗?
我是 web3 和以太坊区块链的新手。我尝试使用以下代码广播交易,但它总是给我一个无效的发件人错误。示例代码如下所示。
const Tx = require('ethereumjs-tx').Transaction;
const Web3 = require('web3');
var url = 'https://ropsten.infura.io/v3/XXX';
const web3 = new Web3(new Web3.providers.HttpProvider(url));
const account1 = '0xaB7BXXX';
web3.eth.defaultAccount = account1;
const privatekey1 = Buffer.from('fee069363aXXX','hex');
web3.eth.getTransactionCount(account1, (err, txCount) => {
data = "0xXXX";
const txObject = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(200000),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
data: data
}
const tx = new Tx(txObject)
tx.sign(privatekey1)
const serializedTx = tx.serialize()
const raw = '0x' + serializedTx.toString('hex')
web3.eth.sendSignedTransaction(raw, (err, txHash) => {
console.log('err:', err, 'txHash:', txHash)
}) …Run Code Online (Sandbox Code Playgroud) 我的 web3.js 检测器定义如下:
const ethEnabled = () => {
if (window.ethereum) {
web3 = new Web3(window.ethereum)
return true
} else if (window.web3) {
web3 = window.web3
return true
}
return false
}
if (!ethEnabled()) {
alert(
'Please install an Ethereum-compatible browser or extension like MetaMask to use this dApp!'
)
}
Run Code Online (Sandbox Code Playgroud)
问题是这在桌面上的 Chrome 和 Brave 浏览器中有效,但在移动设备和 trustwallet 上失败。因此会触发下面的警报消息。
请让我知道我该怎么做。
谢谢。
我刚刚开始探索区块链技术,并于前几天制作了我的第一个智能合约。接下来,我尝试为智能合约创建一个前端,但在使用 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)
重现步骤:
我正在运行一个 Gatsby3 网站,和许多人一样,当我尝试使用某些 web3 插件时,我遇到了有关缺少 Polyfills 的 Webpack 5 错误。我知道他们不再自动执行这些操作,我必须自己处理它们。
在尝试了我能找到的所有解决方案之后,这个似乎应该可行,但不适合我。我猜我做错了什么/错过了什么。
这是我添加到 Gatsby 配置中的内容,用于处理“crypto”和“stream”缺少的 polyfill:
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
fallback: {
crypto: require.resolve('crypto-browserify'),
stream: require.resolve('stream-browserify'),
},
},
})
}
Run Code Online (Sandbox Code Playgroud)
我确实安装了 crypto-browserify 和stream-browserify。
然而,同样的错误仍然存在:
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules
by default.
This is no longer the case. Verify if you need this module and configure a
polyfill for it.
If you want to include a polyfill, you …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 等网站断开连接
我用 . 创建了一个全新的项目npm init vite bar -- --template vue。我已经完成了npm install web3,我可以看到我package-lock.json包含这个包。我的node_modules目录还包括web3模块。
然后我将这一行添加到main.js:
import { createApp } from 'vue'
import App from './App.vue'
import Web3 from 'web3' <-- This line
createApp(App).mount('#app')
Run Code Online (Sandbox Code Playgroud)
我不明白这是怎么回事。我对使用还很陌生npm,所以我不太确定要谷歌什么。错误来自node_modules/web3/lib/index.js、node_modules/web3-core/lib/index.js、node_modules/web3-core-requestmanager/lib/index.js、 最后node_modules/util/util.js。我怀疑这与以下之一有关:
<script setup>标签(但我导入了它,main.js所以我不认为是这个)web3js在 Typescript 中,我的 Vue3 项目没有为 Typescript …我对打字稿相当陌生,所以我收到一个错误,指出“未知”类型的参数不能分配给“错误 | 类型”的参数。null',我不明白为什么我会得到这个。我该如何解决这个问题?
export function subscribeToAccount(
web3: Web3,
callback: (error: Error | null, account: string | null) => any
) {
const id = setInterval(async () => {
try {
const accounts = await web3.eth.getAccounts();
callback(null, accounts[0]);
} catch (error) {
callback(error, null);
}
}, 1000);
return () => {
clearInterval(id);
};
}
Run Code Online (Sandbox Code Playgroud) web3js ×10
ethereum ×6
metamask ×3
blockchain ×2
javascript ×2
reactjs ×2
angular ×1
gatsby ×1
node.js ×1
polyfills ×1
typescript ×1
vite ×1
vue.js ×1
vuejs3 ×1
webpack ×1