Skip to content

Commit

Permalink
feat: get decimals for trade from erc20 repo
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Nov 26, 2024
1 parent 7a0b81b commit 50de46d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
4 changes: 0 additions & 4 deletions apps/api/src/app/routes/trading/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ fetch('http://127.0.0.1:8080/trading/getQuote', {method: 'POST', headers: {'Cont
kind: 'sell',
sellToken: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
buyToken: "0xdef1ca1fb7fbcdc777520aa7f396b4e015f497ab",
sellTokenDecimals: 18,
buyTokenDecimals: 18,
amount: '12000000000000000'
}
})})
Expand All @@ -32,8 +30,6 @@ fetch('http://127.0.0.1:8080/trading/getQuote', {method: 'POST', headers: {'Cont
kind: 'sell',
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
sellTokenDecimals: 18,
buyTokenDecimals: 18,
amount: '100000000000000000'
}

Expand Down
13 changes: 12 additions & 1 deletion apps/api/src/app/routes/trading/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { JSONSchema } from 'json-schema-to-ts';

import { omit } from '@cowprotocol/shared';

import QuoterParametersSchema from '../../../tradingSchemas/QuoterParameters';
import TradeParametersSchema from '../../../tradingSchemas/TradeParameters';
import QuoteResultsSchema from '../../../tradingSchemas/QuoteResultsSerialized';
Expand All @@ -10,7 +12,16 @@ export const getQuoteBodySchema = {
additionalProperties: false,
properties: {
trader: QuoterParametersSchema,
params: TradeParametersSchema
params: {
...TradeParametersSchema,
properties: omit(TradeParametersSchema.properties, ['sellTokenDecimals', 'buyTokenDecimals']),
required: [
'amount',
'kind',
'sellToken',
'buyToken',
]
}
}
} as const satisfies JSONSchema;

Expand Down
2 changes: 1 addition & 1 deletion libs/repositories/src/const.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from 'bignumber.js';
import { SupportedChainId } from '../../shared/src/types';
import { SupportedChainId } from '@cowprotocol/shared';

interface TokenAddressAndDecimals {
address: string;
Expand Down
24 changes: 21 additions & 3 deletions libs/services/src/TradingService/TradingService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectable } from 'inversify';
import { inject, injectable } from 'inversify';
import {
getQuote,
TradeParameters,
Expand All @@ -8,6 +8,7 @@ import {
SupportedChainId,
CowEnv
} from '@cowprotocol/cow-sdk';
import { Erc20Repository, erc20RepositorySymbol } from '@cowprotocol/repositories';

export const tradingServiceSymbol = Symbol.for('TradingServiceSymbol');

Expand All @@ -19,13 +20,30 @@ interface TraderParams {

@injectable()
export class TradingService {
constructor(
@inject(erc20RepositorySymbol)
private erc20Repository: Erc20Repository
) {
}

async getQuote(
trader: Parameters<typeof getQuote>[1],
params: TradeParameters,
params: Omit<TradeParameters, 'sellTokenDecimals' | 'buyTokenDecimals'>,
advancedSettings?: SwapAdvancedSettings
): Promise<QuoteResults> {
return getQuote(params, trader, advancedSettings).then(({result}) => result);
const chainId = trader.chainId as number
const sellToken = await this.erc20Repository.get(chainId, params.sellToken);
const buyToken = await this.erc20Repository.get(chainId, params.buyToken);

if (typeof sellToken?.decimals !== 'number' || typeof buyToken?.decimals !== 'number') {
throw new Error('[TradingService.getQuote] Cannot find tokens decimals')
}

const sellTokenDecimals = sellToken.decimals
const buyTokenDecimals = buyToken.decimals

return getQuote({ ...params, sellTokenDecimals, buyTokenDecimals }, trader, advancedSettings)
.then(({result}) => result);
}

async postOrder(
Expand Down
11 changes: 11 additions & 0 deletions libs/shared/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ export function toSupportedChainId(chain: string | number): SupportedChainId {

return chain;
}


export function omit<T extends object, K extends keyof T>(object: T, omitKeys: K[]): Omit<T, K> {
const result = { ...object };

for (const key of omitKeys) {
delete result[key];
}

return result;
}

0 comments on commit 50de46d

Please sign in to comment.