-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathHardQuoteRequest.ts
More file actions
193 lines (165 loc) · 5.85 KB
/
Copy pathHardQuoteRequest.ts
File metadata and controls
193 lines (165 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { TradeType } from '@uniswap/sdk-core';
import { OrderType, UnsignedV2DutchOrder, UnsignedV3DutchOrder } from '@uniswap/uniswapx-sdk';
import { BigNumber, ethers, utils } from 'ethers';
import { v4 as uuidv4 } from 'uuid';
import { QuoteRequest, QuoteRequestDataJSON } from '.';
import { HardQuoteRequestBody } from '../handlers/hard-quote';
import { ProtocolVersion } from '../providers';
export class HardQuoteRequest {
public order: UnsignedV2DutchOrder | UnsignedV3DutchOrder;
private data: HardQuoteRequestBody;
private readonly _protocol: ProtocolVersion;
public static fromHardRequestBody(body: HardQuoteRequestBody, orderType: OrderType): HardQuoteRequest {
return new HardQuoteRequest(body, orderType);
}
constructor(_data: HardQuoteRequestBody, orderType: OrderType) {
this.data = {
..._data,
requestId: _data.quoteId ?? uuidv4(),
};
if (orderType === OrderType.Dutch_V2) {
this.order = UnsignedV2DutchOrder.parse(_data.encodedInnerOrder, _data.tokenInChainId);
this._protocol = ProtocolVersion.V2;
} else if (orderType === OrderType.Dutch_V3) {
this.order = UnsignedV3DutchOrder.parse(_data.encodedInnerOrder, _data.tokenInChainId);
this._protocol = ProtocolVersion.V3;
} else {
throw new Error('Unsupported order type');
}
}
public toCleanJSON(): QuoteRequestDataJSON {
return {
tokenInChainId: this.tokenInChainId,
tokenOutChainId: this.tokenOutChainId,
swapper: ethers.constants.AddressZero,
requestId: this.requestId,
tokenIn: this.tokenIn,
tokenOut: this.tokenOut,
amount: this.amount.toString(),
type: TradeType[this.type],
numOutputs: this.numOutputs,
...(this.quoteId && { quoteId: this.quoteId }),
protocol: this.protocol,
};
}
// return an opposing quote request,
// i.e. quoting the other side of the trade
public toOpposingCleanJSON(): QuoteRequestDataJSON {
const type = this.type === TradeType.EXACT_INPUT ? TradeType.EXACT_OUTPUT : TradeType.EXACT_INPUT;
return {
...this.toCleanJSON(),
// switch tokenIn/tokenOut
tokenIn: utils.getAddress(this.tokenOut),
tokenOut: utils.getAddress(this.tokenIn),
amount: this.amount.toString(),
// switch tradeType
type: TradeType[type],
};
}
// 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;
}
public get tokenInChainId(): number {
return this.data.tokenInChainId;
}
// See the matching note in QuoteRequest: this getter previously returned the tokenIn chain
// id. Value-identical today because HardQuoteRequestBodyJoi pins tokenOutChainId to
// Joi.ref('tokenInChainId').
public get tokenOutChainId(): number {
return this.data.tokenOutChainId;
}
public get swapper(): string {
return this.order.info.swapper;
}
public get tokenIn(): string {
return utils.getAddress(this.order.info.input.token);
}
public get tokenOut(): string {
return utils.getAddress(this.order.info.outputs[0].token);
}
/**
* Whether every output pays outputs[0].token.
*
* `totalOutputAmountStart` below sums the outputs as raw integers, and `toCleanJSON`
* sends quoters that one scalar alongside a single `tokenOut` taken from outputs[0].
* Both are only meaningful when the outputs agree on a token: otherwise the amount
* mixes assets of different denominations, and the quoter prices something the order
* does not contain. Multi-output orders are the normal case, since fee outputs use the
* swapper output's token.
*/
public get hasUniformOutputTokens(): boolean {
const tokenOut = this.tokenOut;
for (const output of this.order.info.outputs) {
if (utils.getAddress(output.token) !== tokenOut) {
return false;
}
}
return true;
}
public get outputTokens(): string[] {
const tokens: string[] = [];
for (const output of this.order.info.outputs) {
tokens.push(utils.getAddress(output.token));
}
return tokens;
}
public get totalOutputAmountStart(): BigNumber {
let amount = BigNumber.from(0);
for (const output of this.order.info.outputs) {
amount = amount.add(output.startAmount);
}
return amount;
}
public get totalInputAmountStart(): BigNumber {
return this.order.info.input.startAmount;
}
public get amount(): BigNumber {
if (this.type === TradeType.EXACT_INPUT) {
return this.totalInputAmountStart;
} else {
return this.totalOutputAmountStart;
}
}
public get type(): TradeType {
if (this.order instanceof UnsignedV2DutchOrder) {
return this.order.info.input.startAmount.eq(this.order.info.input.endAmount)
? TradeType.EXACT_INPUT
: TradeType.EXACT_OUTPUT;
} else if (this.order instanceof UnsignedV3DutchOrder) {
// If curve doesn't exist OR has all relative amounts are zero, then it's EXACT_INPUT
return !this.order.info.input.curve ||
!this.order.info.input.curve.relativeAmounts.some((relativeAmount) => relativeAmount !== BigInt(0))
? TradeType.EXACT_INPUT
: TradeType.EXACT_OUTPUT;
} else {
throw new Error('Unsupported order type');
}
}
public get numOutputs(): number {
return this.order.info.outputs.length;
}
public get protocol(): ProtocolVersion {
return this._protocol;
}
public get cosigner(): string {
return this.order.info.cosigner;
}
public get innerSig(): string {
return this.data.innerSig;
}
public get quoteId(): string | undefined {
return this.data.quoteId;
}
public set quoteId(quoteId: string | undefined) {
this.data.quoteId = quoteId;
}
}