Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(contracts): extend ids for contract storage and deployment #1794

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions packages/contracts/tasks/helpers/ContractStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ export class ContractStorage {
*
* @param {IRegisterContract} args - register arguments
*/
async register({ id, key, contract, network, args, name }: IRegisterContract): Promise<void> {
async register<ID = EContracts>({ id, key, contract, network, args, name }: IRegisterContract<ID>): Promise<void> {
const contractAddress = await contract.getAddress();

const deploymentTx = contract.deploymentTransaction();
const contractId = String(id);

console.log(`*** ${id} ***\n`);
console.log(`*** ${contractId} ***\n`);
console.log(`Network: ${network}`);
console.log(`contract address: ${contractAddress}`);

Expand All @@ -87,7 +88,7 @@ export class ContractStorage {
console.log();

const logEntry: IStorageInstanceEntry = {
id,
id: contractId,
deploymentTxHash: deploymentTx?.hash,
};

Expand All @@ -100,12 +101,12 @@ export class ContractStorage {

this.db.set(`${network}.instance.${contractAddress}`, logEntry).write();

const namedEntry = this.db.get(`${network}.named.${id}${key !== undefined ? `.${key}` : ""}`).value() as
const namedEntry = this.db.get(`${network}.named.${contractId}${key !== undefined ? `.${key}` : ""}`).value() as
| IStorageNamedEntry
| undefined;
const count = namedEntry?.count ?? 0;
this.db
.set(`${network}.named.${id}${key !== undefined ? `.${key}` : ""}`, {
.set(`${network}.named.${contractId}${key !== undefined ? `.${key}` : ""}`, {
address: contractAddress,
count: count + 1,
})
Expand Down Expand Up @@ -155,7 +156,7 @@ export class ContractStorage {
* @param key - contract key
* @returns deployment arguments
*/
getContractArgs(id: EContracts, network: string, key?: string): string[] | undefined {
getContractArgs<ID extends string = EContracts>(id: ID, network: string, key?: string): string[] | undefined {
const address = this.getAddress(id, network, key);

const collection = this.db.get(`${network}.instance.${address}`);
Expand All @@ -175,7 +176,7 @@ export class ContractStorage {
* @param network - selected network
* @returns contract address
*/
getAddress(id: EContracts, network: string, key?: string): string | undefined {
getAddress<ID extends string = EContracts>(id: ID, network: string, key?: string): string | undefined {
const collection = this.db.get(`${network}.named.${id}${key !== undefined ? `.${key}` : ""}`);
const namedEntry = collection.value() as IStorageNamedEntry | undefined;

Expand All @@ -190,7 +191,7 @@ export class ContractStorage {
* @throws {Error} if there is no address the error will be thrown
* @returns contract address
*/
mustGetAddress(id: EContracts, network: string, key?: string): string {
mustGetAddress<ID extends string = EContracts>(id: ID, network: string, key?: string): string {
const address = this.getAddress(id, network, key);

if (!address) {
Expand All @@ -203,7 +204,7 @@ export class ContractStorage {
/**
* Get Contract Deployment Transaction Hash
*/
getDeploymentTxHash(id: EContracts, network: string, address: string): string | undefined {
getDeploymentTxHash<ID extends string = EContracts>(id: ID, network: string, address: string): string | undefined {
const collection = this.db.get(`${network}.instance.${address}`);
const instanceEntry = collection.value() as IStorageInstanceEntry | undefined;

Expand Down
12 changes: 8 additions & 4 deletions packages/contracts/tasks/helpers/Deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,15 @@ export class Deployment {
* @param args - constructor arguments
* @returns deployed contract
*/
async deployContract<T extends BaseContract>(
{ name, abi, bytecode, signer }: IDeployContractParams,
async deployContract<T extends BaseContract, ID = EContracts>(
{ name, abi, bytecode, signer }: IDeployContractParams<ID>,
...args: unknown[]
): Promise<T> {
const deployer = signer || (await this.getDeployer());
const contractFactory =
abi && bytecode
? new ContractFactory(abi, bytecode, deployer)
: await import("hardhat").then(({ ethers }) => ethers.getContractFactory(name, deployer));
: await import("hardhat").then(({ ethers }) => ethers.getContractFactory(String(name), deployer));
const feeData = await deployer.provider?.getFeeData();

const contract = await contractFactory.deploy(...args, {
Expand Down Expand Up @@ -380,7 +380,11 @@ export class Deployment {
* @param field - config field key
* @returns config field value or null
*/
getDeployConfigField<T = string | number | boolean>(id: EContracts, field: string, mustGet = false): T {
getDeployConfigField<T = string | number | boolean, ID extends string = EContracts>(
id: ID,
field: string,
mustGet = false,
): T {
this.checkHre();

const value = this.config.get(`${this.hre!.network.name}.${id}.${field}`).value() as T;
Expand Down
8 changes: 4 additions & 4 deletions packages/contracts/tasks/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,11 @@ export interface IStorageInstanceEntry {
/**
* Interface that represents register contract arguments
*/
export interface IRegisterContract {
export interface IRegisterContract<ID = EContracts> {
/**
* Contract enum identifier
*/
id: EContracts;
id: ID;

/**
* Contract instance
Expand Down Expand Up @@ -749,11 +749,11 @@ export type TAbi = string | readonly (string | Fragment | JsonFragment)[];
/**
* Interface that represents deploy params
*/
export interface IDeployContractParams {
export interface IDeployContractParams<ID = EContracts> {
/**
* Contract name
*/
name: EContracts;
name: ID;

/**
* Contract abi
Expand Down
Loading