From 166c0e25e14e623439babacafa090966919fb8b6 Mon Sep 17 00:00:00 2001 From: MichaelKorchagin Date: Mon, 15 Jul 2024 15:05:08 -0700 Subject: [PATCH] Initial changes for missions factory --- package.json | 2 +- src/db/mongo-adapter/mongo-adapter.ts | 17 +++++++++++++++ src/db/types.ts | 1 + src/missions/base-deploy-mission.ts | 30 ++++++++++++++++++++------- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index ab43034..ed17e74 100644 --- a/package.json +++ b/package.json @@ -37,4 +37,4 @@ "ts-node": "10.9.1", "typescript": "^5.0.2" } -} +} \ No newline at end of file diff --git a/src/db/mongo-adapter/mongo-adapter.ts b/src/db/mongo-adapter/mongo-adapter.ts index 4d7a4de..5d0c8b9 100644 --- a/src/db/mongo-adapter/mongo-adapter.ts +++ b/src/db/mongo-adapter/mongo-adapter.ts @@ -96,6 +96,23 @@ export class MongoDBAdapter { }); } + async getContracts (contractName : string, version ?: string) { + if (!version) { + ({ dbVersion: version } = await this.versioner.getCheckLatestVersion()); + } + + const cursor = await this.contracts.find({ + name: contractName, + version, + }); + + const foundContracts = await cursor.toArray(); + + if (foundContracts.length > 0) { + return foundContracts; + } else return null; + } + async writeContract (contractName : string, data : Omit, version ?: string) { if (!version) { ({ dbVersion: version } = await this.versioner.getCheckLatestVersion()); diff --git a/src/db/types.ts b/src/db/types.ts index 5478966..bd6adba 100644 --- a/src/db/types.ts +++ b/src/db/types.ts @@ -6,6 +6,7 @@ export interface IContractDbData { address : string; abi : string; bytecode : string; + args : string; implementation : string | null; version : string; } diff --git a/src/missions/base-deploy-mission.ts b/src/missions/base-deploy-mission.ts index f737d68..ebb45c8 100644 --- a/src/missions/base-deploy-mission.ts +++ b/src/missions/base-deploy-mission.ts @@ -41,11 +41,11 @@ export class BaseDeployMission < } async getFromDB () { - return this.campaign.dbAdapter.getContract(this.dbName); + return this.campaign.dbAdapter.getContract(this.contractName); } async saveToDB (contract : IContractV6) { - this.logger.debug(`Writing ${this.dbName} to DB...`); + this.logger.debug(`Writing ${this.contractName} to DB...`); this.implAddress = this.proxyData.isProxy ? await this.campaign.deployer.getProxyImplAddress(await contract.getAddress()) @@ -53,19 +53,18 @@ export class BaseDeployMission < const contractDbDoc = await this.buildDbObject(contract, this.implAddress); - return this.campaign.dbAdapter.writeContract(this.dbName, contractDbDoc); + return this.campaign.dbAdapter.writeContract(this.contractName, contractDbDoc); } async needsDeploy () { const dbContract = await this.getFromDB(); if (!dbContract) { - this.logger.info(`${this.dbName} not found in DB, proceeding to deploy...`); + this.logger.info(`${this.contractName} not found in DB, proceeding to deploy...`); } else { - this.logger.info(`${this.dbName} found in DB at ${dbContract.address}, no deployment needed.`); - + this.logger.info(`${this.contractName} found in DB at ${dbContract.address}, no deployment needed.`); const contract = await this.campaign.deployer.getContractObject( - this.dbName, + this.contractName, dbContract.address, ); @@ -91,11 +90,26 @@ export class BaseDeployMission < implAddress : string | null ) : Promise> { const { abi, bytecode } = this.getArtifact(); + + const bnToString = (key : string, value : TDeployArgs) => { + if (typeof value === "bigint") { + // eslint-disable-next-line @typescript-eslint/ban-types + return `${(value as BigInt).toString()}n`; + } + + if (Array.isArray(value) && value.length === 0) { + return undefined; + } + + return value; + }; + return { - name: this.dbName, + name: this.contractName, address: await hhContract.getAddress(), abi: JSON.stringify(abi), bytecode, + args: JSON.stringify(await this.deployArgs(), bnToString), implementation: implAddress, }; }