use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.
the class PocConsensusResource method depositToAgent.
@POST
@Path("/deposit")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "deposit nuls to a bank! 申请参与共识 ", notes = "返回申请成功交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult depositToAgent(@ApiParam(name = "form", value = "申请参与共识表单数据", required = true) DepositForm form) throws NulsException {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getAddress());
AssertUtil.canNotEmpty(form.getAgentHash());
if (!NulsDigestData.validHash(form.getAgentHash())) {
return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST).toRpcClientResult();
}
AssertUtil.canNotEmpty(form.getDeposit());
if (!AddressTool.validAddress(form.getAddress())) {
throw new NulsRuntimeException(KernelErrorCode.PARAMETER_ERROR);
}
Account account = accountService.getAccount(form.getAddress()).getData();
if (null == account) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
}
if (account.isEncrypted() && account.isLocked()) {
AssertUtil.canNotEmpty(form.getPassword(), "password is wrong");
if (!account.validatePassword(form.getPassword())) {
return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG).toRpcClientResult();
}
}
if (!account.isOk()) {
return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT).toRpcClientResult();
}
DepositTransaction tx = new DepositTransaction();
Deposit deposit = new Deposit();
deposit.setAddress(AddressTool.getAddress(form.getAddress()));
deposit.setAgentHash(NulsDigestData.fromDigestHex(form.getAgentHash()));
deposit.setDeposit(Na.valueOf(form.getDeposit()));
tx.setTxData(deposit);
CoinData coinData = new CoinData();
List<Coin> toList = new ArrayList<>();
toList.add(new Coin(deposit.getAddress(), deposit.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
coinData.setTo(toList);
tx.setCoinData(coinData);
CoinDataResult result = accountLedgerService.getCoinData(deposit.getAddress(), deposit.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
RpcClientResult result1 = this.txProcessing(tx, result, account, form.getPassword());
if (!result1.isSuccess()) {
return result1;
}
Map<String, String> valueMap = new HashMap<>();
valueMap.put("value", tx.getHash().getDigestHex());
return Result.getSuccess().setData(valueMap).toRpcClientResult();
}
use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.
the class PocConsensusResource method getMultiDepositFee.
@GET
@Path("/multiAccount/deposit/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of create agent! 获取加入共识的手续费", notes = "返回加入共识交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getMultiDepositFee(@BeanParam() GetDepositFeeForm form) throws Exception {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getAddress(), "address can not be null");
AssertUtil.canNotEmpty(form.getAgentHash(), "agent hash can not be null");
AssertUtil.canNotEmpty(form.getDeposit(), "deposit can not be null");
DepositTransaction tx = new DepositTransaction();
Deposit deposit = new Deposit();
deposit.setAddress(AddressTool.getAddress(form.getAddress()));
deposit.setAgentHash(NulsDigestData.fromDigestHex(form.getAgentHash()));
deposit.setDeposit(Na.valueOf(form.getDeposit()));
tx.setTxData(deposit);
Result<MultiSigAccount> sigAccountResult = accountService.getMultiSigAccount(form.getAddress());
MultiSigAccount multiSigAccount = sigAccountResult.getData();
Script redeemScript = accountLedgerService.getRedeemScript(multiSigAccount);
if (redeemScript == null) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
}
CoinData coinData = new CoinData();
List<Coin> toList = new ArrayList<>();
toList.add(new Coin(deposit.getAddress(), deposit.getDeposit(), -1));
coinData.setTo(toList);
tx.setCoinData(coinData);
CoinDataResult result = accountLedgerService.getCoinData(deposit.getAddress(), deposit.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
tx.getCoinData().setFrom(result.getCoinList());
if (null != result.getChange()) {
tx.getCoinData().getTo().add(result.getChange());
}
Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
// 交易签名的长度为m*单个签名长度+赎回脚本长度
int scriptSignLenth = redeemScript.getProgram().length + ((int) multiSigAccount.getM()) * 72;
Result rs = accountLedgerService.getMultiMaxAmountOfOnce(AddressTool.getAddress(form.getAddress()), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES, scriptSignLenth);
Map<String, Long> map = new HashMap<>();
Long maxAmount = null;
if (rs.isSuccess()) {
maxAmount = ((Na) rs.getData()).getValue();
}
map.put("fee", fee.getValue());
map.put("maxAmount", maxAmount);
rs.setData(map);
return Result.getSuccess().setData(rs).toRpcClientResult();
}
use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.
the class PocConsensusResource method getCreateMultiAgentFee.
@GET
@Path("/multiAccount/Agent/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of create agent! 获取创建共识(代理)节点的手续费", notes = "返回创建的节点成功的交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getCreateMultiAgentFee(@BeanParam() GetCreateAgentFeeForm form) throws Exception {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getAgentAddress(), "agent address can not be null");
AssertUtil.canNotEmpty(form.getCommissionRate(), "commission rate can not be null");
AssertUtil.canNotEmpty(form.getDeposit(), "deposit can not be null");
AssertUtil.canNotEmpty(form.getPackingAddress(), "packing address can not be null");
if (StringUtils.isBlank(form.getRewardAddress())) {
form.setRewardAddress(form.getAgentAddress());
}
CreateAgentTransaction tx = new CreateAgentTransaction();
tx.setTime(TimeService.currentTimeMillis());
Agent agent = new Agent();
agent.setAgentAddress(AddressTool.getAddress(form.getAgentAddress()));
agent.setPackingAddress(AddressTool.getAddress(form.getPackingAddress()));
if (StringUtils.isBlank(form.getRewardAddress())) {
agent.setRewardAddress(agent.getAgentAddress());
} else {
agent.setRewardAddress(AddressTool.getAddress(form.getRewardAddress()));
}
agent.setDeposit(Na.valueOf(form.getDeposit()));
agent.setCommissionRate(form.getCommissionRate());
tx.setTxData(agent);
Result<MultiSigAccount> sigAccountResult = accountService.getMultiSigAccount(form.getAgentAddress());
MultiSigAccount multiSigAccount = sigAccountResult.getData();
Script redeemScript = accountLedgerService.getRedeemScript(multiSigAccount);
if (redeemScript == null) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
}
CoinData coinData = new CoinData();
List<Coin> toList = new ArrayList<>();
if (agent.getAgentAddress()[2] == 3) {
Script scriptPubkey = SignatureUtil.createOutputScript(agent.getAgentAddress());
toList.add(new Coin(scriptPubkey.getProgram(), agent.getDeposit(), -1));
} else {
toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), -1));
}
coinData.setTo(toList);
tx.setCoinData(coinData);
CoinDataResult result = accountLedgerService.getMutilCoinData(agent.getAgentAddress(), agent.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
tx.getCoinData().setFrom(result.getCoinList());
if (null != result.getChange()) {
tx.getCoinData().getTo().add(result.getChange());
}
Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
// 交易签名的长度为m*单个签名长度+赎回脚本长度
int scriptSignLenth = redeemScript.getProgram().length + ((int) multiSigAccount.getM()) * 72;
Result rs = accountLedgerService.getMultiMaxAmountOfOnce(AddressTool.getAddress(form.getAgentAddress()), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES, scriptSignLenth);
Map<String, Long> map = new HashMap<>();
Long maxAmount = null;
if (rs.isSuccess()) {
maxAmount = ((Na) rs.getData()).getValue();
}
map.put("fee", fee.getValue());
map.put("maxAmount", maxAmount);
rs.setData(map);
return Result.getSuccess().setData(rs).toRpcClientResult();
}
use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.
the class PocConsensusResource method createAgent.
@POST
@Path("/agent")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create an agent for consensus! 创建共识(代理)节点 [3.6.3]", notes = "返回创建的节点成功的交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult createAgent(@ApiParam(name = "form", value = "创建节点表单数据", required = true) CreateAgentForm form) throws NulsException {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getAgentAddress(), "agent address can not be null");
AssertUtil.canNotEmpty(form.getCommissionRate(), "commission rate can not be null");
AssertUtil.canNotEmpty(form.getDeposit(), "deposit can not be null");
AssertUtil.canNotEmpty(form.getPackingAddress(), "packing address can not be null");
if (!AddressTool.isPackingAddress(form.getPackingAddress()) || !AddressTool.validAddress(form.getAgentAddress())) {
throw new NulsRuntimeException(AccountErrorCode.ADDRESS_ERROR);
}
Account account = accountService.getAccount(form.getAgentAddress()).getData();
if (null == account) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
}
if (!account.isOk()) {
return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT).toRpcClientResult();
}
if (account.isEncrypted() && account.isLocked()) {
AssertUtil.canNotEmpty(form.getPassword(), "password is wrong");
if (!account.validatePassword(form.getPassword())) {
return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG).toRpcClientResult();
}
}
CreateAgentTransaction tx = new CreateAgentTransaction();
tx.setTime(TimeService.currentTimeMillis());
Agent agent = new Agent();
agent.setAgentAddress(AddressTool.getAddress(form.getAgentAddress()));
agent.setPackingAddress(AddressTool.getAddress(form.getPackingAddress()));
if (StringUtils.isBlank(form.getRewardAddress())) {
agent.setRewardAddress(agent.getAgentAddress());
} else {
agent.setRewardAddress(AddressTool.getAddress(form.getRewardAddress()));
}
agent.setDeposit(Na.valueOf(form.getDeposit()));
agent.setCommissionRate(form.getCommissionRate());
tx.setTxData(agent);
CoinData coinData = new CoinData();
List<Coin> toList = new ArrayList<>();
toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
coinData.setTo(toList);
tx.setCoinData(coinData);
CoinDataResult result = accountLedgerService.getCoinData(agent.getAgentAddress(), agent.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
RpcClientResult result1 = this.txProcessing(tx, result, account, form.getPassword());
if (!result1.isSuccess()) {
return result1;
}
Map<String, String> valueMap = new HashMap<>();
valueMap.put("value", tx.getHash().getDigestHex());
return Result.getSuccess().setData(valueMap).toRpcClientResult();
}
use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.
the class PocConsensusResource method getDepositFee.
@GET
@Path("/deposit/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of create agent! 获取加入共识的手续费", notes = "返回加入共识交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getDepositFee(@BeanParam() GetDepositFeeForm form) throws NulsException {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getAddress(), "address can not be null");
AssertUtil.canNotEmpty(form.getAgentHash(), "agent hash can not be null");
AssertUtil.canNotEmpty(form.getDeposit(), "deposit can not be null");
Account account = accountService.getAccount(form.getAddress()).getData();
if (null == account) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
}
if (!account.isOk()) {
return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT).toRpcClientResult();
}
DepositTransaction tx = new DepositTransaction();
Deposit deposit = new Deposit();
deposit.setAddress(AddressTool.getAddress(form.getAddress()));
deposit.setAgentHash(NulsDigestData.fromDigestHex(form.getAgentHash()));
deposit.setDeposit(Na.valueOf(form.getDeposit()));
tx.setTxData(deposit);
CoinData coinData = new CoinData();
List<Coin> toList = new ArrayList<>();
toList.add(new Coin(deposit.getAddress(), deposit.getDeposit(), -1));
coinData.setTo(toList);
tx.setCoinData(coinData);
CoinDataResult result = accountLedgerService.getCoinData(deposit.getAddress(), deposit.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
tx.getCoinData().setFrom(result.getCoinList());
if (null != result.getChange()) {
tx.getCoinData().getTo().add(result.getChange());
}
Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
Map<String, Long> map = new HashMap<>();
map.put("fee", fee.getValue());
map.put("maxAmount", getMaxAmount(fee, form.getAddress(), tx));
return Result.getSuccess().setData(map).toRpcClientResult();
}
Aggregations