Search in sources :

Example 1 with ContractTokenTransfer

use of io.nuls.contract.rpc.form.ContractTokenTransfer in project nuls by nuls-io.

the class TokenTransferProcessor method argsValidate.

@Override
public boolean argsValidate(String[] args) {
    boolean result;
    do {
        int length = args.length;
        if (length != 7 && length != 8) {
            result = false;
            break;
        }
        if (!CommandHelper.checkArgsIsNull(args)) {
            result = false;
            break;
        }
        // gasLimit
        if (!StringUtils.isNumeric(args[4])) {
            result = false;
            break;
        }
        // price
        if (!StringUtils.isNumeric(args[5])) {
            result = false;
            break;
        }
        // amount
        if (!StringUtils.isNumberGtZero(args[6])) {
            result = false;
            break;
        }
        ContractTokenTransfer form = getTokenTransferForm(args);
        if (null == form) {
            result = false;
            break;
        }
        paramsData.set(form);
        result = StringUtils.isNotBlank(form.getToAddress());
        if (!result) {
            break;
        }
        result = true;
    } while (false);
    return result;
}
Also used : ContractTokenTransfer(io.nuls.contract.rpc.form.ContractTokenTransfer)

Example 2 with ContractTokenTransfer

use of io.nuls.contract.rpc.form.ContractTokenTransfer in project nuls by nuls-io.

the class TokenTransferProcessor method execute.

@Override
public CommandResult execute(String[] args) {
    ContractTokenTransfer form = paramsData.get();
    if (null == form) {
        form = getTokenTransferForm(args);
    }
    String address = form.getAddress();
    RpcClientResult res = CommandHelper.getPassword(address, restFul);
    if (!res.isSuccess()) {
        return CommandResult.getFailed(res);
    }
    String password = (String) res.getData();
    String contractAddress = form.getContractAddress();
    String url = "/contract/" + contractAddress;
    RpcClientResult checkResult = restFul.get(url, null);
    if (checkResult.isFailed()) {
        return CommandResult.getFailed(checkResult);
    }
    Map<String, Object> data = (Map) checkResult.getData();
    Boolean isNrc20 = (Boolean) data.get("isNrc20");
    if (!isNrc20) {
        return CommandResult.getFailed("Non-NRC20 contract, can not transfer token.");
    }
    Integer decimals = (Integer) data.get("decimals");
    BigDecimal amountBigD = new BigDecimal(form.getAmount()).multiply(BigDecimal.TEN.pow(decimals));
    try {
        BigInteger amountBigI = amountBigD.toBigIntegerExact();
        form.setAmount(amountBigI.toString());
    } catch (Exception e) {
        return CommandResult.getFailed("Illegal amount, you can have up to " + decimals + " valid digits after the decimal point.");
    }
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("address", form.getAddress());
    parameters.put("toAddress", form.getToAddress());
    parameters.put("contractAddress", form.getContractAddress());
    parameters.put("gasLimit", form.getGasLimit());
    parameters.put("price", form.getPrice());
    parameters.put("password", password);
    parameters.put("amount", form.getAmount());
    parameters.put("remark", form.getRemark());
    RpcClientResult result = restFul.post("/contract/token/transfer", parameters);
    if (result.isFailed()) {
        return CommandResult.getFailed(result);
    }
    Map<String, Object> resultMap = MapUtil.createLinkedHashMap(2);
    resultMap.put("txHash", result.getData());
    result.setData(resultMap);
    return CommandResult.getResult(result);
}
Also used : BigInteger(java.math.BigInteger) HashMap(java.util.HashMap) RpcClientResult(io.nuls.kernel.model.RpcClientResult) BigInteger(java.math.BigInteger) ContractTokenTransfer(io.nuls.contract.rpc.form.ContractTokenTransfer) HashMap(java.util.HashMap) Map(java.util.Map) BigDecimal(java.math.BigDecimal)

Example 3 with ContractTokenTransfer

use of io.nuls.contract.rpc.form.ContractTokenTransfer in project nuls by nuls-io.

the class TokenTransferProcessor method getTokenTransferForm.

private ContractTokenTransfer getTokenTransferForm(String[] args) {
    ContractTokenTransfer transfer = null;
    try {
        transfer = new ContractTokenTransfer();
        transfer.setAddress(args[1].trim());
        transfer.setToAddress(args[2].trim());
        transfer.setContractAddress(args[3].trim());
        transfer.setGasLimit(Long.valueOf(args[4].trim()));
        transfer.setPrice(Long.valueOf(args[5].trim()));
        transfer.setAmount(args[6].trim());
        if (args.length == 8) {
            transfer.setRemark(args[7].trim());
        }
        return transfer;
    } catch (Exception e) {
        e.fillInStackTrace();
        return null;
    }
}
Also used : ContractTokenTransfer(io.nuls.contract.rpc.form.ContractTokenTransfer)

Aggregations

ContractTokenTransfer (io.nuls.contract.rpc.form.ContractTokenTransfer)3 RpcClientResult (io.nuls.kernel.model.RpcClientResult)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1