From 1a54eab8ade89e05085a95bf2d7328f05cd156cf Mon Sep 17 00:00:00 2001 From: Maxence Raballand Date: Fri, 24 May 2024 15:10:19 +0200 Subject: [PATCH] feat/result-in-client (#57) * 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 --- .changeset/lucky-birds-kneel.md | 5 ++ src/actions/index.ts | 10 +++ src/actions/market-order.ts | 34 +++++++- src/actions/order/index.ts | 14 ++++ src/actions/order/results.ts | 114 ++++++++++++++++++++++++++ src/bundle/public/market-actions.ts | 120 ++++++++++++++++++++++++++++ src/index.ts | 10 +++ 7 files changed, 306 insertions(+), 1 deletion(-) create mode 100644 .changeset/lucky-birds-kneel.md create mode 100644 src/actions/order/results.ts diff --git a/.changeset/lucky-birds-kneel.md b/.changeset/lucky-birds-kneel.md new file mode 100644 index 0000000..6823907 --- /dev/null +++ b/.changeset/lucky-birds-kneel.md @@ -0,0 +1,5 @@ +--- +"@mangrovedao/mgv": patch +--- + +Add wait for result on orders diff --git a/src/actions/index.ts b/src/actions/index.ts index b7aa21c..438de29 100644 --- a/src/actions/index.ts +++ b/src/actions/index.ts @@ -17,6 +17,10 @@ export type { SimulateRawRemoveOrderArgs, SimulateRemoveOrderArgs, RetractOrderResult, + WaitForLimitOrderResultParams, + WaitForLimitOrderUpdateResultParams, + WaitForSetExpirationResultParams, + WaitForRemoveLimitOrderResult, } from './order/index.js' export { @@ -32,6 +36,10 @@ export { simulateSetExpiration, simulateRawRemoveOrder, simulateRemoveOrder, + waitForLimitOrderResult, + waitForLimitOrderUpdateResult, + waitForSetExpirationResult, + waitForRemoveLimitOrderResult, } from './order/index.js' export type { @@ -71,6 +79,7 @@ export type { SimulateMarketOrderByTickArgs, SimulateMarketOrderByVolumeArgs, SimulateMarketOrderByVolumeAndMarketArgs, + WaitForMarketOrderResultParams, } from './market-order.js' export { @@ -78,4 +87,5 @@ export { simulateMarketOrderByTick, simulateMarketOrderByVolume, simulateMarketOrderByVolumeAndMarket, + waitForMarketOrderResult, } from './market-order.js' diff --git a/src/actions/market-order.ts b/src/actions/market-order.ts index a1c1c8e..406aa72 100644 --- a/src/actions/market-order.ts +++ b/src/actions/market-order.ts @@ -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, @@ -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, @@ -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 @@ -172,3 +182,25 @@ export async function simulateMarketOrderByVolumeAndMarket( request, } } + +export type WaitForMarketOrderResultParams = + WaitForTransactionReceiptParameters & + Omit + +export async function waitForMarketOrderResult( + client: Client, + actionParams: MangroveActionsDefaultParams, + market: MarketParams, + params: WaitForMarketOrderResultParams, +): Promise>> { + const receipt = await getAction( + client, + waitForTransactionReceipt, + 'waitForTransactionReceipt', + )(params) + const result = marketOrderResultFromLogs(actionParams, market, { + ...params, + logs: receipt.logs, + }) + return { result, receipt } +} diff --git a/src/actions/order/index.ts b/src/actions/order/index.ts index 97bbf00..191c820 100644 --- a/src/actions/order/index.ts +++ b/src/actions/order/index.ts @@ -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' diff --git a/src/actions/order/results.ts b/src/actions/order/results.ts new file mode 100644 index 0000000..a666875 --- /dev/null +++ b/src/actions/order/results.ts @@ -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 + +export type ResultWithReceipt = { receipt: TransactionReceipt; result: T } + +export async function waitForLimitOrderResult( + client: Client, + actionParams: MangroveActionsDefaultParams, + market: MarketParams, + params: WaitForLimitOrderResultParams, +): Promise> { + 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 + +export async function waitForLimitOrderUpdateResult( + client: Client, + actionParams: MangroveActionsDefaultParams, + market: MarketParams, + params: WaitForLimitOrderUpdateResultParams, +): Promise> { + 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 + +export async function waitForSetExpirationResult( + client: Client, + actionParams: MangroveActionsDefaultParams, + market: MarketParams, + params: WaitForSetExpirationResultParams, +): Promise> { + 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 + +export async function waitForRemoveLimitOrderResult( + client: Client, + actionParams: MangroveActionsDefaultParams, + market: MarketParams, + params: WaitForRemoveLimitOrderResult, +): Promise> { + const receipt = await getAction( + client, + waitForTransactionReceipt, + 'waitForTransactionReceipt', + )(params) + const result = removeOrderResultFromLogs(actionParams, market, { + ...params, + logs: receipt.logs, + }) + return { receipt, result } +} diff --git a/src/bundle/public/market-actions.ts b/src/bundle/public/market-actions.ts index d6fc3da..c34863c 100644 --- a/src/bundle/public/market-actions.ts +++ b/src/bundle/public/market-actions.ts @@ -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, @@ -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, @@ -137,6 +157,96 @@ export type PublicMarketActions = { simulateRemoveOrder: ( args: SimulateRemoveOrderArgs, ) => Promise + + /** + * 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> + + /** + * 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> + + /** + * 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> + + /** + * 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> + + /** + * 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>> } export function publicMarketActions( @@ -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), }) } diff --git a/src/index.ts b/src/index.ts index c14d3b0..8fae729 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,13 @@ +// -- actions -- + +export type { + WaitForLimitOrderResultParams, + WaitForLimitOrderUpdateResultParams, + WaitForSetExpirationResultParams, + WaitForRemoveLimitOrderResult, + WaitForMarketOrderResultParams, +} from './actions/index.js' + // -- lib functions -- export type {