问题描述
我使用前端 Javascript 而不是 Node 来与 Ganache 上部署的合约进行交互。 NodeJS 版本完美运行,但前端 JS 版本不行。无法调用所有功能。代码如下:
<script src="/node_modules/web3/dist/web3.min.js"></script>
<script>
walletAddress = null;
//Connecting to Blockchain
var web3 = new Web3(Web3.givenProvider || "HTTP://127.0.0.1:7545");
var contactAddress = '0xB16bEc01bfe4F13D5e85A2F75F51893D797Df1F7'
var abi = '[{"constant":true,"inputs":[],"name":"getValue","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"_value","name":"setValue","outputs":[],"stateMutability":"nonpayable",{"constant":true,"name":"getMsgSender","outputs":[{"internalType":"address","type":"address"}],"type":"function"}]'
const abiJson = JSON.parse(abi);
let init = async () => {
ac = await web3.eth.getAccounts()
walletAddress = ac[0]
const contract = new web3.eth.Contract(
abiJson,contactAddress
);
console.log(await contract.methods.setValue(900).send({from:walletAddress}));
contract.methods.getValue().call().then(function (result) {
console.log(result);
});
}
init()
</script>
没有调用任何函数。不知道怎么判断是不是真的在和Ganache互动。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)