db/heromodels/examples/governance_rhai/governance.rhai
2025-05-17 16:03:00 +03:00

86 lines
3.8 KiB
Plaintext

// Get the database instance
let db = get_db();
// Create a new proposal with auto-generated ID (pass 0 for auto-generated ID)
let proposal = create_proposal(0, "user_creator_123", "Community Fund Allocation for Q3",
"Proposal to allocate funds for community projects in the third quarter.");
print("Created Proposal: '" + get_title(proposal) + "' (ID: " + get_id(proposal) + ")");
print("Status: " + get_status(proposal) + ", Vote Status: " + get_vote_status(proposal));
// Add vote options
let proposal_with_options = add_option_to_proposal(proposal, 1, "Approve Allocation");
proposal_with_options = add_option_to_proposal(proposal_with_options, 2, "Reject Allocation");
proposal_with_options = add_option_to_proposal(proposal_with_options, 3, "Abstain");
print("\nAdded Vote Options:");
let option_count = get_option_count(proposal_with_options);
for i in range(0, option_count) {
let option = get_option_at(proposal_with_options, i);
print("- Option ID: " + i + ", Text: '" + get_option_text(option) + "', Votes: " + get_option_votes(option));
}
// Save the proposal to the database
save_proposal(db, proposal_with_options);
print("\nProposal saved to database");
// Simulate casting votes
print("\nSimulating Votes...");
// User 1 votes for 'Approve Allocation' with 100 shares (with explicit ballot ID)
let proposal_with_votes = cast_vote_on_proposal(proposal_with_options, 101, 1, 1, 100);
// User 2 votes for 'Reject Allocation' with 50 shares (with explicit ballot ID)
proposal_with_votes = cast_vote_on_proposal(proposal_with_votes, 102, 2, 2, 50);
// User 3 votes for 'Approve Allocation' with 75 shares (with auto-generated ballot ID)
proposal_with_votes = cast_vote_on_proposal(proposal_with_votes, 0, 3, 1, 75);
// User 4 abstains with 20 shares (with auto-generated ballot ID)
proposal_with_votes = cast_vote_on_proposal(proposal_with_votes, 0, 4, 3, 20);
print("\nVote Counts After Simulation:");
option_count = get_option_count(proposal_with_votes);
for i in range(0, option_count) {
let option = get_option_at(proposal_with_votes, i);
print("- Option ID: " + i + ", Text: '" + get_option_text(option) + "', Votes: " + get_option_votes(option));
}
print("\nBallots Cast:");
let ballot_count = get_ballot_count(proposal_with_votes);
for i in range(0, ballot_count) {
let ballot = get_ballot_at(proposal_with_votes, i);
print("- Ballot ID: " + i + ", User ID: " + get_ballot_user_id(ballot) +
", Option ID: " + get_ballot_option_id(ballot) + ", Shares: " + get_ballot_shares(ballot));
}
// Change proposal status
let active_proposal = change_proposal_status(proposal_with_votes, "Active");
print("\nChanged Proposal Status to: " + get_status(active_proposal));
// Simulate closing the vote
let closed_proposal = change_vote_event_status(active_proposal, "Closed");
print("Changed Vote Event Status to: " + get_vote_status(closed_proposal));
// Final proposal state
print("\nFinal Proposal State:");
print("Title: '" + get_title(closed_proposal) + "'");
print("Status: " + get_status(closed_proposal));
print("Vote Status: " + get_vote_status(closed_proposal));
print("Options:");
option_count = get_option_count(closed_proposal);
for i in range(0, option_count) {
let option = get_option_at(closed_proposal, i);
print(" - " + i + ": " + get_option_text(option) + " (Votes: " + get_option_votes(option) + ")");
}
print("Total Ballots: " + get_ballot_count(closed_proposal));
// Get all proposals from the database
let all_proposals = get_all_proposals(db);
print("\nTotal Proposals in Database: " + all_proposals.len());
for proposal in all_proposals {
print("Proposal ID: " + get_id(proposal) + ", Title: '" + get_title(proposal) + "'");
}
// Delete a proposal
delete_proposal_by_id(db, 1);
print("\nDeleted proposal with ID 1");
print("\nGovernance Proposal Example Finished.");