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

[feature] add tokens to levels rules #1015

Merged
merged 1 commit into from
Nov 9, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class LearnAndEarnLevelModel extends Model<LearnAndEarnLevel, LearnAndEar
public rules!: {
countries: string[];
roles: string[];
tokens: { address: string; amount: number }[];
limitUsers: number;
};

Expand Down
34 changes: 29 additions & 5 deletions packages/core/src/services/learnAndEarn/answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import BigNumber from 'bignumber.js';

import { BaseError } from '../../utils/baseError';
import { cleanLearnAndEarnCache } from '../../utils/cache';
import { contracts } from '../../../index';
import { ethers } from 'ethers';
import { getUserRoles } from '../../subgraph/queries/user';
import { models, sequelize } from '../../database';
import config from '../../config';
Expand Down Expand Up @@ -251,6 +253,32 @@ export async function answer(user: { userId: number; address: string }, answers:
const availableLessons = await countAvailableLessons(lessonRegistry.levelId, user.userId);

if (availableLessons === 0) {
// verify if user comply with rules
const level = await models.learnAndEarnLevel.findOne({
attributes: ['rules', 'rewardLimit'],
where: { id: lessonRegistry.levelId }
});

if (!level) {
throw new BaseError('LEVEL_NOT_FOUND', 'level not found');
}

// validate if user has the required tokens
if (level.rules.tokens) {
const provider = new ethers.providers.StaticJsonRpcProvider(config.chain.jsonRPCUrlCelo, {
name: 'Celo',
chainId: config.chain.isMainnet ? 42220 : 44787
});
for (const token of level.rules.tokens) {
const tokenContract = new ethers.Contract(token.address, contracts.ERC20ABI, provider);
const balance = new BigNumber((await tokenContract.balanceOf(user.address)).toString());
const amount = new BigNumber(token.amount).multipliedBy(new BigNumber(10).pow(18));
if (balance.lt(amount)) {
throw new BaseError('INSUFFICIENT_TOKENS', 'user has insufficient tokens');
}
}
}

// if so, complete the level and make the payment
await models.learnAndEarnUserLevel.update(
{
Expand All @@ -267,15 +295,11 @@ export async function answer(user: { userId: number; address: string }, answers:
);

// create signature
const level = await models.learnAndEarnLevel.findOne({
attributes: ['rewardLimit'],
where: { id: lessonRegistry.levelId }
});
const amount = await calculateReward(user.userId, lessonRegistry.levelId, points);
const signature = await signParams(user.address, lessonRegistry.levelId, amount);

// check available reward
if (level?.rewardLimit) {
if (level.rewardLimit) {
const payments = await models.learnAndEarnPayment.sum('amount', {
where: {
levelId: lessonRegistry.levelId
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/services/learnAndEarn/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export async function listLevels(
prismicId: string;
totalReward: number;
totalLessons: number;
asset: string;
// this is actually a json object
// eslint-disable-next-line @typescript-eslint/no-explicit-any
rules: any;
status: string; // TODO: to be removed
}[];
}> {
Expand All @@ -45,7 +49,7 @@ export async function listLevels(
}

include.push({
attributes: ['id', 'totalReward', 'lessons', 'asset'],
attributes: ['id', 'totalReward', 'lessons', 'asset', 'rules'],
model: learnAndEarnLevel,
where: {
[Op.and]: [
Expand Down Expand Up @@ -103,6 +107,7 @@ export async function listLevels(
totalReward: level!.totalReward,
totalLessons: level!.lessons,
asset: level!.asset,
rules: level!.rules,
status: level!.userLevel?.status || 'available'
}))
};
Expand Down
Loading