Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Not able to intracting with smartcontract with javascript to get useraddress and smartcontract balance

Hi am new to blockchain technology i have made a smart contract that will show contract balance and user data like his address, balance, and user can deposit inserted amount in smartcontract my code is down am not able to show neither contract balance nor user data. its working perfectly in remix.

smartcontract

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;

contract UserData {

    address owner;
    uint bal;

    constructor() {
        owner = msg.sender;
    }

    receive() external payable {}

    function getBalance() view public returns(uint) {
        return bal;
    }

    function deposit(uint amt) external payable {
        bal = bal + amt;
        bal += msg.value;
    }

    // function to get the useraddress
    function getOwner() public view returns (address) {    
        return owner;
    }
    
    // Function to return current balance of user
    function getUserBalance() public view returns(uint256){
        return owner.balance;
    }

    function withdraw(uint withdrawAmount) external payable {
        require(msg.sender == owner, "Only owner can withdraw!");
        payable(msg.sender).transfer(withdrawAmount);
    }
}

and here is my javascript code:

<script>
var contract;

$(document).ready(function(){
    web3 = new Web3(web3.currentProvider);
    //ethereum.request({ method: 'eth_requestAccounts' });

    var address = "0xd3553504e02681C4d4f1969017dAca11003bB496";
    var abi = [];

    contract = new web3.eth.Contract(abi, address);

    contract.methods.getBalance().call().then(function(bal){
        $('#balance').html(bal/10000000000000000);
    })

    contract.methods.getOwner().call().then(function(address){
        $('#userAddress').html(address);
    })

})

$('#deposit').click(function(){
    var amt = 0;
    amt = parseInt($('#amount').val());

    web3.eth.getAccounts().then(function(accounts){
        var acc = accounts[0];
        return contract.methods.deposit(amt).send({from: acc});
    }).then(function(tx){
        console.log(tx);
    }).catch(function(tx){
        console.log(tx);
    })
})
</script>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!