Search in sources :

Example 6 with ContractTokenInfo

use of io.nuls.contract.dto.ContractTokenInfo in project nuls by nuls-io.

the class AccountResource method getAssets.

@GET
@Path("/assets/{address}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "[资产] 查询账户资产 [3.3.8]", notes = "result.data: List<AssetDto>")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = RpcClientResult.class) })
public RpcClientResult getAssets(@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 (!AddressTool.validAddress(address)) {
            return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
        }
        Address addr = new Address(address);
        Result<Balance> balanceResult = accountLedgerService.getBalance(addr.getAddressBytes());
        if (balanceResult.isFailed()) {
            return balanceResult.toRpcClientResult();
        }
        Balance balance = balanceResult.getData();
        List<AssetDto> dtoList = new ArrayList<>();
        dtoList.add(new AssetDto("NULS", balance));
        Result<List<ContractTokenInfo>> allTokenListResult = contractService.getAllTokensByAccount(address);
        if (allTokenListResult.isSuccess()) {
            List<ContractTokenInfo> tokenInfoList = allTokenListResult.getData();
            if (tokenInfoList != null && tokenInfoList.size() > 0) {
                for (ContractTokenInfo tokenInfo : tokenInfoList) {
                    if (tokenInfo.isLock()) {
                        continue;
                    }
                    dtoList.add(new AssetDto(tokenInfo));
                }
            }
        }
        Result result = Result.getSuccess();
        List<AssetDto> infoDtoList = new ArrayList<>();
        Page<AssetDto> page = new Page<>(pageNumber, pageSize, dtoList.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 (dtoList.size() > 0) {
            for (int i = start; i < end; i++) {
                infoDtoList.add(dtoList.get(i));
            }
        }
        page.setList(infoDtoList);
        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();
    }
}
Also used : Page(io.nuls.core.tools.page.Page) ContractTokenInfo(io.nuls.contract.dto.ContractTokenInfo) NulsException(io.nuls.kernel.exception.NulsException) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 7 with ContractTokenInfo

use of io.nuls.contract.dto.ContractTokenInfo in project nuls by nuls-io.

the class ContractBalanceManager method subtractContractToken.

public Result subtractContractToken(String account, String contract, BigInteger token) {
    tokenLock.lock();
    try {
        Map<String, ContractTokenInfo> tokens = contractTokenOfLocalAccount.get(account);
        if (tokens == null) {
            return Result.getSuccess();
        } else {
            ContractTokenInfo info = tokens.get(contract);
            if (info == null) {
                return Result.getSuccess();
            }
            BigInteger currentToken = info.getAmount();
            if (currentToken == null) {
                return Result.getSuccess();
            } else {
                if (currentToken.compareTo(token) < 0) {
                    return Result.getFailed(ContractErrorCode.INSUFFICIENT_BALANCE);
                }
                currentToken = currentToken.subtract(token);
                tokens.put(contract, info.setAmount(currentToken));
            }
        }
        return Result.getSuccess();
    } finally {
        tokenLock.unlock();
    }
}
Also used : BigInteger(java.math.BigInteger) LedgerUtil.asString(io.nuls.ledger.util.LedgerUtil.asString) ContractTokenInfo(io.nuls.contract.dto.ContractTokenInfo)

Example 8 with ContractTokenInfo

use of io.nuls.contract.dto.ContractTokenInfo in project nuls by nuls-io.

the class ContractBalanceManager method addContractToken.

public Result addContractToken(String account, String contract, BigInteger token) {
    tokenLock.lock();
    try {
        Map<String, ContractTokenInfo> tokens = contractTokenOfLocalAccount.get(account);
        do {
            if (tokens == null) {
                break;
            } else {
                ContractTokenInfo info = tokens.get(contract);
                if (info == null) {
                    return Result.getSuccess();
                }
                BigInteger currentToken = info.getAmount();
                if (currentToken == null) {
                    break;
                } else {
                    currentToken = currentToken.add(token);
                    tokens.put(contract, info.setAmount(currentToken));
                }
            }
        } while (false);
    } finally {
        tokenLock.unlock();
    }
    return Result.getSuccess();
}
Also used : BigInteger(java.math.BigInteger) LedgerUtil.asString(io.nuls.ledger.util.LedgerUtil.asString) ContractTokenInfo(io.nuls.contract.dto.ContractTokenInfo)

Example 9 with ContractTokenInfo

use of io.nuls.contract.dto.ContractTokenInfo 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);
}
Also used : Entry(io.nuls.db.model.Entry) LedgerUtil.asString(io.nuls.ledger.util.LedgerUtil.asString) ContractTokenInfo(io.nuls.contract.dto.ContractTokenInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 10 with ContractTokenInfo

use of io.nuls.contract.dto.ContractTokenInfo in project nuls by nuls-io.

the class ContractBalanceManager method refreshContractToken.

public void refreshContractToken(String account, String contract, ContractAddressInfoPo po, BigInteger value) {
    tokenLock.lock();
    try {
        ContractTokenInfo tokenInfo = new ContractTokenInfo(contract, po.getNrc20TokenName(), po.getDecimals(), value, po.getNrc20TokenSymbol(), po.getBlockHeight());
        Map<String, ContractTokenInfo> tokens = contractTokenOfLocalAccount.get(account);
        if (tokens == null) {
            tokens = new HashMap<>();
        }
        tokens.put(contract, tokenInfo);
        contractTokenOfLocalAccount.put(account, tokens);
    } finally {
        tokenLock.unlock();
    }
}
Also used : LedgerUtil.asString(io.nuls.ledger.util.LedgerUtil.asString) ContractTokenInfo(io.nuls.contract.dto.ContractTokenInfo)

Aggregations

ContractTokenInfo (io.nuls.contract.dto.ContractTokenInfo)10 LedgerUtil.asString (io.nuls.ledger.util.LedgerUtil.asString)5 NulsException (io.nuls.kernel.exception.NulsException)4 BigInteger (java.math.BigInteger)4 IOException (java.io.IOException)3 Page (io.nuls.core.tools.page.Page)2 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)1 Account (io.nuls.account.model.Account)1 ContractResult (io.nuls.contract.dto.ContractResult)1 ContractAddressInfoPo (io.nuls.contract.storage.po.ContractAddressInfoPo)1 Entry (io.nuls.db.model.Entry)1 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1