-
Notifications
You must be signed in to change notification settings - Fork 28
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
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,5 +31,6 @@ declare global { | |
tally: any; | ||
bitkeep: any; | ||
mytonwallet: any; | ||
TronLinkEVM: any; | ||
} | ||
} |
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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; | ||||||
} |
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'; | ||
|
||
/* | ||
|
@@ -23,27 +30,63 @@ export const config = { | |
defaultNetwork: Networks.TRON, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default network is |
||
}; | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this line need as long as you have a |
||
.find((blockchain) => blockchain.name === Networks.ETHEREUM)?.chainId; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 && | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
|
@@ -79,6 +122,6 @@ export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = ( | |
DEFAULT: 'https://www.tronlink.org', | ||
}, | ||
color: '#96e7ed', | ||
supportedChains: tron, | ||
supportedChains: [...evms, ...tron], | ||
}; | ||
}; |
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; | ||
} |
There was a problem hiding this comment.
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