Skip to content

Commit

Permalink
Merge pull request #25 from tronprotocol/release/v1.1.8
Browse files Browse the repository at this point in the history
fix(tronlink): fix TronLink disconnect incorrectly after connection
  • Loading branch information
unicornonea authored Aug 8, 2023
2 parents 6b98b67 + aeb168e commit eaaac80
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tronwallet-adapter",
"description": "wallet adapters for TRON blockchain",
"version": "1.1.7",
"version": "1.1.8",
"main": "index.js",
"sideEffects": false,
"private": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/adapters/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tronweb3/tronwallet-adapters",
"version": "1.1.6",
"version": "1.1.7",
"description": "Wallet adapters to help developers interact with Tron wallets using consistent API.",
"keywords": [
"TRON",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/tronlink/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tronweb3/tronwallet-adapter-tronlink",
"version": "1.1.6",
"version": "1.1.7",
"description": "Wallet adapter for TronLink Wallet extension and TronLink app.",
"keywords": [
"TRON",
Expand Down
7 changes: 4 additions & 3 deletions packages/adapters/tronlink/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,16 @@ export class TronLinkAdapter extends Adapter {
this.emit('chainChanged', data);
};

private _onAccountsChanged: TronAccountsChangedCallback = (data) => {
private _onAccountsChanged: TronAccountsChangedCallback = () => {
const preAddr = this.address || '';
if (!data?.length) {
const curAddr = (this._wallet?.tronWeb && this._wallet?.tronWeb.defaultAddress?.base58) || '';
if (!curAddr) {
// change to a new address and if it's disconnected, data will be empty
// tronlink will emit accountsChanged many times, only process when connected
this.setAddress(null);
this.setState(AdapterState.Disconnect);
} else {
const address = data[0] as string;
const address = curAddr as string;
this.setAddress(address);
this.setState(AdapterState.Connected);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/adapters/tronlink/tests/units/adapter-tron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,23 @@ describe('events should work fine', () => {
adapter.on('connect', _onConnect);
adapter.on('disconnect', _onDisconnect);
adapter.on('accountsChanged', _onAccountsChanged);
tron._setAddress('address2');
tron._emit('accountsChanged', ['address2']);
await wait();
expect(_onConnect).not.toHaveBeenCalled();
expect(_onAccountsChanged).toHaveBeenCalledTimes(1);
expect(_onAccountsChanged).toHaveBeenCalledWith('address2', 'address');
expect(adapter.address).toEqual('address2');

tron._setAddress('address3');
tron._emit('accountsChanged', ['address3']);
await wait();
expect(_onAccountsChanged).toHaveBeenCalledTimes(2);
expect(_onAccountsChanged).toHaveBeenLastCalledWith('address3', 'address2');
expect(_onConnect).not.toHaveBeenCalled();
expect(adapter.address).toEqual('address3');

tron._setAddress('');
tron._emit('accountsChanged', []);
await wait();
expect(adapter.address).toEqual(null);
Expand All @@ -253,6 +256,7 @@ describe('events should work fine', () => {
adapter = new TronLinkAdapter();
const _onConnect = jest.fn();
adapter.on('connect', _onConnect);
tron._setAddress('address2');
tron._emit('accountsChanged', ['address2']);
expect(_onConnect).toHaveBeenCalledTimes(1);
expect(_onConnect).toHaveBeenCalledWith('address2');
Expand All @@ -262,6 +266,7 @@ describe('events should work fine', () => {
tron._setAddress('address');
const _onDisconnect = jest.fn();
adapter.on('disconnect', _onDisconnect);
tron._setAddress('');
tron._emit('accountsChanged', []);
expect(_onDisconnect).toHaveBeenCalledTimes(1);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/react/react-hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tronweb3/tronwallet-adapter-react-hooks",
"version": "1.1.4",
"version": "1.1.5",
"description": "A `useWallet()` hook to make it easy to interact with Tron wallets.",
"keywords": [
"TRON",
Expand Down
30 changes: 15 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eaaac80

Please sign in to comment.