Skip to content

Commit

Permalink
Merge pull request #2605 from w3f/will-v3.0.18
Browse files Browse the repository at this point in the history
Add Types to Nominator DB Queries
  • Loading branch information
wpank authored Feb 16, 2024
2 parents 8241bb6 + 12fc985 commit a4ca3da
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 52 deletions.
2 changes: 1 addition & 1 deletion apps/1kv-backend-staging/templates/kusama-otv-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
source:
repoURL: https://w3f.github.io/helm-charts/
chart: otv-backend
targetRevision: v3.0.17
targetRevision: v3.0.18
plugin:
env:
- name: HELM_VALUES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
source:
repoURL: https://w3f.github.io/helm-charts/
chart: otv-backend
targetRevision: v3.0.17
targetRevision: v3.0.18
plugin:
env:
- name: HELM_VALUES
Expand Down
2 changes: 1 addition & 1 deletion apps/1kv-backend/templates/kusama-otv-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
source:
repoURL: https://w3f.github.io/helm-charts/
chart: otv-backend
targetRevision: v3.0.17
targetRevision: v3.0.18
plugin:
env:
- name: HELM_VALUES
Expand Down
2 changes: 1 addition & 1 deletion apps/1kv-backend/templates/polkadot-otv-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
source:
repoURL: https://w3f.github.io/helm-charts/
chart: otv-backend
targetRevision: v3.0.17
targetRevision: v3.0.18
plugin:
env:
- name: HELM_VALUES
Expand Down
4 changes: 2 additions & 2 deletions charts/otv-backend/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
description: 1K Validators Backend
name: otv-backend
version: v3.0.17
appVersion: v3.0.17
version: v3.0.18
appVersion: v3.0.18
apiVersion: v2
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1kv/common",
"version": "3.0.17",
"version": "3.0.18",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type NodeDetails = [

export * from "./queries";

export * from "./models";

export const dbLabel = { label: "DB" };

export class Db {
Expand Down
16 changes: 16 additions & 0 deletions packages/common/src/db/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,22 @@ export const EraSchema = new Schema({

export const EraModel = mongoose.model("Era", EraSchema);

export interface Nominator {
address: string;
stash?: string;
proxy?: string;
bonded?: number;
avgStake?: number;
nominateAmount?: number;
proxyDelay?: number;
rewardDestination?: string;
newBondedAmount?: number;
current?: [{ name?: string; stash?: string; identity?: any }];
lastNomination?: number;
createdAt?: number;
now?: number;
}

export const NominatorSchema = new Schema({
// The controller address
address: String,
Expand Down
63 changes: 24 additions & 39 deletions packages/common/src/db/queries/Nominator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CandidateModel, NominatorModel } from "../models";
import { CandidateModel, Nominator, NominatorModel } from "../models";
import logger from "../../logger";
import { getCandidate } from "./Candidate";

Expand All @@ -24,26 +24,19 @@ export const removeStaleNominators = async (
};

/** Nominator accessor functions */
export const addNominator = async (
nominator: any,
// address: string,
// stash: string,
// proxy: string,
// bonded: number,
// now: number,
// proxyDelay: number,
// rewardDestination: string

// avgStake: number,
// nominateAmount: number,
// newBondedAmount: number
): Promise<boolean> => {
const { address, stash, proxy, bonded, now, proxyDelay, rewardDestination } =
nominator;

logger.info(`(Db::addNominator) Adding ${address} at ${now}.`);

export const addNominator = async (nominator: Nominator): Promise<boolean> => {
try {
const {
address,
stash,
proxy,
bonded,
now,
proxyDelay,
rewardDestination,
} = nominator;

logger.info(`(Db::addNominator) Adding ${address} at ${now}.`);
const data = await NominatorModel.findOne({ address }).lean();
if (!data) {
const nominator = new NominatorModel({
Expand All @@ -53,9 +46,6 @@ export const addNominator = async (
bonded,
proxyDelay,
rewardDestination,
// avgStake,
// nominateAmount,
// newBondedAmount,
current: [],
lastNomination: 0,
createdAt: now,
Expand All @@ -75,19 +65,10 @@ export const addNominator = async (
bonded,
proxyDelay,
rewardDestination,
// avgStake,
// nominateAmount,
// newBondedAmount,
},
);
} catch (e) {
logger.info(JSON.stringify(e));
logger.info(address);
logger.info(stash);
logger.info(proxy);
logger.info(bonded);
logger.info(proxyDelay);
logger.info(rewardDestination);
}
};

Expand Down Expand Up @@ -182,11 +163,15 @@ export const setLastNomination = async (
return true;
};

export const getCurrentTargets = async (address: string): Promise<any[]> => {
export const getCurrentTargets = async (
address: string,
): Promise<{ name?: string; stash?: string; identity?: any }[]> => {
try {
const nominator = await NominatorModel.findOne({ address }).lean();
const nominator = await NominatorModel.findOne({
address,
}).lean<Nominator>();
if (nominator) {
return nominator?.current;
return nominator?.current || [];
} else {
return [];
}
Expand All @@ -195,10 +180,10 @@ export const getCurrentTargets = async (address: string): Promise<any[]> => {
}
};

export const allNominators = async (): Promise<any[]> => {
return NominatorModel.find({ address: /.*/ }).lean().exec();
export const allNominators = async (): Promise<Nominator[]> => {
return NominatorModel.find({ address: /.*/ }).lean();
};

export const getNominator = async (stash: string): Promise<any> => {
return NominatorModel.findOne({ stash: stash }).lean().exec();
export const getNominator = async (stash: string): Promise<Nominator> => {
return NominatorModel.findOne({ stash: stash }).lean();
};
2 changes: 2 additions & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "@polkadot/api-augment";
import { Db } from "./db";
import * as queries from "./db/queries";
import * as Models from "./db/models";
import * as Config from "./config";
import logger from "./logger";
import { ChainData } from "./chaindata";
Expand All @@ -23,4 +24,5 @@ export {
Util,
Constraints,
Score,
Models,
};
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1kv/core",
"version": "3.0.17",
"version": "3.0.18",
"description": "Services for running the Thousand Validator Program.",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/nominator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default class Nominator {
const ledger = await api.query.staking.ledger(this.controller);
if (!ledger.isSome) {
logger.warn(`Account ${this.controller} is not bonded!`);
return "0x";
return this.controller;
}
const { stash } = ledger.unwrap();

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/scorekeeper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Constants,
Constraints,
logger,
Models,
queries,
Types,
Util,
Expand Down Expand Up @@ -356,7 +357,7 @@ export default class ScoreKeeper {
// const { nominationNum, newBondedAmount, targetValStake } =
// await autoNumNominations(api, nom);

const nominator = {
const nominator: Models.Nominator = {
address: nom.controller,
stash: stash,
proxy: proxy,
Expand Down
2 changes: 1 addition & 1 deletion packages/gateway/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1kv/gateway",
"version": "3.0.17",
"version": "3.0.18",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/telemetry/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1kv/telemetry",
"version": "3.0.17",
"version": "3.0.18",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1kv/worker",
"version": "3.0.17",
"version": "3.0.18",
"description": "Services for running the Thousand Validator Program.",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down

0 comments on commit a4ca3da

Please sign in to comment.