communityhub

package
v0.0.0-...-740805f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2024 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CONTRACT_CENSUS_TYPE_FC represents the census type for all farcaster
	// users in the CommunityHub contract
	CONTRACT_CENSUS_TYPE_FC = iota
	// CONTRACT_CENSUS_TYPE_CHANNEL represents the census type for all members
	// of a warpcast channel in the CommunityHub contract
	CONTRACT_CENSUS_TYPE_CHANNEL
	// CONTRACT_CENSUS_TYPE_FOLLOWERS represents the census type for all
	// followers of an user in the CommunityHub contract
	CONTRACT_CENSUS_TYPE_FOLLOWERS
	// CONTRACT_CENSUS_TYPE_CSV represents the census type for all addresses
	// in a CSV file (that are also farcaster users) in the CommunityHub
	// contract
	CONTRACT_CENSUS_TYPE_CSV
	// CONTRACT_CENSUS_TYPE_ERC20 represents the census type for all holders
	// of an ERC20 token (that are also farcaster users) in the CommunityHub
	// contract
	CONTRACT_CENSUS_TYPE_ERC20
	// CONTRACT_CENSUS_TYPE_NFT represents the census type for all holders of
	// an NFT (that are also farcaster users) in the CommunityHub contract
	// contract
	CONTRACT_CENSUS_TYPE_NFT
)
View Source
const DefaultScannerCooldown = time.Second * 20

DefaultScannerCooldown is the default time that the scanner sleeps between scan iterations

Variables

View Source
var (
	// ErrMissingDB is returned when no database is provided during CommunituHub
	// initialization
	ErrMissingDB = fmt.Errorf("missing db")
	// ErrMissingCensus3 is returned when no census3 client is provided during
	// CommunityHub initialization
	ErrMissingCensus3 = fmt.Errorf("missing db")
	// ErrMissingContracts is returned when no contracts addresses and chain ids
	// are provided during CommunityHub initialization
	ErrMissingContracts = fmt.Errorf("missing contracts addresses and chain id")
	// ErrMissingChainAliases is returned when no chain aliases are provided
	// during CommunityHub initialization
	ErrMissingChainAliases = fmt.Errorf("missing chain aliases by chain id")
	// ErrNoValidContracts is returned when no valid contracts are provided
	// during CommunityHub initialization
	ErrNoValidContracts = fmt.Errorf("no valid contracts provided")
	// ErrContractNotFound is returned when the contract is not found in the
	// provided contracts list
	ErrContractNotFound = fmt.Errorf("contract not found for the provided chain id")
	// ErrClosedDB is returned when the database is closed
	ErrClosedDB = fmt.Errorf("db is closed")
	// ErrWeb3Client is returned when no web3 client is found in the provided
	// web3 pool
	ErrWeb3Client = fmt.Errorf("failed to get web3 client from the pool")
	// ErrInitContract is returned when the initialization of the contract fails
	// during CommunityHub initialization
	ErrInitContract = fmt.Errorf("failed to initialize contract")
	// ErrCreatingSigner is returned when the signer cannot be created
	ErrCreatingSigner = fmt.Errorf("failed to create auth transactor signer")
	// ErrInitializingPrivateKey is returned when the private key cannot be
	// initialized
	ErrInitializingPrivateKey = fmt.Errorf("failed to initialize private key")
	// ErrNoPrivKeyConfigured is returned when no private key is provided during
	// CommunityHub initialization and then a transaction is attempted to be
	// sent with a signer
	ErrNoPrivKeyConfigured = fmt.Errorf("no private key defined")
	// ErrDecodingCommunity is returned when an error occurs while decoding a
	// community from the community hub contract
	ErrDecodingCommunity = fmt.Errorf("error decoding creation log from the community hub contract")
	// ErrGettingCommunity is returned when an error occurs while getting
	// community data from the community hub contract
	ErrGettingCommunity = fmt.Errorf("error getting community data from the community hub contract")
	// ErrCommunityIDMismatch is returned when the community ID does not match
	// the expected community ID
	ErrCommunityIDMismatch = fmt.Errorf("community ID mismatch")
	// ErrSettingCommunity is returned when an error occurs while setting
	// community data in the community hub contract
	ErrSettingCommunity = fmt.Errorf("error setting community data in the community hub contract")
	// ErrInvalidCommunityData is returned when invalid community data is
	// provided
	ErrInvalidCommunityData = fmt.Errorf("invalid community data provided")
	// ErrCommunityNotFound is returned when the community is not found in the
	// contract
	ErrCommunityNotFound = fmt.Errorf("community not found")
	// ErrDisabledCommunity is returned when the community is disabled in the
	// contract
	ErrDisabledCommunity = fmt.Errorf("community is disabled")
	// ErrGettingResults is returned when an error occurs while getting results
	// from the community hub contract
	ErrGettingResults = fmt.Errorf("error getting results from the community hub contract")
	// ErrSettingResults is returned when an error occurs while setting results
	// in the community hub contract
	ErrSettingResults = fmt.Errorf("error setting results in the community hub contract")
	// ErrUnknownCensusType is returned when an unknown census type is found
	// while encoding or decoding a community for the community hub contract
	ErrUnknownCensusType = fmt.Errorf("unknown census type")
	// ErrNoChannelProvided is returned when no channel is provided during the
	// creation of a community with a channel census type
	ErrNoChannelProvided = fmt.Errorf("no channel provided")
	// ErrNoUserRefProvided is returned when no user reference is provided
	// during the creation of a community with a followers census type
	ErrNoUserRefProvided = fmt.Errorf("no user reference provided")
	// ErrBadCensusAddressees is returned when the census addressees are not
	// provided in the correct format or are empty
	ErrBadCensusAddressees = fmt.Errorf("bad community census addressees")
	// ErrNoAdminCreator is returned when the provided admin list does not
	// contain the creator of the community
	ErrNoAdminCreator = fmt.Errorf("the creator must be an admin")
	// ErrAddCommunity is returned when an error occurs while adding a new
	// community from the community hub contract to the database
	ErrAddCommunity = fmt.Errorf("error adding community to the database")
	// ErrSendingTx is returned when an error occurs while sending the a
	// transaction to the community hub contract
	ErrSendingTx = fmt.Errorf("error estimating gas")
	// ErrEncodeCommunityID is returned when the community ID cannot be encoded
	// from the chain short name and the ID
	ErrEncodeCommunityID = fmt.Errorf("error encoding chain community ID")
	// ErrDecodeCommunityID is returned when the ID and the chain
	// short name cannot be decoded from the community ID
	ErrDecodeCommunityID = fmt.Errorf("error decoding chain community ID")
)

Functions

func DecodePrefix

func DecodePrefix(prefixed string) (string, string, bool)

DecodePrefix decodes a prefixed content following the format "prefix:content" and returns the prefix and the content separately.

func DecodeUserChannelFID

func DecodeUserChannelFID(channelFID string) (uint64, error)

DecodeUserChannelFID decodes a user reference from farcaster to a user FID following the format "fid:<fid>" from the community census channel metadata. When a community has a census based on the creator followers, the channel metadata is used to store the user reference. If something goes wrong decoding the user FID, it returns an error.

func EncodePrefix

func EncodePrefix(prefix, content string) string

EncodePrefix encodes a content with a prefix following the format "prefix:content".

func EncodeUserChannelFID

func EncodeUserChannelFID(fid uint64) string

EncodeUserChannelFID encodes a user FID to a user reference from farcaster following the format "fid:<fid>" to be stored in the community metadata as the census channel (both, channel and followers census types follow the same creation process so they are interchangeable).

func HubToContract

func HubToContract(hcommunity *HubCommunity) (comhub.ICommunityHubCommunity, error)

HubToContract converts a internal community struct (HubCommunity) to a contract community struct (ICommunityHubCommunity)

func HubToDB

func HubToDB(hcommunity *HubCommunity) (*dbmongo.Community, error)

HubToDB converts a internal community struct (HubCommunity) to a db community struct (*dbmongo.Community) to be stored or updated in the database. It creates the db census according to the community census type, and if the census type is a channel, it sets the channel. If the census type is an erc20 or nft, it decodes every census network address to get the contract address and blockchain. It returns an error if the census type is unknown, if no channel is provided when the census type is a channel, or if no valid addresses were found when the census type is an erc20 or nft.

Types

type CensusType

type CensusType string

CensusType represents the type of census that a community is using to create polls

const (
	// CensusTypeChannel represents the census that includes all the members of
	// a warpcast channel
	CensusTypeChannel CensusType = "channel"
	// CensusTypeERC20 represents the census that includes all the holders of
	// an ERC20 token
	CensusTypeERC20 CensusType = "erc20"
	// CensusTypeNFT represents the census that includes all the holders of an
	// NFT
	CensusTypeNFT CensusType = "nft"
	// CensusTypeFollowers represents the census that includes all the followers
	// of an user in a source (farcaster or other like alfafrens)
	CensusTypeFollowers CensusType = "followers"
)

type CommunityHub

type CommunityHub struct {
	ChainAliases map[string]uint64
	// contains filtered or unexported fields
}

CommunityHub struct defines the CommunityHub wrapper. It includes the the functions to scan for new communities in the contract and create them in the database in background. It also includes some functions to get communities or set and get results using the contract.

func NewCommunityHub

func NewCommunityHub(
	goblalCtx context.Context,
	w3p *c3web3.Web3Pool,
	census3 *c3cli.HTTPclient,
	conf *CommunityHubConfig,
) (*CommunityHub, error)

NewCommunityHub function initializes a new CommunityHub instance. It returns an error if the database is not defined in the configuration or if the web3 client cannot be initialized. It initializes the contract with the web3 client and the contract address, and sets the next community candidate to be created from the database to start to scan for new communities. It also sets the scanner cooldown from the configuration if it is defined, or uses the default one. It receives the global context, the web3 pool, and the configuration of the CommunityHub.

func (*CommunityHub) Census3ChainID

func (ch *CommunityHub) Census3ChainID(shortName string) (uint64, bool)

Census3ChainID method gets the chain ID from the short name of the blockchain in the census3 service. It iterates over the supported chains in the census3 service and returns the chain ID if the short name is found. If the short name is not found, it returns 0 and false.

func (*CommunityHub) ChainAliasAndContractIDFromCommunityID

func (hc *CommunityHub) ChainAliasAndContractIDFromCommunityID(communityID string) (string, uint64, bool)

ChainAliasAndContractIDFromCommunityID method gets the chain alias and the ID of the community by the community ID. It decodes the chain alias and the ID from the community ID. If the community ID is not valid, it returns an empty string, 0, and false.

func (*CommunityHub) ChainAliasFromID

func (ch *CommunityHub) ChainAliasFromID(chainID uint64) (string, bool)

ChainAliasFromID method returns the chain alias by the chain ID. It iterates over the chain aliases map and returns the chain alias if the chain ID is found.

func (*CommunityHub) ChainIDAndIDFromCommunityID

func (h *CommunityHub) ChainIDAndIDFromCommunityID(communityID string) (uint64, uint64, bool)

ChainIDAndIDFromCommunityID method gets the chain ID and the ID of the community by the community ID. It decodes the chain alias and the ID from the community ID and gets the chain ID from the chain alias. If the chain alias is not found, it returns 0, 0, and false.

func (*CommunityHub) ChainIDFromAlias

func (ch *CommunityHub) ChainIDFromAlias(alias string) (uint64, bool)

ChainIDFromAlias method gets the chain ID by the chain alias. It iterates over the chain aliases map and returns the chain ID if the chain alias is found. If the chain alias is not found, it returns 0 and false.

func (*CommunityHub) CommunityContract

func (ch *CommunityHub) CommunityContract(communityID string) (*HubContract, error)

CommunityContract method gets the contract of a community by the community ID. It decodes the chain ID from the community ID and gets the contract from the contracts map. If the contract is not found, it returns an error.

func (*CommunityHub) CommunityIDByChainAlias

func (ch *CommunityHub) CommunityIDByChainAlias(id uint64, chainAlias string) (string, bool)

CommunityIDByChainAlias method gets the community ID by the chain ID and the ID of the community. It gets the chain alias from the chain ID and creates the community ID using the chain alias and the ID. If the chain alias is not found, it returns an empty string and false.

func (*CommunityHub) CommunityIDByChainID

func (ch *CommunityHub) CommunityIDByChainID(id, chainID uint64) (string, bool)

CommunityIDByChainID method gets the community ID by the chain ID and the ID of the community. It gets the chain alias from the chain ID and creates the community ID using the chain alias and the ID. If the chain alias is not found, it returns an empty string and false.

func (*CommunityHub) ScanNewCommunities

func (ch *CommunityHub) ScanNewCommunities()

ScanNewCommunities method starts the listener to scan for new communities in the contract and create them in the database in background. It gets the next community from the contract and, if it exists, it is created it in the database. If something goes wrong getting the community data or creating the community in the database, it logs an error and retries in the next iteration. It sleeps if no communities are found in the contract.

func (*CommunityHub) Stop

func (ch *CommunityHub) Stop()

Stop method stops the listener and waits for the goroutines to finish.

func (*CommunityHub) SyncCommunities

func (ch *CommunityHub) SyncCommunities()

SyncCommunities method starts the listener to sync the communities in the database with the contract. It gets the community data from the contract and updates it in the database. It iterates from the first community (id: 1) to the last one (next - 1) in the contract updating the community data in the database. If something goes wrong it logs an error and continues with the next iteration. It sleeps between iterations the sync cooldown time.

func (*CommunityHub) UpdateCommunity

func (ch *CommunityHub) UpdateCommunity(newData *HubCommunity) error

UpdateCommunity method updates a community in the contract and the database. It merges the new data with the current data of the community and updates it in the contract and the database. If something goes wrong updating the community in the contract or the database, it returns an error.

type CommunityHubConfig

type CommunityHubConfig struct {
	ChainAliases      map[string]uint64
	ContractAddresses map[string]common.Address
	DB                *dbmongo.MongoStorage
	PrivKey           string
	DiscoverCooldown  time.Duration
	SyncCooldown      time.Duration
}

CommunityHubConfig struct defines the configuration for the CommunityHub. It includes the contract address, the chain ID where the contract is deployed, a database instance, and the scanner cooldown (by default 10s (DefaultScannerCooldown)).

type ContractAddress

type ContractAddress struct {
	Blockchain string
	Address    common.Address
}

ContractAddress represents the address of a contract in a certain blockchain, which is included in this struct

type HubCommunity

type HubCommunity struct {
	// CommunityID is the unique identifier of the community in any chain
	CommunityID string
	// ContractID is the unique identifier of the community in the CommunityHub
	// contract in a certain chain
	ContractID uint64
	// ChainID is the unique identifier of the chain where the CommunityHub
	// contract is deployed for this particular community
	ChainID        uint64
	Name           string
	ImageURL       string
	GroupChatURL   string
	CensusType     CensusType
	CensusAddesses []*ContractAddress
	CensusChannel  string   // channels id or user reference (for follower census type)
	Channels       []string // warpcast channels ids
	Admins         []uint64 // farcaster users fids
	Notifications  *bool
	Disabled       *bool
	// contains filtered or unexported fields
}

HubCommunity represents a community in the CommunityHub package

func ContractToHub

func ContractToHub(contractID, chainID uint64, communityID string, cc comhub.ICommunityHubCommunity) (*HubCommunity, error)

ContractToHub converts a contract community struct (ICommunityHubCommunity) to a internal community struct (HubCommunity)

func DBToHub

func DBToHub(dbCommunity *dbmongo.Community, contractID, chainID uint64) (*HubCommunity, error)

DBToHub converts a db community struct (*dbmongo.Community) to a internal community struct (HubCommunity) to be used in the CommunityHub package. It decodes the census addresses according to the census type, and if the census type is a channel, it sets the channel. If the census type is an erc20 or nft, it decodes every census network address to get the contract address and blockchain. It returns an error if the census type is unknown.

type HubContract

type HubContract struct {
	ChainID    uint64
	ChainAlias string
	Address    common.Address
	// contains filtered or unexported fields
}

HubContract struct represents the CommunityHub contract with in a specific chain. It contains the chain ID, the contract address, the web3 client, the contract, the private key, and the private address. It provides a set of methods to interact with the contract, such as getting the next community ID, getting and setting the community data, getting and setting the election results of a community.

func LoadContract

func LoadContract(chainID uint64, chainAlias string, addr common.Address, w3p *c3web3.Web3Pool, pk string) (*HubContract, error)

LoadContract method initializes the CommunityHub struct with the chain ID, contract address, web3 pool, and private key provided. If something goes wrong initializing the web3 client, the contract, or the private key, it returns an error.

func (*HubContract) Community

func (hc *HubContract) Community(communityID string) (*HubCommunity, error)

Community method gets the community data using the community ID from the contract and returns it as a HubCommunity struct. If something goes wrong getting the community data from the contract, it returns an error.

func (*HubContract) NextContractID

func (hc *HubContract) NextContractID() (uint64, error)

NextID method gets the next community ID from the contract and returns it as a uint64. If something goes wrong getting the next community ID from the contract, it returns an error.

func (*HubContract) Results

func (hc *HubContract) Results(communityID uint64, electionID []byte) (*HubResults, error)

Results method gets the election results using the community and elections IDs from the contract and returns them as a HubResults struct. If something goes wrong getting the results from the contract, it returns an error.

func (*HubContract) SetCommunity

func (hc *HubContract) SetCommunity(community *HubCommunity) error

SetCommunity method sets the community data provided in the contract. If something goes wrong setting the community data in the contract, it returns an error.

func (*HubContract) SetResults

func (hc *HubContract) SetResults(community *HubCommunity, results *HubResults) error

SetResults method sets the election results provided to the community and election IDs provided. If something goes wrong setting the results in the contract, it returns an error.

type HubResults

type HubResults struct {
	ElectionID       []byte
	Question         string
	Options          []string
	Date             string
	Tally            [][]*big.Int
	Turnout          *big.Int
	TotalVotingPower *big.Int
	Participants     []*big.Int
	CensusRoot       []byte
	CensusURI        string
	Disabled         bool
	VoteCount        *big.Int
}

HubResult represents the result of a poll in the CommunityHub

Directories

Path Synopsis
contracts

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL