Skip to content

Commit

Permalink
refactor: add generics to quote responses
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsambugs committed Jun 1, 2024
1 parent bf0178e commit ace5fc9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/services/quotes/quote-sources/balmy-quote-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AlwaysValidConfigAndContextSource } from './base/always-valid-source';
import { SourceListQuoteResponse } from '../source-lists/types';
import { StringifyBigInt } from '@utility-types';
import { QuoteTransaction } from '../types';
import { bigintifyTx } from '../source-lists/utils';

export const BALMY_SUPPORTED_CHAINS = [
Chains.ETHEREUM,
Expand Down Expand Up @@ -82,8 +83,7 @@ export class BalmyQuoteSource extends AlwaysValidConfigAndContextSource<BalmySup
estimatedGas,
source: { allowanceTarget },
customData,
}: StringifyBigInt<SourceListQuoteResponse> = await response.json();
const tx = customData.tx as any as StringifyBigInt<QuoteTransaction>;
}: StringifyBigInt<SourceListQuoteResponse<{ tx: QuoteTransaction }>> = await response.json();

return {
sellAmount: BigInt(sellAmount),
Expand All @@ -95,9 +95,9 @@ export class BalmyQuoteSource extends AlwaysValidConfigAndContextSource<BalmySup
type: order.type,
customData: {
tx: {
calldata: tx.data,
to: tx.to,
value: BigInt(tx.value ?? 0),
to: customData.tx.to,
calldata: customData.tx.data,
value: customData.tx.value ? BigInt(customData.tx.value) : undefined,
},
},
};
Expand Down
9 changes: 4 additions & 5 deletions src/services/quotes/quote-sources/sovryn-quote-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ export class SovrynQuoteSource extends AlwaysValidConfigAndContextSource<SovrynS
estimatedGas,
source: { allowanceTarget },
customData,
}: StringifyBigInt<SourceListQuoteResponse> = await response.json();
const tx = customData.tx as any as StringifyBigInt<QuoteTransaction>;
}: StringifyBigInt<SourceListQuoteResponse<{ tx: QuoteTransaction }>> = await response.json();

return {
sellAmount: BigInt(sellAmount),
Expand All @@ -75,9 +74,9 @@ export class SovrynQuoteSource extends AlwaysValidConfigAndContextSource<SovrynS
type: order.type,
customData: {
tx: {
calldata: tx.data,
to: tx.to,
value: BigInt(tx.value ?? 0),
to: customData.tx.to,
calldata: customData.tx.data,
value: customData.tx.value ? BigInt(customData.tx.value) : undefined,
},
},
};
Expand Down
6 changes: 3 additions & 3 deletions src/services/quotes/source-lists/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GasPrice } from '@services/gas/types';
import { BaseTokenMetadata } from '@services/metadata/types';
import { ITriggerablePromise } from '@shared/triggerable-promise';
import { Address, ChainId, TimeString, TokenAddress } from '@types';
import { Address, TimeString } from '@types';
import { QuoteRequest, SourceMetadata, SourceId, QuoteTransaction, QuoteResponse } from '../types';
import { SourceConfig } from '../source-registry';

Expand Down Expand Up @@ -30,7 +30,7 @@ export type SourceListQuoteRequest = Omit<QuoteRequest, 'filters' | 'gasSpeed'>
quoteTimeout?: TimeString;
};

export type SourceListQuoteResponse = {
export type SourceListQuoteResponse<CustomQuoteSourceData extends Record<string, any> = Record<string, any>> = {
sellAmount: bigint;
buyAmount: bigint;
maxSellAmount: bigint;
Expand All @@ -39,5 +39,5 @@ export type SourceListQuoteResponse = {
type: 'sell' | 'buy';
recipient: Address;
source: { id: SourceId; allowanceTarget: Address; name: string; logoURI: string };
customData: Record<string, any>;
customData: CustomQuoteSourceData;
};
4 changes: 2 additions & 2 deletions src/services/quotes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export type QuoteRequest = {
};

type TokenWithOptionalPrice = BaseTokenMetadata & { address: TokenAddress; price?: number };
export type QuoteResponse = {
export type QuoteResponse<CustomQuoteSourceData extends Record<string, any> = Record<string, any>> = {
chainId: ChainId;
sellToken: TokenWithOptionalPrice;
buyToken: TokenWithOptionalPrice;
Expand All @@ -108,7 +108,7 @@ export type QuoteResponse = {
accounts: { takerAddress: Address; recipient: Address };
source: { id: SourceId; allowanceTarget: Address; name: string; logoURI: string };
type: 'sell' | 'buy';
customData: Record<string, any>;
customData: CustomQuoteSourceData;
};

export type QuoteTransaction = BuiltTransaction & { from: Address };
Expand Down

0 comments on commit ace5fc9

Please sign in to comment.