Cannot read property of selected address error

let web3 = new Web3(window.ethereum)
works fine on desktop when calling this function
const encodedFunction= web3.eth.abi.encodeFunctionCall({})

BUT on mobile Iā€™m using this
if(this.isMobile){

      web3 = await Moralis.enable({provider:'walletconnect'})

    }

the same contract call doesnā€™t workā€¦ Iā€™ll get a
ā€œcannot read property of selectedAddress of undefinedā€
errorā€¦

how do I use web3 on mobileā€¦
full code ā€œwindow.ethereum.selectedAddressā€ is the main errorā€¦ I need to get the users address

    const encodedFunction= web3.eth.abi.encodeFunctionCall({
      name:"mint",
      type:"function",
      inputs:[{
        type:'address',
        name:'account'
      },{
        type:'uint256',
        name:'amount'
      },{
        type:'bytes',
        name:'data'
      }
    ]
  },[window.ethereum.selectedAddress,1,web3.utils.asciiToHex(_uri)])

    const transactionParameters= {
      to:contract,
      from:window.ethereum.selectedAddress,
      data:encodedFunction
    }

lastlyā€¦ how do I call this

    const txt = await window.ethereum.request({
      method:'eth_sendTransaction',
      params:[transactionParameters]
    });
1 Like

async getConnect(){

let user:any = await Moralis.authenticate({provider:'walletconnect'})
let web3:any = await Moralis.enable({provider:'walletconnect'})
console.log(web3.currentProvider.selectedAddress)

}

this returns undefined for the users address

1 Like

Hey @blockchainvic, hope you are ok.

Have you check that web3 is enabled?
https://docs.moralis.io/moralis-server/web3/web3#web-3-js

You can also do a quick check with:
https://docs.moralis.io/moralis-server/web3/web3#ensureweb-3-isinstalled

Carlos Z

thanks for your response, Iā€™m well. Hope your awesome also.
This is for walletConnectā€¦ everything works well in browser

canā€™t get it going on mobile

oddly enough

web3.eth.getAccounts()

shows the address

I donā€™t see that WC provider supports selectedAddress

So you can use web3.currentProvider.accounts[0] instead

that workedā€¦ thank you

1 Like

Instead of:

Try this:

const txt = await web3.currentProvider.request({
      method:'eth_sendTransaction',
      params:[transactionParameters]
    });

Take a look at the WC docs:
https://docs.walletconnect.org/quick-start/dapps/client#send-transaction-eth_sendtransaction

A bit edited my code*

fyi, I get an errorā€¦
the send transaction ā€œfromā€ field must be defined

metamask sucksā€¦ the option to pay never comes up

newest error
Invalid ā€œtoā€ address: undefined must be a valid string.

I add the ā€œtoā€ address, next error is
Error: execution reverted

itā€™s just not working

walletconnect is maybe to experimental

please share the code :raised_hands:

got it to work by removing ā€œtransactionParametersā€
I confirmed the transaction on mobile, but I canā€™t get the transaction id to send back to the front end from the service, showing the transaction was successful

    const txt = await web3.eth.sendTransaction({
      from:address,
      to:contract,
      data:encodedFunction
    });

    // console.log(contract)
    resolve(txt)

Iā€™m a bit confused why do you use resolve here.

Could you try this?

await web3.eth.sendTransaction({
      from:address,
      to:contract,
      data:encodedFunction
    }).then((result) => {
    // Returns transaction id (hash)
    console.log(result);
  })
  .catch((error) => {
    // Error returned when rejected
    console.error(error);
  });

Full code, this is called from a service, response sent to the front end letting the user know it was successful or not

     public MINT(_uri,address): Promise<string>{
     return new Promise(async(resolve,reject)=>{
     try {

    web3 = await Moralis.enable({provider:'walletconnect'})
    //const URI:any = web3.utils.asciiToHex(_uri)
    const encodedFunction= web3.eth.abi.encodeFunctionCall({
      name:"mint",
      type:"function",
      inputs:[{
        type:'address',
        name:'account'
      },{
        type:'uint256',
        name:'amount'
      },{
        type:'bytes',
        name:'data'
      }
    ]
  },[address,1,web3.utils.asciiToHex(_uri)])

    const txt = await web3.eth.sendTransaction({
      from:address,
      to:contract,
      data:encodedFunction
    }).then((result) => {
        // Returns transaction id (hash)
        console.log(result);
        resolve(result)

      })

  } catch (error) {
    console.log(error)
    resolve(error)
  }

})
}

itā€™s not returning the transaction IDā€¦ iā€™m using metamask

this worked

    const txt = await web3.eth.sendTransaction({
      from:address,
      to:contract,
      data:encodedFunction
    }).on('transactionHash',(hash)=>{
          resolve(hash)
          // console.log(hash)
      })
      .on('receipt',(receipt)=>{
         console.log(receipt)
      })
      .on('confirmation',(confirmationNumber, receipt)=>
      {
      console.log(confirmationNumber, eceipt)
      }).on('error', console.error);
1 Like

Nice job @blockchainvic

So the problem is solved?