diff --git a/packages/backend/src/registry.ts b/packages/backend/src/registry.ts index 59f3a178..37f1f830 100644 --- a/packages/backend/src/registry.ts +++ b/packages/backend/src/registry.ts @@ -483,6 +483,21 @@ export class Registry { return this.registry.getAuctionsByIds([auctionId]); } + 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: 'tlnt', + destinationAddress: receiverAddress + }, + this.registryConfig.privateKey, + fee + ) + ); + } + 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/service.ts b/packages/backend/src/service.ts index 028b662b..f91f8e0f 100644 --- a/packages/backend/src/service.ts +++ b/packages/backend/src/service.ts @@ -212,6 +212,9 @@ export class Service { if (!deployment.project.fundsReleased) { const fundsReleased = await this.releaseDeployerFundsByProjectId(deployment.projectId); + // Return remaining amount to owner + await this.returnCreatorFundsByProjectId(deployment.projectId); + await this.db.updateProjectById(deployment.projectId, { fundsReleased, }); @@ -1324,6 +1327,24 @@ export class Service { return false; } + async returnCreatorFundsByProjectId(projectId: string) { + const project = await this.db.getProjectById(projectId); + + if (!project || !project.auctionId) { + log(`Project ${projectId} ${!project ? 'not found' : 'does not have an auction'}`); + + return false; + } + + const auction = await this.getAuctionData(project.auctionId); + const amountToBeReturned = auction.winnerPrice * auction.numProviders + + await this.laconicRegistry.sendTokensToAccount( + project.paymentAddress, + amountToBeReturned.toString() + ); + } + async getDeployers(): Promise { const dbDeployers = await this.db.getDeployers();