#
newDAO1.sol
The MyDAO contract implements a decentralized autonomous organization (DAO) that allows members to create proposals and vote on them. It defines a Proposal struct that contains information about each proposal, including its unique identifier, author, name, description, creation timestamp, options, status, power, and proposal information type. The contract also includes a private function stringsEquals that compares two strings and returns a boolean indicating whether they are equal. The public variables of the contract include the name, description, debugging information, image URL, and unique identifier of the DAO. It also defines two enums for voting options and proposal status. The contract imports the itoken.sol and newFactory1.sol contracts and declares a DAOFactory variable for the newFactory1 contract. The contract is responsible for creating and managing proposals, and tracking voting shares and tokens for voters and YK token holders.
The contract contains several mappings and constants used for tracking and storing data.
These include:
proposals
: A mapping of proposal IDs to Proposal objects. This is used to store all the proposals created by the contract.votes
: A mapping of voter addresses to proposal IDs to boolean values. This is used to track which addresses have already voted for a given proposal to prevent double voting.tokens_not_refunded
: A mapping of voter addresses to proposal IDs to boolean values. This is used to track whether tokens have been refunded for a proposal to prevent multiple refunds.token_amount_to_be_refunded
: A mapping of voter addresses to proposal IDs to refund amounts. This is used to store the amount of tokens to be refunded for a voter.voter_shares_to_be_given
: A mapping of voter addresses to voter share amounts. This is used to track the number of voter shares to be given to a voter.yk_shares_to_be_given
: A mapping of YK token holder addresses to YK share amounts. This is used to track the number of YK shares to be given to a YK token holder.voter_token
: An instance of the ISUToken contract. This is the contract for the voter token used by the DAO.yk_token
: An instance of the ISUToken contract. This is the contract for the YK token used by the DAO.CREATE_PROPOSAL_MIN_SHARE
: A constant that sets the minimum number of shares required to create a proposal.VOTING_PERIOD
: A constant that sets the duration of the voting period for proposals.nextProposalId
: An integer that tracks the ID for the next proposal to be created.transferLock
: A mapping of addresses to boolean values that tracks the transfer lock status for addresses.
The constructor function initializes the DAO contract with the following parameters:
_dao_name
: A string representing the name of the DAO._imageUrl
: A string representing the URL of the DAO's image._dao_description
: A string representing the description of the DAO._dao_id
: An integer representing the ID of the DAO.first_yk
: The address of the initial YK token holder.yk_token_in
: An instance of the ISUToken contract representing the YK token used by the DAO.voter_token_in
: An instance of the ISUToken contract representing the voter token used by the DAO._factory
: An instance of the DAOFactory contract representing the DAO factory used to create the DAO.
In the constructor, the factory, dao_name
, dao_id
, dao_description
, yk_token
, voter_token
, and imageUrl
variables are set to their corresponding parameter values. The yk_shares_to_be_given
and voter_shares_to_be_give
n mappings are then initialized with a share value of 1 * 10 ** 18 for the initial YK token holder.
#
Functions:
mint_from_DAO_yk_token
: This internal function allows the DAO contract to mint YK tokens. The function takes in a parameter _amount, which specifies the amount of YK tokens to be minted.mint_from_DAO_voter_token
: This internal function allows the DAO contract to mint voter tokens. The function takes in a parameter _amount, which specifies the amount of voter tokens to be minted.iterate_proposals
: This public function iterates through all proposals and emits proposal information for each proposal. It updates the active voter lock if the proposal's status is not pending. The function does not take in any parameters.accept_proposal
: This external function accepts a proposal by changing its status to Status.Accepted. The function takes in a parameter _proposalId, which specifies the ID of the proposal to be accepted.reject_proposal
: This external function rejects a proposal by changing its status to Status.Rejected. The function takes in a parameter _proposalId, which specifies the ID of the proposal to be rejected.pending_proposal
: This external function sets a proposal's status to Status.Pending. The function takes in a parameter _proposalId, which specifies the ID of the proposal to be set as pending.withdraw_voter_tokens
: This external function allows a user to withdraw voter tokens from the DAO. The function takes in a parameter _amount, which specifies the amount of voter tokens to be withdrawn. The function checks whether the user has enough shares and transfers the specified amount of voter tokens from the DAO to the user. It also mints voter tokens for the DAO.withdraw_yk_tokens
: This external function allows a user to withdraw YK tokens from the DAO. The function takes in a parameter _amount, which specifies the amount of YK tokens to be withdrawn. The function checks whether the user has enough shares and transfers the specified amount of YK tokens from the DAO to the user. It also mints YK tokens for the DAO.send_yk_tokens_to_address_yk
: This function sends YK tokens to a specified address. It takes in two parameters:yk_candidate
, which is the address to send the YK tokens to, and_amount
, which is the amount of YK tokens to send. It requires the caller to have YK privileges and updates the amount of YK tokens that will be given to the specified address.send_yk_tokens_to_address_yk_directly
: This function sends YK tokens directly to a specified address from the DAO. It takes in two parameters:yk_candidate
, which is the address to send the YK tokens to, and_amount
, which is the amount of YK tokens to send. It requires the caller to have YK privileges and updates the amount of YK tokens that will be given to the specified address. In addition to this, it also mints YK tokens from the DAO.send_voter_tokens_to_address_yk
: This function sends voter tokens to a specified address. It takes in two parameters:voter_candidate
, which is the address to send the voter tokens to, and_amount
, which is the amount of voter tokens to send. It requires the caller to have YK privileges and updates the amount of voter tokens that will be given to the specified address.send_voter_tokens_to_address_yk_directly
: This function sends voter tokens directly to a specified address from the DAO. It takes in two parameters:voter_candidate
, which is the address to send the voter tokens to, and_amount
, which is the amount of voter tokens to send. It requires the caller to have YK privileges and updates the amount of voter tokens that will be given to the specified address. In addition to this, it also mints voter tokens from the DAO.createProposal
: This function creates a new proposal. It takes in six parameters:name
, which is the name of the proposal;description
, which is the description of the proposal;_options
, which is an array of vote options;_options_num
, which is an array of corresponding vote option numbers;_power
, which is the maximum vote power for the proposal; and_type
, which is the type of the proposal (0 for normal, 1 for weighted). It requires the caller to have YK privileges and creates a new proposal with the specified parameters. It also updates the proposal count.vote
: This function performs a non-weighted vote on a proposal. It takes in three parameters:_proposalId
, which is the ID of the proposal;_vote
, which is an array of vote options; and_power
, which is an array of corresponding vote powers. It requires the caller to have voter privileges and updates the vote count for the specified proposal.
The code uses several Solidity concepts, such as function modifiers (require
statements) and structs (Proposal
), to ensure that the functions are secure and perform as intended.
vote_power
: This function performs a weighted vote on a proposal. It takes three arguments:_proposalId
(an unsigned integer representing the ID of the proposal),_vote
(a string array representing the vote options), and_power
(an unsigned integer array representing the corresponding vote powers). The function first checks if the voter has already voted, if they have enough shares to vote on the proposal, and if the voting period is still open. If all conditions are met, the function calculates the total power of the votes, checks that the total power does not exceed the proposal power, and then adds the vote powers to the corresponding proposal options. Finally, it updates the voter's vote status and active voter lock for the proposal.vote_power_weighted
: This function is similar tovote_power
, but it includes an additional argumentweight
, which is a uint representing the weight to apply to the vote powers. The weight is used to determine the influence of each voter's shares on the proposal. The function first checks if the voter has already voted, if they have enough shares to vote on the proposal (with the option to change the weight), and if the voting period is still open. If all conditions are met, the function calculates the total power of the votes, checks that the total power does not exceed the proposal power, and then adds the vote powers (multiplied by the weight) to the corresponding proposal options. Finally, it updates the voter's vote status and active voter lock for the proposal.retrieve_proposal_names
: This function simply retrieves the names of all proposals. It returns an array of proposal names.
Note that the code uses some external functions, such as stringsEquals
, which is a custom implementation of string comparison, and voter_token.balanceOf
, which retrieves the balance of a voter's tokens.
getProposalName()
: This function retrieves an array of proposal names. It loops through all proposals and adds the name of each proposal to an array calledmyprops
. The function then returns this array.getDaoid()
: This function retrieves the DAO ID. It simply returns the value of thedao_id
variable.has_yk_priviliges(address chk)
: This function checks whether the given address has YK (Yieldly Token) privileges. It first checks if the balance of YK tokens of the given address is greater than or equal to 1. If this condition is satisfied, it returnstrue
. Otherwise, it retrieves the current DAO using thefactory.getCurrentDAO()
function and checks whether any of the parent DAOs have a YK balance greater than or equal to 1 * 10^18. If this condition is satisfied, it returnstrue
. Otherwise, it returnsfalse
.getProposalDescription()
: This function retrieves an array of proposal descriptions. It loops through all proposals and adds the description of each proposal to an array calledmyprops
. The function then returns this array.getProposalVoteNames(uint proposal_idd)
: This function retrieves an array of vote names/options for a specific proposal. It retrieves theoptions
array of the proposal with the given ID and adds each option to an array calledmypropvotes
. The function then returns this array.getProposalVoteNumbers(uint proposal_idd)
: This function retrieves an array of vote numbers/totals for a specific proposal. It retrieves theoptions_num
array of the proposal with the given ID and adds each vote number to an array calledmypropvotenums
. The function then returns this array.getProposalPower(uint proposal_idd)
: This function retrieves the power of a specific proposal. It retrieves thepower
variable of the proposal with the given ID and returns it.getProposalType(uint proposal_idd)
: This function retrieves the type of a specific proposal. This function is not defined in the provided code.getProposalType(uint proposal_idd)
: This function takes a proposal ID as input and returns a string representing the type of the proposal. The string value is obtained from a mapping calledproposals
which maps a proposal ID to a struct containing information about the proposal. The function isview
type, meaning it doesn't modify the state of the contract and can be called without incurring any gas cost.dao_delegation_single_getback_amount_voter(address from, uint256 amount)
: This function allows a voter to retrieve a single token that was delegated to another address. Thefrom
parameter specifies the address from which the token will be retrieved andamount
specifies the number of tokens to be retrieved. The function checks that the number of tokens being retrieved is less than or equal to the number of debt tokens held by thefrom
address. This function can only be called externally.dao_delegation_single_getback_all_voter(address from)
: This function allows a voter to retrieve all tokens that were delegated to another address. Thefrom
parameter specifies the address from which all tokens will be retrieved. This function can only be called externally.dao_delagation_multiple_getback_all_voter()
: This function allows a voter to retrieve all tokens that were delegated to multiple addresses. This function can only be called externally. This function has no parameters.dao_clawback_single_voter(address from)
: This function allows the DAO to retrieve a single token that was delegated to another address by a voter. Thefrom
parameter specifies the address from which the token will be retrieved. The function checks that the caller has YK (Yield Keeper) privileges. This function can only be called externally.dao_clawback_all_voter()
: This function allows the DAO to retrieve all tokens that were delegated to another address by voters. The function checks that the caller has YK (Yield Keeper) privileges. This function can only be called externally.dao_delegation_single_getback_amount_yk(address from, uint256 amount
: This function allows the DAO to retrieve a single token that was delegated to another address by a YK. Thefrom
parameter specifies the address from which the token will be retrieved andamount
specifies the number of tokens to be retrieved. The function checks that the number of tokens being retrieved is less than or equal to the number of debt tokens held by thefrom
address. This function can only be called externally.dao_delegation_single_getback_all_yk(address from)
: This function allows the DAO to retrieve all tokens that were delegated to another address by a YK. Thefrom
parameter specifies the address from which all tokens will be retrieved. This function can only be called externally.dao_delagation_multiple_getback_all_yk()
: This function allows the DAO to retrieve all tokens that were delegated to multiple addresses by a YK. The function checks that the caller has YK (Yield Keeper) privileges. This function can only be called externally. This function has no parameters.dao_clawback_single_yk(address to)
: This function allows the DAO to retrieve a single token that was delegated to another address by a YK. Theto
parameter specifies the address to which the token will be clawed back. The function checks that the caller has YK.