Skip to content

Commit

Permalink
fix(credits): type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Oct 7, 2023
1 parent 5be3203 commit 79b1631
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
43 changes: 42 additions & 1 deletion src/adapters/supabase/helpers/classes/Settlement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Super } from "./Super";

type DebitInsert = Database["public"]["Tables"]["debits"]["Insert"];
type CreditInsert = Database["public"]["Tables"]["credits"]["Insert"];
type PermitInsert = Database["public"]["Tables"]["permits"]["Insert"];
type SettlementInsert = Database["public"]["Tables"]["settlements"]["Insert"];
type AddDebit = {
userId: number;
Expand All @@ -14,6 +15,13 @@ type AddDebit = {
networkId: number;
address: string;
};
type AddCreditWithPermit = {
userId: number;
amount: Decimal;
comment: Comment;
permit?: PermitInsert;
};

export class Settlement extends Super {
constructor(supabase: SupabaseClient) {
super(supabase);
Expand Down Expand Up @@ -72,7 +80,7 @@ export class Settlement extends Super {
if (!settlementInsertData) throw new Error("Settlement not inserted");
}

public async addCredit({ userId, amount, comment }: AddDebit): Promise<void> {
public async addCredit({ userId, amount, comment, permit }: AddCreditWithPermit): Promise<void> {
// Insert into the credits table
const creditData: CreditInsert = {
amount: amount.toNumber(),
Expand All @@ -90,6 +98,39 @@ export class Settlement extends Super {
if (creditError) throw creditError;
if (!creditInsertData) throw new Error("Credit not inserted");

// Insert into the permits table if permit data is provided
let permitInsertData;
if (permit) {
const permitData: PermitInsert = {
amount: permit.amount,
nonce: permit.nonce,
deadline: permit.deadline,
signature: permit.signature,
token_id: permit.token_id,
partner_id: permit.partner_id,
beneficiary_id: userId,
node_id: comment.node_id,
node_type: "IssueComment",
node_url: comment.html_url,
};

const permitResult = await this.client.from("permits").insert(permitData).select("*").single();

if (permitResult.error) throw permitResult.error;
if (!permitResult.data) throw new Error("Permit not inserted");
permitInsertData = permitResult.data;
}

// Update the credits table with permit_id if permit data is provided
if (permitInsertData) {
const { error: creditUpdateError } = await this.client
.from("credits")
.update({ permit_id: permitInsertData.id })
.eq("id", creditInsertData.id);

if (creditUpdateError) throw creditUpdateError;
}

// Insert into the settlements table
const settlementData: SettlementInsert = {
id: creditInsertData.id,
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/payout/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export async function removePenalty({ userId, amount, node, networkId, tokenAddr
userId: userId,
amount: amount,
comment: node,
networkId: networkId,
address: tokenAddress,
// networkId: networkId,
// address: tokenAddress,
});

// logger.debug(`Removing penalty done, { data: ${JSON.stringify(error)}, error: ${JSON.stringify(error)} }`);
Expand Down
10 changes: 2 additions & 8 deletions src/helpers/permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,8 @@ type TxData = {
signature: string;
};

/**
* Generates permit2 signature data with `spender` and `amountInETH`
*
* @param spender The recipient address we're going to send tokens
* @param amountInETH The token amount in ETH
*
* @returns Permit2 url including base64 encoded data
*/
export const generatePermit2Signature = async (
// Generates permit2 signature data with `spender` and `amountInETH`
spender: string,
amountInEth: Decimal,
identifier: string,
Expand Down Expand Up @@ -142,6 +135,7 @@ export const savePermitToDB = async (contributorId: number, txData: TxData): Pro
};

const savedPermit = await savePermit(permit);

Check failure on line 137 in src/helpers/permit.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'savePermit'. Did you mean 'savedPermit'?

Check failure on line 137 in src/helpers/permit.ts

View workflow job for this annotation

GitHub Actions / e2e-test

Cannot find name 'savePermit'. Did you mean 'savedPermit'?

logger.info(`Saved permit to DB: ${JSON.stringify(savedPermit)}`);
return savedPermit;
};

0 comments on commit 79b1631

Please sign in to comment.