Skip to content

Commit

Permalink
Merge pull request #76 from logical-mechanism/75-review-min-ada-requi…
Browse files Browse the repository at this point in the history
…red-for-create

reduced min ada requirement
  • Loading branch information
quinn-logicalmechanism authored Jan 10, 2025
2 parents ce06c93 + 96072c2 commit 2ea7163
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
36 changes: 15 additions & 21 deletions seedelf-cli/src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ pub async fn run(args: LabelArgs, network_flag: bool) -> Result<(), String> {
// this is used to calculate the real fee
let mut draft_tx: StagingTransaction = StagingTransaction::new();

// we need about 2 ada for the utxo and another 2 for change so make it 5 as it should account for change
let lovelace_goal: u64 = 5_000_000;
// we need about 2 ada for the utxo
let tmp_fee: u64 = 205_000;
let lovelace_goal: u64 = transaction::seedelf_minimum_lovelace() + tmp_fee;

// there may be many collateral utxos, we just need one
let mut found_collateral: bool = false;
Expand All @@ -77,21 +78,18 @@ pub async fn run(args: LabelArgs, network_flag: bool) -> Result<(), String> {
for utxo in utxos {
// get the lovelace on this utxo
let lovelace: u64 = utxo.value.parse::<u64>().expect("Invalid Lovelace");
if lovelace == 5_000_000 {
// its probably a collateral utxo
if !found_collateral {
draft_tx = draft_tx.collateral_input(Input::new(
pallas_crypto::hash::Hash::new(
hex::decode(utxo.tx_hash.clone())
.expect("Invalid hex string")
.try_into()
.expect("Failed to convert to 32-byte array"),
),
utxo.tx_index,
));
// we just want a single collateral here
found_collateral = true;
}
if lovelace == 5_000_000 && !found_collateral {
draft_tx = draft_tx.collateral_input(Input::new(
pallas_crypto::hash::Hash::new(
hex::decode(utxo.tx_hash.clone())
.expect("Invalid hex string")
.try_into()
.expect("Failed to convert to 32-byte array"),
),
utxo.tx_index,
));
// we just want a single collateral here
found_collateral = true;
} else {
// its probably not a collateral utxo
all_utxos.push(utxo.clone());
Expand All @@ -102,7 +100,6 @@ pub async fn run(args: LabelArgs, network_flag: bool) -> Result<(), String> {
eprintln!("Failed to fetch UTxOs: {}", err);
}
}

// lovelace goal here should account for the estimated fee
let selected_utxos: Vec<UtxoResponse> = utxos::select(all_utxos, lovelace_goal, Assets::new());
for utxo in selected_utxos.clone() {
Expand All @@ -125,9 +122,6 @@ pub async fn run(args: LabelArgs, network_flag: bool) -> Result<(), String> {
return Err("Not Enough Lovelace".to_string());
}

// This is some semi legit fee to be used to estimate it
let tmp_fee: u64 = 200_000;

// this is going to be the datum on the seedelf
let sk: Scalar = setup::load_wallet();
let datum_vector: Vec<u8> = Register::create(sk).rerandomize().to_vec();
Expand Down
2 changes: 1 addition & 1 deletion seedelf-cli/src/commands/fund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub async fn run(args: FundArgs, network_flag: bool) -> Result<(), String> {

// we need about 2 ada for change so just add that to the amount
let lovelace: u64 = args.lovelace.unwrap_or(minimum_lovelace);
let lovelace_goal: u64 = 2_000_000 + lovelace;
let lovelace_goal: u64 = lovelace;

// utxos
let seedelf_utxo: UtxoResponse = utxos::find_seedelf_utxo(args.seedelf.clone(), network_flag)
Expand Down
2 changes: 1 addition & 1 deletion seedelf-cli/src/commands/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub async fn run(args: TransforArgs, network_flag: bool) -> Result<(), String> {
// the extra 2.5 ADA should account for the change and fee
let usuable_utxos = utxos::select(
usuable_utxos,
lovelace_goal + 2_500_000,
lovelace_goal,
selected_tokens.clone(),
);
let (total_lovelace_found, tokens) = utxos::assets_of(usuable_utxos.clone());
Expand Down
15 changes: 12 additions & 3 deletions seedelf-cli/src/utxos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,15 @@ pub async fn collect_address_utxos(address: &str, network_flag: bool) -> Vec<Utx

// lets assume that the lovelace here initially accounts for the estimated fee, like 1 ada or something
// use largest first algo but account for change
pub fn select(mut utxos: Vec<UtxoResponse>, lovelace: u64, tokens: Assets) -> Vec<UtxoResponse> {
pub fn select(utxos: Vec<UtxoResponse>, lovelace: u64, tokens: Assets) -> Vec<UtxoResponse> {
do_select(utxos, lovelace, tokens, lovelace)
}
pub fn do_select(
mut utxos: Vec<UtxoResponse>,
lovelace: u64,
tokens: Assets,
lovelace_goal: u64,
) -> Vec<UtxoResponse> {
let mut selected_utxos: Vec<UtxoResponse> = Vec::new();

let mut current_lovelace_sum: u64 = 0;
Expand Down Expand Up @@ -272,16 +280,17 @@ pub fn select(mut utxos: Vec<UtxoResponse>, lovelace: u64, tokens: Assets) -> Ve
1
};
// we need lovelace for the goal and the change here
if current_lovelace_sum - multiplier * minimum >= lovelace {
if current_lovelace_sum - multiplier * minimum >= lovelace_goal {
// it is!
found_enough = true;
break;
} else {
// its not, try again but increase the lovelace by the minimum we would need
select(
return do_select(
utxos.clone(),
lovelace + multiplier * minimum,
tokens.clone(),
lovelace_goal,
);
}
}
Expand Down

0 comments on commit 2ea7163

Please # to comment.