I suspect that your issue is because you have not approved the transfer required to call transferFrom(...)
.
Here are the relevant functions:
/* Send coins */
function transfer(address _to, uint256 _value) {
if (balanceOf[msg.sender] < _value) throw; // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
balanceOf[msg.sender] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
Transfer(msg.sender, _to, _value); // Notify anyone listening that this transfer took place
}
/* Allow another contract to spend some tokens in your behalf */
function approveAndCall(address _spender, uint256 _value, bytes _extraData)
returns (bool success) {
allowance[msg.sender][_spender] = _value;
tokenRecipient spender = tokenRecipient(_spender);
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
/* A contract attempts to get the coins */
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
if (balanceOf[_from] < _value) throw; // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
if (_value > allowance[_from][msg.sender]) throw; // Check allowance
balanceOf[_from] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
allowance[_from][msg.sender] -= _value;
Transfer(_from, _to, _value);
return true;
}
If you are the current owner of the tokens, you can call transfer(...)
to transfer your balance to another address.
However if you want to transfer from another address, you will need to call transferFrom(...)
which will check the allowance[...][...]
data structure to confirm whether this transfer has been approved.
So, call approveAndCall(...)
first to approve your transfer and you should be able to call transferFrom(...)
to transfer your tokens.
When you call transferFrom(...)
without first calling approveAndCall(...)
, and error is thrown in the line
if (_value > allowance[_from][msg.sender]) throw; // Check allowance
This error will appear as an Out Of Gas error in the Ethereum Wallet.
If you are comfortable using geth
, see How can the transaction status from a thrown error be detected when gas can be exactly the same as the gasUsed for a successful transaction? for the following code that can be used to check the exact reason for your error:
> var status = debug.traceTransaction("0xd23219e2ea10528b245deb9e47993cae2ffd3ffe9fc27aeb808e94fc4e75d37b")
undefined
> if (status.structLogs.length > 0) {
console.log(status.structLogs[status.structLogs.length-1].error)
}
"invalid jump destination (PUSH1) 2"
Which version of web3 are you using?
Here is a working example with web3@^1.0.0-beta.34
const Web3 = require('web3');
contractAddress = '0xe7775a6e9bcf904eb39da2b68c5efb4f9360e08c';
provider = 'https://mainnet.infura.io';
const contractAbiJSON = '';//... The JSON abi
// Init
const web3 = new Web3(new web3.providers.HttpProvider(provider));
const contract = new web3.eth.Contract(contractAbiJSON, contractAddress);
contract.methods.totalSupply().call().then((totalSupply) => {
console.log(totalSupply);
});
Best Answer
}