Skip to content

Commit

Permalink
Handle Tally provider type (#67)
Browse files Browse the repository at this point in the history
* handle tally provider type

* Fix error handling issues - Issue #65

* Fix error handling issues - get Accounts - Issue #65

* Provide user warning when clicking Metamask and Tally is set to default

* fix translations

* Check translations issue

* fix translations

* fix translations

* Align modal text

* Align modal text

* Align modal text

* Align modal text

* Fix resfresh issue and wallet auto opening issue

* Remove redundant code

* Use chosen provider storage
  • Loading branch information
0xzoz authored Apr 1, 2022
1 parent 75fa6c7 commit e07cd9f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 27 deletions.
16 changes: 16 additions & 0 deletions sample-app/src/elm/Strings/Translations.elm
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,35 @@ unlock_tally_wallet lang =
En ->
"Unlock Tally Wallet"


decline_unlock_tally_wallet : Lang -> String
decline_unlock_tally_wallet lang =
case lang of
En ->
"We have detected that you have MetaMask installed."


click_tally_extension : Lang -> String
click_tally_extension lang =
case lang of
En ->
"Tip: If you would prefer to use Tally instead, please download Tally or ensure it is set to default"


decline_unlock_mm_wallet : Lang -> String
decline_unlock_mm_wallet lang =
case lang of
En ->
"We have detected that you have Tally installed."


click_mm_extension : Lang -> String
click_mm_extension lang =
case lang of
En ->
"Tip: If you would prefer to use Metamask instead, please ensure Tally is not set as default"


click_extension : Lang -> String
click_extension lang =
case lang of
Expand Down
18 changes: 14 additions & 4 deletions src/elm/CompoundComponents/Eth/ConnectedEthWallet.elm
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,16 @@ connectingModal userLanguage maybeSelectedProvider ({ chooseWalletState } as mod
)

_ ->
( Translations.unlock_wallet userLanguage
, Translations.click_extension userLanguage
)
if model.providerType == EthProviderInfo.Tally then
( Translations.decline_unlock_mm_wallet userLanguage
, Translations.click_mm_extension userLanguage
)

else
( Translations.unlock_wallet userLanguage
, Translations.click_extension userLanguage
)


showBorderClass =
if isCompoundChain then
Expand All @@ -659,7 +666,7 @@ connectingModal userLanguage maybeSelectedProvider ({ chooseWalletState } as mod
[ inCopyBackArrow isCompoundChain
, markSpan isCompoundChain
, h4 [] [ text headerText ]
, p [] [ text instructionsText ]
, p [ class "center-text"] [ text instructionsText ]
, div [ class "connecting-ring" ]
[ div [] [] ]
, termsView userLanguage isCompoundChain
Expand Down Expand Up @@ -990,6 +997,9 @@ providerTypeFromId id =
4 ->
Just WalletConnect

5 ->
Just Tally

_ ->
Nothing

Expand Down
4 changes: 2 additions & 2 deletions src/js/sharedEth/connectedWalletPorts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { shouldAutoConnect } from './utils';
import { providerTypeId, shouldAutoConnect } from './utils';
import { getAccounts, getLedgerAddressAndBalance, getNetworkId, makeEth, setNetworkId } from './eth';
import {
connectLedger,
Expand Down Expand Up @@ -158,7 +158,7 @@ function subscribeToTryConnect(app, eth, globEthereum, defaultNetworkId) {

// We'll try to set to the user's last chosen provider, otherwise
// defaulting to Web3.
let providerType = Number(storage('chosenProvider').get(PROVIDER_TYPE_WEB3));
let providerType = Number(storage('chosenProvider').get(providerTypeId(globEthereum)));
let connected = await connectToTrxProvider(app, eth, globEthereum, providerType, '', true);

if (!connected) {
Expand Down
12 changes: 9 additions & 3 deletions src/js/sharedEth/connectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ async function connectWalletLink(eth, disallowAuthDialog = false) {
}

async function requiresAuthDialog(ethereum) {
let [account, _] = await new Eth(ethereum).getAccounts();

return !account;
try{
let [account, _] = await new Eth(ethereum).getAccounts();
return !account;

}catch(e){
console.log(e);
return true;

}
}

async function connectWeb3(eth, ethereum, disallowAuthDialog = false, isAutoConnect = false) {
Expand Down
50 changes: 34 additions & 16 deletions src/js/sharedEth/eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,16 @@ function withWeb3Eth(eth) {
}

function withTrxWeb3(eth, fnTrxWeb3, fnEls) {
if (eth.trxEth) {
let res = fnTrxWeb3(eth.trxEth, eth.trxEth.trxPromise);
eth.trxEth.trxPromise = res;
return res;
} else {
return fnEls();
try{
if (eth.trxEth) {
let res = fnTrxWeb3(eth.trxEth, eth.trxEth.trxPromise);
eth.trxEth.trxPromise = res;
return res;
} else {
return fnEls();
}
}catch(e){
console.log(e);
}
}

Expand Down Expand Up @@ -463,11 +467,18 @@ function setNetworkId(eth, networkId) {
}

async function getNetworkId(eth) {
return withTrxWeb3(
eth,
(trxEth) => trxEth.net.getId(),
() => eth.defaultNetworkId
);
try{
let networkId = await withTrxWeb3(
eth,
(trxEth) => trxEth.net.getId(),
() => eth.defaultNetworkId
);
return networkId;
}catch(e){
console.log(e)
return eth.defaultNetworkId;

}
}

async function getBalance(eth, address) {
Expand All @@ -483,11 +494,18 @@ async function getBlockNumber(eth) {
}

async function getAccounts(eth) {
return withTrxWeb3(
eth,
(trxEth) => trxEth.getAccounts(),
() => (eth.showAccount ? [eth.showAccount] : [])
);
try{
let accs = await withTrxWeb3(
eth,
(trxEth) => trxEth.getAccounts(),
() => (eth.showAccount ? [eth.showAccount] : [])
)
return accs;

}catch(e){
console.log(e)
return [];
}
}

async function getTransaction(eth, trxHash) {
Expand Down
13 changes: 11 additions & 2 deletions src/js/sharedEth/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const PROVIDER_TYPE_META_MASK = 'meta_mask';
const PROVIDER_TYPE_META_MASK_MOBILE = 'meta_mask_mobile';
const PROVIDER_TYPE_OTHER = 'other';

const NON_AUTOCONNECT_PROVIDERS = [PROVIDER_TYPE_NONE, PROVIDER_TYPE_META_MASK];
const NON_AUTOCONNECT_PROVIDERS = [PROVIDER_TYPE_NONE, PROVIDER_TYPE_META_MASK, PROVIDER_TYPE_TALLY];

function reverseObject(obj) {
return Object.keys(obj).reduce((acc, key) => {
Expand Down Expand Up @@ -61,6 +61,14 @@ function providerType(provider) {
}
}

function providerTypeId(provider) {
if (provider.isTally) {
return 5;
} else {
return 3;
}
}

function networkFromId(id) {
switch (id) {
case 0:
Expand Down Expand Up @@ -143,6 +151,7 @@ export {
log,
networkFromId,
providerType,
providerTypeId,
PROVIDER_TYPE_NONE,
PROVIDER_TYPE_COINBASE_WALLET,
PROVIDER_TYPE_IM_TOKEN,
Expand All @@ -152,4 +161,4 @@ export {
shouldAutoConnect,
supportFromEntries,
langFromURL,
};
};

0 comments on commit e07cd9f

Please sign in to comment.