- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 ethers.js 文档:https://docs.ethers.io/ethers.js/html/cookbook-providers.html .
设置提供程序时出现错误 -: Uncaught ReferenceError: web3 is not defined
我想将我的分散式应用程序与元掩码连接起来。
为此,我试图通过根据文档设置提供程序来将元掩码与 ethers.js 连接起来。
我使用了 provider = new ethers.providers.Web3Provider(web3.currentProvider)。但它会为代码中的 web3 对象抛出错误。此代码与文档完全相同。但仍然无法正常工作。
//let provider = new ethers.getDefaultProvider('rinkeby');
let provider = new ethers.providers.Web3Provider(web3.currentProvider);
let contract;
let wallet;
let abi = [
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
},
{
"indexed": false,
"internalType": "string",
"name": "pwd",
"type": "string"
}
],
"name": "Pwd_Assigned",
"type": "event"
},
{
"inputs": [
{
"internalType": "string",
"name": "_passwd",
"type": "string"
}
],
"name": "setPwd",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_uname",
"type": "string"
}
],
"name": "setUname",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_uType",
"type": "string"
}
],
"name": "setUtype",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
},
{
"indexed": false,
"internalType": "string",
"name": "utype",
"type": "string"
}
],
"name": "Type_Assigned",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
},
{
"indexed": false,
"internalType": "string",
"name": "uname",
"type": "string"
}
],
"name": "Username_Assigned",
"type": "event"
},
{
"inputs": [],
"name": "getContractName",
"outputs": [
{
"internalType": "string",
"name": "_name",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "name",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "pwd",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "uname",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "uType",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
}
];
let contractAddress = "0xECFFa8439Fa4DC64388227fA43a420449E895c3f"; // for rinkeby
let AidTokenAbi = [
{
"inputs": [
{
"internalType": "uint256",
"name": "_initialSupply",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "_owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "_spender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "_value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "_spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_to",
"type": "address"
},
{
"internalType": "uint256",
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "payable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "_from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "_to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "_value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "_from",
"type": "address"
},
{
"internalType": "address",
"name": "_to",
"type": "address"
},
{
"internalType": "uint256",
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
},
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "name",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "standard",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "tokensSold",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
];
let AidtokenAddress = "0x17b0e97DF8217a984294De6bDbbb9D7020190479";
let AidTokenContract;
let signer = provider.getSigner();
let usr_account;
function loadWallet(){
signer.getAddress().then(async(res)=>{
usr_account = res;
await console.log(usr_account);
});
}
// async function setProvider(){
// provider = await new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545');
// }
function getContract(){
contract = new ethers.Contract(contractAddress,abi, signer);
console.log(contract.functions);
}
function getTokenContract(){
AidTokenContract = new ethers.Contract(AidtokenAddress,AidTokenAbi, signer);
}
async function getName(){
// var name = await contract.functions.getContractName();
// console.log(name);
await contract.name().then(async (res)=>{
await console.log(res);
});
}
async function setInfo(event){
event.preventDefault();
loadWallet();
getContract();
var uname = document.getElementById('uname').value;
var pwd = document.getElementById('psw').value;
var utype = document.getElementById('utype').value;
await contract.functions.setUname(uname).then(async(res)=>{
console.log(res);
});
await contract.functions.setUtype(utype).then(async(res)=>{
console.log(res);
});
await contract.functions.setPwd(pwd).then(async(res)=>{
console.log(res);
});
//getInfo();
}
async function getInfo(event){
event.preventDefault();
var uname;
var utype;
var pwd;
loadWallet();
getContract();
await contract.functions.uname(usr_account).then(async(res)=>{
uname=res;
console.log(res);
});
await contract.functions.uType(usr_account).then(async(res)=>{
utype = res;
console.log(res);
});
await contract.functions.pwd(usr_account).then(async(res)=>{
pwd=res
console.log(res);
});
}
async function transfer(event){
event.preventDefault();
loadWallet();
getTokenContract();
await console.log(".....");
var numberOfTokens = document.getElementById('numberOfTokens').value;
await AidTokenContract.functions.transfer(numberOfTokens,{
from: usr_account,
gasLimit: 500000
});
}
//setProvider();
loadWallet();
getContract();
//getName();
// setInfo();
//getInfo();
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcode-generator/1.4.3/qrcode.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<!-- <script src="js/web3.min.js"></script> -->
<script charset="utf-8"
src="https://cdn.ethers.io/scripts/ethers-v4.min.js" type="text/javascript">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.js"></script>
</head>
<body>
<form onsubmit="setInfo(event)">
<div>
<h1>Register</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<br>
<label for="uname"><b>User Name</b></label>
<input type="text" placeholder="Enter Username" id="uname" required>
<br>
<label for="utype">Choose a User type:</label>
<select id="utype" name="utype">
<option value="Donor">Donor</option>
<option value="NGO">NGO</option>
<option value="Government">Government</option>
</select>
<br>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" id="psw" required>
<!-- <label for="psw-repeat"><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="psw-repeat" required>
<hr> -->
<p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p>
<button type="submit" id="registerbtn">Register</button>
</div>
<div class="container signin">
<p>Already have an account? <a href="#">Sign in</a>.</p>
</div>
</form>
<br><br>
<form onsubmit="getInfo(event)">
<button type="submit" >Get Info</button>
</form>
<form onsubmit="transfer(event)">
<input id="numberOfTokens" type="number" name="number" value="1" min="1" pattern="[0-9]">
<button type="submit" >Donate</button>
</form>
<!-- <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script> -->
<script src="js/app.js"></script>
</body>
</html>
最佳答案
使用 ethers.js 与 Metamask 交互
const provider = new ethers.providers.Web3Provider(window.ethereum, "any");
// Prompt user for account connections
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();
console.log("Account:", await signer.getAddress());
关于javascript - 如何将 ethers.js 与元掩码连接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60785630/
假设我运行npm ls ethers来获取hardhat项目中的esers版本,结果如下:-。现在我想要将前5个实体中的ethers版本从[电子邮件受保护]、@onomicFoundation/[电子
我正在使用 Ethers.js允许用户将他们的 Metamask 钱包连接到我的应用程序。这是我的代码: import { ethers } from "ethers" async function
我有一个带有接收函数的智能合约: receive() external payable { Wallets[msg.sender] += msg.value; } 我有一个前端,我想使用 re
在 ethers.js 中,您可以使用 provider.getGasPrice() 来获得您应该支付的 gas 的最佳估计值。有没有什么简单的东西我可以调用或转换到这个值上以获得相当于“快速”交易而
我正在 Hardhat 中开发智能合约并在 RSK Testnet 上进行测试.要创建签名者帐户,我使用助记种子短语以及以下 Hardhat 配置: require('@nomicfoundation
坚固性: function ping() public view returns ( uint ) { return 999999999; } function ping2() public
问题 我正在尝试在测试网络(ropsten)中使用契约(Contract)方法,但由于此错误而失败: reason: 'cannot estimate gas; transaction may fai
我正在使用 ethers.js 文档:https://docs.ethers.io/ethers.js/html/cookbook-providers.html . 设置提供程序时出现错误 -: Un
如何使用 ethers.js 在合约交互中设置 gasPrice?我正在尝试覆盖以下代码中的 gasPrice: let txPromise = contract.populateTransactio
我的 package.json 文件中有: "scripts": { "compile-contract-types": "yarn compile-external-abi-types &
Const Provider=new ethers.Providers.Web3Provider(Connection);。这一行给了我错误。TypeError:无法在异步FetchData(Chat
我正在尝试使用 ethers.js 将交易发送到多边形网络。提交交易后,我 await tx.wait(),但它有 50% 的时间无法解决。我看到其他人也有类似的问题,但这是由于他们的汽油价格太低。我
我正在尝试制作一个简单的 js 机器人,它检查每个区 block 的 eth(或链的主要 token )并将其发送到另一个钱包。 我有一个工作机器人: const { ethers } = requi
请不要评判,我不知道该做什么,该怎么做:) 我的代码:以太.js (^5.6.0) import { ThirdwebSDK } from '@3rdweb/sdk' import { ethers
根据 official docs的 ethers.js ,这应该是如何使用自定义数据连接到特定网络(如 Rinkeby-testnet)的方式: const provider = ethers.ge
安装 Rocks 前端后,我尝试使用以下命令向前端添加新节点 #insert-ethers但我收到此错误: 'bash: insert-ethers: command not found" 我该如何解
有没有办法不仅获取传入消息的正文。还有它的 ether/IP header ?现在我正在使用 gen_tcp 并通过主动控制进程接收消息。 现在套接字是通过'of_driver'以这种方式打开的: g
Ethereal 转储中的 TSV 和 TSER 字段是什么意思?我尝试使用谷歌搜索但找不到答案。 最佳答案 TSV 是 Timestamp Value 字段。它与序列号结合使用以唯一标识段(因为序列
目录 摘要 1 引言 2 提出的TEA-PSE系统 2.1
这是一个非常小的 repo 来显示问题:https://github.com/adamdry/ethers-event-issue 但我也会在这里解释。这是我的契约(Contract): //SPDX
我是一名优秀的程序员,十分优秀!