Skip to content

Commit

Permalink
feat: rename priceInEth and retrieve decimals for payment token
Browse files Browse the repository at this point in the history
  • Loading branch information
whilefoo committed Sep 24, 2023
1 parent 3056bc9 commit e344906
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
18 changes: 9 additions & 9 deletions src/handlers/payout/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { BigNumber } from "ethers";
export interface CreatorCommentResult {
title: string;
account?: string | undefined;
amountInETH?: Decimal | undefined;
rewardInTokens?: Decimal | undefined;
userId?: string | undefined;
tokenSymbol?: string | undefined;
node_id?: string | undefined;
Expand Down Expand Up @@ -148,8 +148,8 @@ export const calculateIssueCreatorReward = async (incentivesCalculation: Incenti
incentivesCalculation.paymentPermitMaxPrice
);

if (!result || !result.account || !result.amountInETH) {
throw new Error("Failed to generate permit for issue creator because of missing account or amountInETH");
if (!result || !result.account || !result.rewardInTokens) {
throw new Error("Failed to generate permit for issue creator because of missing account or rewardInTokens");
}

return {
Expand All @@ -159,7 +159,7 @@ export const calculateIssueCreatorReward = async (incentivesCalculation: Incenti
username: creator.login,
reward: [
{
priceInEth: result?.amountInETH ?? new Decimal(0),
priceInEth: result?.rewardInTokens ?? new Decimal(0),
account: result?.account,
userId: "",
user: "",
Expand Down Expand Up @@ -278,7 +278,7 @@ const generatePermitForComments = async (
multiplier: number,
incentives: Incentives,
paymentPermitMaxPrice: number
): Promise<undefined | { account: string; amountInETH: Decimal }> => {
): Promise<undefined | { account: string; rewardInTokens: Decimal }> => {
const logger = getLogger();
const commentsByNode = await parseComments(comments, ItemsToExclude);
const rewardValue = calculateRewardValue(commentsByNode, incentives);
Expand All @@ -288,15 +288,15 @@ const generatePermitForComments = async (
}
logger.debug(`Comment parsed for the user: ${user}. comments: ${JSON.stringify(commentsByNode)}, sum: ${rewardValue}`);
const account = await getWalletAddress(user);
const amountInETH = rewardValue.mul(multiplier);
if (amountInETH.gt(paymentPermitMaxPrice)) {
const rewardInTokens = rewardValue.mul(multiplier);
if (rewardInTokens.gt(paymentPermitMaxPrice)) {
logger.info(`Skipping issue creator reward for user ${user} because reward is higher than payment permit max price`);
return;
}
if (account) {
return { account, amountInETH };
return { account, rewardInTokens };
} else {
return { account: "0x", amountInETH: new Decimal(0) };
return { account: "0x", rewardInTokens: new Decimal(0) };
}
};
/**
Expand Down
21 changes: 14 additions & 7 deletions src/helpers/permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { keccak256, toUtf8Bytes } from "ethers/lib/utils";
import Decimal from "decimal.js";
import { Payload } from "../types";
import { savePermit } from "../adapters/supabase";
import { ERC20ABI } from "../configs";

const PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3"; // same on all networks

Expand Down Expand Up @@ -45,16 +46,16 @@ type TxData = {
};

/**
* Generates permit2 signature data with `spender` and `amountInETH`
* Generates permit2 signature data with `spender` and `amountInTokens`
*
* @param spender The recipient address we're going to send tokens
* @param amountInETH The token amount in ETH
* @param amountInTokens The token amount
*
* @returns Permit2 url including base64 encocded data
*/
export const generatePermit2Signature = async (
spender: string,
amountInEth: Decimal,
amountInTokens: Decimal,
identifier: string,
userId = ""
): Promise<{ txData: TxData; payoutUrl: string }> => {
Expand All @@ -65,12 +66,15 @@ export const generatePermit2Signature = async (
const provider = new ethers.providers.JsonRpcProvider(rpc);
const adminWallet = new ethers.Wallet(privateKey, provider);

const tokenContract = new ethers.Contract(paymentToken, ERC20ABI, provider);
const decimals = await tokenContract.decimals();

const permitTransferFromData: PermitTransferFrom = {
permitted: {
// token we are permitting to be transferred
token: paymentToken,
// amount we are permitting to be transferred
amount: ethers.utils.parseUnits(amountInEth.toString(), 18),
amount: ethers.utils.parseUnits(amountInTokens.toString(), decimals),
},
// who can transfer the tokens
spender: spender,
Expand Down Expand Up @@ -101,9 +105,12 @@ export const generatePermit2Signature = async (

const base64encodedTxData = Buffer.from(JSON.stringify(txData)).toString("base64");

const payoutUrl = `${permitBaseUrl}?claim=${base64encodedTxData}&network=${networkId}`;
logger.info(`Generated permit2 url: ${payoutUrl}`);
return { txData, payoutUrl };
const payoutUrl = new URL(permitBaseUrl);
payoutUrl.searchParams.append("claim", base64encodedTxData);
payoutUrl.searchParams.append("network", networkId.toString());
logger.info(`Generated permit2 of amount ${amountInTokens} for user ${spender}, url: ${payoutUrl}`);

return { txData, payoutUrl: payoutUrl.toString() };
};

export const savePermitToDB = async (bountyHunterId: number, txData: TxData): Promise<Permit> => {
Expand Down

0 comments on commit e344906

Please sign in to comment.