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

fix: validate bids order #124

Merged
merged 1 commit into from
Feb 5, 2024
Merged
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
21 changes: 17 additions & 4 deletions utils/auction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ export const getUniqueTraders = (bids: Array<Bid>) => {
return Object.keys(uniqueApprovalMap)
}

export const checkNonces = async (bids: Array<Bid>) => {
const nonceCheckPromises = bids.map(async bid => {
const isNonceUsed = await crabV2Contract.nonces(bid.bidder, bid.order.nonce)
return { bidder: bid.bidder, nonce: bid.order.nonce, isNonceUsed }
})

const nonceResults = await Promise.all(nonceCheckPromises)
return nonceResults.reduce((acc, { bidder, nonce, isNonceUsed }) => {
acc[`${bidder}-${nonce}`] = isNonceUsed
return acc
}, {} as { [key: string]: boolean })
Copy link
Collaborator

Choose a reason for hiding this comment

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

Question from my javascript ignorance: do we need to give {} as { [key : string]: boolean} for the initial value of the accumulator or could we just give empty array for the reduce?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

here we are dealing with objects, so need to give empty object ie {}. if it'd have been an array, then empty array would have worked

Copy link
Collaborator

@alpinechicken alpinechicken Feb 1, 2024

Choose a reason for hiding this comment

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

that makes sense but would it work if we just gave it {} like

  return nonceResults.reduce((acc, { bidder, nonce, isNonceUsed }) => {
    acc[`${bidder}-${nonce}`] = isNonceUsed
    return acc
  }, {})

}

export const categorizeBidsWithReason = async (
sortedBids: Array<Bid>,
auction: Auction,
Expand All @@ -77,8 +90,9 @@ export const categorizeBidsWithReason = async (
const auctionPrice = BigNumber.from(auction.price)

let filledAmt = BigNumber.from(0)
const nonceStatuses = await checkNonces(sortedBids)

const filteredBidPromises = sortedBids.map(async b => {
const filteredBids = sortedBids.map(b => {
const _osqth = BigNumber.from(b.order.quantity)
const _price = BigNumber.from(b.order.price)
const erc20Needed = auction.isSelling ? wmul(_osqth, _price) : _osqth
Expand All @@ -105,8 +119,8 @@ export const categorizeBidsWithReason = async (

if (filledAmt.eq(quantity)) return { ...b, status: BidStatus.ALREADY_FILLED }

const nonceUsed = await crabV2Contract.nonces(b.bidder, b.order.nonce)
if (nonceUsed === true) {
const isNonceUsed = nonceStatuses[`${b.bidder}-${b.order.nonce}`]
if (isNonceUsed === true) {
return { ...b, status: BidStatus.NONCE_ALREADY_USED }
}

Expand All @@ -121,7 +135,6 @@ export const categorizeBidsWithReason = async (
return { ...b, status: BidStatus.INCLUDED }
})

const filteredBids = await Promise.all(filteredBidPromises)
return filteredBids.sort((a, b) => a.status - b.status)
}

Expand Down
Loading