Error: insufficient funds for gas * price + value - ethereum

I am going through the first tutorial in the Ethereum website.
I've compiled the contract code using Remix and have created the following script. I have removed the binary strings for brevity:
var _greeting = 'Hello World!';
var browser_untitled_sol_greeterContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
var browser_untitled_sol_greeter = browser_untitled_sol_greeterContract.new(
_greeting,
{
from: web3.eth.accounts[0],
data: 'BINARY_STRING',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
var browser_untitled_sol_mortalContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
var browser_untitled_sol_mortal = browser_untitled_sol_mortalContract.new(
{
from: web3.eth.accounts[0],
data: 'BINARY_STRING',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
When loading this script I get:
Error: insufficient funds for gas * price + value.
When calling eth.getBalance(eth.accounts[0]) I get: 399577000000000
I can't figure out how much eth I should have in my balance in order to run this script or if I need to convert eth to gas.

You don't "convert" ether to gas. You use ether to pay for gas used.
In any case, the error message you're getting seems correct. Your balance is pretty low. eth.getBalance() returns balance in Wei. Your balance is only ~0.0004 ether, which is pretty low (you can convert to ether using https://etherconverter.online/ or convert in code with web3.fromWei(val, 'ether')).
The amount of ether you need varies depending on the operations executed in the contract, and how much you're willing to pay for the gas used (you can override the default gasPrice in the transaction object). Try mining or transferring more ether into your account.

which kind of network, testrpc or private network ? if you send transaction on the private network which initialized with geth init genesis.json , please do not set chainId as 0

Related

Supply API key to avoid Hit Limit error from Maps Service in Apps Script

I have a Google Sheet where we are fetching the driving distance between two Lat/Lng via the Maps Service. The function below works, but the matrix is 4,500 cells, so I'm getting the "Hit Limit" error.
How can I supply my paid account's API key here?
Custom Function
function drivingMeters(origin, destination) {
if (origin=='' || destination==''){return ''}
var directions = Maps.newDirectionFinder()
.setOrigin(origin)
.setDestination(destination)
.getDirections();
return directions.routes[0].legs[0].distance.value ;
}
Example use:
A1: =drivingMeters($E10,G$9)
Where E10 = 42.771328,-91.902281
and G9 = 42.490390,-91.1626620
Per documentation, you should initialize the Maps service with your authentication details prior to calling other methods:
Your client ID and signing key can be obtained from the Google Enterprise Support Portal. Set these values to null to go back to using the default quota allowances.
I recommend storing these values in PropertiesService and using CacheService, to provide fast access. Using this approach, rather than writing them in the body of your script project, means they will not be inadvertently copied by other editors, pushed to a shared code repository, or visible to other developers if your script is published as a library.
Furthermore, I recommend rewriting your custom function to accept array inputs and return the appropriate array output - this will help speed up its execution. Google provides an example of this on the custom function page: https://developers.google.com/apps-script/guides/sheets/functions#optimization
Example with use of props/cache:
function authenticateMaps_() {
// Try to get values from cache:
const cache = CacheService.getScriptCache();
var props = cache.getAll(['mapsClientId', 'mapsSigningKey']);
// If it wasn't there, read it from PropertiesService.
if (!props || !props.mapsClientId || !props.mapsSigningKey) {
const allProps = PropertiesService.getScriptProperties().getProperties();
props = {
'mapsClientId': allProps.mapsClientId,
'mapsSigningKey': allProps.mapsSigningKey
};
// Cache these values for faster access (max 6hrs)
cache.putAll(props, 21600);
}
// Apply these keys to the Maps Service. If they don't exist, this is the
// same as being a default user (i.e. no paid quota).
Maps.setAuthentication(props.mapsClientId, props.mapsSigningKey);
}
function deauthMaps_() {
Maps.setAuthentication(null, null);
}
// Your called custom function. First tries without authentication,
// and then if an error occurs, assumes it was a quota limit error
// and retries. Other errors do exist (like no directions, etc)...
function DRIVINGMETERS(origin, dest) {
if (!origin || !destination)
return;
try {
return drivingMeters_(origin, dest);
} catch (e) {
console.error({
message: "Error when computing directions: " + e.message,
error: e
});
// One of the possible errors is a quota limit, so authenticate and retry:
// (Business code should handle other errors instead of simply assuming this :) )
authenticateMaps_();
var result = drivingMeters_(origin, dest);
deauthMaps_();
return result;
}
}
// Your implementation function.
function drivingMeters_(origin, dest) {
var directions = Maps.newDirectionFinder()
...
}

Why am I getting exceeds gas limit error when I specify the exact gas limit?

I am deploying a contract using truffle, and when I specify the gas limit as the gas I want to use for the transaction I always get the exceeds gas limit error. Why does this happen?
edit
What I am trying to do is deploy the crypto kitties KittyCore.sol contract to my local devnet. I am using truffle to deploy it.
From another page, How to deploy truffle contract to dev network when using inheritance?, I found that since there is a contract hierarchy, I need to deploy my contracts in order. I used this technique, and I am able to deploy 4 out of 7 contracts, with the fifth, KittyAuction, giving the following error: The contract code couldn't be stored, please check your gas amount
Posted below is my truffle deployer script
var KittyCore = artifacts.require("KittyCore");
var KittyMinting = artifacts.require("KittyMinting");
var KittyAuction = artifacts.require("KittyAuction");
var KittyBreeding = artifacts.require("KittyBreeding");
var KittyOwnership = artifacts.require("KittyOwnership");
var KittyBase = artifacts.require("KittyBase");
var KittyAccessControl = artifacts.require("KittyAccessControl");
var SaleClockAuction = artifacts.require("SaleClockAuction");
module.exports = function (deployer) {
deployer.deploy(KittyAccessControl).then(function () {
return deployer.deploy(KittyBase).then(function () {
return deployer.deploy(KittyOwnership).then(function () {
return deployer.deploy(KittyBreeding).then(function () {
return deployer.deploy(KittyAuction, {
gas: 400000
}).then(function () {
return deployer.deploy(KittyMinting).then(function () {
return deployer.deploy(KittyCore);
})
})
})
})
})
});
};
My gas limit is set to 18000000000. This gas number is produced by running the following function on the actual contract that fails to deploy
var gasPrice;
KittyAuction.web3.eth.getGasPrice(function (error, result) {
gasPrice = Number(result);
console.log(gasPrice);
})
I have been fiddling with this number and nothing seems to work.
So I was able to deploy it. What I did was reset my blockchain, and set gasLimit to 0x8000000 and gave each contract that was troublesome a gas value of 0x7000000, and it deployed. Funny thing though, it wouldnt deploy again. I guess the gasLimit adjusted after a few blocks were mined because I got an error saying I was over the limit

ERC20 token. Cannot get variable nor use methods with deployed contract

I have deployed a ERC20 token contract via Metamask on the Ropsten network. The issue is that I am trying to interact with it via web3. I have followed the answers provided in SO and SE, related to token transfer and method calling.
As you will see, I am not transferring tokens here, but I am using my solidity contract method to set the initial owners of the token. The method in the contract receives 2 arguments, an address and also a tokenId.
When I use the code to transfer a token to the test Metamask account, it fails, in the sense that, if I enter into this test account and import the token, it has none.
For reference, This tokenId, is basically provided by my variable nextTokenIdToAssign, which I call this way:
const tokenIdToAssign = contract.nextTokenIdToAssign.call(function(err, res){
if(!err) { tokenIdToAssign = res; }
else { console.log("Error"); }
});
Oddly, when I try to console.log, it returns as undefined. Also, following the question comments, of this question I used
const test = web3.eth.getCode(contractAddress);
It just returns a null. The comments suggest that there is crash in my constructor, or a parent constructor.
Is there some important detail that I am missing here?
The complete code is the following:
const config = require('../config');
var Web3 = require('web3');
var web3 = new Web3();
const infuraApi = (config.infura.infuraApiKey);
//Set a provider (HttpProvider)
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/'+infuraApi));
}
const EthereumTx = require('ethereumjs-tx');
var accountAddressHex = (config.metamaskAccount.metamaskAddressHex);
var accountAddressPrivateKey = (config.metamaskAccount.metamaskAddressPrivateKey);
var privateKey = new Buffer(accountAddressPrivateKey, 'hex');
var count = web3.eth.getTransactionCount(accountAddressHex);
var contractAddress = (config.solidityContract.contractAddress);
var contractAbiArray = (config.solidityContract.contractABI);
var contract = web3.eth.contract(contractAbiArray).at(contractAddress);
const testSendAccount= "0x...";
const gasPrice = web3.eth.gasPrice;
const gasPriceHex = web3.toHex(gasPrice);
const gasLimitHex = web3.toHex(30000000);
//const tokenTransferAmount = 1;
var tokenIdToAssignHex = contract.nextTokenIdToAssign.sendTransaction( {from: accountAddressHex}, function(err, hash){
if(!err) { tokenIdToAssignHex = hash;
console.log(tokenIdToAssignHex); }
else { console.log("Error"); }
});
var tokenIdToAssign = contract.nextTokenIdToAssign.call(function(err, res){
if(!err) { tokenIdToAssign = res; }
else { console.log("Error"); }
});
const test = web3.eth.getCode(contractAddress);
var rawTransaction = {
"from": accountAddressHex,
"nonce": web3.toHex(count),
"gasPrice": gasPriceHex,
"gasLimit": gasLimitHex,
"to": contractAddress,
"value": "0x0",
"data": contract.setInitialOwner.getData(testSendAccount, tokenIdToAssign, {from: accountAddressHex}), //contract.transfer.getData("0xCb...", 10, {from: "0x26..."}),
"chainId": 0x03 //Ropsten id is 3, replace with 1 for main
};
var tx = new EthereumTx(rawTransaction);
tx.sign(privateKey);
var serializedTx = tx.serialize();
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
if (!err) { console.log( 'contract creation tx: ' + hash); }
else {
console.log(err);
return;
}
});
EDIT 1
Here is the contract code for the setInitialOwner:
function setInitialOwner(address _to, uint256 _tokenId) public
onlyOwner
tokensRemainingToAssign
tenKLimit (_tokenId)
yesZeroAddressOwner (_tokenId)
notSelfSend (_to, _tokenId) {
tokenIdToOwner[_tokenId] = _to;
balanceOfAddress[_to] = balanceOfAddress[_to].add(1);
emit Assign(_to, _tokenId);
tokenIndexArray.push(_tokenId);
uint256 length = balanceOf(msg.sender);
ownedTokensIndexMapping[_tokenId] = length;
addressToTokenIdByIndex[msg.sender][length] = _tokenId;
nextTokenIdToAssign = nextTokenFunc(nextTokenIdToAssign);
hypeKillsTokensRemainingToAssign = tokensRemainingToAssign.sub(1);
}
EDIT 2
I have changed the tokenToAssign and added the
var tokenIdToAssignHex = contract.nextTokenIdToAssign.sendTransaction( {from: accountAddressHex}, function(err, hash){
if(!err) { tokenIdToAssignHex = hash;
console.log(tokenIdToAssignHex); }
else { console.log("Error"); }
});
Since others may encounter this problem, I will post the solution I arrived at. The problem here was that my nextTokenToAssign variable was not updating properly.
I used myetherwallet.com to deploy the contract and to check its value. When I deployed the contract, if afterwards I did not call the constructor function, setting the initial value of nextTokenToAssign to 1, it would not update during the successive call to the setInitialOwner function. So the solution simply was to simply call the function.

sensor data is not uploading on artik cloud

I am trying to send sensor data to artik cloud via node.js. (using web socket and serial port). But its sending null. Anyone knows the reason? I just copied the code from tutorial so there is no syntax error.
var webSocketUrl = "wss://api.artik.cloud/v1.1/websocket?ack=true";
var device_id = "####";
var device_token = "#####";
var isWebSocketReady = false;
var ws = null;
var serialport = require("serialport");
var portName = 'COM5';
var sp= new serialport.SerialPort(portName, {
baudRate: 9600,
parser: serialport.parsers.readline("\r\n")
});
var WebSocket = require('ws');
/**
* Gets the current time in millis
*/
function getTimeMillis(){
return parseInt(Date.now().toString());
}
/**
* Create a /websocket bi-directional connection
*/
function start() {
//Create the websocket connection
isWebSocketReady = false;
ws = new WebSocket(webSocketUrl);
ws.on('open', function() {
console.log("Websocket connection is open ....");
register();
});
ws.on('message', function(data, flags) {
console.log("Received message: " + data + '\n');
});
ws.on('close', function() {
console.log("Websocket connection is closed ....");
});
}
/**
* Sends a register message to the websocket and starts the message flooder
*/
function register(){
console.log("Registering device on the websocket connection");
try{
var registerMessage = '{"type":"register", "sdid":"'+device_id+'", "Authorization":"bearer '+device_token+'", "cid":"'+getTimeMillis()+'"}';
console.log('Sending register message ' + registerMessage + '\n');
ws.send(registerMessage, {mask: true});
isWebSocketReady = true;
}
catch (e) {
console.error('Failed to register messages. Error in registering message: ' + e.toString());
}
}
/**
* Send one message to ARTIK Cloud
*/
function sendData(temperature){
try{
// ts = ', "ts": '+getTimeMillis();
var data = {
"temp": temperature
};
var payload = '{"sdid":"'+device_id+'", "data": '+JSON.stringify(data)+', "cid":"'+getTimeMillis()+'"}';
console.log('Sending payload ' + payload);
ws.send(payload, {mask: true});
} catch (e) {
console.error('Error in sending a message: ' + e.toString());
}
}
/**
* All start here
*/
start(); // create websocket connection
sp.on("open", function () {
sp.on('data', function(data) {
if (!isWebSocketReady){
console.log("WebSocket is not ready. Skip sending data to ARTIK Cloud (data:" + data +")");
return;
}
console.log("Serial port received data:" + data);
//var parsedStrs = data.split(",");
var temperature = parseInt(data);
sendData(temperature);
});
});
If you reference our First IoT Sample:
https://developer.artik.cloud/documentation/tutorials/your-first-iot-device.html
The node.js sample sends the value from the temperature sensor. As a dependency it requires a connected Arduino, Raspberry Pi, and a DHT temperature sensor located at the right pin. If you are seeing null "before" sending the data to ARTIK Cloud, you are not getting any value from the sensor.
In particular, output and print to console the "temperature" value from the following function in case of any parsing errors:
function sendData(temperature) //...
Email us at developer#artik.cloud if you need additional information.
Thanks!
In this line:
var temperature = parseInt(data);
If you're getting empty or non numeric data (you can verify this in the previous line where you're logging the variable's content), then temperature will be NaN (not a number). Then, when you build the JSON payload for Artik Cloud, you'll end up with something like:
{
"sdid": "cbd3f844967d464da3c4f4989f80f86c",
"data": {
"temp":null
},
"cid":"1495817841624"
}
Because the JSON.stringify of:
{"temp":NaN}
would be translated to:
{"temp":null}

Why Google place AutocompleteService callback not getting executed?

I have following code for place AutocompleteService() as:
var strAddr = '';
if (address.value != "") {
var service = new google.maps.places.AutocompleteService();
service.getQueryPredictions({ input: address.value }, function (predictions, status) {
console.log('1');
if (status == google.maps.places.PlacesServiceStatus.OK) {
console.log('Original Response: ' + predictions[0].structured_formatting.secondary_text);
strAddr = predictions[0].structured_formatting.secondary_text;
}
});
console.log('strAddr: ' + strAddr);
console.log('2');
}
console.log('3');
I am getting response in console as:
strAddr:
2
3
1
Original Response: //gettting matched address here
But it should be:
1
Original Response: //matched address here>
strAddr: //matched address here
2
3
Why the callback not executing in sequence as expected?
The getQueryPredictions function is asynchronous (it's probably happening via AJAX). So you can't guarantee that it will complete before the code immediately after it.
e.g. the call to getQueryPredictions might take perhaps 0.5 seconds. So while that's happening, these two lines will be able to execute immediately:
console.log('strAddr: ' + strAddr);
console.log('2');
You can't rely on strAddr being available in any code that happens outside of your callback function that's waiting for the getQueryPredictions response.
It is not guaranteed that you get this
1
Original Response: //matched address here>
strAddr: //matched address here
2
3
Because this ( new google.maps.places.AutocompleteService(); ) is asynchronous call and your callback can be called at any sequence