※ 引述《Tim20502 (恩)》之銘言:
: 最近在連接testrpc可以使用合約,但在轉換到metamask使用的時候就發現這個問題…
: https://i.imgur.com/bBu9kVR.jpg
Metamask不支援sync function call
一定要用callback的方式取值
範例 (需Metamask ropsten)
https://output.jsbin.com/yihegurowu
Contract
pragma solidity ^0.4.24;
contract PTT {
function hello () public pure returns (string) {
return 'Hello PTT';
}
}
Contract Address
0x100e3d1600788d4606107120fdf30ec6657e412a
JS
const ABI = [
{
"constant": true,
"inputs": [],
"name": "hello",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "pure",
"type": "function"
}
];
const PttContract = web3.eth.contract(ABI);
const ptt = PttContract.at("0x100e3d1600788d4606107120fdf30ec6657e412a");
ptt.hello((err,result) => document.getElementById("output").innerHTML =
result);