nem-voting
Version:
168 lines (167 loc) • 5.65 kB
TypeScript
import { Address, PublicAccount, Transaction, TransferTransaction, MultisigTransaction } from "nem-library";
import { IResults, IVote } from "./counting";
import { Observable } from "rxjs";
import { PollIndex } from "./poll-index";
interface IFormData {
/**
* Title of the poll
*/
title: string;
/**
* date of ending, as milliseconds from UNIX epoch
*/
doe: number;
/**
* True if multiple votes are accepted
*/
multiple: boolean;
/**
* type of the poll
*/
type: number;
}
interface IPollData {
/**
* General information abount the poll
*/
formData: IFormData;
/**
* Detailed description for the poll
*/
description: string;
/**
* Options of the poll
*/
options: string[];
/**
* (optional) Array of Addresses to be whitelisted. Only for whitelist polls
*/
whitelist?: Address[];
}
interface IBroadcastData {
/**
* Transactions that need to be sent and confirmed for the poll to be broadcasted
*/
transactions: TransferTransaction[];
/**
* Broadcasted Poll object. Can not be used until the transactions have been broadcasted and confirmed
*/
broadcastedPoll: BroadcastedPoll;
}
/**
* Maps strings to addresses, one for each poll option
*/
interface IAddressLink {
[key: string]: Address;
}
/**
* Abstract class that represents a poll
*/
declare abstract class Poll {
readonly data: IPollData;
}
/**
* An unbroadcasted poll. Exists only locally and not on the blockchain yet
*/
declare class UnbroadcastedPoll extends Poll {
constructor(formData: IFormData, description: string, options: string[], whitelist?: Address[]);
/**
* Broadcasts an unbroadcasted poll and returns the resulting broadcasted poll object (as a promise)
* @param creatorPublicKey - public key of the poll creator
* @param pollIndex - optionally provide the poll index to send the poll to.
* If not specified the default public index is used
* @return {pollAddress: Address, transactions: TransferTransaction[]} - returns the poll address
* and the transactions that need to be sent for it to be broadcasted
*/
broadcast: (creatorPublicKey: string, pollIndex?: PollIndex | undefined) => IBroadcastData;
getBroadcastFee: () => number;
}
/**
* A broadcasted poll. Represents a Poll that exists in the blockchain.
*/
declare class BroadcastedPoll extends Poll {
/**
* The poll address
*/
readonly address: Address;
/**
* The block the poll ended on. It is undefined until fetched.
*/
endBlock?: number;
/**
* Map from option to option address
*/
private optionAddresses;
/**
* Fetches a Broadcasted Poll from the blockchain by its address
* @param pollAddress - The poll's NEM Address
* @return Promise<BroadcastedPoll>
*/
private static fromAddressPromise;
/**
* Fetches a Broadcasted Poll from the blockchain by its address
* @param pollAddress - The poll's NEM Address
* @return Observable<BroadcastedPoll>
*/
static fromAddress: (pollAddress: Address) => Observable<BroadcastedPoll>;
/**
* Gets the option address for a given option
* @param option - The option
* @return Address | null
*/
getOptionAddress: (option: string) => Address | null;
/**
* Sets the block when the poll ends
* @param block - The end block
* @return void
*/
setEndBlock: (block: number) => void;
/**
* Get the transactions needed to be broadcasted by the creator of the poll for extending the whitelist before the poll ending
* @param addresses - The additional addresses to be added
*/
extendWhitelist: (addresses: Address[]) => TransferTransaction[];
/**
* Gets the results for the poll
* @param pollAddress - The poll's NEM Address
* @return Observable<IResults>
*/
getResults: () => Observable<IResults>;
/**
* Gets the results for the poll as a csv string
* @param pollAddress - The poll's NEM Address
* @return Observable<string>
*/
getCsvResults: () => Observable<string>;
/**
* Gets the results for the poll as an array of vote objects
* @param pollAddress - The poll's NEM Address
* @return Observable<IResults>
*/
getVoters: () => Observable<IVote[]>;
/**
* validates a poll's structure and returns wether it is correct or not
* @return boolean
*/
validate: () => boolean;
/**
* Votes on the poll from a given account, returns the vote transaction result
* @param option - The option to vote
* @return TransferTransaction - the transaction that needs to be sent to vote
*/
vote: (option: string) => TransferTransaction;
/**
* Votes on the poll from a multisig account, returns the vote transaction result
* @param multisigAccount - The public account of the multisig account that votes
* @param option - The option to vote
* @return MultisigTransaction - the transaction that needs to be sent to vote
*/
voteMultisig: (multisigAccount: PublicAccount, option: string) => MultisigTransaction;
/**
* Gets the votes that an address has sent to the poll, if it has not voted returns null
* @param address - The address of the voter
* @return Observable<Transaction[] | null>
*/
getVotes: (address: Address) => Observable<Transaction[] | null>;
}
export { IPollData, IFormData, IBroadcastData, IAddressLink, Poll, BroadcastedPoll, UnbroadcastedPoll };