问题描述
我正在使用 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');让 provider = new ethers.providers.Web3Provider(web3.currentProvider);让合同;让钱包;让 abi = [{匿名":假,输入":[{索引":真,"internalType": "地址","name": "账户",类型":地址"},{索引":假,"内部类型": "字符串","name": "密码",类型":字符串"}],"name": "Pwd_Assigned",类型":事件"},{输入":[{"内部类型": "字符串","name": "_passwd",类型":字符串"}],"name": "setPwd",输出":[{"internalType": "布尔","name": "成功",类型":布尔"}],"stateMutability": "不可支付",类型":功能"},{输入":[{"内部类型": "字符串","name": "_uname",类型":字符串"}],"name": "setUname",输出":[{"internalType": "布尔","name": "成功",类型":布尔"}],"stateMutability": "不可支付",类型":功能"},{输入":[{"内部类型": "字符串","name": "_uType",类型":字符串"}],"name": "setUtype",输出":[{"internalType": "布尔","name": "成功",类型":布尔"}],"stateMutability": "不可支付",类型":功能"},{匿名":假,输入":[{索引":真,"internalType": "地址","name": "账户",类型":地址"},{索引":假,"内部类型": "字符串","name": "utype",类型":字符串"}],"name": "Type_Assigned",类型":事件"},{匿名":假,输入":[{索引":真,"internalType": "地址","name": "账户",类型":地址"},{索引":假,"内部类型": "字符串","name": "uname",类型":字符串"}],"name": "Username_Assigned",类型":事件"},{输入":[],"name": "getContractName",输出":[{"内部类型": "字符串","name": "_name",类型":字符串"}],"stateMutability": "视图",类型":功能"},{输入":[],"name": "姓名",输出":[{"内部类型": "字符串",姓名": "",类型":字符串"}],"stateMutability": "视图",类型":功能"},{输入":[{"internalType": "地址",姓名": "",类型":地址"}],"name": "密码",输出":[{"内部类型": "字符串",姓名": "",类型":字符串"}],"stateMutability": "视图",类型":功能"},{输入":[],"name": "符号",输出":[{"内部类型": "字符串",姓名": "",类型":字符串"}],"stateMutability": "视图",类型":功能"},{输入":[{"internalType": "地址",姓名": "",类型":地址"}],"name": "uname",输出":[{"内部类型": "字符串",姓名": "",类型":字符串"}],"stateMutability": "视图",类型":功能"},{输入":[{"internalType": "地址",姓名": "",类型":地址"}],"name": "uType",输出":[{"内部类型": "字符串",姓名": "",类型":字符串"}],"stateMutability": "视图",类型":功能"}];让 contractAddress = "0xECFFa8439Fa4DC64388227fA43a420449E895c3f";//对于 rinkeby让 AidTokenAbi = [{输入":[{"internalType": "uint256","name": "_initialSupply",类型":uint256"}],"stateMutability": "不可支付",类型":构造函数"},{匿名":假,输入":[{索引":真,"internalType": "地址","name": "_owner",类型":地址"},{索引":真,"internalType": "地址","name": "_spender",类型":地址"},{索引":假,"internalType": "uint256","name": "_value",类型":uint256"}],"name": "批准",类型":事件"},{输入":[{"internalType": "地址","name": "_spender",类型":地址"},{"internalType": "uint256","name": "_value",类型":uint256"}],"name": "批准",输出":[{"internalType": "布尔","name": "成功",类型":布尔"}],"stateMutability": "payable",类型":功能"},{输入":[{"internalType": "地址","name": "_to",类型":地址"},{"internalType": "uint256","name": "_value",类型":uint256"}],"name": "转让",输出":[{"internalType": "布尔","name": "成功",类型":布尔"}],"stateMutability": "payable",类型":功能"},{匿名":假,输入":[{索引":真,"internalType": "地址","name": "_from",类型":地址"},{索引":真,"internalType": "地址","name": "_to",类型":地址"},{索引":假,"internalType": "uint256","name": "_value",类型":uint256"}],"name": "转让",类型":事件"},{输入":[{"internalType": "地址","name": "_from",类型":地址"},{"internalType": "地址","name": "_to",类型":地址"},{"internalType": "uint256","name": "_value",类型":uint256"}],"name": "transferFrom",输出":[{"internalType": "布尔","name": "成功",类型":布尔"}],"stateMutability": "payable",类型":功能"},{输入":[{"internalType": "地址",姓名": "",类型":地址"},{"internalType": "地址",姓名": "",类型":地址"}],"name": "津贴",输出":[{"internalType": "uint256",姓名": "",类型":uint256"}],"stateMutability": "视图",类型":功能"},{输入":[{"internalType": "地址",姓名": "",类型":地址"}],"name": "balanceOf",输出":[{"internalType": "uint256",姓名": "",类型":uint256"}],"stateMutability": "视图",类型":功能"},{输入":[],"name": "姓名",输出":[{"内部类型": "字符串",姓名": "",类型":字符串"}],"stateMutability": "视图",类型":功能"},{输入":[],"name": "标准",输出":[{"内部类型": "字符串",姓名": "",类型":字符串"}],"stateMutability": "视图",类型":功能"},{输入":[],"name": "符号",输出":[{"内部类型": "字符串",姓名": "",类型":字符串"}],"stateMutability": "视图",类型":功能"},{输入":[],"name": "tokensSold",输出":[{"internalType": "uint256",姓名": "",类型":uint256"}],"stateMutability": "视图",类型":功能"},{输入":[],"name": "总供应量",输出":[{"internalType": "uint256",姓名": "",类型":uint256"}],"stateMutability": "视图",类型":功能"}];让 AidtokenAddress = "0x17b0e97DF8217a984294De6bDbbb9D7020190479";让 AidTokenContract;让签名者 = provider.getSigner();让 usr_account;函数加载钱包(){signer.getAddress().then(async(res)=>{usr_account = res;等待控制台日志(usr_account);});}//异步函数 setProvider(){//provider = await new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545');//}函数 getContract(){contract = new ethers.Contract(contractAddress,abi, signer);控制台日志(合同.功能);}函数 getTokenContract(){AidTokenContract = new ethers.Contract(AidtokenAddress,AidTokenAbi, signer);}异步函数 getName(){//var name = await contract.functions.getContractName();//console.log(name);await contract.name().then(async (res)=>{等待 console.log(res);});}异步函数 setInfo(event){event.preventDefault();加载钱包();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)=>{控制台日志(res);});await contract.functions.setUtype(utype).then(async(res)=>{控制台日志(res);});await contract.functions.setPwd(pwd).then(async(res)=>{控制台日志(res);});//获取信息();}异步函数 getInfo(event){event.preventDefault();变量名;var utype;无功密码;加载钱包();getContract();await contract.functions.uname(usr_account).then(async(res)=>{uname=res;控制台日志(res);});await contract.functions.uType(usr_account).then(async(res)=>{utype = res;控制台日志(res);});await contract.functions.pwd(usr_account).then(async(res)=>{密码=res控制台日志(res);});}异步函数传输(事件){event.preventDefault();加载钱包();getTokenContract();await console.log("....");var numberOfTokens = document.getElementById('numberOfTokens').value;等待 AidTokenContract.functions.transfer(numberOfTokens,{来自:usr_account,气体限制:500000});}//setProvider();加载钱包();getContract();//getName();//设置信息();//获取信息();
<头><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>--><脚本字符集=utf-8"src="https://cdn.ethers.io/scripts/ethers-v4.min.js" type="text/javascript"><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.js"></script>头部><身体><form onsubmit="setInfo(event)"><div><h1>注册</h1><p>请填写此表格以创建帐户.</p><小时><br><label for="uname"><b>用户名</b></label><input type="text" placeholder="输入用户名" id="uname" required><br><label for="utype">选择用户类型:</label><select id="utype" name="utype"><option value="Donor">Donor</option><option value="NGO">NGO</option><option value="Government">Government</option></选择><br><label for="psw"><b>密码</b></label><input type="password" placeholder="输入密码" id="psw" required><!-- <label for="psw-repeat"><b>重复密码</b></label><input type="password" placeholder="重复密码" name="psw-repeat" required><小时>--><p>创建帐户即表示您同意我们的<a href="#">条款和条件隐私</a>.</p><button type="submit" id="registerbtn">注册</button>
<div class="容器登录"><p>已有账号?<a href="#">登录</a>.</p>
</表单><br><br><form onsubmit="getInfo(event)"><button type="submit" >获取信息</button></表单><form onsubmit="transfer(event)"><input id="numberOfTokens" type="number" name="number" value="1" min="1" pattern="[0-9]"><button type="submit" >Donate</button></表单><!-- <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>--><script src="js/app.js"></script>