Error on transfer. invalid token Id

I had the transfer working , but now seems to error out on this invalid tokenId. Can you shed some light on this for me ? thanks .

import {
    Box,
    Button,
    FormLabel,
    FormControl,
    Accordion,
    AccordionItem,
    AccordionButton,
    AccordionPanel,
    AccordionIcon,
    Input,
    NumberInput,
    NumberInputField,
    NumberInputStepper,
    NumberIncrementStepper,
    NumberDecrementStepper
  } from "@chakra-ui/react";
  import { useForm } from "react-hook-form";
  import { useMoralis } from "react-moralis";
  import React from "react";
  
  export default function FormComponent() {
  
    const { user, Moralis, isAuthenticated } = useMoralis();

   const doTransfer = async (
      eth1:string,
      eth2:string,
      sentTs:number): Promise<any> => {
    
        let options = {
          type: "erc20", 
          amount: Moralis.Units.Token(sentTs, 18), 
          receiver: eth2,
          contractAddress: "0x294e1BFd460053C77B991A3ed22482060b54278A"}
        
          let result = await (Moralis as any).transfer(options);
        }
    
    const saveNewUser = async (
      em1: string,
      em2:string,
      eth1:string,
      eth2:string,
      sentTs:number): Promise<any> => {
      const Cuser = Moralis.Object.extend("Cuser");
      const cuser = new Cuser();
      cuser.set('muser', Moralis.User.current())
      cuser.set('email1', em1)
      cuser.set('email2', em2)
      cuser.set('ethadd1', eth1)
      cuser.set('ethadd2', eth2)
      cuser.set('senttokens', sentTs)
      cuser.save()
      console.log("i ran"); 
    // return data
    }

    const queryThisUserFriend = async (
      // querys Moralis backend to see if this email address 
      // has already sent to the other email address
      em1: string,
      em2: string): Promise<any> => {
      let checkInVar = em1 + em2;
      const Cuser = Moralis.Object.extend("Cuser");
      const query = new Moralis.Query(Cuser);
      query.equalTo("email1", "[email protected]");
      const results = await query.find();
      alert("Successfully retrieved " + results.length + " records.");
          for(let i = 0; i < results.length; i++){
            const object = results[i];
            console.log(object);
            let checkVar = object.get("email") + object.get("email2");
            if (checkVar === checkInVar)
              return "allbad";
          }
      return "good";
    }

    const queryThisEthAddressByEmail = async (
      // querys Moralis backend to see if theres already an email address 
      // for this eth address
      et1: string): Promise<any> => {
      const Cuser = Moralis.Object.extend("Cuser");
      const query = new Moralis.Query(Cuser);
      query.equalTo("ethadd1", et1);
      const results = await query.find();
      alert("Successfully retrieved " + results.length + " records.");
          for(let i = 0; i < results.length; i++){
            const object = results[i];
            console.log(object);
            let realEmail = object.get("email1");
           let emailOut =  JSON.stringify(realEmail);
              return emailOut;
          }
      return "";
    }



    const { register, handleSubmit } = useForm(); 
    const onSubmit = async (data: any) => {
    
      // get eth address of user 
      let etadd = user!.get('ethAddress');
      let em1 = data.email;
      let em2 = data.email2;
      let et1 = JSON.stringify(etadd);
      let et2 = data.ethadd2;
      let sentTs = data.numOfTokens;
     
     
        // just check if there's an email address for this eth address on record 
          let ethEmail = queryThisEthAddressByEmail(et1);
          if(await ethEmail !== "") {
            alert(ethEmail);
          }else{
            alert("no email for this eth address -" + et1);
          }


          // check to see that this ethaddress hasn't sent to 
          // the friend's eth address before. 
          queryThisUserFriend(em1, em2);
      
          // now save all user info to moralis db 
          saveNewUser(em1, em2, et1, et2, sentTs);
          
          // now do the transfer 
         await  doTransfer(et1, et2, sentTs);

          
      }
    return(
    <Box>
    {isAuthenticated ? (
      <>
   <Box bg="blue.600" 
        w="95%" 
        ml={15} 
        mt={4} 
        p={4} 
        color="black" 
        borderWidth="2px" 
        borderRadius="lg">
   <Accordion allowToggle>
      <AccordionItem>
          <h2>
          <AccordionButton
              color="darkgreen.500"
          >
                  <Box textAlign="center">
                  Expand for Instructions
                  </Box>
              <AccordionIcon />
          </AccordionButton>
          </h2>
              <AccordionPanel pb={4}
                  color="lightblue"
                  fontWeight="bold"
              >
              <p>*  Authenticating with Metamask doesn't cost any gas. </p>
              <p>1  You need to have our tokens in your account.</p>
              <p>2  Enter the requested Information, remember the account address is NOT the private key and always keep your private keys safe.</p>
              <p>3  Wait time to receive bonus tokens, up to 24 hours.</p>
              <p>4  You can do this to as many friends as you want. </p>
              </AccordionPanel>
      </AccordionItem>
      </Accordion>            
</Box>

<Box bg="blue.600" 
        w="95%" 
        ml={15} 
        mt={4} 
        p={4} 
        color="blue" 
        text-align="center" 
        borderWidth="2px" 
        borderRadius="lg" 
        border-color="white">
<form onSubmit={handleSubmit(onSubmit)}>
<FormControl isRequired mt={2}>
<FormLabel
    color="white"
    paddingTop="1.2rem"
    paddingLeft="3.5rem"
    textAlign="left"
    htmlFor="email">Your email address</FormLabel>
<Input {...register("email")}
   color="black"
    bg="white.800"
    width="100%"
    type="email" 
    id="email"
    fontWeight="bold"
    aria-describedby="email-helper-text" /> 
  </FormControl>
    <FormControl isRequired>
<FormLabel
    color="white"
    paddingTop="1.5rem"
    textAlign="left"
    paddingLeft="3.5rem"
    htmlFor="email2">Friend's email address</FormLabel>
  <Input  {...register("email2")}
    color="black"
    bg="white.800"
    width="100%"
    type="email" 
    id="email2" 
    fontWeight="bold"
    aria-describedby="email-helper-text" /> 
  </FormControl>
  <FormControl isRequired mt={6}>
  <FormLabel
    color="white"
    textAlign="left"
    paddingLeft="3.5rem"
    fontWeight="bold"
    htmlFor="ethadd2">Friends ethereum address</FormLabel>
  <Input  {...register("ethadd2")}
    color="black"
    bg="white.800"
    width="100%"
    id="ethadd2" 
    fontWeight="bold"
     /> 
    </FormControl>
    <FormControl isRequired mt={6}>
    <FormLabel
    color="white"
    textAlign="left"
    paddingLeft="3.5rem"
    fontWeight="bold"
    text-align="center" 
    htmlFor="numOfTokens"
    ># Tokens you want to send</FormLabel>
    <NumberInput max={50} min={3}
    color="black"
    width="40%"
    id="numOfTokens"
    paddingLeft=".5rem"
    fontWeight="bold"              
    >
    <NumberInputField {...register("numOfTokens")}
    bg="white.800"
    fontWeight="bold"
    color="black"
    />
    <NumberInputStepper>
      <NumberIncrementStepper />
      <NumberDecrementStepper />
    </NumberInputStepper>
    </NumberInput>
    </FormControl>
    <Button
      mt={4}
      mb={3}
      colorScheme="teal"
      type="submit"
    >
    Send Friend
    </Button>
  </form>
  </Box>
</> ) : ( <p className="ctr"></p> )}
  </Box>  
 );       
}


//console.log(user!.attributes);
//console.log(user.get('ethAddress'));
/*
const WETH9 = new web3.eth.Contract('xtoken', 'contractAddressWeth');
WETH9.methods.balanceOf('walletAddress').call({from: 'walletAddress'})
            .then((e) => {
               console.log(e)
            });
*/
 // transfer
      // sending 0.5 tokens with 18 decimals
/*const options = {type: "erc20", 
amount: Moralis.Units.Token("0.5", "18"), 
receiver: "0x..",
contractAddress: "0x.."}
let result = await Moralis.transfer(options)
*/ 
        //console.log(data);
       //sendfriend();

       //console.log(JSON.stringify(etadd));
        //let etadd = user!.attributes._ethAddress.toString;

What is the exact error that you get?

WTH now its not a invalid token Id and its this … { I don’t want to call Moralis.authenticate, as it’s already authenticated to get to this point

Unhandled Rejection (Error): Missing web3 instance, make sure to call Moralis.enableWeb3() or Moralis.authenticate()
doTransfer
src/components/FormComponent.tsx:37

  34 |       receiver: eth2,
  35 |       contractAddress: "0x294e1BFd460053C77B991A3ed22482060b54278A"}
  36 |     
> 37 |       let result = await (Moralis as any).transfer(options);
     | ^  38 |     }
  39 | 
  40 | const saveNewUser = async (

onSubmit
src/components/FormComponent.tsx:130

  127 |     saveNewUser(em1, em2, et1, et2, sentTs);
  128 |     
  129 |     // now do the transfer 
> 130 |    await  doTransfer(et1, et2, sentTs);
      | ^  131 | 
  132 |     
  133 | }

async*handleSubmit/<
src/logic/createFormControl.ts:1025

  1022 |     errors: {} as FieldErrors<TFieldValues>,
  1023 |     isSubmitting: true,
  1024 |   });
> 1025 |   await onValid(fieldValues, e);
       | ^  1026 | } else {
  1027 |   onInvalid && (await onInvalid(_formState.errors, e));
  1028 |   _options.shouldFocusError &&

Hi @ericnickus

To make transfers you need an active web3 provider. You activate your web3 after Moralis.authenticate() or Moralis.enableWeb3(). If user is already logged in - call Moralis.enableWeb3()

We have a bug on the latest versions: it requires tokenId even if you transfer erc20 token. For now just provide any tokenId. We will fix it soon

Hope this will help you :rocket:

1 Like