use of io.nuls.db.model.Entry in project nuls by nuls-io.
the class UtxoResource method getAllUtxoByAddress.
private List<Coin> getAllUtxoByAddress(String address) {
List<Coin> coinList = new ArrayList<>();
byte[] addressBytes = AddressTool.getAddress(address);
List<Entry<byte[], byte[]>> coinBytesList = utxoLedgerUtxoStorageService.getAllUtxoEntryBytes();
Coin coin;
for (Entry<byte[], byte[]> coinEntryBytes : coinBytesList) {
coin = new Coin();
try {
coin.parse(coinEntryBytes.getValue(), 0);
} catch (NulsException e) {
Log.info("parse coin form db error");
continue;
}
// if (Arrays.equals(coin.(), addressBytes))
if (Arrays.equals(coin.getAddress(), addressBytes)) {
coin.setOwner(coinEntryBytes.getKey());
coinList.add(coin);
}
}
Collections.sort(coinList, CoinComparator.getInstance());
return coinList;
}
use of io.nuls.db.model.Entry in project nuls by nuls-io.
the class UtxoResource method getInfo.
@GET
@Path("/info")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "查询代币情况")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = TokenInfoDto.class) })
public RpcClientResult getInfo() throws NulsException {
long height = NulsContext.getInstance().getBestHeight();
List<Entry<byte[], byte[]>> coinBytesList = utxoLedgerUtxoStorageService.getAllUtxoEntryBytes();
double totalNuls = 0d;
double lockedNuls = 0d;
Map<String, Holder> map = new HashMap<>();
Coin coin = new Coin();
int index = 0;
for (Entry<byte[], byte[]> coinEntryBytes : coinBytesList) {
coin.parse(coinEntryBytes.getValue(), 0);
double value = coin.getNa().toDouble();
String address = AddressTool.getStringAddressByBytes(coin.getOwner());
Holder holder = map.get(address);
if (null == holder) {
holder = new Holder();
holder.setAddress(address);
map.put(address, holder);
}
holder.addTotal(value);
totalNuls = DoubleUtils.sum(totalNuls, value);
if (coin.getLockTime() == -1 || coin.getLockTime() > System.currentTimeMillis() || (coin.getLockTime() < 1531152000000L && coin.getLockTime() > height)) {
holder.addLocked(value);
lockedNuls = DoubleUtils.sum(lockedNuls, value);
}
System.out.println(index++);
}
Result<TokenInfoDto> result = Result.getSuccess();
TokenInfoDto info = new TokenInfoDto();
info.setTotalNuls(DoubleUtils.getRoundStr(totalNuls, 8, true));
info.setLockedNuls(DoubleUtils.getRoundStr(lockedNuls, 8, true));
List<Holder> holderList = new ArrayList<>(map.values());
Collections.sort(holderList);
List<HolderDto> dtoList = new ArrayList<>();
for (Holder holder : holderList) {
HolderDto dto = new HolderDto(holder);
dtoList.add(dto);
}
info.setAddressList(dtoList);
result.setData(info);
return result.toRpcClientResult();
}
use of io.nuls.db.model.Entry in project nuls by nuls-io.
the class ContractBalanceManager method getAllTokensByAccount.
public Result<List<ContractTokenInfo>> getAllTokensByAccount(String account) {
Map<String, ContractTokenInfo> tokensMap = contractTokenOfLocalAccount.get(account);
if (tokensMap == null || tokensMap.size() == 0) {
return Result.getSuccess().setData(new ArrayList<>());
}
List<ContractTokenInfo> resultList = new ArrayList<>();
Set<Map.Entry<String, ContractTokenInfo>> entries = tokensMap.entrySet();
String contractAddress;
ContractTokenInfo info;
for (Map.Entry<String, ContractTokenInfo> entry : entries) {
contractAddress = entry.getKey();
info = entry.getValue();
info.setContractAddress(contractAddress);
resultList.add(info);
}
return Result.getSuccess().setData(resultList);
}
use of io.nuls.db.model.Entry in project nuls by nuls-io.
the class ContractBalanceManager method initContractBalance.
/**
* 初始化缓存本地所有合约账户的余额信息
*/
public void initContractBalance() {
balanceMap = new ConcurrentHashMap<>();
List<Entry<byte[], byte[]>> rawList = contractUtxoStorageService.loadAllCoinList();
Coin coin;
String strAddress;
ContractBalance balance;
for (Entry<byte[], byte[]> coinEntry : rawList) {
coin = new Coin();
try {
coin.parse(coinEntry.getValue(), 0);
// strAddress = asString(coin.getOwner());
coin.setKey(asString(coinEntry.getKey()));
strAddress = asString(coin.getAddress());
} catch (NulsException e) {
Log.error("parse contract coin error form db", e);
continue;
}
balance = balanceMap.get(strAddress);
if (balance == null) {
balance = new ContractBalance();
balanceMap.put(strAddress, balance);
}
// 共识奖励的utxo
if (coin.getLockTime() != 0) {
balance.getConsensusRewardCoins().put(coin.getKey(), coin);
} else {
balance.addUsable(coin.getNa());
}
}
}
Aggregations