Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/rpc handler #19

Merged
merged 6 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Keyrxng marked this conversation as resolved.
Show resolved Hide resolved

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rpc-handler seems to have types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repo does but the package does not for some reason. My test pkg I published has types and it's the same flow that was used to publish but the UBQ pkg doesn't have types in either of the versions on npmjs

https://www.npmjs.com/package/@ubiquity-dao/rpc-handler?activeTab=code

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
Loading