use of io.nuls.core.chain.entity.Na in project nuls by nuls-io.
the class UtxoData method getTotalNa.
@Override
public Na getTotalNa() {
if (null == this.totalNa) {
Set<String> addressSet = new HashSet<>();
if (null != this.getInputs()) {
for (UtxoInput input : this.getInputs()) {
addressSet.add(input.getFrom().getAddress());
}
}
Na totalNa = Na.ZERO;
if (null != this.getOutputs()) {
for (int i = 0; i < this.getOutputs().size(); i++) {
UtxoOutput output = this.getOutputs().get(i);
if (addressSet.contains(output.getAddress())) {
if (i == 0 && output.isLocked()) {
totalNa = totalNa.add(Na.valueOf(output.getValue()));
break;
}
} else {
totalNa = totalNa.add(Na.valueOf(output.getValue()));
}
}
}
this.setTotalNa(totalNa);
}
return this.totalNa;
}
use of io.nuls.core.chain.entity.Na in project nuls by nuls-io.
the class UtxoCoinManager method getAccountsUnSpend.
public List<UtxoOutput> getAccountsUnSpend(List<String> addressList, Na value) {
lock.lock();
List<UtxoOutput> unSpends = new ArrayList<>();
try {
// check use-able is enough , find unSpend utxo
Na amount = Na.ZERO;
boolean enough = false;
for (String address : addressList) {
UtxoBalance balance = (UtxoBalance) ledgerCacheService.getBalance(address);
if (balance == null || balance.getUnSpends().isEmpty()) {
continue;
}
for (int i = 0; i < balance.getUnSpends().size(); i++) {
UtxoOutput output = balance.getUnSpends().get(i);
if (!output.isUsable()) {
continue;
}
unSpends.add(output);
amount = amount.add(Na.valueOf(output.getValue()));
if (amount.isGreaterThan(value)) {
enough = true;
break;
}
}
if (enough) {
break;
}
}
if (!enough) {
unSpends = new ArrayList<>();
}
} catch (Exception e) {
Log.error(e);
unSpends = new ArrayList<>();
} finally {
lock.unlock();
}
return unSpends;
}
Aggregations