Skip to content

Commit

Permalink
fixes to allow same contractAddress on diff chainId/blockchains (#647)
Browse files Browse the repository at this point in the history
* add findTokenByAddressInCurrency and deprecate findTokenByAddress

* fixes test

* erc20 signature to resolve by contract AND chainId

* remove legacy doc of migrate_eth_app

* Remove invalid tests

* add a real world test with a paraswap of 1INCH & fixed sig

* update doc
  • Loading branch information
gre authored Aug 16, 2021
1 parent ce1e4fd commit 7ad8be3
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 199 deletions.
59 changes: 0 additions & 59 deletions docs/migrate_eth_app.md

This file was deleted.

16 changes: 3 additions & 13 deletions packages/cryptoassets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ There are two modes of usage of this library.
- [Parameters](#parameters-17)
- [findTokenById](#findtokenbyid)
- [Parameters](#parameters-18)
- [findTokenByAddress](#findtokenbyaddress)
- [Parameters](#parameters-19)
- [hasTokenId](#hastokenid)
- [Parameters](#parameters-20)
- [Parameters](#parameters-19)
- [getTokenById](#gettokenbyid)
- [Parameters](#parameters-21)
- [Parameters](#parameters-20)
- [findCompoundToken](#findcompoundtoken)
- [Parameters](#parameters-22)
- [Parameters](#parameters-21)
- [Unit](#unit)
- [Properties](#properties)
- [CurrencyCommon](#currencycommon)
Expand Down Expand Up @@ -244,14 +242,6 @@ Returns **([TokenCurrency](#tokencurrency) | null | [undefined](https://develope

Returns **([TokenCurrency](#tokencurrency) | null | [undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined))**

### findTokenByAddress

#### Parameters

- `address` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**

Returns **([TokenCurrency](#tokencurrency) | null | [undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined))**

### hasTokenId

#### Parameters
Expand Down
32 changes: 31 additions & 1 deletion packages/cryptoassets/src/currencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
getFiatCurrencyByTicker,
hasFiatCurrencyTicker,
} from "./fiats";
import { listTokens, findTokenById } from "./tokens";
import {
listTokens,
findTokenById,
findTokenByAddressInCurrency,
} from "./tokens";
import {
listCryptoCurrencies,
hasCryptoCurrencyId,
Expand Down Expand Up @@ -150,6 +154,32 @@ test("tokens are correct", () => {
}
});

test("findTokenByAddressInCurrency", () => {
expect(
findTokenByAddressInCurrency(
"0x111111111117dC0aa78b770fA6A738034120C302",
"bsc"
)
).toMatchObject({
id: "bsc/bep20/1inch_token",
});
expect(
findTokenByAddressInCurrency(
"0x111111111117dC0aa78b770fA6A738034120C302",
"ethereum"
)
).toMatchObject({
id: "ethereum/erc20/1inch_token",
});
expect(findTokenByAddressInCurrency("0x0", "bsc")).toBe(undefined);
expect(
findTokenByAddressInCurrency(
"0x111111111117dC0aa78b770fA6A738034120C302",
"tron"
)
).toBe(undefined);
});

test("fiats list is sorted by ticker", () => {
expect(
listFiatCurrencies()
Expand Down
22 changes: 18 additions & 4 deletions packages/cryptoassets/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const tokensByCryptoCurrencyWithDelisted: Record<string, TokenCurrency[]> = {};
const tokensById: Record<string, TokenCurrency> = {};
const tokensByTicker: Record<string, TokenCurrency> = {};
const tokensByAddress: Record<string, TokenCurrency> = {};
const tokensByCurrencyAddress: Record<string, TokenCurrency> = {};
addTokens(erc20tokens.map(convertERC20));
addTokens(trc10tokens.map(convertTRONTokens("trc10")));
addTokens(trc20tokens.map(convertTRONTokens("trc20")));
Expand Down Expand Up @@ -84,15 +85,26 @@ export function findTokenById(id: string): TokenCurrency | null | undefined {
return tokensById[id];
}

/**
*
*/
let deprecatedDisplayed = false;
export function findTokenByAddress(
address: string
): TokenCurrency | null | undefined {
if (!deprecatedDisplayed) {
deprecatedDisplayed = true;
console.warn(
"findTokenByAddress is deprecated. use findTokenByAddressInCurrency"
);
}
return tokensByAddress[address.toLowerCase()];
}

export function findTokenByAddressInCurrency(
address: string,
currencyId: string
): TokenCurrency | null | undefined {
return tokensByCurrencyAddress[currencyId + ":" + address.toLowerCase()];
}

/**
*
*/
Expand Down Expand Up @@ -141,8 +153,10 @@ function addTokens(list: TokenCurrency[]) {
tokensByTicker[token.ticker] = token;
}

tokensByAddress[token.contractAddress.toLowerCase()] = token;
const lowCaseContract = token.contractAddress.toLowerCase();
tokensByAddress[lowCaseContract] = token;
const { parentCurrency } = token;
tokensByCurrencyAddress[parentCurrency.id + ":" + lowCaseContract] = token;

if (!(parentCurrency.id in tokensByCryptoCurrency)) {
tokensByCryptoCurrency[parentCurrency.id] = [];
Expand Down
9 changes: 5 additions & 4 deletions packages/hw-app-eth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Ledger Hardware Wallet ETH JavaScript bindings.

- [loadInfosForContractMethod](#loadinfosforcontractmethod)
- [Parameters](#parameters)
- [byContractAddress](#bycontractaddress)
- [byContractAddressAndChainId](#bycontractaddressandchainid)
- [Parameters](#parameters-1)
- [list](#list)
- [Eth](#eth)
Expand Down Expand Up @@ -72,13 +72,14 @@ Retrieve the metadatas a given contract address and a method selector

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;(ContractMethod | [undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined))>**

### byContractAddress
### byContractAddressAndChainId

Retrieve the token information by a given contract address if any

#### Parameters

- `contract` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `chainId` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**

Returns **(TokenInfo | null | [undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined))**

Expand Down Expand Up @@ -138,8 +139,8 @@ calling this contract address to display the proper token information to the use
##### Examples

```javascript
import { byContractAddress } from "@ledgerhq/hw-app-eth/erc20"
const zrxInfo = byContractAddress("0xe41d2489571d322189246dafa5ebde1f4699f498")
import { byContractAddressAndChainId } from "@ledgerhq/hw-app-eth/erc20"
const zrxInfo = byContractAddressAndChainId("0xe41d2489571d322189246dafa5ebde1f4699f498", chainId)
if (zrxInfo) await appEth.provideERC20TokenInformation(zrxInfo)
const signed = await appEth.signTransaction(path, rawTxHex)
```
Expand Down
8 changes: 4 additions & 4 deletions packages/hw-app-eth/src/Eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { EthAppPleaseEnableContractData } from "@ledgerhq/errors";
import type Transport from "@ledgerhq/hw-transport";
import { BigNumber } from "bignumber.js";
import { ethers } from "ethers";
import { byContractAddress } from "./erc20";
import { byContractAddressAndChainId } from "./erc20";
import { loadInfosForContractMethod } from "./contracts";

export type StarkQuantizationType =
Expand Down Expand Up @@ -164,8 +164,8 @@ export default class Eth {
* @param {*} info: a blob from "erc20.js" utilities that contains all token information.
*
* @example
* import { byContractAddress } from "@ledgerhq/hw-app-eth/erc20"
* const zrxInfo = byContractAddress("0xe41d2489571d322189246dafa5ebde1f4699f498")
* import { byContractAddressAndChainId } from "@ledgerhq/hw-app-eth/erc20"
* const zrxInfo = byContractAddressAndChainId("0xe41d2489571d322189246dafa5ebde1f4699f498", chainId)
* if (zrxInfo) await appEth.provideERC20TokenInformation(zrxInfo)
* const signed = await appEth.signTransaction(path, rawTxHex)
*/
Expand Down Expand Up @@ -290,7 +290,7 @@ export default class Eth {
};
}
const provideForContract = async (address) => {
const erc20Info = byContractAddress(address);
const erc20Info = byContractAddressAndChainId(address, chainIdTruncated);
if (erc20Info) {
log(
"ethereum",
Expand Down
19 changes: 12 additions & 7 deletions packages/hw-app-eth/src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import blob from "@ledgerhq/cryptoassets/data/erc20-signatures";
/**
* Retrieve the token information by a given contract address if any
*/
export const byContractAddress = (
contract: string
export const byContractAddressAndChainId = (
contract: string,
chainId: number
): TokenInfo | null | undefined =>
get().byContract(asContractAddress(contract));
get().byContractAndChainId(asContractAddress(contract), chainId);

/**
* list all the ERC20 tokens informations
Expand All @@ -21,7 +22,10 @@ export type TokenInfo = {
data: Buffer;
};
export type API = {
byContract: (arg0: string) => TokenInfo | null | undefined;
byContractAndChainId: (
addr: string,
id: number
) => TokenInfo | null | undefined;
list: () => TokenInfo[];
};

Expand All @@ -36,7 +40,7 @@ const get: () => API = (() => {
return () => {
if (cache) return cache;
const buf = Buffer.from(blob, "base64");
const byContract = {};
const map = {};
const entries: TokenInfo[] = [];
let i = 0;

Expand Down Expand Up @@ -67,13 +71,14 @@ const get: () => API = (() => {
data: item,
};
entries.push(entry);
byContract[contractAddress] = entry;
map[String(chainId) + ":" + contractAddress] = entry;
i += length;
}

const api = {
list: () => entries,
byContract: (contractAddress) => byContract[contractAddress],
byContractAndChainId: (contractAddress, chainId) =>
map[String(chainId) + ":" + contractAddress],
};
cache = api;
return api;
Expand Down
Loading

0 comments on commit 7ad8be3

Please sign in to comment.