[Ethereum] Error: gas required exceeds allowance or always failing transaction when trying to estimate gas

gas-estimateweb3js

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.to = <destination address>
data.token = 0x06012c8cf97bead5deae237070f9587f8e7a266d (cryptokitties contract address)
data.tokenId = 1148166

var getDataForERC721 = function(from, to, contractAddress, tokenId){
    var abi = <myerc721abi>;
    var contract = web3.eth.contract(abi).at(contractAddress);
    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',
  to: '0x06012c8cf97bead5deae237070f9587f8e7a266d',
  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

Best Answer

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('balanceOf(address)')) ^
    bytes4(keccak256('ownerOf(uint256)')) ^
    bytes4(keccak256('approve(address,uint256)')) ^
    bytes4(keccak256('transfer(address,uint256)')) ^
    bytes4(keccak256('transferFrom(address,address,uint256)')) ^
    bytes4(keccak256('tokensOfOwner(address)')) ^
    bytes4(keccak256('tokenMetadata(uint256,string)'));

Other ways to check this, is checking their ABI, or looking at their code.

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.

Good luck!