Skip to content

Commit

Permalink
Merge pull request #76 from Premian-Labs/issue/invalid-quote-signature
Browse files Browse the repository at this point in the history
Fix improper passing of `quote.fillableSize` as `quote.size`
  • Loading branch information
froggiedev authored Dec 5, 2023
2 parents f38b962 + c66a31f commit 4e0c0e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@premia/v3-sdk",
"version": "1.2.0",
"version": "1.2.1",
"description": "The official SDK for building applications on Premia V3.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
47 changes: 27 additions & 20 deletions src/api/ordersAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ export class OrdersAPI extends BaseAPI {
): Promise<OrderbookQuote | null> {
const bestQuotes = quotes.slice().sort((a, b) => {
const betterQuote = this.premia.pricing.better(
a,
b,
{
...a,
size: a.fillableSize,
},
{
...b,
size: b.fillableSize,
},
size,
minimumSize
) as QuoteWithSignatureT
Expand Down Expand Up @@ -82,15 +88,15 @@ export class OrdersAPI extends BaseAPI {
* @private
* @param {string} poolAddress - The address of the pool.
* @param {BigNumberish} size - The size of the trade.
* @param {QuoteWithSignatureT} quote - The quote to be converted.
* @param {OrderbookQuote} quote - The orderbook quote to be converted.
* @param {number} [createdAt] - The timestamp of the quote's creation (optional).
* @param {string} [referrer] - The address of the referrer (optional).
* @returns {Promise<FillableQuote>} - A promise resolving to the fillable quote.
*/
private async tradeQuoteToFillable(
poolAddress: string,
size: BigNumberish,
quote: QuoteWithSignatureT,
quote: OrderbookQuote,
createdAt?: number,
referrer?: string
): Promise<FillableQuote> {
Expand All @@ -100,8 +106,8 @@ export class OrdersAPI extends BaseAPI {
)
const price = toBigInt(quote.price)
const _size =
toBigInt(size) > toBigInt(quote.size)
? toBigInt(quote.size)
toBigInt(size) > toBigInt(quote.fillableSize)
? toBigInt(quote.fillableSize)
: toBigInt(size)
const normalizedPremium = (_size * price) / WAD_BI

Expand Down Expand Up @@ -149,7 +155,15 @@ export class OrdersAPI extends BaseAPI {
approvalAmount,
to: poolAddress,
data: poolContract.interface.encodeFunctionData('fillQuoteOB', [
quote,
{
provider: quote.provider,
taker: quote.taker,
price: quote.price,
size: quote.size,
isBuy: quote.isBuy,
deadline: quote.deadline,
salt: quote.salt,
},
_size,
Signature.from(quote.signature),
this.premia.pools.toReferrer(referrer),
Expand Down Expand Up @@ -228,14 +242,7 @@ export class OrdersAPI extends BaseAPI {
return null
}

quotes = quotes.map((quote) => ({ ...quote, size: quote.fillableSize }))

const bestQuote: SerializedIndexedQuote | null = (await this.bestQuote(
quotes,
size,
minimumSize,
taker
)) as SerializedIndexedQuote | null
const bestQuote = await this.bestQuote(quotes, size, minimumSize, taker)

if (bestQuote === null) {
return null
Expand All @@ -245,7 +252,7 @@ export class OrdersAPI extends BaseAPI {
poolAddress,
size,
bestQuote,
bestQuote.createdAt,
bestQuote.ts,
referrer
)
}
Expand Down Expand Up @@ -309,20 +316,20 @@ export class OrdersAPI extends BaseAPI {
},
async (message) => {
if (message.type == 'POST_QUOTE') {
const quote = (await this.bestQuote(
const quote = await this.bestQuote(
[message.body],
options.size,
options.minimumSize,
options.taker
)) as SerializedIndexedQuote | null
)
if (quote === null) return

if (bestQuote === null) {
bestQuote = await this.tradeQuoteToFillable(
options.poolAddress,
options.size,
quote,
quote.createdAt,
quote.ts,
options.referrer
)
callbackIfNotStale(bestQuote)
Expand All @@ -339,7 +346,7 @@ export class OrdersAPI extends BaseAPI {
options.poolAddress,
options.size,
quote,
quote.createdAt,
quote.ts,
options.referrer
)
callbackIfNotStale(bestQuote)
Expand Down
2 changes: 1 addition & 1 deletion src/entities/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ export interface PublishQuoteResponse {
export interface GetOrdersResponse {
validQuotes: OrderbookQuote[]
invalidQuotes: InvalidQuote[]
}
}

0 comments on commit 4e0c0e0

Please sign in to comment.