Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hard quote logic #271

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bin/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export class APIPipeline extends Stack {
synth: synthStep,
});

const urlSecrets = sm.Secret.fromSecretAttributes(this, 'urlSecrets', {
secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:gouda-service-api-xCINOs',
});

const rfqWebhookConfig = sm.Secret.fromSecretAttributes(this, 'RfqConfig', {
secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:rfq-webhook-config-sy04bH',
});
Expand All @@ -117,6 +121,7 @@ export class APIPipeline extends Stack {
stage: STAGE.BETA,
envVars: {
RFQ_WEBHOOK_CONFIG: rfqWebhookConfig.secretValue.toString(),
ORDER_SERVICE_URL: urlSecrets.secretValueFromJson('GOUDA_SERVICE_BETA').toString(),
FILL_LOG_SENDER_ACCOUNT: '321377678687',
ORDER_LOG_SENDER_ACCOUNT: '321377678687',
URA_ACCOUNT: '665191769009',
Expand All @@ -136,6 +141,7 @@ export class APIPipeline extends Stack {
chatbotSNSArn: 'arn:aws:sns:us-east-2:644039819003:SlackChatbotTopic',
envVars: {
RFQ_WEBHOOK_CONFIG: rfqWebhookConfig.secretValue.toString(),
ORDER_SERVICE_URL: urlSecrets.secretValueFromJson('GOUDA_SERVICE_PROD').toString(),
FILL_LOG_SENDER_ACCOUNT: '316116520258',
ORDER_LOG_SENDER_ACCOUNT: '316116520258',
URA_ACCOUNT: '652077092967',
Expand Down
54 changes: 35 additions & 19 deletions lib/entities/HardQuoteRequest.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { TradeType } from '@uniswap/sdk-core';
import { UnsignedV2DutchOrder } from '@uniswap/uniswapx-sdk';
import { BigNumber, utils } from 'ethers';
import { BigNumber, ethers, utils } from 'ethers';

import { HardQuoteRequestBody } from '../handlers/hard-quote';
import { QuoteRequestDataJSON } from '.';
import { QuoteRequest, QuoteRequestDataJSON } from '.';

export class HardQuoteRequest {
public order: UnsignedV2DutchOrder;

public static fromHardRequestBody(_body: HardQuoteRequestBody): HardQuoteRequest {
// TODO: parse hard request into the same V2 request object format
throw new Error('Method not implemented.');
public static fromHardRequestBody(body: HardQuoteRequestBody): HardQuoteRequest {
return new HardQuoteRequest(body);
}

constructor(private data: HardQuoteRequestBody) {
Expand All @@ -21,7 +20,7 @@ export class HardQuoteRequest {
return {
tokenInChainId: this.tokenInChainId,
tokenOutChainId: this.tokenOutChainId,
swapper: utils.getAddress(this.swapper),
swapper: ethers.constants.AddressZero,
requestId: this.requestId,
tokenIn: this.tokenIn,
tokenOut: this.tokenOut,
Expand All @@ -37,21 +36,26 @@ export class HardQuoteRequest {
public toOpposingCleanJSON(): QuoteRequestDataJSON {
const type = this.type === TradeType.EXACT_INPUT ? TradeType.EXACT_OUTPUT : TradeType.EXACT_INPUT;
return {
tokenInChainId: this.tokenInChainId,
tokenOutChainId: this.tokenOutChainId,
requestId: this.requestId,
swapper: utils.getAddress(this.swapper),
...this.toCleanJSON(),
// switch tokenIn/tokenOut
tokenIn: utils.getAddress(this.tokenOut),
tokenOut: utils.getAddress(this.tokenIn),
amount: this.amount.toString(),
// switch tradeType
type: TradeType[type],
numOutputs: this.numOutputs,
...(this.quoteId && { quoteId: this.quoteId }),
};
}

// transforms into a quote request that can be used to query quoters
public toQuoteRequest(): QuoteRequest {
return new QuoteRequest({
...this.toCleanJSON(),
swapper: this.swapper,
amount: this.amount,
type: this.type,
});
}

public get requestId(): string {
return this.data.requestId;
}
Expand All @@ -76,16 +80,24 @@ export class HardQuoteRequest {
return utils.getAddress(this.order.info.baseOutputs[0].token);
}

public get totalOutputAmountStart(): BigNumber {
let amount = BigNumber.from(0);
for (const output of this.order.info.baseOutputs) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this assumes all outputs are same token which is not necessarily true right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assumed true anyways for now in main rfq flownsince quoters can only be for one token

amount = amount.add(output.startAmount);
}

return amount;
}

public get totalInputAmountStart(): BigNumber {
return this.order.info.baseInput.startAmount;
}

public get amount(): BigNumber {
if (this.type === TradeType.EXACT_INPUT) {
return this.order.info.baseInput.startAmount;
return this.totalInputAmountStart;
} else {
const amount = BigNumber.from(0);
for (const output of this.order.info.baseOutputs) {
amount.add(output.startAmount);
}

return amount;
return this.totalOutputAmountStart;
}
}

Expand All @@ -103,6 +115,10 @@ export class HardQuoteRequest {
return this.order.info.cosigner;
}

public get innerSig(): string {
return this.data.innerSig;
}

public get quoteId(): string | undefined {
return this.data.quoteId;
}
Expand Down
96 changes: 96 additions & 0 deletions lib/entities/HardQuoteResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { CosignedV2DutchOrder } from '@uniswap/uniswapx-sdk';
import { BigNumber } from 'ethers';
import { v4 as uuidv4 } from 'uuid';

import { HardQuoteResponseData } from '../handlers/hard-quote/schema';
import { currentTimestampInMs, timestampInMstoSeconds } from '../util/time';
import { HardQuoteRequest } from '.';

// data class for hard quote response helpers and conversions
export class HardQuoteResponse {
public createdAt: string;

constructor(
public request: HardQuoteRequest,
public order: CosignedV2DutchOrder,
public createdAtMs = currentTimestampInMs()
) {
this.createdAt = timestampInMstoSeconds(parseInt(this.createdAtMs));
}

public toResponseJSON(): HardQuoteResponseData {
return {
requestId: this.request.requestId,
quoteId: this.request.quoteId,
chainId: this.request.tokenInChainId,
filler: this.order.info.cosignerData.exclusiveFiller,
encodedOrder: this.order.serialize(),
orderHash: this.order.hash(),
};
}

public toLog() {
return {
quoteId: this.quoteId,
requestId: this.requestId,
tokenInChainId: this.chainId,
tokenOutChainId: this.chainId,
tokenIn: this.tokenIn,
amountIn: this.amountIn.toString(),
tokenOut: this.tokenOut,
amountOut: this.amountOut.toString(),
swapper: this.swapper,
filler: this.filler,
orderHash: this.order.hash(),
createdAt: this.createdAt,
createdAtMs: this.createdAtMs,
};
}

public get quoteId(): string {
return this.request.quoteId ?? uuidv4();
}

public get requestId(): string {
return this.request.requestId;
}

public get chainId(): number {
return this.order.chainId;
}

public get swapper(): string {
return this.request.swapper;
}

public get tokenIn(): string {
return this.request.tokenIn;
}

public get amountOut(): BigNumber {
const resolved = this.order.resolve({
timestamp: this.order.info.cosignerData.decayStartTime,
});
let amount = BigNumber.from(0);
for (const output of resolved.outputs) {
amount = amount.add(output.amount);
}

return amount;
}

public get amountIn(): BigNumber {
const resolved = this.order.resolve({
timestamp: this.order.info.cosignerData.decayStartTime,
});
return resolved.input.amount;
}

public get tokenOut(): string {
return this.request.tokenOut;
}

public get filler(): string | undefined {
return this.order.info.cosignerData.exclusiveFiller;
}
}
9 changes: 9 additions & 0 deletions lib/entities/aws-metrics-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,19 @@ export enum Metric {
QUOTE_404 = 'QUOTE_404',
QUOTE_500 = 'QUOTE_500',

HARD_QUOTE_200 = 'HARD_QUOTE_200',
HARD_QUOTE_400 = 'HARD_QUOTE_400',
HARD_QUOTE_404 = 'HARD_QUOTE_404',
HARD_QUOTE_500 = 'HARD_QUOTE_500',

QUOTE_REQUESTED = 'QUOTE_REQUESTED',
QUOTE_LATENCY = 'QUOTE_LATENCY',
QUOTE_RESPONSE_COUNT = 'QUOTE_RESPONSE_COUNT',

HARD_QUOTE_REQUESTED = 'HARD_QUOTE_REQUESTED',
HARD_QUOTE_LATENCY = 'HARD_QUOTE_LATENCY',
HARD_QUOTE_RESPONSE_COUNT = 'HARD_QUOTE_RESPONSE_COUNT',

RFQ_REQUESTED = 'RFQ_REQUESTED',
RFQ_SUCCESS = 'RFQ_SUCCESS',
RFQ_RESPONSE_TIME = 'RFQ_RESPONSE_TIME',
Expand Down
1 change: 1 addition & 0 deletions lib/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './analytics-events';
export * from './aws-metrics-logger';
export * from './HardQuoteRequest';
export * from './HardQuoteResponse';
export * from './QuoteRequest';
export * from './QuoteResponse';
Loading
Loading