0

Hello I've been trying to ask question in discord, reddit, and no one them can answer

I'm still struggling for more than a week to call function in web3 react; I have correctly abi, and always able to mint first, i have this mapping on contract;

mapping(address => uint256[]) public userOwnedTokens;

and I call it like this:

const provider = new ethers.providers.Web3Provider(ethereum); 
const signer = provider.getSigner();   
const nftContract = new ethers.Contract(nftContractAddress,NFT.abi,signer  );  
let nftTx = await nftContract.userOwnedTokens.call(currentAccount);  
let nftTx2 = await nftContract.userOwnedTokens.methods.call(currentAccount);  
let nftTx3 = await nftContract.userOwnedTokens(currentAccount);   
console.log(nftTx1, nftTx2, nftTx3 "nftTx"); 

 

but it seems no working at all? what’s the proper way?

Cannot read properties of undefined (reading 'call')
Cannot read properties of undefined (reading 'methods')
also how do I get the list of all NFTS that been minted from all users? from ERC721

surely when using _mint , it will store in somewhere, but how do i get the list data? I want to show it for marketplace.

or this version, because mapping returns an array:

let nftx = [];
let index = 0;
while(true){    
  let x = await nftContract.userOwnedTokens(currentAccount, index).call();  
  if(x){
    nftx.push(x);
  }else{
    break;
  }
  index++;
}
console.log(nftx);

and nope nothing works,

people keep throwing answer without able explaining the usage with method and call() or a need of axios.get() after executing function from wait() after executing function from contract

let tokenUri = await nftContract.tokenURI(tokenId);
let data = await axios.get(tokenUri);

or

  let nftTx = await nftContract.createEternalNFT();
  let tx = await nftTx.wait();

It is almost 2 weeks and none can explain, I have the source in this https://gitlab.com/dellwatson/test, after the contract-compile, copy the abi json into client/utils/...json

this is a simple nft mint;

2 Answers 2

3

From the userOwnedTokens definition

mapping(address => uint256[]) public userOwnedTokens;

The solidity will create a public getter that has this signature userOwnedTokens(address,uint256) public view returns (uint256). If you want to get the whole array you have to create your own getter.

function foo(address addr) public view returns (uint256[] memory) {
    return userOwnedTokens[addr];
}

To call a view function follow the documentation.

let nfts = await nftContract.foo(currentAccount);   
0
const provider = new ethers.providers.Web3Provider(ethereum);

must be

const provider = new ethers.providers.Web3Provider(window.ethereum);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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