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

Commit

Permalink
feat: add ws endpoint to SubnetRegistrator.sol (#112)
Browse files Browse the repository at this point in the history
Signed-off-by: Jawad Tariq <[email protected]>
Co-authored-by: Sébastien Dan <[email protected]>
  • Loading branch information
JDawg287 and sebastiendan authored Nov 6, 2023
1 parent ebe9c83 commit 3f82b10
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 91 deletions.
30 changes: 17 additions & 13 deletions contracts/topos-core/SubnetRegistrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ contract SubnetRegistrator is Initializable, Ownable {
using Bytes32SetsLib for Bytes32SetsLib.Set;

struct Subnet {
string endpoint;
uint256 chainId;
string currencySymbol;
string endpointHttp;
string endpointWs;
string logoURL;
string name;
string currencySymbol;
uint256 chainId;
}

/// @notice Set of subnet IDs
Expand Down Expand Up @@ -56,27 +57,30 @@ contract SubnetRegistrator is Initializable, Ownable {
}

/// @notice Register a new subnet
/// @param endpoint JSON RPC endpoint of a subnet
/// @param chainId subnet network ID
/// @param currencySymbol currencySymbol for a subnet currency
/// @param endpointHttp JSON RPC http endpoint of a subnet
/// @param endpointWs JSON RPC ws endpoint of a subnet
/// @param logoURL URL for the logo of a subnet
/// @param name name of a subnet
/// @param subnetId FROST public key of a subnet
/// @param currencySymbol currencySymbol for a subnet currency
/// @param chainId subnet network ID
function registerSubnet(
string calldata endpoint,
uint256 chainId,
string calldata currencySymbol,
string calldata endpointHttp,
string calldata endpointWs,
string calldata logoURL,
string calldata name,
SubnetId subnetId,
string calldata currencySymbol,
uint256 chainId
SubnetId subnetId
) public onlyOwner {
subnetSet.insert(SubnetId.unwrap(subnetId));
Subnet storage subnet = subnets[subnetId];
subnet.endpoint = endpoint;
subnet.chainId = chainId;
subnet.currencySymbol = currencySymbol;
subnet.endpointHttp = endpointHttp;
subnet.endpointWs = endpointWs;
subnet.logoURL = logoURL;
subnet.name = name;
subnet.currencySymbol = currencySymbol;
subnet.chainId = chainId;
emit NewSubnetRegistered(subnetId);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@topos-protocol/topos-smart-contracts",
"version": "1.2.3",
"version": "2.0.0",
"description": "Topos Smart Contracts",
"repository": {
"type": "git",
Expand Down
19 changes: 13 additions & 6 deletions scripts/register-subnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const main = async function (...args: string[]) {
subnetRegistratorAddress,
subnetName,
subnetChainId,
subnetRPCEndpoint,
subnetEndpointHttp,
subnetEndpointWs,
subnetCurrencySymbol,
subnetLogoUrl,
_adminPrivateKey,
Expand All @@ -28,8 +29,13 @@ const main = async function (...args: string[]) {
process.exit(1)
}

if (!subnetRPCEndpoint) {
console.error('ERROR: Please provide the subnet endpoint private key!')
if (!subnetEndpointHttp) {
console.error('ERROR: Please provide the subnet HTTP endpoint!')
process.exit(1)
}

if (!subnetEndpointWs) {
console.error('ERROR: Please provide the subnet WS endpoint!')
process.exit(1)
}

Expand Down Expand Up @@ -102,12 +108,13 @@ const main = async function (...args: string[]) {
}

const tx: ContractTransaction = await contract.registerSubnet(
subnetRPCEndpoint,
subnetChainId,
subnetCurrencySymbol,
subnetEndpointHttp,
subnetEndpointWs,
subnetLogoUrl,
subnetName,
subnetId,
subnetCurrencySymbol,
subnetChainId,
{ gasLimit: 4_000_000 }
)

Expand Down
155 changes: 84 additions & 71 deletions test/topos-core/SubnetRegistrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { expect } from 'chai'
describe('SubnetRegistrator', () => {
let subnetRegistrator: Contract

const endpoint = 'http://127.0.0.1'
const chainId = 1
const currencySymbol = 'SUB'
const endpointHttp = 'http://127.0.0.1'
const endpointWs = 'ws://127.0.0.1'
const logoURL = 'http://image-url.com'
const subnetName = 'Test Subnet'
const name = 'Test Subnet'
const subnetId = ethers.utils.formatBytes32String('subnetId')
const subnetCurrencySymbol = 'SUB'
const chainId = 1

async function deploySubnetRegistratorFixture() {
const [admin, nonAdmin, toposDeployer] = await ethers.getSigners()
Expand All @@ -36,12 +37,13 @@ describe('SubnetRegistrator', () => {
subnetRegistrator
.connect(nonAdmin)
.registerSubnet(
endpoint,
chainId,
currencySymbol,
endpointHttp,
endpointWs,
logoURL,
subnetName,
subnetId,
subnetCurrencySymbol,
chainId
name,
subnetId
)
).to.be.revertedWith('Ownable: caller is not the owner')
})
Expand All @@ -50,25 +52,27 @@ describe('SubnetRegistrator', () => {
const { admin, subnetRegistrator } =
await deploySubnetRegistratorFixture()
await registerSubnet(
endpoint,
admin,
chainId,
currencySymbol,
endpointHttp,
endpointWs,
logoURL,
subnetName,
name,
subnetId,
subnetCurrencySymbol,
chainId,
subnetRegistrator,
admin
subnetRegistrator
)
await expect(
registerSubnet(
endpoint,
admin,
chainId,
currencySymbol,
endpointHttp,
endpointWs,
logoURL,
subnetName,
name,
subnetId,
subnetCurrencySymbol,
chainId,
subnetRegistrator,
admin
subnetRegistrator
)
).to.be.revertedWith('Bytes32Set: key already exists in the set.')
})
Expand All @@ -77,19 +81,21 @@ describe('SubnetRegistrator', () => {
const { admin, subnetRegistrator } =
await deploySubnetRegistratorFixture()
await registerSubnet(
endpoint,
admin,
chainId,
currencySymbol,
endpointHttp,
endpointWs,
logoURL,
subnetName,
name,
subnetId,
subnetCurrencySymbol,
chainId,
subnetRegistrator,
admin
subnetRegistrator
)
const subnet = await subnetRegistrator.subnets(subnetId)
expect(subnet.name).to.equal(subnetName)
expect(subnet.currencySymbol).to.equal(subnetCurrencySymbol)
expect(subnet.endpoint).to.equal(endpoint)
expect(subnet.name).to.equal(name)
expect(subnet.currencySymbol).to.equal(currencySymbol)
expect(subnet.endpointHttp).to.equal(endpointHttp)
expect(subnet.endpointWs).to.equal(endpointWs)
expect(subnet.logoURL).to.equal(logoURL)
expect(subnet.chainId).to.equal(chainId)
})
Expand All @@ -98,14 +104,15 @@ describe('SubnetRegistrator', () => {
const { admin, subnetRegistrator } =
await deploySubnetRegistratorFixture()
await registerSubnet(
endpoint,
admin,
chainId,
currencySymbol,
endpointHttp,
endpointWs,
logoURL,
subnetName,
name,
subnetId,
subnetCurrencySymbol,
chainId,
subnetRegistrator,
admin
subnetRegistrator
)
const count = await subnetRegistrator.getSubnetCount()
expect(count).to.equal(1)
Expand All @@ -115,14 +122,15 @@ describe('SubnetRegistrator', () => {
const { admin, subnetRegistrator } =
await deploySubnetRegistratorFixture()
await registerSubnet(
endpoint,
admin,
chainId,
currencySymbol,
endpointHttp,
endpointWs,
logoURL,
subnetName,
name,
subnetId,
subnetCurrencySymbol,
chainId,
subnetRegistrator,
admin
subnetRegistrator
)
const id = await subnetRegistrator.getSubnetIdAtIndex(0)
expect(id).to.equal(subnetId)
Expand All @@ -132,14 +140,15 @@ describe('SubnetRegistrator', () => {
const { admin, subnetRegistrator } =
await deploySubnetRegistratorFixture()
await registerSubnet(
endpoint,
admin,
chainId,
currencySymbol,
endpointHttp,
endpointWs,
logoURL,
subnetName,
name,
subnetId,
subnetCurrencySymbol,
chainId,
subnetRegistrator,
admin
subnetRegistrator
)
const exists = await subnetRegistrator.subnetExists(subnetId)
expect(exists).to.be.true
Expand All @@ -150,14 +159,15 @@ describe('SubnetRegistrator', () => {
await deploySubnetRegistratorFixture()
await expect(
registerSubnet(
endpoint,
admin,
chainId,
currencySymbol,
endpointHttp,
endpointWs,
logoURL,
subnetName,
name,
subnetId,
subnetCurrencySymbol,
chainId,
subnetRegistrator,
admin
subnetRegistrator
)
)
.to.emit(subnetRegistrator, 'NewSubnetRegistered')
Expand Down Expand Up @@ -185,14 +195,15 @@ describe('SubnetRegistrator', () => {
const { admin, subnetRegistrator } =
await deploySubnetRegistratorFixture()
await registerSubnet(
endpoint,
admin,
chainId,
currencySymbol,
endpointHttp,
endpointWs,
logoURL,
subnetName,
name,
subnetId,
subnetCurrencySymbol,
chainId,
subnetRegistrator,
admin
subnetRegistrator
)
await expect(removeSubnet(subnetId, subnetRegistrator, admin))
.to.emit(subnetRegistrator, 'SubnetRemoved')
Expand All @@ -201,24 +212,26 @@ describe('SubnetRegistrator', () => {
})

async function registerSubnet(
endpoint: string,
admin: Wallet,
chainId: number,
currencySymbol: string,
endpointHttp: string,
endpointWs: string,
logoURL: string,
subnetName: string,
name: string,
subnetId: string,
subnetCurrencySymbol: string,
chainId: number,
subnetRegistrator: Contract,
admin: Wallet
subnetRegistrator: Contract
) {
return await subnetRegistrator
.connect(admin)
.registerSubnet(
endpoint,
chainId,
currencySymbol,
endpointHttp,
endpointWs,
logoURL,
subnetName,
subnetId,
subnetCurrencySymbol,
chainId
name,
subnetId
)
}

Expand Down

0 comments on commit 3f82b10

Please sign in to comment.