Skip to content

Commit

Permalink
Adds support for viem
Browse files Browse the repository at this point in the history
  • Loading branch information
crystalin committed May 31, 2023
1 parent 9531576 commit 9f8d9f2
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 0 deletions.
203 changes: 203 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"table": "^6.8.0",
"ts-node": "^10.8.1",
"undici": "^5.11.0",
"viem": "^0.3.40",
"web3": "^1.6.0",
"yargs": "^17.2.1"
},
Expand Down
37 changes: 37 additions & 0 deletions src/utils/networks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { WsProvider } from "@polkadot/api";
import chalk from "chalk";
import {
Chain,
PrivateKeyAccount,
PublicClient,
Transport,
WalletClient,
createPublicClient,
createWalletClient,
webSocket,
} from "viem";
import { ApiPromise } from "@polkadot/api";
import { typesBundlePre900 } from "moonbeam-types-bundle";
import { listenBlocks, printBlockDetails, RealtimeBlockDetails } from "./monitoring";
import { Options } from "yargs";
import { privateKeyToAccount } from "viem/accounts";
import { localhost } from "viem/chains";

export type MOONBEAM_NETWORK_NAME =
| "stagenet"
Expand Down Expand Up @@ -117,6 +129,31 @@ export const getApiFor = async (argv: Argv) => {
});
};

export const getViemFor = (argv: Argv): PublicClient<Transport, Chain, true> => {
const url = isKnownNetwork(argv.network) ? NETWORK_WS_URLS[argv.network] : argv.url;
return createPublicClient({
transport: webSocket(url),
});
};

/**
*
* @param argv Network options
* @param key Private key
* @returns
*/
export const getViemAccountFor = (
argv: Argv,
account: PrivateKeyAccount
): WalletClient<Transport, Chain, PrivateKeyAccount, true> => {
const url = isKnownNetwork(argv.network) ? NETWORK_WS_URLS[argv.network] : argv.url;
return createWalletClient({
transport: webSocket(url),
account,
chain: null,
});
};

export const getMonitoredApiFor = async (argv: Argv) => {
const wsProvider = getWsProviderFor(argv);
const api = await ApiPromise.create({
Expand Down

0 comments on commit 9f8d9f2

Please sign in to comment.