Skip to content

Commit

Permalink
feat: encode decode permits (#10)
Browse files Browse the repository at this point in the history
* feat: encode decode functions for permit payloads

* chore: encode decode functions for permit payloads

* chore: changed username to be user id

* chore: wallet upsert for users

* chore: change prototype overload for permit generation

* chore: fixed tests for erc721

* chore: removed useless + in string

* chore: permit type checks for encode decode

* chore: changed permit type checks

* chore: removed unexported types

* chore: removed unnecessary ignoreDependencies directive for Knip

* chore: changed erc721 type to enforce 0 or 1 value

* v1.0.2

* fix!: changed back userId to username

* chore: renamed SUPABASE_ANON_KEY to SUPABASE_KEY

* chore: excluded Numberish in cspell
  • Loading branch information
gentlementlegen authored Apr 14, 2024
1 parent a54158a commit 5673641
Show file tree
Hide file tree
Showing 25 changed files with 1,360 additions and 277 deletions.
3 changes: 2 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"Libsodium",
"ciphertext",
"tweetnacl",
"typeguards"
"typeguards",
"Numberish"
]
}
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
X25519_PRIVATE_KEY=
NFT_MINTER_PRIVATE_KEY=
SUPABASE_URL=
SUPABASE_ANON_KEY=
SUPABASE_KEY=
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"plugins": ["@typescript-eslint", "sonarjs"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "plugin:sonarjs/recommended"],
"ignorePatterns": ["**/*.js"],
"ignorePatterns": ["**/*.js", "src/adapters/supabase/types/database.ts"],
"rules": {
"prefer-arrow-callback": ["warn", { "allowNamedFunctions": true }],
"func-style": ["warn", "declaration", { "allowArrowFunctions": false }],
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ubiquibot/permit-generation",
"version": "1.0.0",
"version": "1.0.2",
"description": "ECR20 / ECR721 permit generation for automated payments.",
"author": "Ubiquity DAO",
"license": "MIT",
Expand Down Expand Up @@ -31,7 +31,7 @@
"@octokit/rest": "^20.0.2",
"@octokit/webhooks": "^13.1.0",
"@sinclair/typebox": "^0.32.5",
"@supabase/supabase-js": "^2.39.7",
"@supabase/supabase-js": "2.42.0",
"@uniswap/permit2-sdk": "^1.2.0",
"dotenv": "^16.4.4",
"ethers": "6.11.1",
Expand Down
27 changes: 8 additions & 19 deletions src/adapters/supabase/helpers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,30 @@ export class User extends Super {
super(supabase, context);
}

async getUsernameById(userId: number) {
const { data, error } = await this.supabase.from("users").select("username").eq("user_id", userId).single();
async getUserById(userId: number) {
const { data, error } = await this.supabase.from("users").select("*").eq("id", userId).single();
if (error) {
console.error(FAILED_TO_GET_USER, { userId, error });
throw error;
}

console.log(SUCCESSFULLY_FETCHED_USER, { userId, username: data?.username });
return data?.username;
}

async getUserIdByUsername(username: string) {
const { data, error } = await this.supabase.from("users").select("user_id").eq("username", username).single();
if (error) {
console.error(FAILED_TO_GET_USER, { username, error });
throw error;
}

console.log(SUCCESSFULLY_FETCHED_USER, { username, userId: data?.user_id });
return data?.user_id;
console.log(SUCCESSFULLY_FETCHED_USER, { userId, ...data });
return data;
}

async getUserIdByWallet(wallet: string) {
const { data, error } = await this.supabase.from("wallets").select("user_id").eq("address", wallet).single();
const { data, error } = await this.supabase.from("wallets").select("id").eq("address", wallet).single();
if (error) {
console.error(FAILED_TO_GET_USER, { wallet, error });
throw error;
}

console.log(SUCCESSFULLY_FETCHED_USER, { wallet, userId: data?.user_id });
return data?.user_id;
console.log(SUCCESSFULLY_FETCHED_USER, { wallet, userId: data?.id });
return data?.id.toString();
}

async upsertUser(userId: number, username: string) {
const { error } = await this.supabase.from("users").upsert({ user_id: userId, username }).select();
const { error } = await this.supabase.from("users").upsert({ id: userId, username }).select();
if (error) {
console.error("Failed to upsert user", { userId, username, error });
throw error;
Expand Down
40 changes: 11 additions & 29 deletions src/adapters/supabase/helpers/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,31 @@ export class Wallet extends Super {
}

async getWalletByUserId(userId: number) {
const { data, error } = await this.supabase.from("wallets").select("address").eq("user_id", userId).single();
const { data, error } = await this.supabase.from("users").select("wallets(*)").eq("id", userId).single();
if (error) {
console.error("Failed to get wallet", { userId, error });
throw error;
}

console.info("Successfully fetched wallet", { userId, address: data.address });
return data.address;
console.info("Successfully fetched wallet", { userId, address: data.wallets?.address });
return data.wallets?.address;
}

async getWalletByUsername(username: string) {
const { data, error } = await this.supabase.from("users").select("id").eq("username", username).single();
if (error) {
console.error("Failed to get user", { username, error });
throw error;
}

const userId = data?.id;

if (!userId) {
console.error("Failed to get user", { username });
throw new Error("User not found");
}

const { data: walletData, error: walletError } = await this.supabase.from("wallets").select("address").eq("user_id", userId).single();
async upsertWallet(userId: number, address: string) {
const { error: walletError, data } = await this.supabase.from("wallets").upsert([{ address }]).select().single();

if (walletError) {
console.error("Failed to get wallet", { userId, error });
console.error("Failed to upsert wallet", { userId, address, walletError });
throw walletError;
}

console.info("Successfully fetched wallet", { userId, address: walletData?.address });

return walletData?.address as `0x${string}` | undefined;
}
const { error: userError } = await this.supabase.from("users").upsert([{ id: userId, wallet_id: data.id }]);

async upsertWallet(userId: number, address: string) {
const { error } = await this.supabase.from("wallets").upsert([{ user_id: userId.toString(), address }]);
if (error) {
console.error("Failed to upsert wallet", { userId, address, error });
throw error;
if (userError) {
console.error("Failed to upsert user with new wallet", { userId, address, userError });
throw userError;
}

console.info("Successfully upserted wallet", { userId, address });
console.info("Successfully upsert wallet", { userId, address });
}
}
Loading

0 comments on commit 5673641

Please sign in to comment.