diff --git a/CHANGELOG.md b/CHANGELOG.md index c7acd44e0..b944a36a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [v0.29.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.28.0...v0.29.0) (2020-2-28) + +### Refactor + +* Remove useless code ([a858559](https://github.com/nervosnetwork/ckb-sdk-java/commit/a858559abd8dee81d0969334b5d6487533545ca6)) +* Update transaction fee calculating ([004c6e3](https://github.com/nervosnetwork/ckb-sdk-java/commit/004c6e30b571b805ec2905df0c909a1287c6917d)) + # [v0.28.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.27.1...v0.28.0) (2020-2-7) Bump version to v0.28.0 diff --git a/README.md b/README.md index e8df212c0..b01b62881 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,9 @@ You can see more JSON-RPC requests from [RPC Document](https://github.com/nervos #### Single-sig Transfer +> Note: If you want to run transfer example, you should update example private key of sender whose balance is not zero. +> And if you want to use example default private key to run, you should make the example sender's balance is not zero or set the blake160 of default sender's public key to CKB dev chain node configuration file to be a miner. + [SingleKeySingleSigTxExample](https://github.com/nervosnetwork/ckb-sdk-java/tree/develop/example/src/main/java/org/nervos/ckb/SingleKeySingleSigTxExample.java) provides `sendCapacity` method with any amount inputs which belong to a private key. [MultiKeySingleSigTxExample](https://github.com/nervosnetwork/ckb-sdk-java/tree/develop/example/src/main/java/org/nervos/ckb/MultiKeySingleSigTxExample.java) provides `sendCapacity` method with any amount inputs which belong to any amount private keys. @@ -138,6 +141,9 @@ You can reference detail example in `example/MultiKeySingleSigTxExample.java`. #### Multi-sig Transfer +> Note: If you want to run transfer example, you should update example private key of sender whose balance is not zero. +> And if you want to use example default private key to run, you should make the example sender's balance is not zero or set the blake160 of default sender's public key to CKB dev chain node configuration file to be a miner. + [SendToMultiSigAddressTxExample](https://github.com/nervosnetwork/ckb-sdk-java/tree/develop/example/src/main/java/org/nervos/ckb/SendToMultiSigAddressTxExample.java) provides `sendCapacity` method which single-sig address sends capacity to 2/3 format multi-sig address. [MultiSignTransactionExample](https://github.com/nervosnetwork/ckb-sdk-java/tree/develop/example/src/main/java/org/nervos/ckb/MultiSignTransactionExample.java) provides `sendCapacity` method which 2/3 format multi-sig address sends capacity to single-sig address. diff --git a/build.gradle b/build.gradle index aa3e0fb5c..aca51b2f8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,10 @@ buildscript { ext.bouncycastleVersion = '1.64' - ext.rxjavaVersion = '2.2.17' + ext.rxjavaVersion = '2.2.18' ext.gsonVersion = '2.8.6' - ext.okhttpVersion = '4.3.1' - ext.loggingOkhttpVersion = '4.3.1' + ext.okhttpVersion = '4.4.0' + ext.loggingOkhttpVersion = '4.4.0' ext.slf4jVersion = '1.7.30' ext.guavaVersion = '28.2-jre' @@ -43,7 +43,7 @@ allprojects { targetCompatibility = 1.8 group 'org.nervos.ckb' - version '0.28.0' + version '0.29.0' apply plugin: 'java' @@ -113,7 +113,7 @@ configure(subprojects.findAll { it.name != 'tests' }) { publications { mavenJava(MavenPublication) { groupId 'org.nervos.ckb' - version '0.28.0' + version '0.29.0' from components.java } } diff --git a/ckb/src/main/java/org/nervos/ckb/transaction/CellCollector.java b/ckb/src/main/java/org/nervos/ckb/transaction/CellCollector.java index 94287b969..4ace112ff 100644 --- a/ckb/src/main/java/org/nervos/ckb/transaction/CellCollector.java +++ b/ckb/src/main/java/org/nervos/ckb/transaction/CellCollector.java @@ -130,8 +130,7 @@ public CollectResult collectInputs( } private BigInteger calculateTxFee(Transaction transaction, BigInteger feeRate) { - int txSize = Serializer.serializeTransaction(transaction).toBytes().length; - return Calculator.calculateTransactionFee(BigInteger.valueOf(txSize), feeRate); + return Calculator.calculateTransactionFee(transaction, feeRate); } private BigInteger calculateOutputSize(CellOutput cellOutput) { diff --git a/ckb/src/main/java/org/nervos/ckb/utils/Calculator.java b/ckb/src/main/java/org/nervos/ckb/utils/Calculator.java index 944042b8a..53c760d02 100644 --- a/ckb/src/main/java/org/nervos/ckb/utils/Calculator.java +++ b/ckb/src/main/java/org/nervos/ckb/utils/Calculator.java @@ -12,14 +12,15 @@ public class Calculator { private static final int SERIALIZED_TX_OFFSET_BYTE_SIZE = 4; private static final int MIN_CONFIRM_BLOCKS = 3; - public static int calculateSerializedSizeInBlock(Transaction transaction) { + public static int calculateTransactionSize(Transaction transaction) { Table serializedTx = Serializer.serializeTransaction(transaction); return serializedTx.getLength() + SERIALIZED_TX_OFFSET_BYTE_SIZE; } private static final BigInteger RADIO = BigInteger.valueOf(1000); - public static BigInteger calculateTransactionFee(BigInteger transactionSize, BigInteger feeRate) { + private static BigInteger calculateTransactionFee( + BigInteger transactionSize, BigInteger feeRate) { BigInteger base = transactionSize.multiply(feeRate); BigInteger fee = base.divide(RADIO); if (fee.multiply(RADIO).compareTo(base) < 0) { @@ -35,7 +36,12 @@ public static BigInteger calculateTransactionFee( } BigInteger feeRate = Numeric.toBigInt(api.estimateFeeRate(String.valueOf(expectedConfirmBlocks)).feeRate); - BigInteger txSize = BigInteger.valueOf(calculateSerializedSizeInBlock(transaction)); + BigInteger txSize = BigInteger.valueOf(calculateTransactionSize(transaction)); + return calculateTransactionFee(txSize, feeRate); + } + + public static BigInteger calculateTransactionFee(Transaction transaction, BigInteger feeRate) { + BigInteger txSize = BigInteger.valueOf(calculateTransactionSize(transaction)); return calculateTransactionFee(txSize, feeRate); } } diff --git a/ckb/src/test/java/utils/CalculatorTest.java b/ckb/src/test/java/utils/CalculatorTest.java index 4774af517..c73881e4c 100644 --- a/ckb/src/test/java/utils/CalculatorTest.java +++ b/ckb/src/test/java/utils/CalculatorTest.java @@ -6,7 +6,9 @@ import java.util.Collections; import java.util.List; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.nervos.ckb.type.OutPoint; import org.nervos.ckb.type.Script; import org.nervos.ckb.type.cell.CellDep; @@ -16,10 +18,12 @@ import org.nervos.ckb.utils.Calculator; /** Copyright © 2019 Nervos Foundation. All rights reserved. */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) public class CalculatorTest { + private Transaction tx; - @Test - void testCalculateTransactionSize() { + @BeforeAll + void init() { List cellOutputs = new ArrayList<>(); cellOutputs.add( new CellOutput( @@ -40,7 +44,7 @@ void testCalculateTransactionSize() { "0x59a27ef3ba84f061517d13f42cf44ed020610061", Script.TYPE))); - Transaction tx = + tx = new Transaction( "0x0", Arrays.asList( @@ -65,17 +69,16 @@ void testCalculateTransactionSize() { Arrays.asList("0x1234", "0x"), Collections.singletonList( "0x82df73581bcd08cb9aa270128d15e79996229ce8ea9e4f985b49fbf36762c5c37936caf3ea3784ee326f60b8992924fcf496f9503c907982525a3436f01ab32900")); + } - Assertions.assertEquals(Calculator.calculateSerializedSizeInBlock(tx), 536); + @Test + void testCalculateTransactionSize() { + Assertions.assertEquals(Calculator.calculateTransactionSize(tx), 536); } @Test public void testCalculateTransactionFee() { Assertions.assertEquals( - Calculator.calculateTransactionFee(BigInteger.valueOf(1035), BigInteger.valueOf(900)), - BigInteger.valueOf(932)); - Assertions.assertEquals( - Calculator.calculateTransactionFee(BigInteger.valueOf(900), BigInteger.valueOf(900)), - BigInteger.valueOf(810)); + Calculator.calculateTransactionFee(tx, BigInteger.valueOf(900)), BigInteger.valueOf(483)); } } diff --git a/example/src/main/java/org/nervos/ckb/MultiKeySingleSigTxExample.java b/example/src/main/java/org/nervos/ckb/MultiKeySingleSigTxExample.java index 957dd9894..6b141933b 100644 --- a/example/src/main/java/org/nervos/ckb/MultiKeySingleSigTxExample.java +++ b/example/src/main/java/org/nervos/ckb/MultiKeySingleSigTxExample.java @@ -137,10 +137,6 @@ private static String sendCapacity( private static Transaction generateTx( String address, List receivers, String changeAddress) throws IOException { - BigInteger needCapacity = BigInteger.ZERO; - for (Receiver receiver : receivers) { - needCapacity = needCapacity.add(receiver.capacity); - } return generateTx(Collections.singletonList(address), receivers, changeAddress); } diff --git a/example/src/main/java/org/nervos/ckb/SendToMultiSigAddressTxExample.java b/example/src/main/java/org/nervos/ckb/SendToMultiSigAddressTxExample.java index a93d2f818..c88a7cb35 100644 --- a/example/src/main/java/org/nervos/ckb/SendToMultiSigAddressTxExample.java +++ b/example/src/main/java/org/nervos/ckb/SendToMultiSigAddressTxExample.java @@ -66,11 +66,6 @@ private static String getMultiSigBalance() { private static String sendCapacity(List receivers, String changeAddress) throws IOException { - BigInteger needCapacity = BigInteger.ZERO; - for (Receiver receiver : receivers) { - needCapacity = needCapacity.add(receiver.capacity); - } - TransactionBuilder txBuilder = new TransactionBuilder(api); CollectUtils txUtils = new CollectUtils(api); diff --git a/example/src/main/java/org/nervos/ckb/SingleKeySingleSigTxExample.java b/example/src/main/java/org/nervos/ckb/SingleKeySingleSigTxExample.java index 5416077a1..6c3ff5533 100644 --- a/example/src/main/java/org/nervos/ckb/SingleKeySingleSigTxExample.java +++ b/example/src/main/java/org/nervos/ckb/SingleKeySingleSigTxExample.java @@ -71,11 +71,7 @@ private static String getBalance(String address) { private static String sendCapacity(List receivers, String changeAddress) throws IOException { - BigInteger needCapacity = BigInteger.ZERO; List scriptGroupWithPrivateKeysList = new ArrayList<>(); - for (Receiver receiver : receivers) { - needCapacity = needCapacity.add(receiver.capacity); - } TransactionBuilder txBuilder = new TransactionBuilder(api); CollectUtils txUtils = new CollectUtils(api); diff --git a/example/src/main/java/org/nervos/ckb/SingleSigWithIndexerTxExample.java b/example/src/main/java/org/nervos/ckb/SingleSigWithIndexerTxExample.java index 21305084a..85529109d 100644 --- a/example/src/main/java/org/nervos/ckb/SingleSigWithIndexerTxExample.java +++ b/example/src/main/java/org/nervos/ckb/SingleSigWithIndexerTxExample.java @@ -83,11 +83,7 @@ private static BigInteger getBalance(String address) { private static String sendCapacity(List receivers, String changeAddress) throws IOException { - BigInteger needCapacity = BigInteger.ZERO; List scriptGroupWithPrivateKeysList = new ArrayList<>(); - for (Receiver receiver : receivers) { - needCapacity = needCapacity.add(receiver.capacity); - } TransactionBuilder txBuilder = new TransactionBuilder(api); CollectUtils txUtils = new CollectUtils(api); diff --git a/example/src/main/java/org/nervos/ckb/transaction/CollectUtils.java b/example/src/main/java/org/nervos/ckb/transaction/CollectUtils.java index b3da924d5..dcd203aed 100644 --- a/example/src/main/java/org/nervos/ckb/transaction/CollectUtils.java +++ b/example/src/main/java/org/nervos/ckb/transaction/CollectUtils.java @@ -79,10 +79,6 @@ public List generateOutputs(List receivers, String changeA new CellOutput( Numeric.toHexStringWithPrefix(receiver.capacity), addressParseResult.script)); } - BigInteger needCapacity = BigInteger.ZERO; - for (Receiver receiver : receivers) { - needCapacity = needCapacity.add(receiver.capacity); - } AddressParseResult addressParseResult = AddressParser.parse(changeAddress); cellOutputs.add(new CellOutput("0x0", addressParseResult.script)); return cellOutputs;