I want to call the methods with metamask of this simple contract:
contract AssetPrices {
mapping (uint => uint) public prices;
uint public data;
function EnterPrice(uint _time, uint _price){
prices[_time] = _price;
}
function GetPrice(uint _time) returns (uint){
data = prices[_time];
return data;
}
}
The contract is deployed at address 0xd884b7b7d6e21798814f218f2daa1054cff33ee5
The index.html file is:
<!DOCTYPE html>
<html>
<head>
</head>
<body class="container">
<h1>A Simple METAMASK controlled contract</h1>
</body>
<script src="https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script>
$(document).ready(function() {
window.addEventListener('load', function() {
// Check if Web3 has been injected by the browser:
if (typeof web3 !== 'undefined') {
// You have a web3 browser! Continue below!
startApp(web3);
} else {
alert("no web3 detected");
}
})
function startApp(web3) {
web3.eth.defaultAccount = web3.eth.accounts[0];
abi= JSON.parse('[{"constant":false,"inputs":[{"name":"_time","type":"uint256"},{"name":"_price","type":"uint256"}],"name":"EnterPrice","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"data","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_time","type":"uint256"}],"name":"GetPrice","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"prices","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"}]');
PriceContract = web3.eth.contract(abi);
ContractInstance = PriceContract.at('0xd884b7b7d6e21798814f218f2daa1054cff33ee5');
// Enter new price
ContractInstance.EnterPrice(25,150, {from: web3.eth.accounts[0]});
// Get the price you entered
x = ContractInstance.GetPrice(25, {from: web3.eth.accounts[0]});
}
})
</script>
</html>
However when I serve the page I get the following error:
Uncaught Error: invalid address
at inputAddressFormatter (https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js:3920:11)
at inputTransactionFormatter (https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js:3746:20)
at https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js:5005:28
at Array.map (native)
at Method.formatInput (https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js:5004:32)
at Method.toPayload (https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js:5030:23)
at Eth.send [as sendTransaction] (https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js:5055:30)
at SolidityFunction.sendTransaction (https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js:4121:26)
at SolidityFunction.execute (https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js:4207:37)
at startApp (http://127.0.0.1:8000/:27:20)
web3.eth.accounts
"solution", but this doens't work for me, cause metamask put the correct account with injected web3. Maybe somethign like this can help you, if you found the answer write here again ^^web3.eth.getAccounts(function(err, accounts) {...}
. Maybe, it'll help.