use of io.nuls.contract.dto.ContractTokenInfo in project nuls by nuls-io.
the class ContractServiceImpl method getAllTokensByAccount.
@Override
public Result<List<ContractTokenInfo>> getAllTokensByAccount(String address) {
try {
if (StringUtils.isBlank(address)) {
return Result.getFailed(ContractErrorCode.NULL_PARAMETER);
}
if (!AddressTool.validAddress(address)) {
return Result.getFailed(AccountErrorCode.ADDRESS_ERROR);
}
Result<List<ContractTokenInfo>> tokenListResult = contractBalanceManager.getAllTokensByAccount(address);
List<ContractTokenInfo> list = tokenListResult.getData();
if (list != null && list.size() > 0) {
byte[] prevStateRoot = ContractUtil.getStateRoot(NulsContext.getInstance().getBestBlock().getHeader());
ProgramExecutor track = programExecutor.begin(prevStateRoot);
for (ContractTokenInfo tokenInfo : list) {
tokenInfo.setStatus(track.status(AddressTool.getAddress(tokenInfo.getContractAddress())).ordinal());
}
}
return tokenListResult;
} catch (Exception e) {
Log.error("initial all tokens of the account error.", e);
return Result.getFailed(ContractErrorCode.SYS_UNKOWN_EXCEPTION);
}
}
use of io.nuls.contract.dto.ContractTokenInfo in project nuls by nuls-io.
the class ContractBalanceManager method initialContractToken.
public void initialContractToken(String account, String contract) {
tokenLock.lock();
try {
Result<ContractTokenInfo> result = contractService.getContractTokenViaVm(account, contract);
if (result.isFailed()) {
return;
}
ContractTokenInfo tokenInfo = result.getData();
BigInteger amount = tokenInfo.getAmount();
if (amount == null || amount.equals(BigInteger.ZERO)) {
return;
}
Map<String, ContractTokenInfo> tokens = contractTokenOfLocalAccount.get(account);
if (tokens == null) {
tokens = new HashMap<>();
}
tokens.put(contract, tokenInfo);
contractTokenOfLocalAccount.put(account, tokens);
} finally {
tokenLock.unlock();
}
}
use of io.nuls.contract.dto.ContractTokenInfo in project nuls by nuls-io.
the class ContractServiceImpl method getContractTokenViaVm.
@Override
public Result<ContractTokenInfo> getContractTokenViaVm(String address, String contractAddress) {
try {
if (StringUtils.isBlank(contractAddress) || StringUtils.isBlank(address)) {
return Result.getFailed(ContractErrorCode.NULL_PARAMETER);
}
if (!AddressTool.validAddress(contractAddress) || !AddressTool.validAddress(address)) {
return Result.getFailed(AccountErrorCode.ADDRESS_ERROR);
}
byte[] contractAddressBytes = AddressTool.getAddress(contractAddress);
Result<ContractAddressInfoPo> contractAddressInfoResult = contractAddressStorageService.getContractAddressInfo(contractAddressBytes);
ContractAddressInfoPo po = contractAddressInfoResult.getData();
if (po == null) {
return Result.getFailed(ContractErrorCode.CONTRACT_ADDRESS_NOT_EXIST);
}
if (!po.isNrc20()) {
return Result.getFailed(ContractErrorCode.CONTRACT_NOT_NRC20);
}
ProgramResult programResult = vmHelper.invokeViewMethod(contractAddressBytes, "balanceOf", null, address);
Result<ContractTokenInfo> result;
if (!programResult.isSuccess()) {
result = Result.getFailed(ContractErrorCode.DATA_ERROR);
result.setMsg(ContractUtil.simplifyErrorMsg(programResult.getErrorMessage()));
} else {
result = Result.getSuccess();
ContractTokenInfo tokenInfo = new ContractTokenInfo(contractAddress, po.getNrc20TokenName(), po.getDecimals(), new BigInteger(programResult.getResult()), po.getNrc20TokenSymbol(), po.getBlockHeight());
byte[] prevStateRoot = ContractUtil.getStateRoot(NulsContext.getInstance().getBestBlock().getHeader());
ProgramExecutor track = programExecutor.begin(prevStateRoot);
tokenInfo.setStatus(track.status(AddressTool.getAddress(tokenInfo.getContractAddress())).ordinal());
result.setData(tokenInfo);
}
return result;
} catch (Exception e) {
Log.error("get contract token via VM error.", e);
return Result.getFailed(ContractErrorCode.SYS_UNKOWN_EXCEPTION);
}
}
use of io.nuls.contract.dto.ContractTokenInfo in project nuls by nuls-io.
the class ContractResource method getTokenList.
@GET
@Path("/token/list/{address}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "获取NRC20合约的资产列表")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = ContractTokenInfoDto.class) })
public RpcClientResult getTokenList(@ApiParam(name = "address", value = "钱包账户地址", required = true) @PathParam("address") String address, @ApiParam(name = "pageNumber", value = "页码", required = true) @QueryParam("pageNumber") Integer pageNumber, @ApiParam(name = "pageSize", value = "每页条数", required = false) @QueryParam("pageSize") Integer pageSize) {
try {
if (null == pageNumber || pageNumber == 0) {
pageNumber = 1;
}
if (null == pageSize || pageSize == 0) {
pageSize = 10;
}
if (pageNumber < 0 || pageSize < 0 || pageSize > 100) {
return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult();
}
if (StringUtils.isBlank(address)) {
return Result.getFailed(LedgerErrorCode.NULL_PARAMETER).toRpcClientResult();
}
Result<Account> accountResult = accountService.getAccount(address);
if (accountResult.isFailed()) {
return accountResult.toRpcClientResult();
}
Result<List<ContractTokenInfo>> tokenListResult = contractBalanceManager.getAllTokensByAccount(address);
if (tokenListResult.isFailed()) {
return tokenListResult.toRpcClientResult();
}
List<ContractTokenInfo> tokenInfoList = tokenListResult.getData();
Result result = Result.getSuccess();
List<ContractTokenInfoDto> tokenInfoDtoList = new ArrayList<>();
Page<ContractTokenInfoDto> page = new Page<>(pageNumber, pageSize, tokenInfoList.size());
int start = pageNumber * pageSize - pageSize;
if (start >= page.getTotal()) {
result.setData(page);
return result.toRpcClientResult();
}
int end = start + pageSize;
if (end > page.getTotal()) {
end = (int) page.getTotal();
}
if (tokenInfoList.size() > 0) {
for (int i = start; i < end; i++) {
ContractTokenInfo info = tokenInfoList.get(i);
tokenInfoDtoList.add(new ContractTokenInfoDto(info));
}
}
if (tokenInfoDtoList != null && tokenInfoDtoList.size() > 0) {
byte[] prevStateRoot = ContractUtil.getStateRoot(NulsContext.getInstance().getBestBlock().getHeader());
ProgramExecutor track = programExecutor.begin(prevStateRoot);
for (ContractTokenInfoDto tokenInfo : tokenInfoDtoList) {
tokenInfo.setStatus(track.status(AddressTool.getAddress(tokenInfo.getContractAddress())).ordinal());
}
}
page.setList(tokenInfoDtoList);
result.setSuccess(true);
result.setData(page);
return result.toRpcClientResult();
} catch (Exception e) {
Log.error(e);
Result result = Result.getFailed(LedgerErrorCode.SYS_UNKOWN_EXCEPTION);
return result.toRpcClientResult();
}
}
use of io.nuls.contract.dto.ContractTokenInfo in project nuls by nuls-io.
the class ContractResource method getAccountTokenBalance.
@GET
@Path("/balance/token/{contractAddress}/{address}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "获取账户地址的指定token余额")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success") })
public RpcClientResult getAccountTokenBalance(@ApiParam(name = "contractAddress", value = "合约地址", required = true) @PathParam("contractAddress") String contractAddress, @ApiParam(name = "address", value = "账户地址", required = true) @PathParam("address") String address) {
Result<ContractTokenInfo> tokenInfoResult = contractService.getContractTokenViaVm(address, contractAddress);
if (tokenInfoResult.isFailed()) {
return tokenInfoResult.toRpcClientResult();
}
ContractTokenInfo data = tokenInfoResult.getData();
ContractTokenInfoDto dto = null;
if (data != null) {
dto = new ContractTokenInfoDto(data);
dto.setStatus(data.getStatus());
}
return Result.getSuccess().setData(dto).toRpcClientResult();
}
Aggregations