Introduce directory contract (#102)

This commit is contained in:
Szymon Szlachtowicz 2021-07-09 08:42:49 +02:00 committed by GitHub
parent 990d662296
commit 28a623216d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,43 @@
pragma solidity ^0.8.5;
contract Directory {
address public votingContract;
address[] public communities;
mapping(address => uint256) private communitiesIdx;
constructor(address _votingContract) {
votingContract = _votingContract;
}
function isCommunityInDirectory(address community) public view returns (bool) {
return communitiesIdx[community] > 0;
}
function getCommunities() public view returns (address[] memory) {
return communities;
}
modifier onlyVotingContract() {
require(msg.sender == votingContract, 'Invalid sender');
_;
}
function addCommunity(address community) public onlyVotingContract {
require(communitiesIdx[community] == 0, 'Community already exist');
communities.push(community);
communitiesIdx[community] = communities.length;
}
function removeCommunity(address community) public onlyVotingContract {
uint256 index = communitiesIdx[community];
if (index == 0) return;
index--;
if (communities.length > 1) {
communities[index] = communities[communities.length - 1];
communitiesIdx[communities[index]] = index + 1;
}
communities.pop();
communitiesIdx[community] = 0;
}
}

View File

@ -0,0 +1,69 @@
import { expect, use } from 'chai'
import { loadFixture, deployContract, solidity } from 'ethereum-waffle'
import Directory from '../build/Directory.json'
use(solidity)
describe('directory contract', () => {
const communities = [
'0xAcdd71e5Ef3Ab3356d62da78A941aAc08a18CF2b',
'0xF1B65D4b7e5D6aE45c66Bc015e2556f228A6968f',
'0xadd590e785c0Da8B7A39A344e76fCF02193b3641',
]
async function fixture([alice, bob]: any[], provider: any) {
const contract = await deployContract(alice, Directory, [alice.address])
return { contract, alice, bob, provider }
}
it('deploys', async () => {
const { contract, alice } = await loadFixture(fixture)
expect(await contract.votingContract()).to.eq(alice.address)
})
it('adds community', async () => {
const { contract } = await loadFixture(fixture)
await contract.addCommunity(communities[0])
expect(await contract.getCommunities()).to.deep.eq([communities[0]])
await contract.addCommunity(communities[1])
expect(await contract.getCommunities()).to.deep.eq([communities[0], communities[1]])
})
it('only owner can add', async () => {
const { contract, bob } = await loadFixture(fixture)
const bobContract = contract.connect(bob)
await expect(bobContract.addCommunity(communities[0])).to.be.revertedWith('Invalid sender')
expect(await contract.getCommunities()).to.deep.eq([])
})
it('only owner can remove', async () => {
const { contract, bob } = await loadFixture(fixture)
await contract.addCommunity(communities[0])
const bobContract = contract.connect(bob)
await expect(bobContract.removeCommunity(communities[0])).to.be.revertedWith('Invalid sender')
expect(await contract.getCommunities()).to.deep.eq([communities[0]])
})
it('removes community', async () => {
const { contract } = await loadFixture(fixture)
await contract.addCommunity(communities[0])
await contract.addCommunity(communities[1])
await contract.addCommunity(communities[2])
await contract.removeCommunity(communities[1])
expect(await contract.getCommunities()).to.deep.eq([communities[0], communities[2]])
})
it("can't add duplicate", async () => {
const { contract } = await loadFixture(fixture)
await contract.addCommunity(communities[0])
await expect(contract.addCommunity(communities[0])).to.be.revertedWith('Community already exist')
expect(await contract.getCommunities()).to.deep.eq([communities[0]])
})
it('community in directory', async () => {
const { contract } = await loadFixture(fixture)
await contract.addCommunity(communities[0])
expect(await contract.isCommunityInDirectory(communities[0])).to.eq(true)
expect(await contract.isCommunityInDirectory(communities[1])).to.eq(false)
})
})