Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

chore: create permit table and move data #817

Open
wants to merge 16 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
35 changes: 14 additions & 21 deletions src/adapters/supabase/helpers/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,37 +469,30 @@ export const removePenalty = async (username: string, repoName: string, tokenAdd

const getDbDataFromPermit = (permit: InsertPermit): Record<string, unknown> => {
return {
organization_id: permit.organizationId,
repository_id: permit.repositoryId,
issue_id: permit.issueId,
network_id: permit.networkId,
bounty_hunter_id: permit.bountyHunterId,
token_address: permit.tokenAddress,
payout_amount: permit.payoutAmount,
bounty_hunter_address: permit.bountyHunterAddress,
network: permit.networkId,
token: permit.tokenAddress,
amount: permit.payoutAmount,
nonce: permit.nonce,
deadline: permit.deadline,
beneficiary: permit.bountyHunterAddress,
owner: permit.walletOwnerAddress,
signature: permit.signature,
wallet_owner_address: permit.walletOwnerAddress,
};
};

const getPermitFromDbData = (data: Record<string, unknown>): Permit => {
return {
id: data.id,
createdAt: new Date(Date.parse(data.created_at as string)),
organizationId: data.organization_id,
repositoryId: data.repository_i,
issueId: data.issue_id,
networkId: data.network_id,
bountyHunterId: data.bounty_hunter_id,
tokenAddress: data.token_address,
payoutAmount: data.payout_amount,
bountyHunterAddress: data.bounty_hunter_address,
created: new Date(Date.parse(data.created as string)),
updated: new Date(Date.parse(data.updated as string)),
networkId: data.network,
tokenAddress: data.token,
payoutAmount: data.amount,
nonce: data.nonce,
deadline: data.deadline,
bountyHunterAddress: data.beneficiary,
walletOwnerAddress: data.owner,
signature: data.signature,
walletOwnerAddress: data.wallet_owner_address,
} as Permit;
};

Expand All @@ -509,8 +502,8 @@ export const savePermit = async (permit: InsertPermit): Promise<Permit> => {
.from("permits")
.insert({
...getDbDataFromPermit(permit),
created_at: new Date().toISOString(),
id: undefined, // id is auto-generated
created: new Date().toISOString(),
updated: new Date().toISOString(),
})
.select();
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/payout/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ export const handleIssueClosed = async (

const comment = createDetailsTable(price, payoutUrl, reward.user, detailsValue, reward.debug);

await savePermitToDB(Number(reward.userId), txData);
await savePermitToDB(txData);
permitComment += comment;

logger.info(`Skipping to generate a permit url for missing accounts. fallback: ${JSON.stringify(conversationRewards.fallbackReward)}`);
Expand Down
17 changes: 5 additions & 12 deletions src/helpers/permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ const PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3"; // same on

export type Permit = {
id: number;
createdAt: Date;
organizationId: number | null;
repositoryId: number;
issueId: number;
created: Date;
updated: Date;
networkId: number;
bountyHunterId: number;
bountyHunterAddress: string;
0x4007 marked this conversation as resolved.
Show resolved Hide resolved
tokenAddress: string;
payoutAmount: string;
Expand All @@ -25,7 +22,7 @@ export type Permit = {
walletOwnerAddress: string;
};

export type InsertPermit = Omit<Permit, "id" | "createdAt">;
export type InsertPermit = Omit<Permit, "id" | "created" | "updated">;

type TxData = {
permit: {
Expand Down Expand Up @@ -106,14 +103,14 @@ export const generatePermit2Signature = async (
return { txData, payoutUrl };
};

export const savePermitToDB = async (bountyHunterId: number, txData: TxData): Promise<Permit> => {
export const savePermitToDB = async (txData: TxData): Promise<Permit> => {
const logger = getLogger();

const context = getBotContext();
const payload = context.payload as Payload;
const issue = payload.issue;
const repository = payload.repository;
const organization = payload.organization;
//const organization = payload.organization;
if (!issue || !repository) {
logger.error("Cannot save permit to DB, missing issue, repository or organization");
throw new Error("Cannot save permit to DB, missing issue, repository or organization");
Expand All @@ -123,11 +120,7 @@ export const savePermitToDB = async (bountyHunterId: number, txData: TxData): Pr
const { networkId } = payout;

const permit: InsertPermit = {
organizationId: organization?.id ?? null,
repositoryId: repository?.id,
issueId: issue?.id,
networkId: networkId,
bountyHunterId: bountyHunterId,
tokenAddress: txData.permit.permitted.token,
payoutAmount: txData.permit.permitted.amount,
bountyHunterAddress: txData.transferDetails.to,
Expand Down
48 changes: 48 additions & 0 deletions supabase/migrations/20230923153903_new_permit.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
CREATE TABLE IF NOT EXISTS
new_permits (
id serial primary key,
created timestamptz not null,
updated timestamptz not null,
network smallserial not null,
token varchar(42) not null,
seprintour marked this conversation as resolved.
Show resolved Hide resolved
amount numeric not null,
nonce numeric not null,
0x4007 marked this conversation as resolved.
Show resolved Hide resolved
deadline numeric not null,
beneficiary varchar(42) not null,
owner varchar(42) not null,
signature varchar(132) not null
);

INSERT INTO
new_permits (
id,
created,
updated,
network,
token,
amount,
nonce,
deadline,
beneficiary,
owner,
signature
)
SELECT
id,
created_at,
created_at,
network_id,
token_address,
CAST(payout_amount AS numeric),
CAST(nonce AS numeric),
CAST(deadline AS numeric),
bounty_hunter_address,
wallet_owner_address,
signature
FROM
permits;

DROP TABLE permits;

ALTER TABLE new_permits
RENAME TO permits;
seprintour marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions supabase/migrations/20230923182214_fix_debit.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ALTER TABLE Debits
ALTER COLUMN amount TYPE NUMERIC;

ALTER TABLE Debits
RENAME COLUMN created_at TO created;

ALTER TABLE Debits
RENAME COLUMN updated_at TO updated;