JavaScript Examples

Examples based on JavaScript Reference and use JavaScript API. ABI files are available from GitHub

The first step is to install the latest JavaScript API: see the instructions and release <https://github.com/collectivexyz/governance/pkgs/npm/governance> information.

Connect

The following code block demonstrates how to connect to the Ethereum RPC client

The next step is to create a VoterClass

CommunityClass

The first step to build a CommunityClass for your project is to use the Builder. The community class can be reused for every CollectiveGovernance contract.

logger.info('Building CommunityClass');
const communityBuilder = new CommunityBuilder(config.abiPath, config.communityAddress, web3, wallet, config.getGas());
await communityBuilder.aCommunity();
await communityBuilder.asErc721Community(config.tokenContract);
await communityBuilder.withQuorum(100);
const classAddress = await communityBuilder.build();
logger.info(`CommunityClass created at ${classAddress}`);

Make a note of the Ethereum address for the class you created. For example 0x6bAc373e27f844259F3B79A6E7dAFf3868eBDc13

Governance

The next step is to build the Governance Contract for your community. This will take the VoterClass as an argument.

const governanceBuilder = new GovernanceBuilder(config.abiPath, config.builderAddress, web3, wallet, config.getGas());
const name = await governanceBuilder.name();
logger.info(name);
await governanceBuilder.aGovernance();
await governanceBuilder.withSupervisor(wallet.getAddress());
await governanceBuilder.withCommunityClassAddress(config.communityClass);
const governanceAddress = await governanceBuilder.build();
logger.info(`Governance contract created at ${governanceAddress}`);

Make a note of the address of the created contract as this will be used for all future governance operations.

Voting

Now you can introduce a vote using the governance contract.

const web3 = new Web3(config.rpcUrl);
const wallet = new EthWallet(config.privateKey, web3);
wallet.connect();
logger.info(`Wallet connected: ${wallet.getAddress()}`);
const governance = new CollectiveGovernance(config.abiPath, config.contractAddress, web3, wallet, config.getGas());
logger.info(`Connected to contract: ${config.contractAddress}`);
const name = await governance.name();
const version = await governance.version();
logger.info(`${name}: ${version}`);
const proposalId = await governance.propose();

Next configure the proposal and open voting

await governance.configure(proposalId, 1, 5);
const storage = new Storage(config.abiPath, storageAddress, web3);
const storageName = await storage.name();
const storageVersion = await storage.version();
logger.info(`${storageName}: ${storageVersion}`);
const quorum = await storage.quorumRequired(proposalId);
const duration = await storage.voteDuration(proposalId);
logger.info(`New Vote - ${proposalId}: quorum=${quorum}, duration=${duration}`);
await governance.startVote(proposalId);
logger.info('Voting is open...');

Finally just vote

await governance.voteFor(proposalId);