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

feat/result-in-client #57

Merged
merged 12 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
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
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