Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
fix: re-allow unknown tokens to be queried in the swap/quote endpoint (
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Agosti authored May 18, 2020
1 parent 84f7da0 commit 1379e63
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/utils/token_metadata_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ChainId, TokenMetadata } from '../types';
*/
export function getTokenMetadataIfExists(tokenAddressOrSymbol: string, chainId: ChainId): TokenMetadata | undefined {
let entry: TokenMetadataAndChainAddresses | undefined;
if (tokenAddressOrSymbol.startsWith('0x') && tokenAddressOrSymbol.length === ADDRESS_HEX_LENGTH) {
if (isTokenAddress(tokenAddressOrSymbol)) {
entry = TokenMetadatasForChains.find(
tm => tm.tokenAddresses[chainId].toLowerCase() === tokenAddressOrSymbol.toLowerCase(),
);
Expand Down Expand Up @@ -59,6 +59,9 @@ export function isWETHSymbolOrAddress(tokenAddressOrSymbol: string, chainId: num
* @param chainId the Network where the address should be hosted on.
*/
export function findTokenAddressOrThrow(symbolOrAddress: string, chainId: ChainId): string {
if (isTokenAddress(symbolOrAddress)) {
return symbolOrAddress;
}
const entry = getTokenMetadataIfExists(symbolOrAddress, chainId);
if (!entry) {
// NOTE(jalextowle): Use the original symbol to increase readability.
Expand All @@ -67,6 +70,15 @@ export function findTokenAddressOrThrow(symbolOrAddress: string, chainId: ChainI
return entry.tokenAddress;
}

/**
* Returns whether a string is an address or not.
*
* @param symbolOrAddress the uppercase symbol of the token (ex. `REP`) or the address of the contract
*/
export function isTokenAddress(symbolOrAddress: string): boolean {
return symbolOrAddress.startsWith('0x') && symbolOrAddress.length === ADDRESS_HEX_LENGTH;
}

/**
* Attempts to find the address of the token and throws if not found
*
Expand Down
2 changes: 2 additions & 0 deletions test/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export const ZRX_TOKEN_ADDRESS = CONTRACT_ADDRESSES.zrxToken;
export const WETH_TOKEN_ADDRESS = CONTRACT_ADDRESSES.etherToken;
export const ZRX_ASSET_DATA = assetDataUtils.encodeERC20AssetData(ZRX_TOKEN_ADDRESS);
export const WETH_ASSET_DATA = assetDataUtils.encodeERC20AssetData(WETH_TOKEN_ADDRESS);
export const UNKNOWN_TOKEN_ADDRESS = '0xbe0037eaf2d64fe5529bca93c18c9702d3930376';
export const UNKNOWN_TOKEN_ASSET_DATA = assetDataUtils.encodeERC20AssetData(UNKNOWN_TOKEN_ADDRESS);
export const SYMBOL_TO_ADDRESS: ObjectMap<string> = {
ZRX: ZRX_TOKEN_ADDRESS,
WETH: WETH_TOKEN_ADDRESS,
Expand Down
9 changes: 9 additions & 0 deletions test/swap_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
CONTRACT_ADDRESSES,
MAX_MINT_AMOUNT,
SYMBOL_TO_ADDRESS,
UNKNOWN_TOKEN_ADDRESS,
UNKNOWN_TOKEN_ASSET_DATA,
WETH_ASSET_DATA,
WETH_TOKEN_ADDRESS,
ZRX_ASSET_DATA,
Expand Down Expand Up @@ -92,6 +94,12 @@ describe(SUITE_NAME, () => {
makerAssetAmount: MAKER_WETH_AMOUNT,
takerAssetAmount: ONE_THOUSAND_IN_BASE,
},
{
makerAssetData: ZRX_ASSET_DATA,
takerAssetData: UNKNOWN_TOKEN_ASSET_DATA,
makerAssetAmount: ONE_THOUSAND_IN_BASE,
takerAssetAmount: ONE_THOUSAND_IN_BASE,
},
]);
});
after(async () => {
Expand Down Expand Up @@ -121,6 +129,7 @@ describe(SUITE_NAME, () => {
{ buyToken: 'WETH', sellToken: 'ZRX', buyAmount: '1000' },
{ buyToken: ZRX_TOKEN_ADDRESS, sellToken: 'WETH', buyAmount: '1000' },
{ buyToken: ZRX_TOKEN_ADDRESS, sellToken: WETH_TOKEN_ADDRESS, buyAmount: '1000' },
{ buyToken: 'ZRX', sellToken: UNKNOWN_TOKEN_ADDRESS, buyAmount: '1000' },
];
for (const parameters of parameterPermutations) {
it(`should return a valid quote with ${JSON.stringify(parameters)}`, async () => {
Expand Down

0 comments on commit 1379e63

Please sign in to comment.