Hi there, I am a newcomer to the world of blockchain technology. Recently, I created a smart contract designed to display both the contract balance and user data, including the address and balance of the user. The smart contract allows users to deposit funds into it as well. However, despite functioning perfectly in Remix, I am facing issues with displaying the contract balance and user data.
Below is the code for my smart contract:
// 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 getOwner() public view returns (address) {
return owner;
}
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);
}
}
Additionally, here is my JavaScript code snippet:
<script>
var contract;
$(document).ready(function(){
web3 = new Web3(web3.currentProvider);
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>