Skip to content

Commit

Permalink
feat/result-in-client (#57)
Browse files Browse the repository at this point in the history
* feat: Add orderLabel, updateOrderResultFromLogs, getDefaultLimitOrderGasreq.

* chore: format

* feat: Refactor update order test and fix variable declaration.

* feat: Add set expiration result from logs params to limit order exports.

* feat: Add remove order functionality with deprovision flag.

* feat: Add logs result on update and remove unnecessary code.

* feat(market-actions): Add functions to wait for order results.

* feat: Remove changeset

* chore: changeset

* refactor: Update return type of waitForMarketOrderResult to exclude 'request' field.

---------

Co-authored-by: maxencerb <[email protected]>
  • Loading branch information
maxencerb and maxencerb authored May 24, 2024
1 parent 5cd92e1 commit 1a54eab
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lucky-birds-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Add wait for result on orders
10 changes: 10 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export type {
SimulateRawRemoveOrderArgs,
SimulateRemoveOrderArgs,
RetractOrderResult,
WaitForLimitOrderResultParams,
WaitForLimitOrderUpdateResultParams,
WaitForSetExpirationResultParams,
WaitForRemoveLimitOrderResult,
} from './order/index.js'

export {
Expand All @@ -32,6 +36,10 @@ export {
simulateSetExpiration,
simulateRawRemoveOrder,
simulateRemoveOrder,
waitForLimitOrderResult,
waitForLimitOrderUpdateResult,
waitForSetExpirationResult,
waitForRemoveLimitOrderResult,
} from './order/index.js'

export type {
Expand Down Expand Up @@ -71,11 +79,13 @@ export type {
SimulateMarketOrderByTickArgs,
SimulateMarketOrderByVolumeArgs,
SimulateMarketOrderByVolumeAndMarketArgs,
WaitForMarketOrderResultParams,
} from './market-order.js'

export {
getMarketOrderSteps,
simulateMarketOrderByTick,
simulateMarketOrderByVolume,
simulateMarketOrderByVolumeAndMarket,
waitForMarketOrderResult,
} from './market-order.js'
34 changes: 33 additions & 1 deletion src/actions/market-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import {
type Client,
type ReadContractParameters,
type SimulateContractParameters,
type WaitForTransactionReceiptParameters,
type erc20Abi,
maxUint128,
maxUint256,
} from 'viem'
import { readContract, simulateContract } from 'viem/actions'
import {
readContract,
simulateContract,
waitForTransactionReceipt,
} from 'viem/actions'
import type {
MarketOrderByTickParams,
MarketOrderByVolumeAndMarketParams,
Expand All @@ -21,6 +26,10 @@ import {
} from '../builder/market-order.js'
import { tokenAllowanceParams } from '../builder/tokens.js'
import { BS } from '../lib/enums.js'
import {
type MarketOrderResultFromLogsParams,
marketOrderResultFromLogs,
} from '../lib/market-order.js'
import type {
BuiltArgs,
MangroveActionsDefaultParams,
Expand All @@ -30,6 +39,7 @@ import type { MarketOrderResult } from '../types/actions/market-order.js'
import type { MarketOrderSteps } from '../types/actions/steps.js'
import type { Prettify } from '../types/lib.js'
import { getAction } from '../utils/getAction.js'
import type { ResultWithReceipt } from './order/results.js'

export type GetMarketOrderStepsParams = {
user: Address
Expand Down Expand Up @@ -172,3 +182,25 @@ export async function simulateMarketOrderByVolumeAndMarket(
request,
}
}

export type WaitForMarketOrderResultParams =
WaitForTransactionReceiptParameters &
Omit<MarketOrderResultFromLogsParams, 'logs'>

export async function waitForMarketOrderResult(
client: Client,
actionParams: MangroveActionsDefaultParams,
market: MarketParams,
params: WaitForMarketOrderResultParams,
): Promise<ResultWithReceipt<Omit<MarketOrderResult, 'request'>>> {
const receipt = await getAction(
client,
waitForTransactionReceipt,
'waitForTransactionReceipt',
)(params)
const result = marketOrderResultFromLogs(actionParams, market, {
...params,
logs: receipt.logs,
})
return { result, receipt }
}
14 changes: 14 additions & 0 deletions src/actions/order/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,17 @@ export {
simulateRawRemoveOrder,
simulateRemoveOrder,
} from './remove.js'

export type {
WaitForLimitOrderResultParams,
WaitForLimitOrderUpdateResultParams,
WaitForSetExpirationResultParams,
WaitForRemoveLimitOrderResult,
} from './results.js'

export {
waitForLimitOrderResult,
waitForLimitOrderUpdateResult,
waitForSetExpirationResult,
waitForRemoveLimitOrderResult,
} from './results.js'
114 changes: 114 additions & 0 deletions src/actions/order/results.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import type {
Client,
TransactionReceipt,
WaitForTransactionReceiptParameters,
} from 'viem'
import { waitForTransactionReceipt } from 'viem/actions'
import {
type LimitOrderResult,
type LimitOrderResultFromLogsParams,
type RemoveOrderResult,
type RemoveOrderResultFromLogsParams,
type SetExpirationResultFromLogsParams,
type UpdateOrderResult,
type UpdateOrderResultFromLogsParams,
limitOrderResultFromLogs,
removeOrderResultFromLogs,
setExpirationResultFromLogs,
updateOrderResultFromLogs,
} from '../../lib/limit-order.js'
import type {
MangroveActionsDefaultParams,
MarketParams,
} from '../../types/index.js'
import { getAction } from '../../utils/getAction.js'

export type WaitForLimitOrderResultParams =
WaitForTransactionReceiptParameters &
Omit<LimitOrderResultFromLogsParams, 'logs'>

export type ResultWithReceipt<T> = { receipt: TransactionReceipt; result: T }

export async function waitForLimitOrderResult(
client: Client,
actionParams: MangroveActionsDefaultParams,
market: MarketParams,
params: WaitForLimitOrderResultParams,
): Promise<ResultWithReceipt<LimitOrderResult>> {
const receipt = await getAction(
client,
waitForTransactionReceipt,
'waitForTransactionReceipt',
)(params)
const result = limitOrderResultFromLogs(actionParams, market, {
...params,
logs: receipt.logs,
})
return { receipt, result }
}

export type WaitForLimitOrderUpdateResultParams =
WaitForTransactionReceiptParameters &
Omit<UpdateOrderResultFromLogsParams, 'logs'>

export async function waitForLimitOrderUpdateResult(
client: Client,
actionParams: MangroveActionsDefaultParams,
market: MarketParams,
params: WaitForLimitOrderUpdateResultParams,
): Promise<ResultWithReceipt<UpdateOrderResult>> {
const receipt = await getAction(
client,
waitForTransactionReceipt,
'waitForTransactionReceipt',
)(params)
const result = updateOrderResultFromLogs(actionParams, market, {
...params,
logs: receipt.logs,
})
return { receipt, result }
}

export type WaitForSetExpirationResultParams =
WaitForTransactionReceiptParameters &
Omit<SetExpirationResultFromLogsParams, 'logs'>

export async function waitForSetExpirationResult(
client: Client,
actionParams: MangroveActionsDefaultParams,
market: MarketParams,
params: WaitForSetExpirationResultParams,
): Promise<ResultWithReceipt<bigint | undefined>> {
const receipt = await getAction(
client,
waitForTransactionReceipt,
'waitForTransactionReceipt',
)(params)
const result = setExpirationResultFromLogs(actionParams, market, {
...params,
logs: receipt.logs,
})
return { receipt, result }
}

export type WaitForRemoveLimitOrderResult =
WaitForTransactionReceiptParameters &
Omit<RemoveOrderResultFromLogsParams, 'logs'>

export async function waitForRemoveLimitOrderResult(
client: Client,
actionParams: MangroveActionsDefaultParams,
market: MarketParams,
params: WaitForRemoveLimitOrderResult,
): Promise<ResultWithReceipt<RemoveOrderResult>> {
const receipt = await getAction(
client,
waitForTransactionReceipt,
'waitForTransactionReceipt',
)(params)
const result = removeOrderResultFromLogs(actionParams, market, {
...params,
logs: receipt.logs,
})
return { receipt, result }
}
120 changes: 120 additions & 0 deletions src/bundle/public/market-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
getMarketOrderSteps,
simulateMarketOrderByVolumeAndMarket,
} from '../../actions/market-order.js'
import {
type WaitForMarketOrderResultParams,
waitForMarketOrderResult,
} from '../../actions/market-order.js'
import {
type GetLimitOrderStepsArgs,
type SimulateLimitOrderArgs,
Expand All @@ -18,11 +22,27 @@ import {
type SimulateRemoveOrderArgs,
simulateRemoveOrder,
} from '../../actions/order/remove.js'
import {
type ResultWithReceipt,
type WaitForLimitOrderResultParams,
type WaitForLimitOrderUpdateResultParams,
type WaitForRemoveLimitOrderResult,
type WaitForSetExpirationResultParams,
waitForLimitOrderResult,
waitForLimitOrderUpdateResult,
waitForRemoveLimitOrderResult,
waitForSetExpirationResult,
} from '../../actions/order/results.js'
import {
type SimulateUpdateOrderArgs,
type SimulateUpdateOrderResult,
simulateUpdateOrder,
} from '../../actions/order/update.js'
import type {
LimitOrderResult,
RemoveOrderResult,
UpdateOrderResult,
} from '../../lib/limit-order.js'
import type { Book } from '../../types/actions/book.js'
import type {
MangroveActionsDefaultParams,
Expand Down Expand Up @@ -137,6 +157,96 @@ export type PublicMarketActions = {
simulateRemoveOrder: (
args: SimulateRemoveOrderArgs,
) => Promise<RetractOrderResult>

/**
* Wait for the limit order result
* @param args args for the wait for limit order result call
* @returns the limit order result
* @example
* ```ts
* const { request, result } = await publicMarketActions.simulateLimitOrder({ ... });
* const tx = await walletClient.writeContract(request);
* const { receipt, result } = await publicMarketActions.waitForLimitOrderResult({
* hash: tx,
* bs: BS.buy,
* user: userAddress,
* });
*/
waitForLimitOrderResult: (
args: WaitForLimitOrderResultParams,
) => Promise<ResultWithReceipt<LimitOrderResult>>

/**
* Wait for the limit order update result
* @param args args for the wait for limit order update result call
* @returns the limit order result
* @example
* ```ts
* const { request, result } = await publicMarketActions.simulateUpdateOrder({ ...});
* const tx = await walletClient.writeContract(request);
* const { receipt, result } = await publicMarketActions.waitForLimitOrderUpdateResult({
* hash: tx,
* bs: BS.buy,
* offerId: 1n,
* });
*/
waitForLimitOrderUpdateResult: (
args: WaitForLimitOrderUpdateResultParams,
) => Promise<ResultWithReceipt<UpdateOrderResult>>

/**
* Wait for the limit order remove result
* @param args args for the wait for limit order remove result call
* @returns the limit order result
* @example
* ```ts
* const { request, result } = await publicMarketActions.simulateRemoveOrder({ ... });
* const tx = await walletClient.writeContract(request);
* const { receipt, result } = await publicMarketActions.waitForRemoveOrderResult({
* hash: tx,
* bs: BS.buy,
* offerId: 1n,
* });
*/
waitForRemoveLimitOrderResult: (
args: WaitForRemoveLimitOrderResult,
) => Promise<ResultWithReceipt<RemoveOrderResult>>

/**
* Wait for the set expiration result
* @param args args for the wait for set expiration result call
* @returns the set expiration result
* @example
* ```ts
* const { request, result } = await publicMarketActions.simulateSetExpiration({ ... });
* const tx = await walletClient.writeContract(request);
* const { receipt, result } = await publicMarketActions.waitForSetExpirationResult({
* hash: tx,
* bs: BS.buy,
* offerId: 1n,
* });
*/
waitForSetExpirationResult: (
args: WaitForSetExpirationResultParams,
) => Promise<ResultWithReceipt<bigint | undefined>>

/**
* Wait for the market order result
* @param args args for the wait for market order result call
* @returns the market order result
* @example
* ```ts
* const { request, result } = await publicMarketActions.simulateMarketOrderByVolumeAndMarket({ ... });
* const tx = await walletClient.writeContract(request);
* const { receipt, result } = await publicMarketActions.waitForMarketOrderResult({
* hash: tx,
* bs: BS.buy,
* taker: userAddress,
* });
*/
waitForMarketOrderResult: (
args: WaitForMarketOrderResultParams,
) => Promise<ResultWithReceipt<Omit<MarketOrderResult, 'request'>>>
}

export function publicMarketActions(
Expand All @@ -156,5 +266,15 @@ export function publicMarketActions(
simulateUpdateOrder(client, actionParams, market, args),
simulateRemoveOrder: (args) =>
simulateRemoveOrder(client, actionParams, market, args),
waitForLimitOrderResult: (args) =>
waitForLimitOrderResult(client, actionParams, market, args),
waitForLimitOrderUpdateResult: (args) =>
waitForLimitOrderUpdateResult(client, actionParams, market, args),
waitForRemoveLimitOrderResult: (args) =>
waitForRemoveLimitOrderResult(client, actionParams, market, args),
waitForSetExpirationResult: (args) =>
waitForSetExpirationResult(client, actionParams, market, args),
waitForMarketOrderResult: (args) =>
waitForMarketOrderResult(client, actionParams, market, args),
})
}
Loading

0 comments on commit 1a54eab

Please sign in to comment.