function depositToken(address token,address User,uint amount)public {
  if (!ERC20(token).transferFrom(msg.sender, address(this), amount)) revert();
  _balances[token][User] = _balances[token][User]+amount;

what is the web3 code for solidity function to connect the web3.

      deposit(req, res) {
    // validator.validateRequest(req, res, () => {
        let tokenAmount = req.body.amount;
        let receiverAddress = req.body.address;
        let senderPrivateKey = process.env.OWNER_PRIVATE_KEY;
        let hashedPrivateKey = web3.utils.toHex(senderPrivateKey);

        const txn = {
            to: receiverAddress,
            gasLimit: web3.utils.toHex(4700000),
            data: contract.methods.depositToken(process.env.TOKEN_ADDRESS, receiverAddress, tokenAmount).encodeABI(),
            value: web3.utils.toHex(tokenAmount),
            from: process.env.OWNER_ADDRESS

is it correct?

  • Show us what you've got, wee may be able to help you extending it to what you need. – goodvibration Feb 11 at 8:10

Well the first thing you have to do, is connect the web3. Assuming you are using metamask this would look something like:

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

if (typeof web3 !== 'undefined') {
    console.log("Using web3 detected from external source like Metamask");
    web3 = new Web3(window.web3.currentProvider);

} else {
    console.log("Using localhost");
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

var account = web3.eth.accounts[0];


The next step would be to create an instance of your contract:

const Contract = web3.eth.contract("ContractABIHere").at("Address");

Finally you would have to do something like:

var "parameters1"
var "parametersx"

function contractCall("Your_Parameters"){
     var deposit = Contract.depositToken.getData("Your_Parameters")
              window.web3.eth.sendTransaction({to:"deposit_address", data: deposit}, function(err, data){

finally if you can call this by placing the function on an onLoad() or alternatively place it on an onCLick()

This should give you an idea of what you have to do. Feel free to ask any questions you may have. or if you specify what you're building this on I can probably help to structure it better, but this should work on a single page js

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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