Skip to content

Commit

Permalink
Merge pull request #16 from clober-dex/refactor/limit-order
Browse files Browse the repository at this point in the history
refactor: limit order function
  • Loading branch information
graykode authored Apr 18, 2024
2 parents 47f66d7 + 2a0f035 commit e93da46
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ export const limitOrder = decorator(
}> => {
const market = await fetchMarket(chainId, [inputToken, outputToken])
const isBid = isAddressEqual(market.quote.address, inputToken)
const [inputCurrency, outputCurrency] = isBid
? [market.quote, market.base]
: [market.base, market.quote]
if ((isBid && !market.bidBookOpen) || (!isBid && !market.askBookOpen)) {
throw new Error(`
Open the market before placing a limit order.
Expand All @@ -191,12 +194,9 @@ export const limitOrder = decorator(
const tokensToSettle = [inputToken, outputToken].filter(
(address) => !isAddressEqual(address, zeroAddress),
)
const quoteAmount = parseUnits(
amount,
isBid ? market.quote.decimals : market.base.decimals,
)
const quoteAmount = parseUnits(amount, inputCurrency.decimals)
const [unit, { spendAmount, bookId }] = await Promise.all([
calculateUnit(chainId, isBid ? market.quote : market.base),
calculateUnit(chainId, inputCurrency),
getExpectedOutput({
chainId,
inputToken,
Expand Down Expand Up @@ -244,16 +244,13 @@ export const limitOrder = decorator(
}),
result: {
make: {
amount: formatUnits(
quoteAmount,
isBid ? market.quote.decimals : market.base.decimals,
),
currency: isBid ? market.quote : market.base,
amount: formatUnits(quoteAmount, inputCurrency.decimals),
currency: inputCurrency,
direction: 'in',
},
take: {
amount: '0',
currency: isBid ? market.base : market.quote,
currency: outputCurrency,
direction: 'out',
},
},
Expand Down Expand Up @@ -287,16 +284,13 @@ export const limitOrder = decorator(
}),
result: {
make: {
amount: formatUnits(
quoteAmount,
isBid ? market.quote.decimals : market.base.decimals,
),
currency: isBid ? market.quote : market.base,
amount: formatUnits(quoteAmount, inputCurrency.decimals),
currency: inputCurrency,
direction: 'in',
},
take: {
amount: spendAmount,
currency: isBid ? market.base : market.quote,
currency: outputCurrency,
direction: 'out',
},
},
Expand Down

0 comments on commit e93da46

Please sign in to comment.