diff --git a/packages/backend/src/entity/Deployer.ts b/packages/backend/src/entity/Deployer.ts index 4328a122..b894d98f 100644 --- a/packages/backend/src/entity/Deployer.ts +++ b/packages/backend/src/entity/Deployer.ts @@ -18,6 +18,9 @@ export class Deployer { @Column('varchar', { nullable: true }) minimumPayment!: string | null; + @Column('varchar', { nullable: true }) + deployerAddress!: string | null; + @ManyToMany(() => Project, (project) => project.deployers) projects!: Project[]; } diff --git a/packages/backend/src/entity/Deployment.ts b/packages/backend/src/entity/Deployment.ts index cf0889e8..5c772b32 100644 --- a/packages/backend/src/entity/Deployment.ts +++ b/packages/backend/src/entity/Deployment.ts @@ -38,20 +38,17 @@ export interface ApplicationDeploymentRequest { auction?: string; config: string; meta: string; - payment: string; + payment?: string; } export interface ApplicationDeploymentRemovalRequest { type: string; version: string; deployment: string; + auction?: string; + payment?: string; } -export interface ApplicationDeploymentRemovalRequest { - type: string; - version: string; - deployment: string; -} export interface ApplicationRecord { type: string; diff --git a/packages/backend/src/entity/Project.ts b/packages/backend/src/entity/Project.ts index aa622dd7..a27b0f06 100644 --- a/packages/backend/src/entity/Project.ts +++ b/packages/backend/src/entity/Project.ts @@ -52,6 +52,10 @@ export class Project { @Column('varchar', { nullable: true }) auctionId!: string | null; + // Tx hash for sending coins from snowball to deployer + @Column('varchar', { nullable: true }) + txHash!: string | null; + @ManyToMany(() => Deployer, (deployer) => (deployer.projects)) @JoinTable() deployers!: Deployer[] @@ -66,12 +70,10 @@ export class Project { @Column('varchar', { nullable: true }) framework!: string | null; + // Address of the user who created the project i.e. requested deployments @Column('varchar') paymentAddress!: string; - @Column('varchar') - txHash!: string; - @Column({ type: 'simple-array' }) diff --git a/packages/backend/src/registry.ts b/packages/backend/src/registry.ts index 3e35d779..d19d83f4 100644 --- a/packages/backend/src/registry.ts +++ b/packages/backend/src/registry.ts @@ -5,8 +5,8 @@ import { Octokit } from 'octokit'; import { inc as semverInc } from 'semver'; import { DeepPartial } from 'typeorm'; -import { Account, Registry as LaconicRegistry, getGasPrice, parseGasAndFees } from '@cerc-io/registry-sdk'; -import { IndexedTx } from '@cosmjs/stargate'; +import { Account, DEFAULT_GAS_ESTIMATION_MULTIPLIER, Registry as LaconicRegistry, getGasPrice, parseGasAndFees } from '@cerc-io/registry-sdk'; +import { DeliverTxResponse, IndexedTx } from '@cosmjs/stargate'; import { RegistryConfig } from './config'; import { @@ -248,7 +248,7 @@ export class Registry { lrn: string, environmentVariables: { [key: string]: string }, dns: string, - payment: string + payment?: string | null }): Promise<{ applicationDeploymentRequestId: string; applicationDeploymentRequestData: ApplicationDeploymentRequest; @@ -268,7 +268,6 @@ export class Registry { name: `${applicationRecord.attributes.name}@${applicationRecord.attributes.app_version}`, application: `${lrn}@${applicationRecord.attributes.app_version}`, dns: data.dns, - payment: data.payment, // https://git.vdb.to/cerc-io/laconic-registry-cli/commit/129019105dfb93bebcea02fde0ed64d0f8e5983b config: JSON.stringify({ @@ -283,6 +282,7 @@ export class Registry { }), deployer: data.lrn, ...(data.auctionId && { auction: data.auctionId }), + ...(data.payment && { payment: data.payment }), }; await sleep(SLEEP_DURATION); @@ -430,6 +430,8 @@ export class Registry { async createApplicationDeploymentRemovalRequest(data: { deploymentId: string; deployerLrn: string; + auctionId?: string | null; + payment?: string | null; }): Promise<{ applicationDeploymentRemovalRequestId: string; applicationDeploymentRemovalRequestData: ApplicationDeploymentRemovalRequest; @@ -438,7 +440,9 @@ export class Registry { type: APP_DEPLOYMENT_REMOVAL_REQUEST_TYPE, version: '1.0.0', deployment: data.deploymentId, - deployer: data.deployerLrn + deployer: data.deployerLrn, + ...(data.auctionId && { auction: data.auctionId }), + ...(data.payment && { payment: data.payment }), }; const fee = parseGasAndFees(this.registryConfig.fee.gas, this.registryConfig.fee.fees); @@ -486,19 +490,23 @@ export class Registry { return this.registry.getAuctionsByIds([auctionId]); } - async sendTokensToAccount(receiverAddress: string, amount: string): Promise { + async sendTokensToAccount(receiverAddress: string, amount: string): Promise { const fee = parseGasAndFees(this.registryConfig.fee.gas, this.registryConfig.fee.fees); - await registryTransactionWithRetry(() => - this.registry.sendCoins( - { - amount, - denom: 'alnt', - destinationAddress: receiverAddress - }, - this.registryConfig.privateKey, - fee - ) - ); + const account = await this.getAccount(); + const laconicClient = await this.registry.getLaconicClient(account); + const txResponse: DeliverTxResponse = + await registryTransactionWithRetry(() => + laconicClient.sendTokens(account.address, receiverAddress, + [ + { + denom: 'alnt', + amount + } + ], + fee || DEFAULT_GAS_ESTIMATION_MULTIPLIER) + ); + + return txResponse; } async getAccount(): Promise { diff --git a/packages/backend/src/service.ts b/packages/backend/src/service.ts index b5c57cf5..f2c3afb3 100644 --- a/packages/backend/src/service.ts +++ b/packages/backend/src/service.ts @@ -649,7 +649,7 @@ export class Service { environmentVariables: environmentVariablesObj, dns: `${newDeployment.project.name}`, lrn: deployer!.deployerLrn!, - payment: data.project.txHash! + payment: data.project.txHash }); } @@ -661,7 +661,7 @@ export class Service { lrn: deployer!.deployerLrn!, environmentVariables: environmentVariablesObj, dns: `${newDeployment.project.name}-${newDeployment.id}`, - payment: data.project.txHash! + payment: data.project.txHash }); await this.db.updateDeploymentById(newDeployment.id, { @@ -727,7 +727,7 @@ export class Service { dns: `${newDeployment.project.name}`, auctionId: project.auctionId!, lrn: deployerLrn, - payment: project.txHash! + payment: project.txHash }); } @@ -741,7 +741,7 @@ export class Service { lrn: deployerLrn, environmentVariables: environmentVariablesObj, dns: `${newDeployment.project.name}-${newDeployment.id}`, - payment: project.txHash! + payment: project.txHash }); await this.db.updateDeploymentById(newDeployment.id, { @@ -890,21 +890,50 @@ export class Service { per_page: 1, }); - // Create deployment with prod branch and latest commit - const deploymentData = { - project, - branch: project.prodBranch, - environment: Environment.Production, - domain: null, - commitHash: latestCommit.sha, - commitMessage: latestCommit.commit.message, - }; - if (auctionParams) { + // Create deployment with prod branch and latest commit + const deploymentData = { + project, + branch: project.prodBranch, + environment: Environment.Production, + domain: null, + commitHash: latestCommit.sha, + commitMessage: latestCommit.commit.message, + }; const { applicationDeploymentAuctionId } = await this.laconicRegistry.createApplicationDeploymentAuction(repo, octokit, auctionParams!, deploymentData); await this.updateProject(project.id, { auctionId: applicationDeploymentAuctionId }); } else { - const newDeployment = await this.createDeployment(user.id, octokit, deploymentData, lrn); + const deployer = await this.db.getDeployerByLRN(lrn!); + + if (!deployer) { + log('Invalid deployer LRN'); + return; + } + + if (deployer.minimumPayment && project.txHash) { + const txResponse = await this.laconicRegistry.sendTokensToAccount( + deployer?.deployerAddress!, + deployer?.minimumPayment + ); + + const txHash = txResponse.transactionHash; + if (txHash) { + await this.updateProject(project.id, { txHash }); + log('Funds transferrend to deployer'); + } + } + + const deploymentData = { + project, + branch: project.prodBranch, + environment: Environment.Production, + domain: null, + commitHash: latestCommit.sha, + commitMessage: latestCommit.commit.message, + deployer + }; + + const newDeployment = await this.createDeployment(user.id, octokit, deploymentData); // Update project with deployer await this.updateProjectWithDeployer(newDeployment.projectId, newDeployment.deployer); } @@ -1145,14 +1174,18 @@ export class Service { await this.laconicRegistry.createApplicationDeploymentRemovalRequest({ deploymentId: latestRecord.id, - deployerLrn: deployment.deployer.deployerLrn + deployerLrn: deployment.deployer.deployerLrn, + auctionId: deployment.project.auctionId, + payment: deployment.project.txHash }); } const result = await this.laconicRegistry.createApplicationDeploymentRemovalRequest({ deploymentId: deployment.applicationDeploymentRecordId, - deployerLrn: deployment.deployer.deployerLrn + deployerLrn: deployment.deployer.deployerLrn, + auctionId: deployment.project.auctionId, + payment: deployment.project.txHash }); await this.db.updateDeploymentById(deployment.id, { @@ -1346,13 +1379,13 @@ export class Service { } const auction = await this.getAuctionData(project.auctionId); - const maxPrice = Number(auction.maxPrice.quantity) * auction.numProviders; + const totalAuctionPrice = Number(auction.maxPrice.quantity) * auction.numProviders; let amountToBeReturned; if (winningDeployersPresent) { - amountToBeReturned = maxPrice - auction.winnerAddresses.length * Number(auction.winnerPrice.quantity); + amountToBeReturned = totalAuctionPrice - auction.winnerAddresses.length * Number(auction.winnerPrice.quantity); } else { - amountToBeReturned = maxPrice; + amountToBeReturned = totalAuctionPrice; } await this.laconicRegistry.sendTokensToAccount( @@ -1389,7 +1422,8 @@ export class Service { const deployerId = record.id; const deployerLrn = record.names[0]; const deployerApiUrl = record.attributes.apiUrl; - const minimumPayment = record.attributes.minimumPayment + const minimumPayment = record.attributes.minimumPayment; + const deployerAddress = record.attributes.paymentAddress; const baseDomain = deployerApiUrl.substring(deployerApiUrl.indexOf('.') + 1); const deployerData = { @@ -1397,7 +1431,8 @@ export class Service { deployerId, deployerApiUrl, baseDomain, - minimumPayment + minimumPayment, + deployerAddress }; // TODO: Update deployers table in a separate job