From f1e758f25dda8dd6e33ddaa85d434d21179fa30f Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Mon, 7 Oct 2024 10:14:03 +0530 Subject: [PATCH] Check for auction status in a loop --- .../backend/environments/local.toml.example | 14 ++--- packages/backend/src/config.ts | 1 + packages/backend/src/entity/Project.ts | 3 -- packages/backend/src/registry.ts | 15 +++++- packages/backend/src/schema.gql | 1 - packages/backend/src/service.ts | 52 ++++++++++++++++--- 6 files changed, 68 insertions(+), 18 deletions(-) diff --git a/packages/backend/environments/local.toml.example b/packages/backend/environments/local.toml.example index 8c7374ef..5d392f39 100644 --- a/packages/backend/environments/local.toml.example +++ b/packages/backend/environments/local.toml.example @@ -29,8 +29,8 @@ [registryConfig] fetchDeploymentRecordDelay = 5000 - restEndpoint = "http://localhost:1317" - gqlEndpoint = "http://localhost:9473/api" + checkAuctionStatusDelay = 5000 + restEndpoint = "http://dss-daemon.test1.wireitin.com:26657" chainId = "laconic_9000-1" privateKey = "" bondId = "" @@ -41,11 +41,11 @@ gasPrice = "1" [auction] - commitFee = 1000 - commitsDuration = 60s - revealFee = 1000 - revealsDuration = 60s - denom = alnt + commitFee = "1000" + commitsDuration = "60s" + revealFee = "1000" + revealsDuration = "60s" + denom = "alnt" [misc] projectDomain = "apps.snowballtools.com" diff --git a/packages/backend/src/config.ts b/packages/backend/src/config.ts index b4a70c95..38285f72 100644 --- a/packages/backend/src/config.ts +++ b/packages/backend/src/config.ts @@ -34,6 +34,7 @@ export interface RegistryConfig { privateKey: string; bondId: string; fetchDeploymentRecordDelay: number; + checkAuctionStatusDelay: number; authority: string; fee: { gas: string; diff --git a/packages/backend/src/entity/Project.ts b/packages/backend/src/entity/Project.ts index ac028b4c..8b5f424a 100644 --- a/packages/backend/src/entity/Project.ts +++ b/packages/backend/src/entity/Project.ts @@ -49,9 +49,6 @@ export class Project { @Column('varchar', { nullable: true }) auctionId?: string | null; - @Column('varchar', { nullable: true }) - auctionStatus?: string | null; - @Column('varchar', { nullable: true }) deployerLrn?: string[] | null; diff --git a/packages/backend/src/registry.ts b/packages/backend/src/registry.ts index b2f53e45..efa739a4 100644 --- a/packages/backend/src/registry.ts +++ b/packages/backend/src/registry.ts @@ -6,6 +6,7 @@ import { DeepPartial } from 'typeorm'; import { Octokit } from 'octokit'; import { Registry as LaconicRegistry, parseGasAndFees } from '@cerc-io/registry-sdk'; +import { Auction } from '@cerc-io/registry-sdk/dist/proto/cerc/auction/v1/auction'; import { RegistryConfig } from './config'; import { @@ -328,7 +329,7 @@ export class Registry { const records = await this.registry.getAuctionsByIds([auctionId]); const auctionResult = records[0]; - let deployerLrns = []; + let deployerLrns = []; const { winnerAddresses } = auctionResult.auction; for (const auctionWinner of winnerAddresses) { @@ -441,6 +442,18 @@ export class Registry { }; } + async getCompletedAuctionIds(auctionIds: string[] | null | undefined): Promise { + if(auctionIds === null || auctionIds === undefined) { + return []; + } + const auctions = await this.registry.getAuctionsByIds(auctionIds); + const completedAuctions = auctions + .filter((auction: Auction) => auction.status === 'completed') + .map((auction: Auction) => auction.id); + + return completedAuctions; + } + getLrn(appName: string): string { assert(this.registryConfig.authority, "Authority doesn't exist"); return `lrn://${this.registryConfig.authority}/applications/${appName}`; diff --git a/packages/backend/src/schema.gql b/packages/backend/src/schema.gql index 17079448..d3178e40 100644 --- a/packages/backend/src/schema.gql +++ b/packages/backend/src/schema.gql @@ -67,7 +67,6 @@ type Project { description: String deployerLrn: [String] auctionId: String - auctionStatus: String template: String framework: String webhooks: [String!] diff --git a/packages/backend/src/service.ts b/packages/backend/src/service.ts index 23785505..50d5b511 100644 --- a/packages/backend/src/service.ts +++ b/packages/backend/src/service.ts @@ -1,6 +1,6 @@ import assert from 'assert'; import debug from 'debug'; -import { DeepPartial, FindOptionsWhere } from 'typeorm'; +import { DeepPartial, FindOptionsWhere, IsNull } from 'typeorm'; import { Octokit, RequestError } from 'octokit'; import { OAuthApp } from '@octokit/oauth-app'; @@ -263,6 +263,44 @@ export class Service { await Promise.all(deploymentUpdatePromises); } + /** + * Checks for auction status for all ongoing auctions + * Calls the createDeploymentFromAuction method for deployments with completed auctions + */ + async checkAuctionStatus( + ): Promise { + // Deployment should be in building state and should not have domain. + const deployments = await this.db.getDeployments({ + where: { + status: DeploymentStatus.Building, + domain: IsNull() + }, + }); + + // Check the auctionId for those deployments with auctions + const auctionIds = deployments + .filter(deployment => deployment.project && deployment.project.auctionId) + .map(deployment => deployment.project!.auctionId) as string[]; + // Get all the auctions for those ids with auction status completed + const completedAuctionIds = await this.registry.getCompletedAuctionIds(auctionIds); + // If the deplyer lrn array is empty then call createDeploymentFromAuction + const auctionDeployments = deployments.filter(deployment => + completedAuctionIds.includes(deployment.project!.auctionId!) && + deployment.project!.deployerLrn?.length === 0 + ); + + for (const auctionDeployment of auctionDeployments) { + await this.createDeploymentFromAuction( + 'user id', + auctionDeployment.project!.auctionId!, + auctionDeployment + ); + } + this.deployRecordCheckTimeout = setTimeout(() => { + this.checkAuctionStatus(); + }, this.config.registryConfig.fetchDeploymentRecordDelay); + } + async getUser(userId: string): Promise { return this.db.getUser({ where: { @@ -645,13 +683,10 @@ export class Service { }); // Save deployer lrn only if present - let updateData: Partial = {}; if (lrn) { - updateData.deployerLrn = [lrn]; + newDeployment.project.deployerLrn = [lrn]; } - await this.db.updateProjectById(data.project.id!, updateData); - return newDeployment; } @@ -659,6 +694,7 @@ export class Service { userId: string, auctionId: string, // take project data + // project: DeepPartial, data: DeepPartial, ) { // TODO: If data.domain is present then call createDeployment (don't need auction) @@ -727,7 +763,11 @@ export class Service { }); } - return newDeployment; + // update project with deployerlrns + + await this.db.updateProjectById(data.project?.id!, { + deployerLrn: deployerLrns + }) } async addProjectFromTemplate(