-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add simple proposal voting example #12
Conversation
contracts/voting/src/lib.rs
Outdated
pub fn create_proposal(env: Env, id: u64) -> Result<(), Error> { | ||
let voting_period_secs = 3600; // one hour | ||
let target_approval_rate_bps = 50_00; // At least 50% of the votes to be approved. | ||
let total_voters = 1000; // Up to 1000 participants. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this parameters could be easily configured in the Wizard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn this happen in init?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make them init parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can move them to the init. Lets just take into account if we move them there, we need to store them in the ledger for later retrieval in the create_proposal
function. Like we are doing here.
I have doubts regarding this, because adding/retrieving elements from the storage, has consequences in fees. See this docs. So maybe they are just good as hardcoded values ? I really dont know.
I will proceed on moving them to init in the meanwhile 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. Maybe add a comment in the code for future consideration of making them hardcoded if the fee is too high :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed at e3cf2f7
.set(&DataKey::Proposals, &Map::<u64, Proposal>::new(&env)); | ||
} | ||
|
||
pub fn create_proposal(env: Env, id: u64) -> Result<(), Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently its an ID .. but maybe adding a hash field would be better. Let me know your thoughts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn this happen in init?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You said this is not doable in init?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SOrry missed this reply, i replied here : #12 (comment)
fa0daa0
to
675b8b3
Compare
); | ||
} | ||
|
||
fn setup_test<'a>() -> (Env, ProposalVotingContractClient<'a>, Address) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could make this kind of helpers more generic in the future, whenever we see this patterns repeating.
assert_eq!(Err(Error::VotingClosed), result) | ||
} | ||
|
||
fn advance_ledger_time_in(time: u64, env: &mut Env) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is another test helper i found useful.
7e3eb6f
to
5b38e9d
Compare
This will need further study due to fees: https://soroban.stellar.org/docs/fundamentals-and-concepts/fees-and-metering#resource-fee
5b38e9d
to
db17fc5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
.set(&DataKey::Proposals, &Map::<u64, Proposal>::new(&env)); | ||
} | ||
|
||
pub fn create_proposal(env: Env, id: u64) -> Result<(), Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn this happen in init?
contracts/voting/src/lib.rs
Outdated
#[contractimpl] | ||
impl ProposalVotingContract { | ||
pub fn init(env: Env, admin: Address) { | ||
env.storage().set(&DataKey::Admin, &admin); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to ensure you are logged in as admin
env.storage().set(&DataKey::Admin, &admin); | |
admin.require_auth(); | |
env.storage().set(&DataKey::Admin, &admin); |
contracts/voting/src/lib.rs
Outdated
pub fn create_proposal(env: Env, id: u64) -> Result<(), Error> { | ||
let voting_period_secs = 3600; // one hour | ||
let target_approval_rate_bps = 50_00; // At least 50% of the votes to be approved. | ||
let total_voters = 1000; // Up to 1000 participants. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn this happen in init?
7bbab66
to
a170e98
Compare
@geofmureithi just added rust docs and checked add. See last 2 commits. Requesting re-review ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is a specific reason not to run creation in init, please highlight it. Otherwise, LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is a specific reason not to run creation in init, please highlight it. Otherwise, LGTM
Thanks for review @geofmureithi . This contract has the capability of tracking multiple proposals during its lifecycle. The parameters we store in the If an user, wants different parameters for an specific proposal during the lifecycle of this contract, it can invoke directly the |
But once you deploy you get a contact id, shouldn't we be using that? |
With the current example, we can have multiple proposals in the same smart contract. So for voting we would need the contract id + the proposal id. An example of how to invoke the contract can be checked here: #5 (comment) |
I have not seen any other examples approach it that way. Especially because everything else is already hardcoded hence it wont create a new proposal but rather a clone of the same proposal just with different id. |
I was probably influenced by this . Although is true eth is a different environment. There are other examples, like the multi swap or auth . They use multiple data structures to represent different entities. Anyway, if you think there is something wrong with the current design, let me know and we can change it. |
I think the problem you ate trying to solve is already solved by wasm. I see how you can see it that way but instead of your approach we would rather have a single proposal per contract. I think there is no objectively better one because it matters in use case. |
This PR adds a basic voting smart contract with the following features:
This is a simplified version of https://github.com/eigerco/smart-proposals