# Error: gas required exceeds allowance or always failing transaction when trying to estimate gas

I am trying to run estimateGas function on mainnet like the following to send erc721 cryptokitties from one account to another:

data.from = <my address>
data.tokenId = 1148166

var getDataForERC721 = function(from, to, contractAddress, tokenId){
var abi = <myerc721abi>;
var data = contract.safeTransferFrom.getData(from, to, tokenId);
return data;
}

var txdata = getDataForERC721(data.from, data.to, data.token, data.tokenId;

getEstimateGas = async function(from, to, data, value){
var self = this;
return new Promise((resolve, reject) => {
web3.eth.estimateGas({
'from': from,
'to': to,
'data': data,
'value': value
}, function(err, tx){
console.log("errror is -> " + err);
if( err ) resolve(null);
else resolve(tx);
});
});

getEstimateGas(data.from, data.to , txdata, 0x00);


I get this error Error: gas required exceeds allowance or always failing transaction. This function works well on Ropsten network, but gives the above error on mainnet.

I am using web3 version ^0.20.6

# EDIT

I have made sure that contract.ownerOf(tokenId) returns the correct address. I have also tried replacing contract.safeTransferFrom.getData(from, to, tokenId) with contract.transferFrom.getData(from, to, tokenId). However, I still get the same error.

console.log(contract.transferFrom.getData(from, to, tokenId)) prints the following:

0x23b872dd00000000000000000000000057e7674b1c58a0edce5a62af228ab34f09c00fca000000000000000000000000ae08cf4e3678843bbeb39d0608029b1daa4d51210000000000000000000000000000000000000000000000000000000000118506


The data that I am now passing to getEstimateGas is as follows:

{ from: '0x57E7674B1c58A0edce5a62AF228ab34f09C00Fca',
data:    '0x23b872dd00000000000000000000000057e7674b1c58a0edce5a62af228ab34f09c00fca000000000000000000000000ae08cf4e3678843bbeb39d0608029b1daa4d51210000000000000000000000000000000000000000000000000000000000118506',
value: '0x00' }


Here from is the address which contains the token, to is the contract address (cryptokitties) and data is the data returned from contract.transferFrom.getData function.

Here the from address is also the owner of the token. So, I guess I don't need to use the approve function.

I gained this token with this transaction https://etherscan.io/tx/0x5119c2d5eae0fdc4a0e000b84231cfb5db075efd30fcca3fabe91d2bbfdc9006

web3.eth.estimateGas will return that error when there is not enough gas, or when the transaction fails, and for the looks of it, sounds like your transaction might be failing, here is why:

The transaction safeTransferFrom is not implemented for cryptokitties, looking at their code, they've implemented interface signatures (ERC-165), there you can check if you can call a function from your code:

bytes4 constant InterfaceSignature_ERC721 =
bytes4(keccak256('name()')) ^
bytes4(keccak256('symbol()')) ^
bytes4(keccak256('totalSupply()')) ^
bytes4(keccak256('ownerOf(uint256)')) ^

Point is that safeTransferFrom is not implemented, and that's why estimateGas fails, likely it worked on Ropsten because the token you are using does implement it.
• Thanks, I changed to transferFrom function, but still get the same error. I tried to look for their abi. Where can I get it? – kosta Nov 22 '18 at 22:10
• I tried using their ABI, but I still get the same error. Do you need to know any specific information? I can confirm that contract.ownerOf(tokenId) returns the correct address. – kosta Nov 23 '18 at 11:36
• I added more information to the original post. I did not use the approve function since the from address is also the owner of the token. Could you check? – kosta Nov 23 '18 at 23:46