Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make TxMonitor's methods throw a single error & reduce boilerplate.
- Loading branch information
Showing
7 changed files
with
175 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 52 additions & 33 deletions
85
clients/TypeScript/packages/client/src/TxMonitor/nextTx.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,63 @@ | ||
import { Null, Ogmios, TxAlonzo, TxId } from "@cardano-ogmios/schema" | ||
import { UnknownResultError } from "../errors"; | ||
import { Ogmios, TxAlonzo, TxId } from "@cardano-ogmios/schema" | ||
import { UnknownResultError } from "../errors" | ||
import { InteractionContext } from '../Connection' | ||
import { Query } from '../StateQuery' | ||
|
||
export const isNextTxResult = (result: TxId | TxAlonzo | Null | Error[]): result is TxId | TxAlonzo | Null => | ||
((typeof (result as TxId) === 'string' || typeof (result as TxAlonzo) === 'object' || typeof (result as Null) === 'object') && !Array.isArray(result)) | ||
|
||
/** | ||
* Request the next mempool transaction from an acquired snapshot. | ||
* | ||
* @category TxMonitor | ||
*/ | ||
export const nextTx = (context: InteractionContext, args?: { fields?: "all"}) => | ||
Query< | ||
Ogmios['NextTx'], | ||
Ogmios['NextTxResponse'], | ||
TxId | TxAlonzo | Null | ||
>({ | ||
methodName: 'NextTx', | ||
args: args | ||
}, { | ||
handler: (response, resolve, reject) => { | ||
const result = handleNextTxResponse(response) | ||
if (isNextTxResult(result)) { | ||
return resolve(result as TxId | TxAlonzo | Null) | ||
} else { | ||
return reject(result as Error[]) | ||
export function nextTx (context: InteractionContext, args?: { fields: "all" }) : Promise<TxAlonzo|null> | ||
export function nextTx (context: InteractionContext, args?: {}) : Promise<TxId|null> | ||
export function nextTx (context: InteractionContext, args?: { fields: "all" }) : Promise<TxId|TxAlonzo|null> { | ||
return Query< | ||
Ogmios['NextTx'], | ||
Ogmios['NextTxResponse'], | ||
TxAlonzo | null | ||
>({ | ||
methodName: 'NextTx', | ||
args: args | ||
}, { | ||
handler: (response, resolve, reject) => { | ||
try { | ||
resolve(handleNextTxResponse(response, args)) | ||
} catch (e) { | ||
reject(e) | ||
} | ||
} | ||
} | ||
}, context) | ||
}, context) | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const isNextTxResultId = (result: any): result is TxId | null => | ||
((result as null) === null) || (typeof (result as TxId) === 'string') | ||
|
||
export const handleNextTxResponse = (response: Ogmios['NextTxResponse']): (TxId | TxAlonzo | Null | Error[]) => { | ||
try { | ||
const { result } = response | ||
if (result !== undefined) { | ||
return result; | ||
} else { | ||
return [new UnknownResultError(response)] | ||
} | ||
} catch (e) { | ||
return [new UnknownResultError(response)] | ||
/** | ||
* @internal | ||
*/ | ||
export const isNextTxResultAll = (result: any): result is TxAlonzo | null => | ||
((result as null) === null) || (typeof (result as TxAlonzo) === 'object') | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function handleNextTxResponse (response: Ogmios['NextTxResponse'], args?: { fields: "all" }): (TxAlonzo | null) | ||
export function handleNextTxResponse (response: Ogmios['NextTxResponse'], args?: {}): (TxId | null) | ||
export function handleNextTxResponse (response: Ogmios['NextTxResponse'], args?: { fields: "all" }): (TxId | TxAlonzo | null) { | ||
const { result } = response | ||
|
||
if (args.fields === "all") { | ||
if (isNextTxResultAll(result)) { | ||
return result | ||
} | ||
} else { | ||
if (isNextTxResultId(result)) { | ||
return result | ||
} | ||
} | ||
} | ||
|
||
throw new UnknownResultError(response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.