2024-02-14 05:33:22 +00:00
|
|
|
import assert from 'assert';
|
2024-10-18 12:37:01 +00:00
|
|
|
import debug from 'debug';
|
2024-02-14 05:33:22 +00:00
|
|
|
import { DateTime } from 'luxon';
|
2024-10-18 12:37:01 +00:00
|
|
|
import { Octokit } from 'octokit';
|
|
|
|
import { inc as semverInc } from 'semver';
|
|
|
|
import { DeepPartial } from 'typeorm';
|
2024-02-12 06:04:01 +00:00
|
|
|
|
2024-10-18 12:37:01 +00:00
|
|
|
import { Registry as LaconicRegistry, getGasPrice, parseGasAndFees } from '@cerc-io/registry-sdk';
|
2024-02-12 06:04:01 +00:00
|
|
|
|
|
|
|
import { RegistryConfig } from './config';
|
2024-02-22 05:45:17 +00:00
|
|
|
import {
|
|
|
|
ApplicationRecord,
|
|
|
|
Deployment,
|
2024-03-27 16:41:53 +00:00
|
|
|
ApplicationDeploymentRequest,
|
|
|
|
ApplicationDeploymentRemovalRequest
|
2024-02-22 05:45:17 +00:00
|
|
|
} from './entity/Deployment';
|
2024-10-22 09:12:59 +00:00
|
|
|
import { AppDeploymentRecord, AppDeploymentRemovalRecord, AuctionParams, DeployerRecord } from './types';
|
2024-10-24 11:38:17 +00:00
|
|
|
import { getConfig, getRepoDetails, registryTransactionWithRetry, sleep } from './utils';
|
2024-02-12 06:04:01 +00:00
|
|
|
|
|
|
|
const log = debug('snowball:registry');
|
|
|
|
|
|
|
|
const APP_RECORD_TYPE = 'ApplicationRecord';
|
2024-10-18 12:37:01 +00:00
|
|
|
const APP_DEPLOYMENT_AUCTION_RECORD_TYPE = 'ApplicationDeploymentAuction';
|
2024-02-19 08:13:29 +00:00
|
|
|
const APP_DEPLOYMENT_REQUEST_TYPE = 'ApplicationDeploymentRequest';
|
2024-03-27 16:41:53 +00:00
|
|
|
const APP_DEPLOYMENT_REMOVAL_REQUEST_TYPE = 'ApplicationDeploymentRemovalRequest';
|
2024-02-19 08:13:29 +00:00
|
|
|
const APP_DEPLOYMENT_RECORD_TYPE = 'ApplicationDeploymentRecord';
|
2024-04-15 14:06:04 +00:00
|
|
|
const APP_DEPLOYMENT_REMOVAL_RECORD_TYPE = 'ApplicationDeploymentRemovalRecord';
|
2024-10-22 09:12:59 +00:00
|
|
|
const WEBAPP_DEPLOYER_RECORD_TYPE = 'WebappDeployer'
|
2024-02-29 13:03:34 +00:00
|
|
|
const SLEEP_DURATION = 1000;
|
2024-02-12 06:04:01 +00:00
|
|
|
|
2024-10-16 08:43:51 +00:00
|
|
|
// TODO: Move registry code to registry-sdk/watcher-ts
|
2024-02-12 06:04:01 +00:00
|
|
|
export class Registry {
|
|
|
|
private registry: LaconicRegistry;
|
|
|
|
private registryConfig: RegistryConfig;
|
|
|
|
|
2024-10-18 12:37:01 +00:00
|
|
|
constructor(registryConfig: RegistryConfig) {
|
2024-02-12 06:04:01 +00:00
|
|
|
this.registryConfig = registryConfig;
|
2024-10-18 12:37:01 +00:00
|
|
|
|
|
|
|
const gasPrice = getGasPrice(registryConfig.fee.gasPrice);
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
this.registry = new LaconicRegistry(
|
|
|
|
registryConfig.gqlEndpoint,
|
|
|
|
registryConfig.restEndpoint,
|
2024-10-18 12:37:01 +00:00
|
|
|
{ chainId: registryConfig.chainId, gasPrice }
|
2024-02-22 05:45:17 +00:00
|
|
|
);
|
2024-02-12 06:04:01 +00:00
|
|
|
}
|
|
|
|
|
2024-10-18 12:37:01 +00:00
|
|
|
async createApplicationRecord({
|
|
|
|
octokit,
|
|
|
|
repository,
|
2024-02-14 05:33:22 +00:00
|
|
|
commitHash,
|
2024-02-15 11:54:57 +00:00
|
|
|
appType,
|
2024-02-14 05:33:22 +00:00
|
|
|
}: {
|
2024-10-18 12:37:01 +00:00
|
|
|
octokit: Octokit
|
|
|
|
repository: string;
|
2024-02-22 05:45:17 +00:00
|
|
|
commitHash: string;
|
|
|
|
appType: string;
|
|
|
|
}): Promise<{
|
|
|
|
applicationRecordId: string;
|
|
|
|
applicationRecordData: ApplicationRecord;
|
|
|
|
}> {
|
2024-10-18 12:37:01 +00:00
|
|
|
const { repo, repoUrl, packageJSON } = await getRepoDetails(octokit, repository, commitHash)
|
2024-10-16 08:43:51 +00:00
|
|
|
// Use registry-sdk to publish record
|
2024-02-12 06:04:01 +00:00
|
|
|
// Reference: https://git.vdb.to/cerc-io/test-progressive-web-app/src/branch/main/scripts/publish-app-record.sh
|
|
|
|
// Fetch previous records
|
2024-02-22 05:45:17 +00:00
|
|
|
const records = await this.registry.queryRecords(
|
|
|
|
{
|
|
|
|
type: APP_RECORD_TYPE,
|
|
|
|
name: packageJSON.name
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
2024-02-12 06:04:01 +00:00
|
|
|
|
|
|
|
// Get next version of record
|
2024-02-22 05:45:17 +00:00
|
|
|
const bondRecords = records.filter(
|
|
|
|
(record: any) => record.bondId === this.registryConfig.bondId
|
|
|
|
);
|
|
|
|
const [latestBondRecord] = bondRecords.sort(
|
|
|
|
(a: any, b: any) =>
|
|
|
|
new Date(b.createTime).getTime() - new Date(a.createTime).getTime()
|
|
|
|
);
|
|
|
|
const nextVersion = semverInc(
|
|
|
|
latestBondRecord?.attributes.version ?? '0.0.0',
|
|
|
|
'patch'
|
|
|
|
);
|
2024-02-12 06:04:01 +00:00
|
|
|
|
2024-02-14 05:33:22 +00:00
|
|
|
assert(nextVersion, 'Application record version not valid');
|
|
|
|
|
2024-02-12 06:04:01 +00:00
|
|
|
// Create record of type ApplicationRecord and publish
|
|
|
|
const applicationRecord = {
|
|
|
|
type: APP_RECORD_TYPE,
|
2024-02-14 05:33:22 +00:00
|
|
|
version: nextVersion,
|
|
|
|
repository_ref: commitHash,
|
2024-02-15 11:54:57 +00:00
|
|
|
repository: [repoUrl],
|
2024-02-14 05:33:22 +00:00
|
|
|
app_type: appType,
|
2024-10-18 12:37:01 +00:00
|
|
|
name: repo,
|
2024-02-14 05:33:22 +00:00
|
|
|
...(packageJSON.description && { description: packageJSON.description }),
|
|
|
|
...(packageJSON.homepage && { homepage: packageJSON.homepage }),
|
|
|
|
...(packageJSON.license && { license: packageJSON.license }),
|
2024-02-22 05:45:17 +00:00
|
|
|
...(packageJSON.author && {
|
|
|
|
author:
|
|
|
|
typeof packageJSON.author === 'object'
|
|
|
|
? JSON.stringify(packageJSON.author)
|
|
|
|
: packageJSON.author
|
|
|
|
}),
|
2024-02-14 05:33:22 +00:00
|
|
|
...(packageJSON.version && { app_version: packageJSON.version })
|
2024-02-12 06:04:01 +00:00
|
|
|
};
|
|
|
|
|
2024-10-16 08:43:51 +00:00
|
|
|
const fee = parseGasAndFees(this.registryConfig.fee.gas, this.registryConfig.fee.fees);
|
|
|
|
|
2024-10-24 11:38:17 +00:00
|
|
|
const result = await registryTransactionWithRetry(() =>
|
|
|
|
this.registry.setRecord(
|
|
|
|
{
|
|
|
|
privateKey: this.registryConfig.privateKey,
|
|
|
|
record: applicationRecord,
|
|
|
|
bondId: this.registryConfig.bondId
|
|
|
|
},
|
|
|
|
this.registryConfig.privateKey,
|
|
|
|
fee
|
|
|
|
)
|
2024-02-12 06:04:01 +00:00
|
|
|
);
|
|
|
|
|
2024-10-18 12:37:01 +00:00
|
|
|
log(`Published application record ${result.id}`);
|
2024-02-12 06:04:01 +00:00
|
|
|
log('Application record data:', applicationRecord);
|
|
|
|
|
2024-10-16 08:43:51 +00:00
|
|
|
// TODO: Discuss computation of LRN
|
2024-10-18 12:37:01 +00:00
|
|
|
const lrn = this.getLrn(repo);
|
2024-10-16 08:43:51 +00:00
|
|
|
log(`Setting name: ${lrn} for record ID: ${result.id}`);
|
2024-02-12 06:04:01 +00:00
|
|
|
|
2024-02-29 13:03:34 +00:00
|
|
|
await sleep(SLEEP_DURATION);
|
2024-10-24 11:38:17 +00:00
|
|
|
await registryTransactionWithRetry(() =>
|
|
|
|
this.registry.setName(
|
|
|
|
{
|
|
|
|
cid: result.id,
|
|
|
|
lrn
|
|
|
|
},
|
|
|
|
this.registryConfig.privateKey,
|
|
|
|
fee
|
|
|
|
)
|
2024-02-22 05:45:17 +00:00
|
|
|
);
|
2024-02-29 13:03:34 +00:00
|
|
|
|
|
|
|
await sleep(SLEEP_DURATION);
|
2024-10-24 11:38:17 +00:00
|
|
|
await registryTransactionWithRetry(() =>
|
|
|
|
this.registry.setName(
|
|
|
|
{
|
|
|
|
cid: result.id,
|
|
|
|
lrn: `${lrn}@${applicationRecord.app_version}`
|
|
|
|
},
|
|
|
|
this.registryConfig.privateKey,
|
|
|
|
fee
|
|
|
|
)
|
2024-02-22 05:45:17 +00:00
|
|
|
);
|
2024-02-29 13:03:34 +00:00
|
|
|
|
|
|
|
await sleep(SLEEP_DURATION);
|
2024-10-24 11:38:17 +00:00
|
|
|
await registryTransactionWithRetry(() =>
|
|
|
|
this.registry.setName(
|
|
|
|
{
|
|
|
|
cid: result.id,
|
|
|
|
lrn: `${lrn}@${applicationRecord.repository_ref}`
|
|
|
|
},
|
|
|
|
this.registryConfig.privateKey,
|
|
|
|
fee
|
|
|
|
)
|
2024-02-22 05:45:17 +00:00
|
|
|
);
|
2024-02-12 06:04:01 +00:00
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
return {
|
2024-10-16 08:43:51 +00:00
|
|
|
applicationRecordId: result.id,
|
2024-02-22 05:45:17 +00:00
|
|
|
applicationRecordData: applicationRecord
|
|
|
|
};
|
2024-02-12 06:04:01 +00:00
|
|
|
}
|
|
|
|
|
2024-10-18 12:37:01 +00:00
|
|
|
async createApplicationDeploymentAuction(
|
|
|
|
appName: string,
|
|
|
|
octokit: Octokit,
|
|
|
|
auctionParams: AuctionParams,
|
|
|
|
data: DeepPartial<Deployment>,
|
|
|
|
): Promise<{
|
|
|
|
applicationDeploymentAuctionId: string;
|
|
|
|
}> {
|
|
|
|
assert(data.project?.repository, 'Project repository not found');
|
|
|
|
|
|
|
|
await this.createApplicationRecord({
|
|
|
|
octokit,
|
|
|
|
repository: data.project.repository,
|
|
|
|
appType: data.project!.template!,
|
|
|
|
commitHash: data.commitHash!,
|
|
|
|
});
|
|
|
|
|
|
|
|
const lrn = this.getLrn(appName);
|
|
|
|
const config = await getConfig();
|
|
|
|
const auctionConfig = config.auction;
|
|
|
|
|
|
|
|
const fee = parseGasAndFees(this.registryConfig.fee.gas, this.registryConfig.fee.fees);
|
2024-10-24 11:38:17 +00:00
|
|
|
const auctionResult = await registryTransactionWithRetry(() =>
|
|
|
|
this.registry.createProviderAuction(
|
|
|
|
{
|
|
|
|
commitFee: auctionConfig.commitFee,
|
|
|
|
commitsDuration: auctionConfig.commitsDuration,
|
|
|
|
revealFee: auctionConfig.revealFee,
|
|
|
|
revealsDuration: auctionConfig.revealsDuration,
|
|
|
|
denom: auctionConfig.denom,
|
|
|
|
maxPrice: auctionParams.maxPrice,
|
|
|
|
numProviders: auctionParams.numProviders,
|
|
|
|
},
|
|
|
|
this.registryConfig.privateKey,
|
|
|
|
fee
|
|
|
|
)
|
|
|
|
);
|
2024-10-18 12:37:01 +00:00
|
|
|
|
|
|
|
if (!auctionResult.auction) {
|
|
|
|
throw new Error('Error creating auction');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create record of type applicationDeploymentAuction and publish
|
|
|
|
const applicationDeploymentAuction = {
|
|
|
|
application: lrn,
|
|
|
|
auction: auctionResult.auction.id,
|
|
|
|
type: APP_DEPLOYMENT_AUCTION_RECORD_TYPE,
|
|
|
|
};
|
|
|
|
|
2024-10-24 11:38:17 +00:00
|
|
|
const result = await registryTransactionWithRetry(() =>
|
|
|
|
this.registry.setRecord(
|
|
|
|
{
|
|
|
|
privateKey: this.registryConfig.privateKey,
|
|
|
|
record: applicationDeploymentAuction,
|
|
|
|
bondId: this.registryConfig.bondId
|
|
|
|
},
|
|
|
|
this.registryConfig.privateKey,
|
|
|
|
fee
|
|
|
|
)
|
2024-10-18 12:37:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
log(`Application deployment auction created: ${auctionResult.auction.id}`);
|
|
|
|
log(`Application deployment auction record published: ${result.id}`);
|
|
|
|
log('Application deployment auction data:', applicationDeploymentAuction);
|
|
|
|
|
|
|
|
return {
|
|
|
|
applicationDeploymentAuctionId: auctionResult.auction.id,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async createApplicationDeploymentRequest(data: {
|
2024-02-22 07:20:35 +00:00
|
|
|
deployment: Deployment,
|
2024-02-14 05:33:22 +00:00
|
|
|
appName: string,
|
2024-02-15 12:32:37 +00:00
|
|
|
repository: string,
|
2024-10-18 12:37:01 +00:00
|
|
|
auctionId?: string,
|
|
|
|
lrn: string,
|
2024-02-29 13:03:34 +00:00
|
|
|
environmentVariables: { [key: string]: string },
|
|
|
|
dns: string,
|
2024-02-14 05:33:22 +00:00
|
|
|
}): Promise<{
|
2024-02-22 05:45:17 +00:00
|
|
|
applicationDeploymentRequestId: string;
|
|
|
|
applicationDeploymentRequestData: ApplicationDeploymentRequest;
|
2024-02-14 05:33:22 +00:00
|
|
|
}> {
|
2024-10-16 08:43:51 +00:00
|
|
|
const lrn = this.getLrn(data.appName);
|
|
|
|
const records = await this.registry.resolveNames([lrn]);
|
2024-02-12 06:04:01 +00:00
|
|
|
const applicationRecord = records[0];
|
|
|
|
|
|
|
|
if (!applicationRecord) {
|
2024-10-16 08:43:51 +00:00
|
|
|
throw new Error(`No record found for ${lrn}`);
|
2024-02-12 06:04:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create record of type ApplicationDeploymentRequest and publish
|
|
|
|
const applicationDeploymentRequest = {
|
2024-02-19 08:13:29 +00:00
|
|
|
type: APP_DEPLOYMENT_REQUEST_TYPE,
|
2024-02-12 06:04:01 +00:00
|
|
|
version: '1.0.0',
|
|
|
|
name: `${applicationRecord.attributes.name}@${applicationRecord.attributes.app_version}`,
|
2024-10-16 08:43:51 +00:00
|
|
|
application: `${lrn}@${applicationRecord.attributes.app_version}`,
|
2024-02-29 13:03:34 +00:00
|
|
|
dns: data.dns,
|
2024-02-12 06:04:01 +00:00
|
|
|
|
2024-02-14 05:33:22 +00:00
|
|
|
// https://git.vdb.to/cerc-io/laconic-registry-cli/commit/129019105dfb93bebcea02fde0ed64d0f8e5983b
|
|
|
|
config: JSON.stringify({
|
2024-02-15 12:32:37 +00:00
|
|
|
env: data.environmentVariables
|
2024-02-14 05:33:22 +00:00
|
|
|
}),
|
|
|
|
meta: JSON.stringify({
|
2024-02-22 05:45:17 +00:00
|
|
|
note: `Added by Snowball @ ${DateTime.utc().toFormat(
|
|
|
|
"EEE LLL dd HH:mm:ss 'UTC' yyyy"
|
|
|
|
)}`,
|
2024-02-14 05:33:22 +00:00
|
|
|
repository: data.repository,
|
2024-02-22 07:20:35 +00:00
|
|
|
repository_ref: data.deployment.commitHash
|
2024-10-18 12:37:01 +00:00
|
|
|
}),
|
|
|
|
deployer: data.lrn,
|
|
|
|
...(data.auctionId && { auction: data.auctionId }),
|
2024-02-12 06:04:01 +00:00
|
|
|
};
|
|
|
|
|
2024-02-29 13:03:34 +00:00
|
|
|
await sleep(SLEEP_DURATION);
|
2024-10-16 08:43:51 +00:00
|
|
|
|
|
|
|
const fee = parseGasAndFees(this.registryConfig.fee.gas, this.registryConfig.fee.fees);
|
|
|
|
|
2024-10-24 11:38:17 +00:00
|
|
|
const result = await registryTransactionWithRetry(() =>
|
|
|
|
this.registry.setRecord(
|
|
|
|
{
|
|
|
|
privateKey: this.registryConfig.privateKey,
|
|
|
|
record: applicationDeploymentRequest,
|
|
|
|
bondId: this.registryConfig.bondId
|
|
|
|
},
|
|
|
|
this.registryConfig.privateKey,
|
|
|
|
fee
|
|
|
|
)
|
2024-02-12 06:04:01 +00:00
|
|
|
);
|
2024-10-23 15:36:19 +00:00
|
|
|
|
2024-10-16 08:43:51 +00:00
|
|
|
log(`Application deployment request record published: ${result.id}`);
|
2024-02-12 06:04:01 +00:00
|
|
|
log('Application deployment request data:', applicationDeploymentRequest);
|
|
|
|
|
2024-02-22 05:45:17 +00:00
|
|
|
return {
|
2024-10-16 08:43:51 +00:00
|
|
|
applicationDeploymentRequestId: result.id,
|
2024-02-22 05:45:17 +00:00
|
|
|
applicationDeploymentRequestData: applicationDeploymentRequest
|
|
|
|
};
|
2024-02-19 08:13:29 +00:00
|
|
|
}
|
|
|
|
|
2024-10-22 09:12:59 +00:00
|
|
|
async getAuctionWinningDeployerRecords(
|
2024-10-18 12:37:01 +00:00
|
|
|
auctionId: string
|
2024-10-22 09:12:59 +00:00
|
|
|
): Promise<DeployerRecord[]> {
|
2024-10-18 12:37:01 +00:00
|
|
|
const records = await this.registry.getAuctionsByIds([auctionId]);
|
|
|
|
const auctionResult = records[0];
|
|
|
|
|
2024-10-22 09:12:59 +00:00
|
|
|
let deployerRecords = [];
|
2024-10-18 12:37:01 +00:00
|
|
|
const { winnerAddresses } = auctionResult;
|
|
|
|
|
|
|
|
for (const auctionWinner of winnerAddresses) {
|
2024-10-22 09:12:59 +00:00
|
|
|
const records = await this.getDeployerRecordsByFilter({
|
|
|
|
paymentAddress: auctionWinner,
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const record of records) {
|
|
|
|
if (record.id) {
|
|
|
|
deployerRecords.push(record);
|
2024-10-18 12:37:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 09:12:59 +00:00
|
|
|
return deployerRecords;
|
2024-10-18 12:37:01 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 14:25:49 +00:00
|
|
|
async releaseDeployerFunds(
|
|
|
|
auctionId: string
|
|
|
|
): Promise<any> {
|
|
|
|
const fee = parseGasAndFees(this.registryConfig.fee.gas, this.registryConfig.fee.fees);
|
2024-10-24 11:38:17 +00:00
|
|
|
const auction = await registryTransactionWithRetry(() =>
|
|
|
|
this.registry.releaseFunds(
|
|
|
|
{
|
|
|
|
auctionId
|
|
|
|
},
|
|
|
|
this.registryConfig.privateKey,
|
|
|
|
fee
|
|
|
|
)
|
2024-10-21 14:25:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return auction;
|
|
|
|
}
|
|
|
|
|
2024-02-19 08:13:29 +00:00
|
|
|
/**
|
|
|
|
* Fetch ApplicationDeploymentRecords for deployments
|
|
|
|
*/
|
2024-10-18 12:37:01 +00:00
|
|
|
async getDeploymentRecords(
|
2024-02-22 05:45:17 +00:00
|
|
|
deployments: Deployment[]
|
|
|
|
): Promise<AppDeploymentRecord[]> {
|
2024-02-19 08:13:29 +00:00
|
|
|
// Fetch ApplicationDeploymentRecords for corresponding ApplicationRecord set in deployments
|
|
|
|
// TODO: Implement Laconicd GQL query to filter records by multiple values for an attribute
|
2024-02-22 05:45:17 +00:00
|
|
|
const records = await this.registry.queryRecords(
|
|
|
|
{
|
|
|
|
type: APP_DEPLOYMENT_RECORD_TYPE
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
2024-02-19 08:13:29 +00:00
|
|
|
|
2024-10-18 12:37:01 +00:00
|
|
|
// Filter records with ApplicationDeploymentRequestId ID and Deployment specific URL
|
2024-02-22 05:45:17 +00:00
|
|
|
return records.filter((record: AppDeploymentRecord) =>
|
|
|
|
deployments.some(
|
|
|
|
(deployment) =>
|
2024-10-18 12:37:01 +00:00
|
|
|
deployment.applicationDeploymentRequestId === record.attributes.request &&
|
2024-02-29 13:03:34 +00:00
|
|
|
record.attributes.url.includes(deployment.id)
|
2024-02-22 05:45:17 +00:00
|
|
|
)
|
|
|
|
);
|
2024-02-12 06:04:01 +00:00
|
|
|
}
|
|
|
|
|
2024-10-22 09:12:59 +00:00
|
|
|
/**
|
|
|
|
* Fetch WebappDeployer Records by filter
|
|
|
|
*/
|
|
|
|
async getDeployerRecordsByFilter(filter: { [key: string]: any }): Promise<DeployerRecord[]> {
|
|
|
|
return this.registry.queryRecords(
|
|
|
|
{
|
|
|
|
type: WEBAPP_DEPLOYER_RECORD_TYPE,
|
|
|
|
...filter
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-25 11:21:22 +00:00
|
|
|
/**
|
|
|
|
* Fetch ApplicationDeploymentRecords by filter
|
|
|
|
*/
|
2024-10-18 12:37:01 +00:00
|
|
|
async getDeploymentRecordsByFilter(filter: { [key: string]: any }): Promise<AppDeploymentRecord[]> {
|
2024-04-25 11:21:22 +00:00
|
|
|
return this.registry.queryRecords(
|
|
|
|
{
|
|
|
|
type: APP_DEPLOYMENT_RECORD_TYPE,
|
|
|
|
...filter
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-15 14:06:04 +00:00
|
|
|
/**
|
|
|
|
* Fetch ApplicationDeploymentRemovalRecords for deployments
|
|
|
|
*/
|
2024-10-18 12:37:01 +00:00
|
|
|
async getDeploymentRemovalRecords(
|
2024-04-15 14:06:04 +00:00
|
|
|
deployments: Deployment[]
|
|
|
|
): Promise<AppDeploymentRemovalRecord[]> {
|
|
|
|
// Fetch ApplicationDeploymentRemovalRecords for corresponding ApplicationDeploymentRecord set in deployments
|
|
|
|
const records = await this.registry.queryRecords(
|
|
|
|
{
|
|
|
|
type: APP_DEPLOYMENT_REMOVAL_RECORD_TYPE
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
// Filter records with ApplicationDeploymentRecord and ApplicationDeploymentRemovalRequest IDs
|
|
|
|
return records.filter((record: AppDeploymentRemovalRecord) =>
|
|
|
|
deployments.some(
|
|
|
|
(deployment) =>
|
|
|
|
deployment.applicationDeploymentRemovalRequestId === record.attributes.request &&
|
|
|
|
deployment.applicationDeploymentRecordId === record.attributes.deployment
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-18 12:37:01 +00:00
|
|
|
async createApplicationDeploymentRemovalRequest(data: {
|
2024-03-27 16:54:34 +00:00
|
|
|
deploymentId: string;
|
2024-10-18 12:37:01 +00:00
|
|
|
deployerLrn: string;
|
2024-03-27 16:41:53 +00:00
|
|
|
}): Promise<{
|
|
|
|
applicationDeploymentRemovalRequestId: string;
|
|
|
|
applicationDeploymentRemovalRequestData: ApplicationDeploymentRemovalRequest;
|
|
|
|
}> {
|
|
|
|
const applicationDeploymentRemovalRequest = {
|
|
|
|
type: APP_DEPLOYMENT_REMOVAL_REQUEST_TYPE,
|
|
|
|
version: '1.0.0',
|
2024-10-18 12:37:01 +00:00
|
|
|
deployment: data.deploymentId,
|
|
|
|
deployer: data.deployerLrn
|
2024-03-27 16:41:53 +00:00
|
|
|
};
|
|
|
|
|
2024-10-16 08:43:51 +00:00
|
|
|
const fee = parseGasAndFees(this.registryConfig.fee.gas, this.registryConfig.fee.fees);
|
|
|
|
|
2024-10-24 11:38:17 +00:00
|
|
|
const result = await registryTransactionWithRetry(() =>
|
|
|
|
this.registry.setRecord(
|
|
|
|
{
|
|
|
|
privateKey: this.registryConfig.privateKey,
|
|
|
|
record: applicationDeploymentRemovalRequest,
|
|
|
|
bondId: this.registryConfig.bondId
|
|
|
|
},
|
|
|
|
this.registryConfig.privateKey,
|
|
|
|
fee
|
|
|
|
)
|
2024-03-27 16:41:53 +00:00
|
|
|
);
|
|
|
|
|
2024-10-16 08:43:51 +00:00
|
|
|
log(`Application deployment removal request record published: ${result.id}`);
|
2024-03-27 16:41:53 +00:00
|
|
|
log('Application deployment removal request data:', applicationDeploymentRemovalRequest);
|
|
|
|
|
|
|
|
return {
|
2024-10-16 08:43:51 +00:00
|
|
|
applicationDeploymentRemovalRequestId: result.id,
|
2024-03-27 16:41:53 +00:00
|
|
|
applicationDeploymentRemovalRequestData: applicationDeploymentRemovalRequest
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:05:35 +00:00
|
|
|
async getCompletedAuctionIds(auctionIds: string[]): Promise<string[]> {
|
2024-10-21 14:25:49 +00:00
|
|
|
if (auctionIds.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:05:35 +00:00
|
|
|
const auctions = await this.registry.getAuctionsByIds(auctionIds);
|
2024-10-18 12:37:01 +00:00
|
|
|
|
|
|
|
const completedAuctions = auctions
|
2024-10-22 09:12:59 +00:00
|
|
|
.filter((auction: { id: string, status: string }) => auction.status === 'completed')
|
|
|
|
.map((auction: { id: string, status: string }) => auction.id);
|
2024-10-18 12:37:01 +00:00
|
|
|
|
|
|
|
return completedAuctions;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getRecordsByName(name: string): Promise<any> {
|
|
|
|
return this.registry.resolveNames([name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAuctionData(auctionId: string): Promise<any> {
|
|
|
|
return this.registry.getAuctionsByIds([auctionId]);
|
|
|
|
}
|
|
|
|
|
2024-10-16 08:43:51 +00:00
|
|
|
getLrn(appName: string): string {
|
2024-02-23 09:17:29 +00:00
|
|
|
assert(this.registryConfig.authority, "Authority doesn't exist");
|
2024-10-16 08:43:51 +00:00
|
|
|
return `lrn://${this.registryConfig.authority}/applications/${appName}`;
|
2024-02-12 06:04:01 +00:00
|
|
|
}
|
|
|
|
}
|