*import pocket as a commonjs

This commit is contained in:
Derrick Hammer 2022-07-05 15:54:57 -04:00
parent 5db7bc847f
commit 81834bb41b
2 changed files with 26 additions and 17 deletions

View File

@ -5,14 +5,9 @@ import { Mutex } from "async-mutex";
import { createRequire } from "module"; import { createRequire } from "module";
import NodeCache from "node-cache"; import NodeCache from "node-cache";
import { get as getDHT } from "./dht.js"; import { get as getDHT } from "./dht.js";
import { Server as JSONServer } from "jayson/promise/index.js";
import { rpcMethods } from "./rpc/index.js"; import { rpcMethods } from "./rpc/index.js";
import { import PocketPKG from "@pokt-network/pocket-js";
Configuration, const { Configuration, HttpRpcProvider, PocketAAT, Pocket } = PocketPKG;
HttpRpcProvider,
Pocket,
PocketAAT,
} from "@pokt-network/pocket-js";
import { import {
JSONRPCRequest, JSONRPCRequest,
JSONRPCResponseWithError, JSONRPCResponseWithError,
@ -28,8 +23,10 @@ const processedRequests = new NodeCache({
stdTTL: 60 * 60 * 12, stdTTL: 60 * 60 * 12,
}); });
let pocketServer: Pocket; type PocketAATObject = typeof PocketAAT;
let _aat: PocketAAT;
let pocketServer: typeof Pocket;
let _aat: PocketAATObject;
let jsonServer: jayson.Server; let jsonServer: jayson.Server;
interface RPCRequest { interface RPCRequest {
@ -142,14 +139,14 @@ async function processRequest(request: RPCRequest): Promise<RPCResponse> {
return dbData; return dbData;
} }
export function updateAat(aat: PocketAAT): void { export function updateAat(aat: PocketAATObject): void {
_aat = aat; _aat = aat;
} }
export function getAat(): PocketAAT { export function getAat(): PocketAATObject {
return _aat; return _aat;
} }
export function getPocketServer(): Pocket { export function getPocketServer(): typeof Pocket {
return pocketServer; return pocketServer;
} }
@ -157,8 +154,9 @@ export async function unlockAccount(
accountPrivateKey: string, accountPrivateKey: string,
accountPublicKey: string, accountPublicKey: string,
accountPassphrase: string accountPassphrase: string
): Promise<PocketAAT> { ): Promise<PocketAATObject> {
try { try {
// @ts-ignore
const account = await pocketServer.keybase.importAccount( const account = await pocketServer.keybase.importAccount(
Buffer.from(accountPrivateKey, "hex"), Buffer.from(accountPrivateKey, "hex"),
accountPassphrase accountPassphrase
@ -169,12 +167,14 @@ export async function unlockAccount(
throw account; throw account;
} }
// @ts-ignore
await pocketServer.keybase.unlockAccount( await pocketServer.keybase.unlockAccount(
account.addressHex, account.addressHex,
accountPassphrase, accountPassphrase,
0 0
); );
// @ts-ignore
return await PocketAAT.from( return await PocketAAT.from(
"0.0.1", "0.0.1",
accountPublicKey, accountPublicKey,
@ -215,6 +215,7 @@ export async function start() {
); );
const rpcProvider = new HttpRpcProvider(dispatchURL); const rpcProvider = new HttpRpcProvider(dispatchURL);
const configuration = new Configuration(); const configuration = new Configuration();
// @ts-ignore
pocketServer = new Pocket([dispatchURL], rpcProvider, configuration); pocketServer = new Pocket([dispatchURL], rpcProvider, configuration);
updateUsePocketGateway(false); updateUsePocketGateway(false);
} }
@ -229,7 +230,7 @@ export async function start() {
); );
} }
jsonServer = new JSONServer(rpcMethods, { useContext: true }); jsonServer = new jayson.Server(rpcMethods, { useContext: true });
(await getDHT()).on("connection", (socket: any) => { (await getDHT()).on("connection", (socket: any) => {
socket.on("data", async (data: any) => { socket.on("data", async (data: any) => {

View File

@ -1,5 +1,5 @@
import { ethers } from "ethers"; import { ethers } from "ethers";
import { PocketAAT } from "@pokt-network/pocket-js"; import { Pocket, PocketAAT } from "@pokt-network/pocket-js";
import { maybeMapChainId, reverseMapChainId } from "../util.js"; import { maybeMapChainId, reverseMapChainId } from "../util.js";
import { Connection } from "@solana/web3.js"; import { Connection } from "@solana/web3.js";
import { getAat, getPocketServer } from "../rpc.js"; import { getAat, getPocketServer } from "../rpc.js";
@ -73,7 +73,11 @@ export function proxyRpcMethod(
return await provider(method, args); return await provider(method, args);
} }
return await sendRelay(JSON.stringify(args), <string>chainId, getAat()); return await sendRelay(
JSON.stringify(args),
<string>chainId,
getAat() as unknown as PocketAAT
);
}; };
} }
@ -84,7 +88,11 @@ async function sendRelay(
pocketAAT: PocketAAT pocketAAT: PocketAAT
) { ) {
try { try {
return await getPocketServer().sendRelay(rpcQuery, blockchain, pocketAAT); return await (getPocketServer() as unknown as Pocket).sendRelay(
rpcQuery,
blockchain,
pocketAAT
);
} catch (e) { } catch (e) {
console.log(e); console.log(e);
throw e; throw e;