1.0.7 • Published 1 year ago

eas-poll-action-module v1.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

EasPollActionModule

EasPollActionModule.sol is an Open Action Module (Publication Module) for Lens Protocol. It allows users to create and vote on polls using the Ethereum Attestation Service (EAS).

Using the EasPollActionModule Contract

To use the live EasPollActionModule you can use the address and metadata below:

NetworkChain IDDeployed ContractMetadataEAS Schema UID
Mumbai800010xBd43F2Bc51020347619c2cC243E3B21859f4f64clink0x5e67b8b854d74789f6fa56f202907f85e3e53b87abe3d218c9f6dee1cc60ecbd

The EasPollActionModule contract can be used as an Open Action Module on Lens Protocol publications. Here are examples of successful transactions on Mumbai using the poll module:

Post transaction

Act transaction

Attestation

Install eas-poll-action-module Helper Library

The helper library provides functions for creating the Lens SDK OpenActionModuleInput and ActOnOpenActionRequest which can be used to attach and act on the Open Action Module. To use the eas-poll-action-module helper library, you can install it using npm:

npm install eas-poll-action-module

Create a Poll

To create a poll, the initialize calldata ABI is:

NameDescriptionType
optionsAn array of 2 to 4 voting choice strings that have been encoded into bytes32 formatbytes32[4]
followersOnlyRestrict voting to followers of the publication authorbool
endTimestampThe timestamp (in seconds) when the poll ends or zero for open-endeduint40
signatureRequiredWhether a signature is required for votingbool

Here's an example of creating the encoded poll calldata with the Lens SDK:

import { type EasPoll, createPollActionModuleInput } from "eas-poll-action-module";

const poll: EasPoll = {
  options: ["Option A", "Option B", "Option C", "Option D"],
  followersOnly: true, // Optional
  endTimestamp: Math.floor(Date.now() / 1000) + 60 * 60 * 24, // Optional
  signatureRequired: false, // Optional
};

const pollAction: OpenActionModuleInput = createPollActionModuleInput(poll);

Vote on a Poll

To vote on a poll, you create a vote tuple:

ParameterDescriptionType
publicationProfileIdThe profile id of the publication authoruint256
publicationIdThe publication iduint256
actorProfileIdThe profile id of the voteruint256
actorProfileOwnerThe address of the voteraddress
transactionExecutorThe address of the transaction executoraddress
optionIndexThe index of the option the voter selected (0 to 3)uint8
timestampThe timestamp (in seconds) when the vote was castuint40

Here's how you can use the eas-poll-action-module helper library to create the vote "act on" request:

import { type EasVote, createVoteActionRequest } from "eas-poll-action-module";

const vote: EasVote = {
  publicationId: "0xd8-0x01",
  actorProfileId: "0x01",
  actorProfileOwner: "0x1234567890123456789012345678901234567890",
  optionIndex: 1,
};

const voteAction: ActOnOpenActionRequest = createVoteActionRequest(vote, post.id);

⚠️ Note:

When signatureRequired is true on the Poll you must sign the vote using the transactionExecutor address. You can provide an ethers.Signer to the createVoteActionRequest function to sign the vote.

EAS GraphQL API

The scemaUid can also be used to query the EAS GraphQL API for votes. The eas-poll-action-module helper library provides functions for encoding the pollId and optionIndex to be used in the GraphQL query.

Here's how you can get an attestation (vote) count from EAS:

import { createVoteCountQueryVariables, getVoteCount } from "eas-poll-action-module";

const variables = createVoteCountQueryVariables(publicationId);
const count = await getVoteCount(variables);

Here's how you can get an attestation (vote) count for a specific option from EAS.

import { createVoteCountForOptionQueryVariables, getVoteCountForOption } from "eas-poll-action-module";

const optionIndex = 1;
const variables = createVoteCountForOptionQueryVariables(publicationId, optionIndex);
const count = await getVoteCountForOption(variables);

Get Poll Results

The contract also provides many ways to get poll results.

/**
 * @dev Get the number of votes/attestations for a publication.
 * @param profileId The profile id of the publication author
 * @param pubId The publication id
 * @return The number of attestations
 */
function getAttestationCount(
    uint256 profileId,
    uint256 pubId
) external view returns (uint256);

/**
 * @dev Get the attestation for a vote at a specific index.
 * @param profileId The profile id of the publication author
 * @param pubId The publication id
 * @param index The index of the vote
 * @return The vote
 */
function getAttestationByIndex(
    uint256 profileId,
    uint256 pubId,
    uint256 index
) external view returns (bytes32);

/**
 * @dev Get the attested vote for an actor.
 * @param profileId The profile id of the publication author
 * @param pubId The publication id
 * @param actor The actor address
 * @return The vote
 */
function getVote(
    uint256 profileId,
    uint256 pubId,
    address actor
) external view returns (Vote memory);

/**
 * @dev Get the attestation for a vote at a specific index.
 * @param profileId The profile id of the publication author
 * @param pubId The publication id
 * @param index The index of the vote
 * @return The vote
 */
function getVoteByIndex(
    uint256 profileId,
    uint256 pubId,
    uint256 index
) external view returns (Vote memory);
1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago