2

Using web3 and setting metamask as the provider I want to be able to build a deposit function that takes a user's current metamask balance for a defined ERC20 contract and allows them to send that off to another wallet. This example is using ethjs as a convenience library.

How does the transfer function know that we want the user's metamask account to be the sender when our button is clicked. I've set the recipient address as the Ownerwallet since that is known ahead of time. Does Web3 automatically allow the transfer function from our contract to know who the sender(function caller is?).

var Web3 = require('web3');
var web3;

//metamask 
  if (typeof window.web3 !== 'undefined' && typeof window.web3.currentProvider !== 'undefined') {
    var eth = new Eth(web3.currentProvider);
  } else {}

var TokenContractAddress = "0x...";
var Token = eth.contract(TokenABI, { from: accounts[0], gas: 3000000 });
var ContractInstance = Token.at(TokenContractAddress);

var OwnerWallet = "0x1...";  

el('#depositTokens').addEventListener('click', function(){
   ContractInstance.transfer(OwnerWallet, el('#depositAmount').value)
    });
1

After searching and trying out variations I discovered that if the contract owner is not the same as accounts[0] then an additional data point is necessary in the transfer call to validate in Web3

So the above works with this modification.

ContractInstance.transfer(mySecondEthAddress, el('#depositAmount').value, {from: accounts[0], gas:300000})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.