Javascript 从按钮调用approve函数时出错
我有这两个功能Javascript 从按钮调用approve函数时出错,javascript,solidity,web3js,metamask,Javascript,Solidity,Web3js,Metamask,我有这两个功能 function getData() { var a = document.getElementById("amount").value; var b = a * 0.1; var c = a - b; document.getElementById("receive").innerHTML = c; }; function approve(){ if (typeof window !== 'undefined' && typeof wind
function getData() {
var a = document.getElementById("amount").value;
var b = a * 0.1;
var c = a - b;
document.getElementById("receive").innerHTML = c;
};
function approve(){
if (typeof window !== 'undefined' && typeof window.web3 !== 'undefined') {
// We are in the browser and metamask is running.
web3 = new Web3(window.web3.currentProvider.enable());
}
var accounts= web3.eth.getAccounts();
var a = document.getElementById("amount").value;
var agiContract = new web3.eth.Contract(agiabi,agiContractAddress);
agiContract.methods.approve("0xE462CbEE0cd420f6c199B0194B1D8D93Fb5e7720", a).send({ from: accounts[0] });
};
函数getData正在从用户处获取一个输入量,然后将该输入量传递到函数approve():
使用“批准”功能,我将从不同的合同中获得批准的金额,以便我可以在合同中使用该金额
但当我单击“批准”时,会出现以下错误:
web3.min.js:1 Uncaught TypeError: this.provider[(intermediate value)(intermediate value)(intermediate value)] is not a function
at t.u.send (web3.min.js:1)
at h (web3.min.js:1)
at M.r [as getAccounts] (web3.min.js:1)
at approve (exchange.js:84)
at HTMLButtonElement.onclick (VM15448 :227)
我使用了以下版本的函数approve
function approve(){
var accounts= web3.eth.getAccounts();
var a = document.getElementById("amount").value;
var agiContract = new web3.eth.Contract(agiabi,agiContractAddress);
agiContract.methods.approve("0xE462CbEE0cd420f6c199B0194B1D8D93Fb5e7720", a).send();
};
这给了我以下的错误
Uncaught (in promise) Error: No "from" address specified in neither the given options, nor the default options.
at Object.d._executeMethod (web3.min.js:1)
at approve (exchange.js:84)
at HTMLButtonElement.onclick (VM15481 :227)
inpage.js:1 Uncaught DOMException: Failed to execute 'postMessage' on 'Window': #<Promise> could not be cloned.
这里有什么问题
我现在尝试了这个代码,但它也不起作用
function getData() {
var a = document.getElementById("amount").value;
var b = a * 0.1;
var c = a - b;
document.getElementById("receive").innerHTML = c;
};
function approve() {
web3.eth.sendTransaction({
from: web3.eth.coinbase,
to: '0xE462CbEE0cd420f6c199B0194B1D8D93Fb5e7720',
value: web3.utils.toWei(document.getElementById("amount").value, 'ether')
}, function(error, result) {
if (!error) {
console.log(result) ;
} else {
console.log(error);
}
})
}
这给了我
Uncaught Error: The send transactions "from" field must be defined!
at f.inputTransactionFormatter (web3.min.js:1)
at web3.min.js:1
我再次更新了代码
function getData() {
var a = document.getElementById("amount").value;
var b = a * 0.1;
var c = a - b;
document.getElementById("receive").innerHTML = c;
};
function approve() {
var accounts= web3.eth.getAccounts();
web3.eth.sendTransaction({
from: accounts ,
to: '0xE462CbEE0cd420f6c199B0194B1D8D93Fb5e7720',
value: web3.utils.toWei(document.getElementById("amount").value, 'ether')
}, function(error, result) {
if (!error) {
console.log(result) ;
} else {
console.log(error);
}
})
}
这给了我以下的错误
Uncaught (in promise) Error: No "from" address specified in neither the given options, nor the default options.
at Object.d._executeMethod (web3.min.js:1)
at approve (exchange.js:84)
at HTMLButtonElement.onclick (VM15481 :227)
inpage.js:1 Uncaught DOMException: Failed to execute 'postMessage' on 'Window': #<Promise> could not be cloned.
inpage.js:1未捕获的DomeException:未能在“窗口”上执行“postMessage”:#无法克隆。
Ok,那么您试图解决什么问题呢?根据给出的错误消息,您要么在合同方法中缺少一个参数,要么给出了一个无效的参数。知道为什么就意味着要共享这个函数……你是在要求人们解决一个有缺失部分的难题。我不知道我在哪里遗漏了一个参数。批准是拿着要使用代币的合同和金额。你可以在这里查看全部内容:parallel-taucet.surge.sh只有approve按钮连接好了,其余的你可以查看文档不同意。我如何实现这一点?