Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallets): add evm to tron link #350

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
// "project": ["tsconfig.base.json"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yeager-eren
please check eslint parser issue

"project": true
"project": ["tsconfig.base.json"]
// "project": true
},
"rules": {
"no-extra-boolean-cast": "off",
Expand Down
1 change: 1 addition & 0 deletions global-wallets-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ declare global {
tally: any;
bitkeep: any;
mytonwallet: any;
TronLinkEVM: any;
}
}
27 changes: 23 additions & 4 deletions wallets/provider-tron-link/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
export function tronLink() {
const { tronLink } = window;
if (!!tronLink) return tronLink;
return null;
import type { Network } from '@rango-dev/wallets-shared';

import { Networks } from '@rango-dev/wallets-shared';

type Provider = Map<Network, any>;

export function tronLinkInstances(): Provider | null {
const { tronLink, TronLinkEVM } = window;

if (!tronLink) {
return tronLink;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return tronLink;
return null;

Make it more explicit.

}

const instances: Provider = new Map();

if (TronLinkEVM && TronLinkEVM.isTronLink) {
instances.set(Networks.ETHEREUM, TronLinkEVM);
}

if (tronLink) {
instances.set(Networks.TRON, tronLink);
}
return instances;
}
83 changes: 63 additions & 20 deletions wallets/provider-tron-link/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import type {
CanSwitchNetwork,
Connect,
ProviderConnectResult,
Subscribe,
WalletInfo,
} from '@rango-dev/wallets-shared';
import type { BlockchainMeta, SignerFactory } from 'rango-types';

import { Networks, WalletTypes } from '@rango-dev/wallets-shared';
import { tronBlockchain } from 'rango-types';
import {
canSwitchNetworkToEvm,
chooseInstance,
getEvmAccounts,
Networks,
WalletTypes,
} from '@rango-dev/wallets-shared';
import { evmBlockchains, isEvmBlockchain, tronBlockchain } from 'rango-types';

import { tronLink as tronLink_instance } from './helpers';
import { tronLinkInstances } from './helpers';
import signer from './signer';

/*
Expand All @@ -23,27 +30,63 @@ export const config = {
defaultNetwork: Networks.TRON,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default network is tron, When you are on Ethereum network on TronLink, it goes through an error. How we can fix it?

};

export const getInstance = tronLink_instance;
export const getInstance = tronLinkInstances;

export const connect: Connect = async ({ instance, meta }) => {
const ethInstance = chooseInstance(instance, meta, Networks.ETHEREUM);
const tronInstance = chooseInstance(instance, meta, Networks.TRON);

export const connect: Connect = async ({ instance }) => {
let r = undefined;
if (!!instance && !instance.ready) {
r = await instance.request({ method: 'tron_requestAccounts' });
if (!r) {
const results: ProviderConnectResult[] = [];

if (ethInstance) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It tries to connect to Ethereum and Tron (instance) at the same time but the wallet seems doesn't support that and one network should be connected at the same time.

const evmResult = await getEvmAccounts(ethInstance);
results.push(evmResult);
}

if (tronInstance && !tronInstance.ready) {
const res = await tronInstance.request({ method: 'tron_requestAccounts' });
if (!res) {
throw new Error('Please unlock your TronLink extension first.');
}
if (!!r?.code && !!r.message) {
throw new Error(r.message);
if (!!res?.code && !!res.message) {
throw new Error(res.message);
}

// TODO check connected network
const address = tronInstance.tronWeb.address.fromHex(
(await instance.tronWeb.trx.getAccount()).address.toString()
);
results.push({
accounts: address ? [address] : [],
chainId: Networks.TRON,
});
}
const address = instance.tronWeb.address.fromHex(
(await instance.tronWeb.trx.getAccount()).address.toString()
);
// TODO check connected network
return { accounts: address ? [address] : [], chainId: Networks.TRON };

return results;
};

export const subscribe: Subscribe = ({ updateAccounts, disconnect }) => {
export const subscribe: Subscribe = ({
instance,
state,
updateChainId,
updateAccounts,
meta,
disconnect,
}) => {
const ethInstance = chooseInstance(instance, meta, Networks.ETHEREUM);

ethInstance?.on('accountsChanged', (addresses: string[]) => {
const eth_chainId = meta
.filter(isEvmBlockchain)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line need as long as you have a find?

.find((blockchain) => blockchain.name === Networks.ETHEREUM)?.chainId;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to support for BSC as well?

if (state.connected) {
if (state.network != Networks.ETHEREUM && eth_chainId) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we need this condition here.

updateChainId(eth_chainId);
}
updateAccounts(addresses);
}
});

window.addEventListener('message', (e) => {
if (
e.data.isTronLink &&
Expand All @@ -59,14 +102,14 @@ export const subscribe: Subscribe = ({ updateAccounts, disconnect }) => {
}
});
};

export const canSwitchNetworkTo: CanSwitchNetwork = () => false;
export const canSwitchNetworkTo: CanSwitchNetwork = canSwitchNetworkToEvm;

export const getSigners: (provider: any) => SignerFactory = signer;

export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
allBlockChains
) => {
const evms = evmBlockchains(allBlockChains);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns the whole list evm compatible networks, but we only need BSC and ETH here as the wallet only has support for this.

const tron = tronBlockchain(allBlockChains);
return {
name: 'TronLink',
Expand All @@ -79,6 +122,6 @@ export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
DEFAULT: 'https://www.tronlink.org',
},
color: '#96e7ed',
supportedChains: tron,
supportedChains: [...evms, ...tron],
};
};
15 changes: 9 additions & 6 deletions wallets/provider-tron-link/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { SignerFactory } from 'rango-types';

import { DefaultEvmSigner } from '@rango-dev/signer-evm';
import { DefaultTronSigner } from '@rango-dev/signer-tron';
import {
DefaultSignerFactory,
SignerFactory,
TransactionType as TxType,
} from 'rango-types';
import { getNetworkInstance, Networks } from '@rango-dev/wallets-shared';
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types';

export default function getSigners(provider: any): SignerFactory {
const ethProvider = getNetworkInstance(provider, Networks.ETHEREUM);
const tronProvider = getNetworkInstance(provider, Networks.TRON);
const signers = new DefaultSignerFactory();
signers.registerSigner(TxType.TRON, new DefaultTronSigner(provider));
signers.registerSigner(TxType.EVM, new DefaultEvmSigner(ethProvider));
signers.registerSigner(TxType.TRON, new DefaultTronSigner(tronProvider));
return signers;
}