Skip to content

Commit

Permalink
Merge pull request #1775 from privacy-scaling-explorations/feat/contr…
Browse files Browse the repository at this point in the history
…act-storage-path-and-args

feat(storage): add storage path and function to get deployment args
  • Loading branch information
0xmad authored Aug 19, 2024
2 parents d9e2f5d + 46b7350 commit 86689a1
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions packages/contracts/tasks/helpers/ContractStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,23 @@ export class ContractStorage {
/**
* Initialize class properties only once
*/
private constructor() {
private constructor(storagePath?: string) {
this.db = low(
typeof window !== "undefined"
? new LocalStorageSync<TStorage>("deployed-contracts")
: new FileSync<TStorage>(path.resolve(__dirname, "..", "..", "./deployed-contracts.json")),
: new FileSync<TStorage>(storagePath ?? path.resolve(__dirname, "..", "..", "./deployed-contracts.json")),
);
}

/**
* Get singleton object
*
* @param storagePath - path to the storage file
* @returns {ContractStorage} singleton object
*/
static getInstance(): ContractStorage {
static getInstance(storagePath?: string): ContractStorage {
if (!ContractStorage.INSTANCE) {
ContractStorage.INSTANCE = new ContractStorage();
ContractStorage.INSTANCE = new ContractStorage(storagePath);
}

return ContractStorage.INSTANCE;
Expand Down Expand Up @@ -146,6 +147,27 @@ export class ContractStorage {
this.db.set(`${network}.verified.${address}`, verified).write();
};

/**
* Get deployment arguments from the json file
*
* @param id - contract name
* @param network - selected network
* @param key - contract key
* @returns deployment arguments
*/
getContractArgs(id: EContracts, network: string, key?: string): string[] | undefined {
const address = this.getAddress(id, network, key);

const collection = this.db.get(`${network}.instance.${address}`);
const instanceEntry = collection.value() as IStorageInstanceEntry | undefined;

if (!instanceEntry?.verify?.args) {
return undefined;
}

return JSON.parse(instanceEntry.verify.args) as string[];
}

/**
* Get contract address by name from the json file
*
Expand Down

0 comments on commit 86689a1

Please sign in to comment.