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?