Automating Snapshot Proposals for Voting

April 1, 2023 - Darrel Herbst

Lots of Decentralized Autonomous Organizations (DAOs) use snapshot.org to organize voting across their members. Snapshot provides an API to be able to create new proposals, and do a lot more. Below is an example for automating proposal creation:

import * as dotenv from 'dotenv'
dotenv.config()
import { InfuraProvider } from '@ethersproject/providers';
import { Wallet } from '@ethersproject/wallet';
import snapshot from '@snapshot-labs/snapshot.js';
const hub = 'https://testnet.snapshot.org';
const client = new snapshot.Client712(hub);

const provider = new InfuraProvider('goerli', process.env.PROJECT_ID)
const signer = new Wallet(process.env.PRIVKEY, provider);

const blocknumber = await provider.getBlockNumber();
const begindate = new Date();
const enddate = new Date(begindate);
enddate.setDate(begindate.getDate() + 7);
const begintimestamp = Math.floor(begindate.getTime()/1000);
const endtimestamp = Math.floor(enddate.getTime()/1000);

const receipt = await client.proposal(signer, process.env.PUBADDR, {
  space: 'YOUR_SPACE_NAME',
  type: 'single-choice', // define the voting system
  title: 'P-0000 Test proposal with blocknumber ' + blocknumber.toString(),
  body: 'P-0000 This is the content of the proposal.',
  choices: ['In Favor', 'Against', 'Abstain'],
  start: begintimestamp,
  end: endtimestamp,
  snapshot: blocknumber,
  network: '5',
  plugins: JSON.stringify({}),
  app: 'your_app_name' // provide the name of your project which is using this snapshot.js integration
});