From 7df5899a3249f9c61ad8b7f6d61bc9ac33865d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Migone?= Date: Tue, 24 Oct 2023 12:25:06 -0300 Subject: [PATCH] feat(registry): add insert entry task (#93) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Migone --- contracts/hardhat.config.ts | 1 + contracts/tasks/registry.ts | 46 ++++++++++++++++++++++++++++++++++++ contracts/utils/artifacts.ts | 22 +++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 contracts/tasks/registry.ts create mode 100644 contracts/utils/artifacts.ts diff --git a/contracts/hardhat.config.ts b/contracts/hardhat.config.ts index 6591590..3f3a2e2 100644 --- a/contracts/hardhat.config.ts +++ b/contracts/hardhat.config.ts @@ -3,6 +3,7 @@ import '@nomiclabs/hardhat-ethers'; import '@typechain/hardhat'; import '@nomiclabs/hardhat-etherscan'; import './tasks/deploy'; +import './tasks/registry'; task('accounts', 'Print a list of accounts', async (_, hre) => { const accounts = await hre.ethers.getSigners(); diff --git a/contracts/tasks/registry.ts b/contracts/tasks/registry.ts new file mode 100644 index 0000000..5881f0f --- /dev/null +++ b/contracts/tasks/registry.ts @@ -0,0 +1,46 @@ +import {Wallet, ethers} from 'ethers'; +import {task, types} from 'hardhat/config'; +import {HardhatRuntimeEnvironment} from 'hardhat/types'; +import {loadArtifact} from '../utils/artifacts'; +import {Registry} from '../types/contracts/Registry'; + +import addresses from '../addresses.json'; + +task('registry:insert', 'Insert an entry to the registry') + .addParam('entryId', 'Entry ID to insert', undefined, types.int) + .addParam('subscriptions', 'Comma separated list of subscription contract addresses to insert', undefined, types.string) + .addParam('metadataHash', 'Metadata hash to insert', undefined, types.string) + .setAction(async (taskArgs, hre: HardhatRuntimeEnvironment) => { + const accounts = await hre.ethers.getSigners(); + + if (accounts.length === 0) { + throw new Error( + 'No accounts available, set PRIVATE_KEY or MNEMONIC env variables' + ); + } + console.log( + 'Using the account:', + accounts[0].address + ); + + const chainId = (hre.network.config.chainId as number).toString(); + const registryAddress = (addresses as any)[chainId]['Registry']; + + const artifact = loadArtifact('Registry'); + const registry = new ethers.Contract( + registryAddress, + artifact.abi, + hre.ethers.provider + ) as Registry; + + console.log(`Registry contract address: ${registry.address}`); + + const tx = await registry.connect(accounts[0]).insertEntry(taskArgs.entryId, { + subscriptions: taskArgs.subscriptions.split(','), + metadataHash: taskArgs.metadataHash, + }) + + const receipt = await tx.wait(); + console.log(`Transaction successful: ${receipt.transactionHash}`); + + }); diff --git a/contracts/utils/artifacts.ts b/contracts/utils/artifacts.ts new file mode 100644 index 0000000..33aa9ef --- /dev/null +++ b/contracts/utils/artifacts.ts @@ -0,0 +1,22 @@ +import path from 'path' +import { Artifacts } from 'hardhat/internal/artifacts' +import { LinkReferences } from 'hardhat/types' +import { utils } from 'ethers' + +type Abi = Array + +type Artifact = { + contractName: string + abi: Abi + bytecode: string + deployedBytecode: string + linkReferences?: LinkReferences + deployedLinkReferences?: LinkReferences +} + +const ARTIFACTS_PATH = path.resolve('artifacts') +const artifacts = new Artifacts(ARTIFACTS_PATH) + +export const loadArtifact = (name: string): Artifact => { + return artifacts.readArtifactSync(name) +}