tronprotocol / tronwallet-adapter

MIT License
64 stars 29 forks source link

Init connection event sequence exception #24

Closed xiaoai7904 closed 1 year ago

xiaoai7904 commented 1 year ago
image image image image

"@tronweb3/tronwallet-adapter-react-hooks": "^1.1.4", "@tronweb3/tronwallet-adapter-react-ui": "^1.1.5", "@tronweb3/tronwallet-adapters": "^1.1.6"

Clear the cache and initialize the connection exception for the first time The wallet is successfully connected, but the disconnect event will be triggered at the end, and the address will be assigned a null value, resulting in an abnormal page display

pdazcom commented 1 year ago

+1 same problem without using react

way2ex commented 1 year ago

disconnect event is triggered when TronLink emits accountsChanged with empty accounts. It seems that TronLink extension has changed their events. Does the bug occurs in production ? We can publish a hot fix if necessary.

pdazcom commented 1 year ago

yes, the error appeared after updating the tronLink extension. fixes are needed. thank you!

alienzhangyw commented 1 year ago

+1, accountsChanged and disconnect are triggered after connect.

way2ex commented 1 year ago

Sorry for any inconvenience. v1.1.8 has fixed this error.

lytieuphong commented 1 year ago

v1.1.8 still not fixed

way2ex commented 1 year ago

v1.1.8 still not fixed

I have checked the adapter with the latest TronLink extension(v4.1.3) and it work fine. When i first call adapter.connect() , it will not disconnect then. Could you please describe the detail of the bug? Thank you!

lytieuphong commented 1 year ago

Sure, Here is my code: @tronweb3/tronwallet-adapters@1.1.8 tronlink extension v4.1.3

    const tronLinkAdapter = new TronLinkAdapter();
    let address = "";
    let chainId = 0;

    await tronLinkAdapter.connect();

    if (tronLinkAdapter.address) {
      address = tronLinkAdapter.address;
      try {
        const network = await tronLinkAdapter.network(); // call this will throw issue
        chainId = parseInt(network.chainId);
      } catch (e) {
    // catch error here
      }
    }
  1. Connect success
  2. Lock wallet from Tron Link extension
  3. Reload Dapp then click connect to TronLink

Error message: WalletDisconnectedError: The wallet is disconnected. Please connect first.

Adapter object:

{
_readyState: "Found",
_state: "Connected"
}

Note that It just happen for some times, not always.

Thank you

way2ex commented 1 year ago

It seems that TronLink has an issue that when first connect to TronLink, window.tron.tronWeb is not available immediately. setTimeout should work fine. network() method depends window.tron.tronWeb to get network information. So error occurs. image

You can use the following code when tron.tronWeb is available.

 const chainIdNetworkMap = {
    '0x2b6653dc': 'Mainnet',
    '0x94a9059e': 'Shasta',
    '0xcd8690dc': 'Nile',
};

async function getNetworkInfo() {
    const tronWeb = window.tron.tronWeb;
    const { blockID = '' } = await tronWeb.trx.getBlockByNumber(0);
    const chainId = `0x${blockID.slice(-8)}`;
    return {
        networkType: chainIdNetworkMap[chainId],
        chainId,
        fullNode: tronWeb.fullNode?.host || '',
        solidityNode: tronWeb.solidityNode?.host || '',
        eventServer: tronWeb.eventServer?.host || '',
    };
}