Hi I’m getting the following error when adding the following lines to main.js. Please let help.
let array = await contract.methods.getAllTokensForUser(ethereum.selecedAddress).call({from: ethereum.selecedAddress});
console.log(array);
Uncaught (in promise) Error: invalid address (argument="address", value=undefined, code=INVALID_ARGUMENT, version=address/5.5.0) (argument="user", value=undefined, code=INVALID_ARGUMENT, version=abi/5.0.7)
at e.value (index.ts:225:28)
at e.value (index.ts:237:20)
at e.value (index.ts:241:21)
at r.value (abstract-coder.ts:68:16)
at r.value (address.ts:18:18)
at array.ts:71:19
at Array.forEach (<anonymous>)
at y (array.ts:54:12)
at r.value (tuple.ts:23:16)
at e.value (abi-coder.ts:106:15)
Here is my amin.js:
/** Connect to Moralis server */
const serverUrl = "xxxxxxx";
const appId = "xxxxxxxxx";
const CONTRACT_ADDRESS = "xxxxxxxx";
Moralis.start({serverUrl, appId});
async function init() {
try {
let user = Moralis.User.current();
if (! user) {
$("#login_button").click(async () => {
user = await Moralis.authenticate();
})
}
renderGame();
} catch (error) {
console.log(error);
}
}
async function renderGame() {
$("#login_button").hide();
// Get and render propertize from Smart contract
let petId = 0;
let abi = await getAbi();
const web3 = await Moralis.enableWeb3();
let contract = new web3.eth.Contract(abi, CONTRACT_ADDRESS);
//console.log(contract);
let array = await contract.methods.getAllTokensForUser(ethereum.selecedAddress).call({from: ethereum.selecedAddress});
console.log(array);
let data = await contract.methods.getTokenDetails(petId).call({from: ethereum.selecedAddress});
console.log(data);
renderPet(0,data);
$("#game").show();
}
function renderPet(id, data){
$("#pet_id").html(id);
$("#pet_damage").html(data.damage);
$("#pet_magic").html(data.magic);
$("#pet_endurance").html(data.endurance);
$("#feed_button").attr("data-pet-id", id);
let deathTime = new Date( (parseInt(data.lastMeal) + parseInt(data.endurance)) * 1000);
let now = new Date();
if (now > deathTime) {
deathTime = "<b>DEAD</d>"
}
$("#pet_starvation_time").html(deathTime);
}
function getAbi() {
return new Promise((res) => {
$.getJSON("Token.json", ((json) => {
res(json.abi);
}))
})
}
async function feed(petId){
let abi = await getAbi();
const web3 = await Moralis.enableWeb3();
let contract = new web3.eth.Contract(abi, CONTRACT_ADDRESS);
contract.methods.feed(petId).send({from: ethereum.selectedAddress}).on("receipt", ( () => {
console.log("done");
renderGame();
}))
}
$("#feed_button").click( () => {
let petId = $("#feed_button").attr("data-pet-id");
feed(petId);
})
init();
Token.sol
pragma solidity 0.8.11;
import "../node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";
contract Token is ERC721, Ownable {
struct Pet {
uint8 damage;
uint8 magic;
uint256 lastMeal;
uint256 endurance; //24 hours without food will die
}
mapping(uint256 => Pet) private _tokenDetails;
uint256 nextId = 0;
constructor(string memory name, string memory symbol)
ERC721(name, symbol)
{}
function getTokenDetails(uint256 tokenId) public view returns (Pet memory) {
return _tokenDetails[tokenId];
}
function mint(
uint8 damage,
uint8 magic,
uint256 endurance
) public onlyOwner {
_tokenDetails[nextId] = Pet(damage, magic, block.timestamp, endurance);
_safeMint(msg.sender, nextId);
nextId++;
}
function feed(uint256 tokenId) public {
Pet storage pet = _tokenDetails[tokenId];
require(pet.lastMeal + pet.endurance > block.timestamp);
pet.lastMeal = block.timestamp;
}
function getAllTokensForUser(address user)
public
view
returns (uint256[] memory)
{
uint256 tokenCount = balanceOf(user);
if (tokenCount == 0) {
return new uint256[](0);
} else {
uint256[] memory result = new uint256[](tokenCount); //uint256?
uint256 totalPets = nextId;
uint256 resultIndex = 0;
uint256 i;
for (i = 0; i < totalPets; i++) {
if (ownerOf(i) == user) {
result[resultIndex] = i;
resultIndex++;
}
}
return result;
}
}
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal override {
Pet storage pet = _tokenDetails[tokenId];
require(pet.lastMeal + pet.endurance > block.timestamp);
}
}