From 58b993c30e0ee59bab5e63c9db707c9b6bf496e9 Mon Sep 17 00:00:00 2001 From: will pankiewicz Date: Mon, 15 Apr 2024 13:21:01 +0200 Subject: [PATCH] add kyc validity check --- packages/common/src/config.ts | 1 + .../common/src/constraints/CheckCandidates.ts | 12 +++++++++++- .../common/src/constraints/ValidityChecks.ts | 18 ++++++++++++++++++ packages/common/src/db/models.ts | 1 + packages/common/src/db/queries/Candidate.ts | 15 +++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/common/src/config.ts b/packages/common/src/config.ts index eb8aea873..8afc25487 100644 --- a/packages/common/src/config.ts +++ b/packages/common/src/config.ts @@ -31,6 +31,7 @@ export type ConfigSchema = { minSelfStake: number; commission: number; unclaimedEraThreshold: number; + enableKYC: boolean; }; cron: { monitor: string; diff --git a/packages/common/src/constraints/CheckCandidates.ts b/packages/common/src/constraints/CheckCandidates.ts index 7a8a61a5c..a74c213b9 100644 --- a/packages/common/src/constraints/CheckCandidates.ts +++ b/packages/common/src/constraints/CheckCandidates.ts @@ -8,6 +8,7 @@ import { checkConnectionTime, checkIdentity, checkKusamaRank, + checkKYC, checkLatestClientVersion, checkOffline, checkOnline, @@ -129,6 +130,14 @@ export const checkCandidate = async ( logger.info(`${candidate.name} beefy keys not valid`, constraintsLabel); } + const kycValid = + constraints.config?.constraints?.enableKYC == true + ? await checkKYC(candidate) + : true; + if (!kycValid) { + logger.info(`${candidate.name} kyc not valid`, constraintsLabel); + } + valid = onlineValid && validateValid && @@ -142,7 +151,8 @@ export const checkCandidate = async ( blockedValid && kusamaValid && providerValid && - beefyValid; + beefyValid && + kycValid; await setValid(candidate, valid); diff --git a/packages/common/src/constraints/ValidityChecks.ts b/packages/common/src/constraints/ValidityChecks.ts index e99d4adc4..faf7a73bc 100644 --- a/packages/common/src/constraints/ValidityChecks.ts +++ b/packages/common/src/constraints/ValidityChecks.ts @@ -8,6 +8,7 @@ import { setConnectionTimeInvalidity, setIdentityInvalidity, setKusamaRankInvalidity, + setKYCInvalidity, setLatestClientReleaseValidity, setOfflineAccumulatedInvalidity, setOnlineValidity, @@ -434,3 +435,20 @@ export const checkBeefyKeys = async ( throw new Error("could not make validity check"); } }; + +export const checkKYC = async (candidate: Candidate): Promise => { + try { + const isKYC = await queries.isKYC(candidate.stash); + if (isKYC) { + const invalidityString = `${candidate.name} is not KYC`; + await setKYCInvalidity(candidate, false, invalidityString); + return false; + } else { + await setKYCInvalidity(candidate, true); + return true; + } + } catch (e) { + logger.warn(`Error trying to get kyc...`, constraintsLabel); + throw new Error("could not make validity check"); + } +}; diff --git a/packages/common/src/db/models.ts b/packages/common/src/db/models.ts index 799e6483e..aa2f62a4e 100644 --- a/packages/common/src/db/models.ts +++ b/packages/common/src/db/models.ts @@ -88,6 +88,7 @@ export enum InvalidityReasonType { KUSAMA_RANK = "KUSAMA_RANK", PROVIDER = "PROVIDER", BEEFY = "BEEFY", + KYC = "KYC", } export interface InvalidityReason { diff --git a/packages/common/src/db/queries/Candidate.ts b/packages/common/src/db/queries/Candidate.ts index cde5ffc48..5668e2991 100644 --- a/packages/common/src/db/queries/Candidate.ts +++ b/packages/common/src/db/queries/Candidate.ts @@ -1053,6 +1053,21 @@ export const setBeefyKeysInvalidity = async ( ); }; +export const setKYCInvalidity = async ( + candidate: Candidate, + isValid: boolean, + message?: string, +): Promise => { + const invalidityMessage = message ? message : `${candidate.name} is not KYC`; + setCandidateInvalidity( + candidate, + InvalidityReasonType.KYC, + isValid, + invalidityMessage, + true, + ); +}; + // Sets valid boolean for node export const setValid = async ( candidate: Candidate,