Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Promote a closure to a function
Browse files Browse the repository at this point in the history
Addresses review comment #2541 (comment)
  • Loading branch information
feuGeneA committed Apr 9, 2020
1 parent 63bfd23 commit 3f5ceb2
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions packages/asset-swapper/src/utils/quote_requestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ import { MarketOperation } from '../types';
/**
* Request quotes from RFQ-T providers
*/

function getTokenAddressOrThrow(assetData: string): string {
const decodedAssetData = assetDataUtils.decodeAssetDataOrThrow(assetData);
if (decodedAssetData.hasOwnProperty('tokenAddress')) {
// type cast necessary here as decodeAssetDataOrThrow returns
// an AssetData object, which doesn't necessarily contain a
// token address. (it could possibly be a StaticCallAssetData,
// which lacks an address.) so we'll just assume it's a token
// here. should be safe, with the enclosing guard condition
// and subsequent error.
// tslint:disable-next-line:no-unnecessary-type-assertion
return (decodedAssetData as ERC20AssetData).tokenAddress;
} else {
throw new Error(
`Decoded asset data (${JSON.stringify(decodedAssetData)}) does not contain a token address`,
);
}
}

export class QuoteRequestor {
private readonly _rfqtMakerEndpoints: string[];

Expand All @@ -26,24 +45,6 @@ export class QuoteRequestor {
): Promise<SignedOrder[]> {
const makerEndpointMaxResponseTimeMs = 1000;

const getTokenAddressOrThrow = (assetData: string): string => {
const decodedAssetData = assetDataUtils.decodeAssetDataOrThrow(assetData);
if (decodedAssetData.hasOwnProperty('tokenAddress')) {
// type cast necessary here as decodeAssetDataOrThrow returns
// an AssetData object, which doesn't necessarily contain a
// token address. (it could possibly be a StaticCallAssetData,
// which lacks an address.) so we'll just assume it's a token
// here. should be safe, with the enclosing guard condition
// and subsequent error.
// tslint:disable-next-line:no-unnecessary-type-assertion
return (decodedAssetData as ERC20AssetData).tokenAddress;
} else {
throw new Error(
`Decoded asset data (${JSON.stringify(decodedAssetData)}) does not contain a token address`,
);
}
};

const buyToken = getTokenAddressOrThrow(makerAssetData);
const sellToken = getTokenAddressOrThrow(takerAssetData);

Expand Down

0 comments on commit 3f5ceb2

Please sign in to comment.