Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
chore: add AC for registering/removing subnets (#102)
Browse files Browse the repository at this point in the history
Signed-off-by: Jawad Tariq <[email protected]>
  • Loading branch information
JDawg287 authored Sep 6, 2023
1 parent d9a291d commit 4375919
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 57 deletions.
14 changes: 0 additions & 14 deletions contracts/interfaces/IOwnable.sol

This file was deleted.

27 changes: 0 additions & 27 deletions contracts/topos-core/Ownable.sol

This file was deleted.

8 changes: 5 additions & 3 deletions contracts/topos-core/SubnetRegistrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
pragma solidity ^0.8.9;

import "./Bytes32Sets.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

type SubnetId is bytes32;

contract SubnetRegistrator {
contract SubnetRegistrator is Ownable {
using Bytes32SetsLib for Bytes32SetsLib.Set;

struct Subnet {
Expand Down Expand Up @@ -59,7 +61,7 @@ contract SubnetRegistrator {
SubnetId subnetId,
string calldata currencySymbol,
uint256 chainId
) public {
) public onlyOwner {
subnetSet.insert(SubnetId.unwrap(subnetId));
Subnet storage subnet = subnets[subnetId];
subnet.endpoint = endpoint;
Expand All @@ -72,7 +74,7 @@ contract SubnetRegistrator {

/// @notice Remove an already registered subnet
/// @param subnetId FROST public key of a subnet
function removeSubnet(SubnetId subnetId) public {
function removeSubnet(SubnetId subnetId) public onlyOwner {
subnetSet.remove(SubnetId.unwrap(subnetId));
delete subnets[subnetId];
emit SubnetRemoved(subnetId);
Expand Down
54 changes: 41 additions & 13 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { providers, utils, Wallet } from 'ethers'
/* eslint-disable no-case-declarations */
import { ContractFactory, providers, utils, Wallet } from 'ethers'
import fs from 'fs'

import {
Expand All @@ -8,7 +9,14 @@ import {
} from './const-addr-deployer'

const main = async function (..._args: Arg[]) {
const [providerEndpoint, contractJsonPath, salt, gasLimit, ...args] = _args
const [
providerEndpoint,
contractJsonPath,
salt,
gasLimit,
deployConstant,
...args
] = _args
const provider = new providers.JsonRpcProvider(<string>providerEndpoint)
const privateKey = process.env.PRIVATE_KEY

Expand Down Expand Up @@ -39,17 +47,37 @@ const main = async function (..._args: Arg[]) {
return
}

const address = await deployContractConstant(
wallet,
contractJson,
<string>salt,
args,
<number>gasLimit
)
.then(({ address }) => address)
.catch(console.error)

console.log(address)
switch (deployConstant) {
case 'true':
const address = await deployContractConstant(
wallet,
contractJson,
<string>salt,
args,
<number>gasLimit
)
.then(({ address }) => address)
.catch(console.error)

console.log(address)
break
case 'false':
const contractFactory = new ContractFactory(
contractJson.abi,
contractJson.bytecode,
wallet
)
const contract = await contractFactory.deploy(...args, {
gasLimit: BigInt(gasLimit),
})
await contract.deployed()
console.log(contract.address)
break
default:
console.error(
`ERROR: Please provide a valid deployConstant flag! (true|false)`
)
}
}

const args = process.argv.slice(2)
Expand Down
23 changes: 23 additions & 0 deletions test/topos-core/SubnetRegistrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ describe('SubnetRegistrator', () => {
})

describe('registerSubnet', () => {
it('reverts if non-admin tries to register a subnet', async () => {
const [, nonAdmin] = await ethers.getSigners()
await expect(
subnetRegistrator
.connect(nonAdmin)
.registerSubnet(
endpoint,
logoURL,
subnetName,
subnetId,
subnetCurrencySymbol,
chainId
)
).to.be.revertedWith('Ownable: caller is not the owner')
})

it('reverts if the subnet is already registered', async () => {
await registerSubnet(
endpoint,
Expand Down Expand Up @@ -114,6 +130,13 @@ describe('SubnetRegistrator', () => {
})

describe('removeSubnet', () => {
it('reverts if non-admin tries to remove a subnet', async () => {
const [, nonAdmin] = await ethers.getSigners()
await expect(
subnetRegistrator.connect(nonAdmin).removeSubnet(subnetId)
).to.be.revertedWith('Ownable: caller is not the owner')
})

it('reverts when removing a non-existent subnet', async () => {
await expect(removeSubnet(subnetId)).to.be.revertedWith(
'Bytes32Set: key does not exist in the set.'
Expand Down

0 comments on commit 4375919

Please sign in to comment.