Skip to content

Commit

Permalink
feat: use rpc-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jun 16, 2024
1 parent 74ac030 commit aaa912f
Show file tree
Hide file tree
Showing 6 changed files with 7,032 additions and 10,507 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@octokit/webhooks": "^13.1.0",
"@sinclair/typebox": "^0.32.5",
"@supabase/supabase-js": "2.42.0",
"@ubiquity-dao/rpc-handler": "^1.1.0",
"@uniswap/permit2-sdk": "^1.2.0",
"dotenv": "^16.4.4",
"ethers": "6.11.1",
Expand Down
17 changes: 7 additions & 10 deletions src/handlers/generate-erc20-permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers, keccak256, MaxInt256, parseUnits, toUtf8Bytes } from "ethers";
import { Context, Logger } from "../types/context";
import { PermitReward, TokenType } from "../types";
import { decryptKeys } from "../utils/keys";
import { getPayoutConfigByNetworkId } from "../utils/payoutConfigByNetworkId";
import { getFastestProvider } from "../utils/get-fastest-provider";

export interface Payload {
evmNetworkId: number;
Expand Down Expand Up @@ -63,24 +63,21 @@ export async function generateErc20PermitSignature(contextOrPayload: Context | P
throw new Error(errorMessage);
}

const { rpc } = getPayoutConfigByNetworkId(_evmNetworkId);
const provider = await getFastestProvider(_evmNetworkId);
if (!provider) {
_logger.error("Provider is not defined");
throw new Error("Provider is not defined");
}

const { privateKey } = await decryptKeys(_evmPrivateEncrypted);
if (!privateKey) {
const errorMessage = "Private key is not defined";
_logger.fatal(errorMessage);
throw new Error(errorMessage);
}

let provider;
let adminWallet;
let tokenDecimals;
try {
provider = new ethers.JsonRpcProvider(rpc);
} catch (error) {
const errorMessage = `Failed to instantiate provider: ${error}`;
_logger.debug(errorMessage);
throw new Error(errorMessage);
}

try {
adminWallet = new ethers.Wallet(privateKey, provider);
Expand Down
17 changes: 5 additions & 12 deletions src/handlers/generate-erc721-permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers, keccak256, toUtf8Bytes } from "ethers";
import { Context, Logger } from "../types/context";
import { PermitReward, TokenType } from "../types";
import { isIssueEvent } from "../types/typeguards";
import { getPayoutConfigByNetworkId } from "../utils/payoutConfigByNetworkId";
import { getFastestProvider } from "../utils/get-fastest-provider";

interface Erc721PermitSignatureData {
beneficiary: string;
Expand Down Expand Up @@ -95,11 +95,11 @@ export async function generateErc721PermitSignature(
_walletAddress = walletAddress;
}

const { rpc } = getPayoutConfigByNetworkId(_evmNetworkId);
const provider = await getFastestProvider(_evmNetworkId);

if (!rpc) {
_logger.error("RPC is not defined");
throw new Error("RPC is not defined");
if (!provider) {
_logger.error("Provider is not defined");
throw new Error("Provider is not defined");
}

if (!_nftContractAddress) {
Expand All @@ -108,14 +108,7 @@ export async function generateErc721PermitSignature(
throw new Error(errorMessage);
}

let provider;
let adminWallet;
try {
provider = new ethers.JsonRpcProvider(rpc);
} catch (error) {
_logger.error("Failed to instantiate provider", error);
throw new Error("Failed to instantiate provider");
}

try {
adminWallet = new ethers.Wallet(_nftMinterPrivateKey, provider);
Expand Down
21 changes: 21 additions & 0 deletions src/utils/get-fastest-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { RPCHandler, HandlerConstructorConfig } from "@ubiquity-dao/rpc-handler"
import { JsonRpcProvider } from "ethers";

function getHandler(networkId: number | string) {
const config = {
networkId,
autoStorage: false,
cacheRefreshCycles: 5,
rpcTimeout: 1500,
networkName: null,
runtimeRpcs: null,
networkRpcs: null,
};

return new RPCHandler(config as HandlerConstructorConfig)
}

export async function getFastestProvider(networkId: number | string): Promise<JsonRpcProvider> {
const handler = getHandler(networkId)
return await handler.getFastestRpcProvider()
}
21 changes: 0 additions & 21 deletions src/utils/payoutConfigByNetworkId.ts

This file was deleted.

Loading

0 comments on commit aaa912f

Please sign in to comment.