use of io.nuls.utxo.accounts.rpc.dto.AccountBalanceDto in project nuls by nuls-io.
the class UtxoAccountsResource method get.
@GET
@Path("/{address}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("[查询] 查询账户信息")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = RpcClientResult.class) })
public RpcClientResult get(@ApiParam(name = "address", value = "账户地址", required = true) @PathParam("address") String address) {
if (!AddressTool.validAddress(address)) {
return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
}
try {
Result<UtxoAccountsBalancePo> utxoAccountsBalance = utxoAccountsStorageService.getUtxoAccountsBalanceByAddress(AddressTool.getAddress(address));
long synBlockHeight = utxoAccountsStorageService.getHadSynBlockHeight();
if (null == utxoAccountsBalance || null == utxoAccountsBalance.getData()) {
return Result.getFailed(UtxoAccountsErrorCode.DATA_NOT_FOUND).toRpcClientResult();
}
UtxoAccountsBalancePo dbAccountsBalance = utxoAccountsBalance.getData();
AccountBalanceDto accountBalance = new AccountBalanceDto();
accountBalance.setLockedTimeList(new ArrayList<>());
accountBalance.setLockedHeightList(new ArrayList<>());
accountBalance.setAddress(address);
long totalNa = dbAccountsBalance.getOutputBalance() - (dbAccountsBalance.getInputBalance());
totalNa += dbAccountsBalance.getContractToBalance();
totalNa -= dbAccountsBalance.getContractFromBalance();
accountBalance.setNuls(new BigDecimal(totalNa).toPlainString());
long timeLockedNa = 0;
long heightLockedNa = 0;
long permanentLockedNa = dbAccountsBalance.getLockedPermanentBalance() - (dbAccountsBalance.getUnLockedPermanentBalance());
long lockedNa = permanentLockedNa;
List<LockedBalance> timeLockedBalance = dbAccountsBalance.getLockedTimeList();
long currentTime = TimeService.currentTimeMillis();
for (LockedBalance balance : timeLockedBalance) {
if (balance.getLockedTime() > currentTime) {
lockedNa += balance.getLockedBalance();
timeLockedNa += balance.getLockedBalance();
accountBalance.getLockedTimeList().add(balance);
} else {
break;
}
}
List<LockedBalance> heightLockedBalance = dbAccountsBalance.getLockedHeightList();
for (LockedBalance balance : heightLockedBalance) {
if (balance.getLockedTime() > synBlockHeight) {
lockedNa += balance.getLockedBalance();
heightLockedNa += balance.getLockedBalance();
accountBalance.getLockedHeightList().add(balance);
} else {
break;
}
}
accountBalance.setPermanentLocked(new BigDecimal(permanentLockedNa).toPlainString());
accountBalance.setLocked(new BigDecimal(lockedNa).toPlainString());
accountBalance.setTimeLocked(new BigDecimal(timeLockedNa).toPlainString());
accountBalance.setHeightLocked(new BigDecimal(heightLockedNa).toPlainString());
accountBalance.setSynBlockHeight(String.valueOf(synBlockHeight));
long netHeight = NulsContext.getInstance().getNetBestBlockHeight();
accountBalance.setNetBlockHeight(String.valueOf(netHeight));
accountBalance.setContractIn(String.valueOf(dbAccountsBalance.getContractToBalance()));
accountBalance.setContractOut(String.valueOf(dbAccountsBalance.getContractFromBalance()));
return Result.getSuccess().setData(accountBalance).toRpcClientResult();
} catch (NulsException e) {
Log.error(e);
}
return Result.getFailed(UtxoAccountsErrorCode.SYS_UNKOWN_EXCEPTION).toRpcClientResult();
}
use of io.nuls.utxo.accounts.rpc.dto.AccountBalanceDto in project nuls by nuls-io.
the class UtxoAccountsResource method getBalance.
@GET
@Path("/balance/{address}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("[查询] 查询账户余额信息")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = RpcClientResult.class) })
public RpcClientResult getBalance(@ApiParam(name = "address", value = "账户地址", required = true) @PathParam("address") String address) {
if (!AddressTool.validAddress(address)) {
return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
}
try {
Result<UtxoAccountsBalancePo> utxoAccountsBalance = utxoAccountsStorageService.getUtxoAccountsBalanceByAddress(AddressTool.getAddress(address));
long synBlockHeight = utxoAccountsStorageService.getHadSynBlockHeight();
if (null == utxoAccountsBalance || null == utxoAccountsBalance.getData()) {
return Result.getFailed(UtxoAccountsErrorCode.DATA_NOT_FOUND).toRpcClientResult();
}
UtxoAccountsBalancePo dbAccountsBalance = utxoAccountsBalance.getData();
AccountBalanceDto accountBalance = new AccountBalanceDto();
accountBalance.setAddress(address);
long totalNa = dbAccountsBalance.getOutputBalance() - (dbAccountsBalance.getInputBalance());
totalNa += dbAccountsBalance.getContractToBalance();
totalNa -= dbAccountsBalance.getContractFromBalance();
accountBalance.setNuls(new BigDecimal(totalNa).toPlainString());
long permanentLockedNa = dbAccountsBalance.getLockedPermanentBalance() - (dbAccountsBalance.getUnLockedPermanentBalance());
long lockedNa = permanentLockedNa;
List<LockedBalance> timeLockedBalance = dbAccountsBalance.getLockedTimeList();
long currentTime = TimeService.currentTimeMillis();
for (LockedBalance balance : timeLockedBalance) {
if (balance.getLockedTime() > currentTime) {
lockedNa += balance.getLockedBalance();
} else {
break;
}
}
List<LockedBalance> heightLockedBalance = dbAccountsBalance.getLockedHeightList();
for (LockedBalance balance : heightLockedBalance) {
if (balance.getLockedTime() > synBlockHeight) {
lockedNa += balance.getLockedBalance();
} else {
break;
}
}
accountBalance.setLocked(new BigDecimal(lockedNa).toPlainString());
accountBalance.setSynBlockHeight(String.valueOf(synBlockHeight));
long netHeight = NulsContext.getInstance().getNetBestBlockHeight();
accountBalance.setNetBlockHeight(String.valueOf(netHeight));
return Result.getSuccess().setData(accountBalance).toRpcClientResult();
} catch (NulsException e) {
Log.error(e);
}
return Result.getFailed(UtxoAccountsErrorCode.SYS_UNKOWN_EXCEPTION).toRpcClientResult();
}
Aggregations