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

Merge development into main #30

Merged
merged 16 commits into from
Jul 8, 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
5 changes: 3 additions & 2 deletions .cspell.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
"version": "0.2",
"ignorePaths": ["**/*.json", "**/*.css", "node_modules", "**/*.log", "supabase"],
"ignorePaths": ["**/*.json", "**/*.css", "node_modules", "**/*.log", "supabase", "bun.lockb"],
"useGitignore": true,
"language": "en",
"words": ["dataurl", "devpool", "outdir", "servedir", "typebox"],
Expand Down Expand Up @@ -31,6 +31,7 @@
"ciphertext",
"tweetnacl",
"typeguards",
"Numberish"
"Numberish",
"Rpcs"
]
}
2 changes: 1 addition & 1 deletion .github/workflows/knip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
name: knip-results
path: |
knip-results.json
pr-number.txt
pr-number.txt
2 changes: 1 addition & 1 deletion .github/workflows/publish-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.10.0'
node-version: "20.10.0"
registry-url: https://registry.npmjs.org/
- run: |
yarn install --immutable --immutable-cache --check-cache
Expand Down
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Changelog

## 1.3.1 (2024-06-20)

### ⚠ BREAKING CHANGES

- bundle splitting ([#13](https://github.com/ubiquibot/permit-generation/issues/13))

### Features

- add custom reward token to permit ([16b08bc](https://github.com/ubiquibot/permit-generation/commit/16b08bc2ea6cadaba5f1446c832ec85d609bedab))
- bundle splitting ([#13](https://github.com/ubiquibot/permit-generation/issues/13)) ([38eb308](https://github.com/ubiquibot/permit-generation/commit/38eb30843665724ca6521f19b72e572d0f59c1c2))
- encode decode permits ([#10](https://github.com/ubiquibot/permit-generation/issues/10)) ([5673641](https://github.com/ubiquibot/permit-generation/commit/567364137dd48bfa7adf1c5c8a9b55e48d470181))
- package generation and publish ([f743a24](https://github.com/ubiquibot/permit-generation/commit/f743a242bfe4487e4309647690228d98342bad74))
- permit module ([b3b95d1](https://github.com/ubiquibot/permit-generation/commit/b3b95d1a8e9081ee4d33b8408ddf194b961ae090))
- permits ([3b928a1](https://github.com/ubiquibot/permit-generation/commit/3b928a1c0544d8c117133cf80c4a3bcc58697345))
- return data to kernel ([de23301](https://github.com/ubiquibot/permit-generation/commit/de2330129fe709292e4598f81cb1461ac43c0df6))
- rollup for package bundling ([628cb59](https://github.com/ubiquibot/permit-generation/commit/628cb59da66c16f1ea8d76c95caaa25426486641))

### Bug Fixes

- fixed compilation and erc20 tests ([63c3823](https://github.com/ubiquibot/permit-generation/commit/63c3823ae49cbda768dfd01d58d25e926101d7e9))
- fixed erc721 tests and errors ([414f29f](https://github.com/ubiquibot/permit-generation/commit/414f29fe852f7a5360eef1344d1e5d7b0da0dda5))
- this makes no sense ([881f006](https://github.com/ubiquibot/permit-generation/commit/881f006ca7f8dc64ba9fdeb7f02b3da8f0d68476))
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ General remarks:
Return value should just be an array of generated permits. A separate module can convert them into URLs for pay.ubq.fi.

[^1^]: I put a lot of thought into this and unless the plugins can hack X25519 encryption I think its fine for them to attempt brute forcing etc. In exchange its simpler infrastructure vs only accepting requests from the kernel (which seems more secure, but for V1 perhaps unnecessary.)

[^2^]: We pass in the username for enhanced developer experience for plugin development. I believe it will be much easier to use GitHub usernames and let the system do the lookup.

[^3^]: Mixed feelings on this lookup because it does not self encapsulate this module well. We already have an optional database write for the permit record, but this plugin WILL break if there is a database issue for reading the user's registered wallet, which can make testing and development more difficult here. The alternative is to pass in the user's wallet here, but then the kernel (or another plugin) needs to look up their wallet address (this seems like the better approach.)
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
31 changes: 3 additions & 28 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,23 @@ const config = [
dir: "dist/types",
format: "cjs",
},
plugins: [
del({ targets: ["dist/*"] }),
nodeResolve({ browser: true }),
commonjs(),
typescript(),
yaml(),
json(),
generateDtsBundle(),
terser(),
],
plugins: [del({ targets: ["dist/*"] }), nodeResolve({ browser: true }), commonjs(), typescript(), yaml(), json(), generateDtsBundle(), terser()],
},
{
input: "src/handlers/index.ts",
output: {
dir: "dist/handlers",
format: "cjs",
},
plugins: [
nodeResolve({ browser: true }),
commonjs(),
typescript(),
yaml(),
json(),
generateDtsBundle(),
terser(),
],
plugins: [nodeResolve({ browser: true }), commonjs(), typescript(), yaml(), json(), generateDtsBundle(), terser()],
},
{
input: "src/index.ts",
output: {
dir: "dist/core",
format: "cjs",
},
plugins: [
nodeResolve(),
commonjs(),
typescript(),
yaml(),
json(),
generateDtsBundle(),
terser(),
],
plugins: [nodeResolve(), commonjs(), typescript(), yaml(), json(), generateDtsBundle(), terser()],
},
];

Expand Down
6 changes: 3 additions & 3 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 @@ -50,6 +53,3 @@ export class User extends Super {
console.log("Successfully deleted user", { userId });
}
}

const FAILED_TO_GET_USER = "Failed to get user";
const SUCCESSFULLY_FETCHED_USER = "Successfully fetched user";
24 changes: 13 additions & 11 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 All @@ -16,7 +16,12 @@ export interface Payload {

export async function generateErc20PermitSignature(payload: Payload, username: string, amount: number, tokenAddress: string): Promise<PermitReward>;
export async function generateErc20PermitSignature(context: Context, username: string, amount: number, tokenAddress: string): Promise<PermitReward>;
export async function generateErc20PermitSignature(contextOrPayload: Context | Payload, username: string, amount: number, tokenAddress: string): Promise<PermitReward> {
export async function generateErc20PermitSignature(
contextOrPayload: Context | Payload,
username: string,
amount: number,
tokenAddress: string
): Promise<PermitReward> {
let _logger: Logger;
const _username = username;
let _walletAddress: string | null | undefined;
Expand Down Expand Up @@ -63,24 +68,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
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/* Modules */
"module": "ES2022" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "Node", /* Specify how TypeScript looks up a file from a given module specifier. */
"moduleResolution": "Node" /* Specify how TypeScript looks up a file from a given module specifier. */,
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
Expand Down
Loading
Loading