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>