Skip to content

Commit

Permalink
Add connect to TrustWallet browser extension
Browse files Browse the repository at this point in the history
  • Loading branch information
ccali11 committed May 16, 2023
1 parent 5667c50 commit 99c9c0f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
10 changes: 10 additions & 0 deletions apps/web/src/components/Wallet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<div>
<h5>Connect Wallet to Log In or Add Account</h5>
<div>
<button
class="trust-btn"
@click="selectProvider('TrustWallet')"
>
Select Trust Wallet
</button>
<button @click="selectProvider('MetaMask')">
Select MetaMask
</button>
Expand Down Expand Up @@ -292,6 +298,10 @@ button {
background-color: blue;
}
.trust-btn {
background-color: rgb(61, 61, 228);
}
.iopay-btn {
background-color: rgb(0, 218, 180);
}
Expand Down
22 changes: 21 additions & 1 deletion apps/web/src/composables/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ethereum: any = window.ethereum
const availableProviders = ref<BrowserProviders>(getBrowserProviders(ethereum))

export default function useEthers() {
const ethersProviderList = ['MetaMask', 'CoinbaseWallet']
const ethersProviderList = ['CoinbaseWallet', 'MetaMask' /*, 'TrustWallet' */]

async function addEthersNetwork (providerString: ProviderString, network: any) {
const provider = availableProviders.value[providerString as keyof BrowserProviders]
Expand Down Expand Up @@ -118,6 +118,25 @@ export default function useEthers() {
}
}

async function getTrustWalletAddressWithBalance() {
const trustWalletProvider = detectTrustWalletAndGetProvider()
if (trustWalletProvider) {
const address = (await requestEthersAccount(trustWalletProvider))[0]
const balance = await getEthersBalance(address)
return [{ address, balance }]
}
}

function detectTrustWalletAndGetProvider() {
const { ethereum } = window
const providers = ethereum?.providers
if (providers) {
for (const provider of providers) {
if (provider.isTrustWallet) return provider
}
}
}

async function getEthersBalance(address: string, ) {
const provider = new ethers.providers.JsonRpcProvider(ethereumURL)
const balance = await provider.getBalance(address)
Expand Down Expand Up @@ -258,6 +277,7 @@ export default function useEthers() {
getEthersBrowserSigner,
getEthersBrowserProviderSelectedCurrency,
getGasPriceAndLimit,
getTrustWalletAddressWithBalance,
loginWithEthers,
signEthersMessage,
sendEthersTransaction,
Expand Down
6 changes: 5 additions & 1 deletion apps/web/src/composables/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const selectedCurrency = ref<Currency>('')
const toAddress = ref<string>('0x728474D29c2F81eb17a669a7582A2C17f1042b57')

export default function useWallet() {
const { estimateEIP1559GasFee, ethersProviderList, getEthersAddress, getEthersAddressWithBalance, getEthersBalance, sendEthersTransaction, signEthersMessage, loginWithEthers, getEthersBrowserProviderSelectedCurrency, switchEthersNetwork } = useEthers()
const { estimateEIP1559GasFee, ethersProviderList, getEthersAddress, getEthersAddressWithBalance, getEthersBalance, getTrustWalletAddressWithBalance, sendEthersTransaction, signEthersMessage, loginWithEthers, getEthersBrowserProviderSelectedCurrency, switchEthersNetwork } = useEthers()
const { solanaProviderList, getSolanaAddress, sendSolanaTransaction, signSolanaMessage } = useSolana()
const { getLedgerAddress, loginWithLedger, sendLedgerTransaction, signLedgerMessage } = useLedger()
const { getTrezorAddress, loginWithTrezor, sendTrezorTransaction, signTrezorMessage } = useTrezor()
Expand Down Expand Up @@ -324,6 +324,10 @@ export default function useWallet() {
setSelectedProvider(provider)
const trezorAddresses = await getTrezorAddress[currency]() as CryptoAddress[]
setUserAddresses(trezorAddresses)
} else if (provider === 'TrustWallet') {
setSelectedProvider(provider)
const trustWalletAddresses = await getTrustWalletAddressWithBalance() as CryptoAddress[]
setUserAddresses(trustWalletAddresses)
}
} catch (error) {
console.error('There was an error in selectProvider :>> ', error)
Expand Down
1 change: 1 addition & 0 deletions common/types/src/interfaces/ProviderString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type ProviderString =
| 'IoPay'
| 'Ledger'
| 'Trezor'
| 'TrustWallet'
| 'WalletConnect'
| 'Phantom'
| ''

0 comments on commit 99c9c0f

Please sign in to comment.