Skip to content

Commit

Permalink
Merge pull request #194 from nervosnetwork/rc/v0.23.1
Browse files Browse the repository at this point in the history
[ᚬmaster] Rc/v0.23.1
  • Loading branch information
duanyytop authored Oct 22, 2019
2 parents 1eaf72c + 75e263f commit a0a0551
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 101 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [v0.23.1](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.23.0...v0.23.1) (2019-10-22)

### Feature

* Add transaction fee to sendCapacity example([60f2faf](https://github.com/nervosnetwork/ckb-sdk-java/commit/60f2fafedb129d31100c26827379ef4a56dbe9c8))
* Move exceptions to outside application([52b85e5](https://github.com/nervosnetwork/ckb-sdk-java/commit/52b85e540fdecbf04225a6ba44b89e7d4df59c93))
* Add maven config to build.gradle([61f08ae](https://github.com/nervosnetwork/ckb-sdk-java/commit/61f08aedffab372b03fd93720f15a6ee9ec54a90))

# [v0.23.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.22.0...v0.23.0) (2019-10-19)

### Feature
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,26 @@ Java SDK for Nervos [CKB](https://github.com/nervosnetwork/ckb).

### Installation

#### Install from repositories:

- Maven
```
<dependency>
<groupId>org.nervos.ckb</groupId>
<artifactId>core</artifactId>
<version>{version}</version>
</dependency>
```

Gradle
```
implementation 'org.nervos.ckb:core:{version}'
```

#### Install manually

You can generate the jar and import manually.

```shell
git clone https://github.com/nervosnetwork/ckb-sdk-java.git

Expand Down
113 changes: 110 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {

ext.bouncycastleVersion = '1.63'
ext.bouncycastleVersion = '1.64'
ext.rxjavaVersion = '2.2.13'
ext.gsonVersion = '2.8.6'
ext.okhttpVersion = '4.2.2'
Expand All @@ -10,7 +10,13 @@ buildscript {

ext.junitVersion = '5.5.2'

repositories {
jcenter()
mavenCentral()
}

dependencies {
classpath 'io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.21.1'
classpath 'com.github.jengelman.gradle.plugins:shadow:5.1.0'
}
}
Expand All @@ -19,12 +25,14 @@ plugins {
id 'com.github.johnrengelman.shadow' version '5.1.0'
id 'java'
id 'com.github.sherter.google-java-format' version '0.8'
id 'com.jfrog.bintray' version '1.8.4'
}

apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'application'
apply plugin: 'io.codearte.nexus-staging'

mainClassName = 'org.nervos.ckb.RpcClient'
mainClassName = 'org.nervos.ckb.example.RpcExample'
applicationName = 'ckb-sdk-java'

description 'ckb-sdk-java base project'
Expand All @@ -35,7 +43,7 @@ allprojects {
targetCompatibility = 1.8

group 'org.nervos.ckb'
version '0.23.0'
version '0.23.1'

apply plugin: 'java'

Expand Down Expand Up @@ -68,3 +76,102 @@ subprojects {
build.dependsOn installGitHooks
}

configure(subprojects.findAll { it.name != 'tests' }) {
// Required for Maven Nexus repository
apply plugin: 'maven'
apply plugin: 'signing'

// Required for JFrog Artifactory repository
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

task javadocJar(type: Jar) {
classifier = 'javadoc'
from javadoc
}

task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}

task testJar(type: Jar) {
classifier = 'tests'
from sourceSets.test.output
}

artifacts {
archives sourcesJar, javadocJar, testJar
}

ext {
ossrhUsername = project.hasProperty('ossrhUsername') ? project.property('ossrhUsername') : ''
ossrhPassword = project.hasProperty('ossrhPassword') ? project.property('ossrhPassword') : ''
}

publishing {
publications {
mavenJava(MavenPublication) {
groupId 'org.nervos.ckb'
version '0.23.1'
from components.java
}
}
}

uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

repository(url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/') {
authentication(
userName: ossrhUsername,
password: ossrhPassword
)
}

pom.project {
name 'ckb-sdk-java'
packaging 'jar'
description project.description
url 'https://github.com/nervosnetwork/ckb-sdk-java.git'

scm {
connection 'scm:[email protected]:nervosnetwork/ckb-sdk-java.git'
url 'https://github.com/nervosnetwork/ckb-sdk-java.git'
}

licenses {
license {
name 'The MIT License'
url 'https://opensource.org/licenses/MIT'
}
}

developers {
developer {
id 'nervos'
name 'nervos developer'
email '[email protected]'
}
}
}
}
}
}

signing {
required { gradle.taskGraph.hasTask('uploadArchives') } // only execute as part of this task
sign configurations.archives
}

task release {
dependsOn 'build'
dependsOn 'uploadArchives'
dependsOn 'bintrayUpload'

tasks.findByName('uploadArchives').mustRunAfter 'build'
tasks.findByName('bintrayUpload').mustRunAfter 'build'
}
}
9 changes: 5 additions & 4 deletions console/src/main/java/org/nervos/ckb/example/RpcExample.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.nervos.ckb.example;

import com.google.gson.Gson;
import java.io.IOException;
import java.math.BigInteger;
import org.nervos.ckb.service.Api;
import org.nervos.ckb.type.Block;
Expand All @@ -17,7 +18,7 @@ public RpcExample() {
api = new Api(NODE_URL, false);
}

public static void main(String[] args) {
public static void main(String[] args) throws IOException {
System.out.println("Welcome to use SDK to visit CKB Blockchain");
RpcExample client = new RpcExample();
System.out.println(
Expand All @@ -29,15 +30,15 @@ public static void main(String[] args) {
+ new Gson().toJson(client.getBlockByNumber(currentBlockNumber.toString())));
}

public Block getBlockByNumber(String blockNumber) {
public Block getBlockByNumber(String blockNumber) throws IOException {
return api.getBlockByNumber(blockNumber);
}

public BigInteger getTipBlockNumber() {
public BigInteger getTipBlockNumber() throws IOException {
return api.getTipBlockNumber();
}

public BlockchainInfo getBlockchainInfo() {
public BlockchainInfo getBlockchainInfo() throws IOException {
return api.getBlockchainInfo();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static void main(String[] args) throws Exception {
new Receiver(KeyPairs.get(0).address, new BigInteger("800").multiply(UnitCKB)),
new Receiver(KeyPairs.get(1).address, new BigInteger("900").multiply(UnitCKB)),
new Receiver(KeyPairs.get(2).address, new BigInteger("1000").multiply(UnitCKB)));
BigInteger txFee = BigInteger.valueOf(10000);

System.out.println(
"Before transfer, miner's balance: "
Expand All @@ -65,7 +66,7 @@ public static void main(String[] args) throws Exception {
+ " CKB");

// miner send capacity to three receiver1 accounts with 800, 900 and 1000 CKB
String hash = sendCapacity(minerPrivateKey, receivers1, minerAddress);
String hash = sendCapacity(minerPrivateKey, receivers1, minerAddress, txFee);
System.out.println("First transaction hash: " + hash);
Thread.sleep(30000); // waiting transaction into block, sometimes you should wait more seconds

Expand Down Expand Up @@ -95,7 +96,7 @@ public static void main(String[] args) throws Exception {
+ " CKB");

// sender1 accounts send capacity to three receiver2 accounts with 400, 500 and 600 CKB
String hash2 = sendCapacity(senders1, receivers2, changeAddress);
String hash2 = sendCapacity(senders1, receivers2, changeAddress, txFee);
System.out.println("Second transaction hash: " + hash2);
Thread.sleep(30000); // waiting transaction into block, sometimes you should wait more seconds

Expand All @@ -111,17 +112,19 @@ private static BigInteger getBalance(String address) throws IOException {
}

private static String sendCapacity(
String privateKey, List<Receiver> receivers, String changeAddress) throws IOException {
String privateKey, List<Receiver> receivers, String changeAddress, BigInteger fee)
throws IOException {
BigInteger needCapacity = BigInteger.ZERO;
for (Receiver receiver : receivers) {
needCapacity = needCapacity.add(receiver.capacity);
}
List<Sender> senders = Collections.singletonList(new Sender(privateKey, needCapacity));
return sendCapacity(senders, receivers, changeAddress);
return sendCapacity(senders, receivers, changeAddress, fee);
}

private static String sendCapacity(
List<Sender> senders, List<Receiver> receivers, String changeAddress) throws IOException {
List<Sender> senders, List<Receiver> receivers, String changeAddress, BigInteger fee)
throws IOException {
TransactionBuilder builder = new TransactionBuilder(api);
CollectUtils txUtils = new CollectUtils(api);

Expand All @@ -130,7 +133,7 @@ private static String sendCapacity(
builder.addInputs(cellsWithPrivateKey.inputs);
}

builder.addOutputs(txUtils.generateOutputs(receivers, changeAddress));
builder.addOutputs(txUtils.generateOutputs(receivers, changeAddress, fee));

builder.buildTx();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public List<CellsWithPrivateKey> collectInputs(List<Sender> senders) throws IOEx
return cellsWithPrivateKeys;
}

public List<CellOutput> generateOutputs(List<Receiver> receivers, String changeAddress) {
public List<CellOutput> generateOutputs(
List<Receiver> receivers, String changeAddress, BigInteger fee) throws IOException {
if (fee.compareTo(BigInteger.ZERO) < 0) {
throw new IOException("Transaction fee should not be smaller than zero");
}
List<CellOutput> cellOutputs = new ArrayList<>();
AddressUtils addressUtils = new AddressUtils(Network.TESTNET);
for (Receiver receiver : receivers) {
Expand All @@ -62,6 +66,8 @@ public List<CellOutput> generateOutputs(List<Receiver> receivers, String changeA
for (Receiver receiver : receivers) {
needCapacity = needCapacity.add(receiver.capacity);
}
needCapacity = needCapacity.add(fee);

if (collectedCapacity.compareTo(needCapacity) > 0) {
String changeAddressBlake160 = addressUtils.getBlake160FromAddress(changeAddress);
cellOutputs.add(
Expand Down
Loading

0 comments on commit a0a0551

Please sign in to comment.