Skip to content

Commit

Permalink
Merge pull request #19 from ubq-testing/feat/rpc-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 authored Jul 6, 2024
2 parents 37e28cc + 67338e3 commit 4c97215
Show file tree
Hide file tree
Showing 8 changed files with 7,041 additions and 10,510 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
5 changes: 3 additions & 2 deletions src/adapters/supabase/helpers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { SupabaseClient } from "@supabase/supabase-js";
import { Super } from "./supabase";
import { Context } from "../../../types/context";

const FAILED_TO_GET_USER = "Failed to get user";
const SUCCESSFULLY_FETCHED_USER = "Successfully fetched user";

export class User extends Super {
constructor(supabase: SupabaseClient<Database>, context: Context) {
super(supabase, context);
Expand Down Expand Up @@ -51,5 +54,3 @@ export class User extends Super {
}
}

const FAILED_TO_GET_USER = "Failed to get user";
const SUCCESSFULLY_FETCHED_USER = "Successfully fetched user";
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
26 changes: 26 additions & 0 deletions src/utils/get-fastest-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @ts-expect-error - no types
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> {
try {
const handler = getHandler(networkId)
return await handler.getFastestRpcProvider()
} catch (e) {
throw new Error(`Failed to get fastest provider for networkId: ${networkId}`)
}
}
21 changes: 0 additions & 21 deletions src/utils/payoutConfigByNetworkId.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/generate-erc721-permit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe("generateErc721PermitSignature", () => {

it("should throw an error if RPC is not defined", async () => {
context.config.evmNetworkId = 123;
await expect(generateErc721PermitSignature(context, "123", "contribution")).rejects.toThrow("No config" + " setup for evmNetworkId: 123");
await expect(generateErc721PermitSignature(context, "123", "contribution")).rejects.toThrow("Failed to get fastest provider for networkId: 123");
});

it("should throw an error if NFT minter private key is not defined", async () => {
Expand Down
Loading

0 comments on commit 4c97215

Please sign in to comment.