Skip to content

Commit

Permalink
Merge pull request iotaledger#366 from iotaledger/fix-balance
Browse files Browse the repository at this point in the history
fix get_balance and address_index with initial_address_index, add example
  • Loading branch information
bingyanglin authored Feb 26, 2021
2 parents fe108c0 + f8326ff commit 12fcb20
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 18 deletions.
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ path = "message_time.rs"
[[example]]
name = "send_all"
path = "send_all.rs"

[[example]]
name = "split_all"
path = "split_all.rs"
9 changes: 6 additions & 3 deletions examples/send_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use dotenv::dotenv;
use std::env;

/// In this example, we get the balance of the first account of the seed and send everything
// Todo: automatically detect amount of inputs and if > 127 create multiple transactions

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -21,16 +22,18 @@ async fn main() -> Result<()> {
// Insert your seed in the .env. Since the output amount cannot be zero. The seed must contain non-zero balance.
println!("This example uses dotenv, which is not safe for use in production.");
dotenv().ok();
let seed_1 = Seed::from_bytes(&hex::decode(env::var("NONSECURE_USE_OF_DEVELOPMENT_SEED_1").unwrap())?)?;
let seed_1 = Seed::from_bytes(&hex::decode(env::var("NONSECURE_USE_OF_DEVELOPMENT_SEED_2").unwrap())?)?;

let total_balance = iota.get_balance(&seed_1).finish().await?;
let total_balance = iota.get_balance(&seed_1).with_initial_address_index(0).finish().await?;
println!("total_balance {}", total_balance);
let message = iota
.message()
.with_seed(&seed_1)
.with_output(
&"atoi1qrk69lxuxljdgeqt7tucvtdfk3hrvrly7rzz65w57te6drf3expsj3gqrf9".into(),
&"atoi1qzt0nhsf38nh6rs4p6zs5knqp6psgha9wsv74uajqgjmwc75ugupx3y7x0r".into(),
total_balance,
)?
.with_initial_address_index(0)
.finish()
.await?;
println!(
Expand Down
70 changes: 70 additions & 0 deletions examples/split_all.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! cargo run --example split_all --release
use iota::{client::Result, Client, MessageId, Seed};
use std::time::Duration;
use tokio::time::sleep;
extern crate dotenv;
use dotenv::dotenv;
use std::env;

/// In this example, we get the balance of the first account of the seed and send everything splitted to the second seed
#[tokio::main]
async fn main() -> Result<()> {
let iota = Client::builder() // Crate a client instance builder
.with_node("http://api.lb-0.testnet.chrysalis2.com")?
.finish()
.await?;

// Insert your seed in the .env. Since the output amount cannot be zero. The seed must contain non-zero balance.
println!("This example uses dotenv, which is not safe for use in production.");
dotenv().ok();
let seed_1 = Seed::from_bytes(&hex::decode(env::var("NONSECURE_USE_OF_DEVELOPMENT_SEED_1").unwrap())?)?;
let seed_2 = Seed::from_bytes(&hex::decode(env::var("NONSECURE_USE_OF_DEVELOPMENT_SEED_2").unwrap())?)?;

let total_balance = iota.get_balance(&seed_1).finish().await?;
let mut available = total_balance;
println!("total_balance {}", total_balance);
let addresses_from_seed_2 = iota
.get_addresses(&seed_2)
.with_range(0..available as usize / 1_000_000)
.finish()
.await?;
let mut message_builder = iota.message().with_seed(&seed_1);
for i in 0..total_balance / 1_000_000 {
let mut amount = 1_000_000;
// Don't add more than we have or is allowed; 1 less here for remaining iotas
if available == 0 || i > 125 {
break;
}
available -= amount;
// Add last amount so we don't create dust
if available < amount {
amount += available;
available = 0;
}
message_builder = message_builder.with_output(&addresses_from_seed_2[i as usize], amount)?;
}

let message = message_builder.finish().await?;
println!(
"Transaction sent: https://explorer.iota.org/chrysalis/message/{}",
message.id().0
);
reattach_promote_until_confirmed(message.id().0, &iota).await;
Ok(())
}

async fn reattach_promote_until_confirmed(message_id: MessageId, iota: &Client) {
while let Ok(metadata) = iota.get_message().metadata(&message_id).await {
if let Some(state) = metadata.ledger_inclusion_state {
println!("Leder inclusion state: {:?}", state);
break;
} else if let Ok(msg_id) = iota.reattach(&message_id).await {
println!("Reattached or promoted {}", msg_id.0);
}
sleep(Duration::from_secs(5)).await;
}
}
6 changes: 3 additions & 3 deletions iota-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ name = "iota_client"
# bee-pow = { git = "https://github.com/iotaledger/bee.git", branch = "chrysalis-pt-2" }
# bee-common-derive = { git = "https://github.com/iotaledger/bee.git", branch = "dev" }
# bee-crypto = { git = "https://github.com/iotaledger/bee.git", branch = "dev" }
bee-rest-api = { git = "https://github.com/iotaledger/bee.git", rev = "8baba875c0f07e62f159f264e8edf1667e013433" }
bee-message = { git = "https://github.com/iotaledger/bee.git", rev = "8baba875c0f07e62f159f264e8edf1667e013433" }
bee-pow = { git = "https://github.com/iotaledger/bee.git", rev = "8baba875c0f07e62f159f264e8edf1667e013433" }
bee-rest-api = { git = "https://github.com/iotaledger/bee.git", rev = "ce3bd4e1b1ba423c8268e435dcc98c90c397379e" }
bee-message = { git = "https://github.com/iotaledger/bee.git", rev = "ce3bd4e1b1ba423c8268e435dcc98c90c397379e" }
bee-pow = { git = "https://github.com/iotaledger/bee.git", rev = "ce3bd4e1b1ba423c8268e435dcc98c90c397379e" }
bee-common = { git = "https://github.com/iotaledger/bee.git", branch = "dev" }
bee-common-derive = { git = "https://github.com/iotaledger/bee.git", rev = "c42171ff33c80cc2efb183e244dc79b7f58d9ac4" }
bee-crypto = { git = "https://github.com/iotaledger/bee.git", rev = "c42171ff33c80cc2efb183e244dc79b7f58d9ac4" }
Expand Down
19 changes: 10 additions & 9 deletions iota-client/src/api/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ impl<'a> GetBalanceBuilder<'a> {

// get account balance and check with value
let mut balance = 0;
// Count addresses with zero balances in a row
let mut found_zero_balance = 0;
loop {
let addresses = self
.client
Expand All @@ -51,23 +53,22 @@ impl<'a> GetBalanceBuilder<'a> {
.get_all()
.await?;

// TODO we assume all addresses are unspent and valid if balance > 0
let mut found_zero_balance = false;
for (address, _) in addresses {
let address_balance = self.client.get_address().balance(&address).await?;
match address_balance.balance {
0 => {
found_zero_balance = true;
break;
0 => found_zero_balance += 1,
_ => {
balance += address_balance.balance;
// reset
found_zero_balance = 0;
}
_ => balance += address_balance.balance,
}
}

match found_zero_balance {
true => break,
false => index += 20,
if found_zero_balance >= 20 {
break;
}
index += 20;
}

Ok(balance)
Expand Down
2 changes: 1 addition & 1 deletion iota-client/src/api/message_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl<'a> ClientMessageBuilder<'a> {
.get_all()
.await?;
// For each address, get the address outputs
let mut address_index = 0;
let mut address_index = index;
for (index, (address, internal)) in addresses.iter().enumerate() {
let address_outputs = self.client.get_address().outputs(&address).await?;
let mut outputs = vec![];
Expand Down
4 changes: 2 additions & 2 deletions iota-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ homepage = "https://iota.org"
name = "iota"

[dependencies]
bee-message = { git = "https://github.com/iotaledger/bee.git", rev = "8baba875c0f07e62f159f264e8edf1667e013433" }
bee-pow = { git = "https://github.com/iotaledger/bee.git", rev = "8baba875c0f07e62f159f264e8edf1667e013433" }
bee-message = { git = "https://github.com/iotaledger/bee.git", rev = "ce3bd4e1b1ba423c8268e435dcc98c90c397379e" }
bee-pow = { git = "https://github.com/iotaledger/bee.git", rev = "ce3bd4e1b1ba423c8268e435dcc98c90c397379e" }
bee-common = { git = "https://github.com/iotaledger/bee.git", rev = "c42171ff33c80cc2efb183e244dc79b7f58d9ac4" }
# bee-common = { git = "https://github.com/iotaledger/bee.git", branch = "dev" }
# bee-message = { git = "https://github.com/iotaledger/bee.git", branch = "chrysalis-pt-2" }
Expand Down

0 comments on commit 12fcb20

Please sign in to comment.