Skip to content

Commit

Permalink
Participation account methods (#1594)
Browse files Browse the repository at this point in the history
* Start implementing participation account methods

* Add account methods to message interface

* Remove storage feature

* Put VotingError under participation feature

* Fix comment

* Fix response debug

* Avoid cloning all unspent outputs

* Rework remove_ended_participation_events

* Rework voting power

* get_voting_output returns Option

* Voting power nits

* Example nits

* Voting nits

* Last nits

Co-authored-by: Thibault Martinez <thibault@iota.org>
  • Loading branch information
Thoralf-M and thibault-martinez authored Nov 24, 2022
1 parent 6b2068c commit 5dc7bf9
Show file tree
Hide file tree
Showing 15 changed files with 873 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ backtrace = { version = "0.3.66", default-features = false, features = [ "std" ]
futures = { version = "0.3.25", default-features = false }
getset = { version = "0.1.2", default-features = false }
# iota-client = { version = "2.0.1-rc.3", default-features = false, features = [ "message_interface", "tls" ] }
iota-client = { git = "https://github.com/iotaledger/iota.rs", rev = "05a4b32dfaeb4471031c7f0d7d6cf60c8688130c", default-features = false, features = [ "message_interface", "tls" ] }
iota-client = { git = "https://github.com/iotaledger/iota.rs", rev = "9fc711b380585fc5001f31e380ea322644d3d24f", default-features = false, features = [ "message_interface", "tls" ] }
iota-crypto = { version = "0.15.3", default-features = false, features = [ "std", "chacha", "blake2b", "ed25519", "random", "slip10", "bip39", "bip39-en" ] }
log = { version = "0.4.17", default-features = false }
packable = { version = "0.7.0", default-features = false, features = [ "serde", "primitive-types" ] }
Expand Down
157 changes: 131 additions & 26 deletions examples/participation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use std::{env, str::FromStr};

use dotenv::dotenv;
use iota_client::{node_api::participation::types::EventId, node_manager::node::Node, Url};
use iota_client::{node_api::participation::types::EventId, node_manager::node::Node, request_funds_from_faucet, Url};
use iota_wallet::{
account_manager::AccountManager,
iota_client::constants::SHIMMER_COIN_TYPE,
Expand All @@ -16,7 +16,7 @@ use iota_wallet::{

#[tokio::main]
async fn main() -> Result<()> {
// Generates a wallet.log file with logs for debugging
// Generates a wallet.log file with logs for debugging.
let logger_output_config = fern_logger::LoggerOutputConfigBuilder::new()
.name("wallet.log")
.target_exclusions(&["h2", "hyper", "rustls"])
Expand All @@ -26,7 +26,7 @@ async fn main() -> Result<()> {
.finish();
fern_logger::logger_init(config).unwrap();

// This example uses dotenv, which is not safe for use in production
// This example uses dotenv, which is not safe for use in production.
dotenv().ok();

let client_options = ClientOptions::new()
Expand All @@ -43,7 +43,7 @@ async fn main() -> Result<()> {
.finish()
.await?;

let event_id = EventId::from_str("0x0344c97dc9cddc47f880fc1934e361636bf83029268f17faaac97c7be3865f7f")?;
let event_id = EventId::from_str("0x80f57f6368933b61af9b3d8e1b152cf5d23bf4537f6362778b0a7302a7000d48")?;
let event_nodes = vec![Node {
url: Url::parse("http://localhost:14265").map_err(iota_client::Error::UrlError)?,
auth: None,
Expand All @@ -56,7 +56,7 @@ async fn main() -> Result<()> {

println!("registered events: {registered_participation_events:?}");

// Update nodes
// Update nodes.
manager
.register_participation_event(
event_id,
Expand All @@ -78,27 +78,132 @@ async fn main() -> Result<()> {
let event = manager.get_participation_event(event_id).await;
println!("event: {event:?}");

manager.deregister_participation_event(event_id).await?;

let registered_participation_events = manager.get_participation_events().await?;

println!("registered events: {registered_participation_events:?}");

// TODO: add participation account methods

// // Get account or create a new one
// let account_alias = "participation";
// let account = match manager.get_account(account_alias).await {
// Ok(account) => account,
// _ => {
// // first we'll create an example account and store it
// manager
// .create_account()
// .with_alias(account_alias.to_string())
// .finish()
// .await?
// }
// };
let event_status = manager.get_participation_event_status(&event_id).await?;
println!("event status: {event_status:?}");

// manager.deregister_participation_event(event_id).await?;
// let registered_participation_events = manager.get_participation_events().await?;
// println!("registered events: {registered_participation_events:?}");

// Get account or create a new one.
let account_alias = "participation";
let account = match manager.get_account(account_alias).await {
Ok(account) => account,
_ => {
// First we'll create an example account and store it.
manager
.create_account()
.with_alias(account_alias.to_string())
.finish()
.await?
}
};

let address = account.addresses().await?;
let faucet_response =
request_funds_from_faucet(&env::var("FAUCET_URL").unwrap(), &address[0].address().to_bech32()).await?;
println!("{}", faucet_response);

account.sync(None).await?;

////////////////////////////////
//// create voting output or increase voting power
//// ////////////////////////////

let transaction = account.increase_voting_power(1000001).await?;
println!(
"Increase voting power Transaction: {} Block sent: {}/api/core/v2/blocks/{}",
transaction.transaction_id,
&env::var("NODE_URL").unwrap(),
transaction.block_id.expect("no block created yet")
);

account
.retry_until_included(&transaction.block_id.expect("no block created yet"), None, None)
.await?;
account.sync(None).await?;

let voting_output = account.get_voting_output().await?.unwrap();
println!("Voting output: {:?}", voting_output.output);

////////////////////////////////
//// decrease voting power
//// ////////////////////////////

// let transaction = account.decrease_voting_power(1).await?;
// println!(
// "Decrease voting power Transaction: {} Block sent: {}/api/core/v2/blocks/{}",
// transaction.transaction_id,
// &env::var("NODE_URL").unwrap(),
// transaction.block_id.expect("no block created yet")
// );

// account
// .retry_until_included(&transaction.block_id.expect("no block created yet"), None, None)
// .await?;
// account.sync(None).await?;

////////////////////////////////
//// vote
//// ////////////////////////////

let transaction = account.vote(event_id, vec![0]).await?;
println!(
"Vote Transaction: {} Block sent: {}/api/core/v2/blocks/{}",
transaction.transaction_id,
&env::var("NODE_URL").unwrap(),
transaction.block_id.expect("no block created yet")
);
account
.retry_until_included(&transaction.block_id.expect("no block created yet"), None, None)
.await?;
account.sync(None).await?;

////////////////////////////////
//// get voting overview
//// ////////////////////////////

let overview = account.get_participation_overview().await?;
println!("overview: {overview:?}");

////////////////////////////////
//// stop vote
//// ////////////////////////////

let transaction = account.stop_participating(event_id).await?;
println!(
"Vote Transaction: {} Block sent: {}/api/core/v2/blocks/{}",
transaction.transaction_id,
&env::var("NODE_URL").unwrap(),
transaction.block_id.expect("no block created yet")
);
account
.retry_until_included(&transaction.block_id.expect("no block created yet"), None, None)
.await?;
account.sync(None).await?;

////////////////////////////////
//// destroy voting output
//// ////////////////////////////

// let voting_output = account.get_voting_output().await?;
// println!("Voting output: {:?}", voting_output.output);

// // Decrease full amount, there should be no voting output afterwards
// let transaction = account.decrease_voting_power(voting_output.output.amount()).await?;
// println!(
// "Transaction: {} Block sent: {}/api/core/v2/blocks/{}",
// transaction.transaction_id,
// &env::var("NODE_URL").unwrap(),
// transaction.block_id.expect("no block created yet")
// );

// account
// .retry_until_included(&transaction.block_id.expect("no block created yet"), None, None)
// .await?;
// account.sync(None).await?;

// assert!(account.get_voting_output().await.is_err());

Ok(())
}
3 changes: 3 additions & 0 deletions src/account/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub(crate) mod output_claiming;
pub(crate) mod output_consolidation;
/// The module to find additional addresses with unspent outputs
pub(crate) mod output_finder;
/// The module for participation
#[cfg(feature = "participation")]
pub(crate) mod participation;
/// The module for synchronization of an account
pub(crate) mod syncing;
/// The module for transactions
Expand Down
Loading

0 comments on commit 5dc7bf9

Please sign in to comment.