Skip to content

Commit

Permalink
Merge pull request #1381 from emeraldpay/fix/no-weth-on-sepolia
Browse files Browse the repository at this point in the history
problem: no Wrapped Ether on Sepolia
  • Loading branch information
splix authored Apr 30, 2024
2 parents eb4c5fd + c124367 commit 964c856
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/blockchains/tokens/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export class TokenRegistry {
const instances = this.instances.get(blockchain);

if (instances == null) {
throw new Error(`Can't find wrapped token by blockchain ${blockchain}`);
throw new Error(`Can't find tokens by blockchain ${blockchain}`);
}

const instance = instances.get(address.toLowerCase());
Expand Down
9 changes: 9 additions & 0 deletions packages/desktop/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@
"type": "ERC20",
"pinned": true,
"stablecoin": true
},
{
"name": "Wrapped Ether",
"blockchain": 10009,
"address": "0xc31e8a1087bf1460b9926274de4a03b0dd44a6da",
"symbol": "WETS",
"decimals": 18,
"type": "ERC20",
"pinned": true
}
]
}
42 changes: 20 additions & 22 deletions packages/desktop/src/main/utils/update-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,33 @@ const HOST: Readonly<string> =
const log = Logger.forCategory('Store::UpdateTokens');

export default async function (appVersion: string, stored: TokenData[]): Promise<Updated> {
const response = await fetch(`https://${HOST}/tokens-v2.json?ref_app=desktop-wallet&ref_version=${appVersion}`);
let remoteTokens: TokenData[] = [];

const response = await fetch(`https://${HOST}/tokens-v2.json?ref_app=desktop-wallet&ref_version=${appVersion}`);
if (response.status === 200) {
try {
const tokens = ((await response.json()) as TokenData[]).filter((token) => isBlockchainId(token.blockchain));

const addresses = stored.map(({ address }) => address.toLowerCase());

if (tokens.reduce((carry, { address }) => carry || !addresses.includes(address.toLowerCase()), false)) {
const merged: Map<string, TokenData> = [...tokens, ...stored].reduce((carry, token) => {
const key = `${token.blockchain}:${token.address.toLowerCase()}`;

if (carry.has(key)) {
return carry;
}

return carry.set(key, token);
}, new Map());

return { changed: true, tokens: [...merged.values()] };
}
remoteTokens = (await response.json()) as TokenData[];
} catch (exception) {
log.error('Error while parsing tokens update from server:', exception);
}
}

if (stored.length > 0) {
return { changed: false, tokens: stored };
}
// @ts-ignore
const defaultTokens: TokenData[] = defaults;

// merge all sources, because a new token may be added at the remote server, or also added to the defaults, etc.
const merged: Map<string, TokenData> = [...remoteTokens, ...stored, ...defaultTokens]
// make sure it has only token for blockchain that the current app version supports
.filter((token) => isBlockchainId(token.blockchain))
// keep all tokens defined at any of the sources
.reduce((carry, token) => {
const key = `${token.blockchain}:${token.address.toLowerCase()}`;
// this order ensure the priority, the first defined token will be kept
if (carry.has(key)) {
return carry;
}
return carry.set(key, token);
}, new Map());

return { changed: true, tokens: defaults as TokenData[] };
return { changed: true, tokens: [...merged.values()] }
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,24 @@ const EthereumEntryItem: React.FC<DispatchProps & OwnProps & StateProps> = ({

const buttonClasses = { root: styles.button, disabled: styles.buttonDisabled };

let wrapActions = tokenWrappedBalances.map(({ balance: wrappedBalance }) => (
<Tooltip
key={`convert-${wrappedBalance.token.address}`}
title={`Convert between ${balance.units.top.name} and ${wrappedBalance.token.name}`}
>
<span>
<IconButton
classes={buttonClasses}
disabled={balance.isZero()}
size="small"
onClick={() => gotoConvert()}
>
<ConvertIcon fontSize="small" />
</IconButton>
</span>
</Tooltip>
));

return (
<>
<div className={styles.entry}>
Expand All @@ -151,23 +169,7 @@ const EthereumEntryItem: React.FC<DispatchProps & OwnProps & StateProps> = ({
{fiatBalance?.isPositive() && <FiatBalance balance={fiatBalance} />}
</div>
<div className={styles.entryActions}>
{tokenWrappedBalances.map(({ balance: wrappedBalance }) => (
<Tooltip
key={`convert-${wrappedBalance.token.address}`}
title={`Convert between ${balance.units.top.name} and ${wrappedBalance.token.name}`}
>
<span>
<IconButton
classes={buttonClasses}
disabled={balance.isZero()}
size="small"
onClick={() => gotoConvert()}
>
<ConvertIcon fontSize="small" />
</IconButton>
</span>
</Tooltip>
))}
{wrapActions}
<Tooltip title="Send">
<span>
<IconButton classes={buttonClasses} disabled={balance.isZero()} size="small" onClick={() => gotoSend()}>
Expand Down

0 comments on commit 964c856

Please sign in to comment.