-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert the example to use the
withChainContext
- Loading branch information
1 parent
75c392e
commit f35adae
Showing
2 changed files
with
68 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NetworkName } from "@therootnetwork/api"; | ||
import PrettyError from "pretty-error"; | ||
|
||
import { ApiPromise, getChainApi } from "./getChainApi"; | ||
import { getLogger, Logger } from "./getLogger"; | ||
|
||
const pe = new PrettyError(); | ||
|
||
export async function withChainContext( | ||
network: string, | ||
callback: (api: ApiPromise, logger: Logger) => Promise<void> | ||
) { | ||
const logger = getLogger(); | ||
const api = await getChainApi(network as NetworkName); | ||
|
||
logger.info({ network }, `create an ApiPromise instance`); | ||
await callback(api, logger).catch((error) => console.log(pe.render(error))); | ||
|
||
await api.disconnect(); | ||
|
||
logger.info("call ended 🎉 "); | ||
} |