Skip to content

Commit

Permalink
Merge pull request #32 from BaojunCZ/rc/v0.11.0
Browse files Browse the repository at this point in the history
Rc/v0.11.0
  • Loading branch information
BaojunCZ authored May 13, 2019
2 parents 8625625 + a07ccdf commit c848d2a
Show file tree
Hide file tree
Showing 19 changed files with 264 additions and 219 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## 0.11.0

Support [ ckb release v0.11.0](https://github.com/nervosnetwork/ckb/releases/tag/v0.11.0)

- add RPC getBlockByBlockNumber
- add RPC request time out
- add parse address

## 0.10.1

Support [ ckb release v0.10.0](https://github.com/nervosnetwork/ckb/releases/tag/v0.10.0)
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Dart SDK for [CKB](https://github.com/nervosnetwork/ckb).Nervos test network has

To build CKB SDK, you'll need:

* Dart sdk: ">=2.1.0 <3.0.0"
- Dart sdk: ">=2.1.0 <3.0.0"

## Usage

Expand All @@ -20,17 +20,21 @@ final apiClient = CKBApiClient(nodeUrl: NodeUrl);
String blockHash = await apiClient.getBlockHash("1");
```

You can see more JSON-RPC requests from [RPC Document](https://github.com/nervosnetwork/ckb/blob/develop/rpc/README.md)

### Address

```dart
var ckbAddress = CKBAddress(Network.TestNet);
var address = ckbAddress.generate(PublicKey);
final ckbAddress = CKBAddress(Network.TestNet);
final address = ckbAddress.generate(PublicKey);
```

You can find more in here.

[api_client_example](example/api_client_example.dart)

[address_example](example/address_example.dart)

[test](test/)

## [License](LICENSE)
Expand Down
13 changes: 13 additions & 0 deletions example/address_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:ckb_sdk/ckb-utils/network.dart';
import 'package:ckb_sdk/ckb_sdk.dart';

main() {
final ckbAddressTestNet = CKBAddress(Network.TestNet);
final addressTestNet =
ckbAddressTestNet.generate("0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01");
print(addressTestNet);
final ckbAddressMainNet = CKBAddress(Network.MainNet);
final addressMainNet =
ckbAddressMainNet.generate("0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01");
print(addressMainNet);
}
46 changes: 19 additions & 27 deletions example/api_client_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,31 @@ import 'package:ckb_sdk/ckb-types/item/transaction_with_status.dart';
import 'package:ckb_sdk/ckb_sdk.dart';

main() async {
final apiClient = new CKBApiClient();
String blockHash = await apiClient.getBlockHash("1");
// print(blockHash.error.message);
print(blockHash);
final apiClient = new CKBApiClient('http://192.168.2.78:8114');

String genesisbBlockHash = await apiClient.genesisBlockHash();
print(genesisbBlockHash);
Block blockRes = await apiClient.genesisBlock();
print(jsonEncode(blockRes));
String blockHash = await apiClient.getBlockHash("20");
print(blockHash);
TransactionWithStatus transactionWithStatus =
await apiClient.getTransaction("0x3abd21e6e51674bb961bb4c5f3cee9faa5da30e64be10628dc1cef292cbae324");
List<CellOutput> cellOutputs = transactionWithStatus.transaction.outputs;
print(jsonEncode(cellOutputs));

await apiClient.getTransaction('0x3abd21e6e51674bb961bb4c5f3cee9faa5da30e64be10628dc1cef292cbae324');
print(jsonEncode(transactionWithStatus));
Header header = await apiClient.getTipHeader();
print(header.cellbaseId);

print(jsonEncode(header));
List<CellWithOutPoint> cells = await apiClient.getCellsByLockHash(
'0x0da2fe99fe549e082d4ed483c2e968a89ea8d11aabf5d79e5cbf06522de6e674', "1", "1000");
CellWithOutPoint cell = cells[0];
OutPoint point = cell.outPoint;
print(point.txHash);
print(point.index);

'0x266cec97cbede2cfbce73666f08deed9560bdf7841a7a5a51b3a3f09da249e21', '0', '2');
print(jsonEncode(cells));
CellWithStatus liveCellRes = await apiClient
.getLiveCell(new OutPoint("0xff50745e53c9af867763834dda3a94fbe833e9318ddb3570a2e914630fcaea17", 0));
print(liveCellRes.cell.capacity);

.getLiveCell(new OutPoint("0x8d37f0856ebb70c12871830667d82224e6619896c7f12bb73a14dd9329af9c8d", 0));
print(jsonEncode(liveCellRes));
String tipBlockNumber = await apiClient.getTipBlockNumber();
print(tipBlockNumber);

NodeInfo localNodeInfo = await apiClient.getLocalNodeInfo();
print(jsonEncode(localNodeInfo));

Block block = await apiClient.getBlock('0xdc3b61fe382b3a6297453a712ca2d2581b254f62baab509c35e50b2d09c25702');
NodeInfo localNodeId = await apiClient.getLocalNodeInfo();
print(jsonEncode(localNodeId));
Block block = await apiClient.getBlock(await apiClient.getBlockHash("20"));
print(jsonEncode(block));

String transationHash = await apiClient.sendTransaction(new SendTransaction([], [], [], 2));
print(transationHash);
Block blockByNumber = await apiClient.getBlockByBlockNumber("20");
print(jsonEncode(blockByNumber));
}
37 changes: 19 additions & 18 deletions lib/ckb-rpc/api_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
import 'dart:convert';

import 'package:ckb_sdk/ckb-types/item/error.dart';
import 'package:ckb_sdk/ckb_error/ckb_error.dart';
import 'package:http/http.dart' as http;

Expand All @@ -20,36 +21,36 @@ class ApiRequest {
}

requestRpc(String url, params) async {
try {
var body = {"jsonrpc": jsonrpc, "id": id};
body["method"] = url;
body["params"] = params;
var response = await http.post(_nodeUrl, headers: {'Content-type': 'application/json'}, body: jsonEncode(body));
id = id + 1;
if (response.statusCode == 200) {
return handlerResult(body, response.body);
} else {
throw genericError("Request failed with status: ${response.statusCode}.");
}
} catch (e) {
rethrow;
var body = {"jsonrpc": jsonrpc, "id": id};
body["method"] = url;
body["params"] = params;
var response = await http
.post(_nodeUrl, headers: {'Content-type': 'application/json'}, body: jsonEncode(body))
.timeout(Duration(seconds: 10), onTimeout: () {
throw RPCTimeOutException(url);
});
id = id + 1;
if (response.statusCode == 200) {
return handlerResult(url, body, response.body);
} else {
throw RPCRequestException("Request failed with status: ${response.statusCode}.", url);
}
}

handlerResult(body, data) {
handlerResult(url, body, data) {
if (null == data) {
throw emptyResponse;
throw EmptyResponseException(url);
}
var json = jsonDecode(data);
if (null == json) {
throw emptyResponse;
throw EmptyResponseException(url);
}
if (null == json['result']) {
throw nullResult;
throw NullResultException(url);
}
if (null != json["error"]) {
var error = json["error"];
throw getError(error["code"], error["message"]);
throw RPCErrorException(RPCError(error["code"], error["message"]), url);
}
return json;
}
Expand Down
46 changes: 23 additions & 23 deletions lib/ckb-rpc/ckb_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:ckb_sdk/ckb_error/ckb_error.dart';
class CKBApiClient {
ApiRequest _request;

CKBApiClient({String nodeUrl = 'http://192.168.2.78:8114'}) {
CKBApiClient(String nodeUrl) {
_request = ApiRequest(nodeUrl);
}

Expand All @@ -34,18 +34,16 @@ class CKBApiClient {
//==============================Chain RPC Methods================================

Future<String> getBlockHash(String blockNumber) async {
return BlockHashRes.fromJson(await _request.requestRpc(ServiceUrl.blockHash, [blockNumber])).result;
return BlockHashRes.fromJson(await _request.requestRpc(ServiceUrl.blockHash, [blockNumber]))
.result;
}

Future<TransactionWithStatus> getTransaction(String hash) async {
try {
return TransactionRes.fromJson(await _request.requestRpc(ServiceUrl.transaction, [hash])).result;
} on RPCError catch (error) {
if (error.code == nullResultCode) {
return null;
} else {
rethrow;
}
return TransactionRes.fromJson(await _request.requestRpc(ServiceUrl.transaction, [hash]))
.result;
} on NullResultException {
return null;
} catch (error) {
rethrow;
}
Expand All @@ -55,9 +53,10 @@ class CKBApiClient {
return HeaderRes.fromJson(await _request.requestRpc(ServiceUrl.tipHeader, [])).result;
}

Future<List<CellWithOutPoint>> getCellsByLockHash(String hash, String fromBlockNumber, String toBlockNumber) async {
return CellsByLockHashRes.fromJson(
await _request.requestRpc(ServiceUrl.cellsByLockHash, [hash, fromBlockNumber, toBlockNumber]))
Future<List<CellWithOutPoint>> getCellsByLockHash(
String hash, String fromBlockNumber, String toBlockNumber) async {
return CellsByLockHashRes.fromJson(await _request
.requestRpc(ServiceUrl.cellsByLockHash, [hash, fromBlockNumber, toBlockNumber]))
.result;
}

Expand All @@ -66,29 +65,30 @@ class CKBApiClient {
}

Future<String> getTipBlockNumber() async {
return TipBlockNumberRes.fromJson(await _request.requestRpc(ServiceUrl.tipBlockNumber, [])).result;
return TipBlockNumberRes.fromJson(await _request.requestRpc(ServiceUrl.tipBlockNumber, []))
.result;
}

Future<NodeInfo> getLocalNodeInfo() async {
return LocalNodeInfoRes.fromJson(await _request.requestRpc(ServiceUrl.localNodeInfo, [])).result;
return LocalNodeInfoRes.fromJson(await _request.requestRpc(ServiceUrl.localNodeInfo, []))
.result;
}

Future<Block> getBlock(String blockHash) async {
return BlockRes.fromJson(await _request.requestRpc(ServiceUrl.block, [blockHash])).result;
}

//================================Pool RPC Methods===============================

Future<String> sendTransaction(transaction) async {
return SendTransactionRes.fromJson(await _request.requestRpc(ServiceUrl.sendTransaction, [transaction])).result;
Future<Block> getBlockByBlockNumber(String blockNumber) async {
return BlockRes.fromJson(
await _request.requestRpc(ServiceUrl.getBlockByBlockNumber, [blockNumber]))
.result;
}

Future<String> traceTransaction(transaction) async {
return SendTransactionRes.fromJson(await _request.requestRpc(ServiceUrl.traceTransaction, [transaction])).result;
}
//================================Pool RPC Methods===============================

Future<List<TraceTransaction>> getTraceTransaction(String transationHash) async {
return TraceTransactionRes.fromJson(await _request.requestRpc(ServiceUrl.getTransactionTrace, [transationHash]))
Future<String> sendTransaction(SendTransaction transaction) async {
return SendTransactionRes.fromJson(
await _request.requestRpc(ServiceUrl.sendTransaction, [transaction]))
.result;
}
}
3 changes: 1 addition & 2 deletions lib/ckb-rpc/service_url.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ class ServiceUrl {
static final tipBlockNumber = "get_tip_block_number";
static final localNodeInfo = "local_node_info";
static final sendTransaction = "send_transaction";
static final traceTransaction = "trace_transaction";
static final getTransactionTrace = "get_transaction_trace";
static final getBlockByBlockNumber = "get_block_by_number";
}
4 changes: 3 additions & 1 deletion lib/ckb-types/item/cell_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class CellInput {
CellInput(this.previousOutput, this.args, this.since);

factory CellInput.fromJson(Map<String, dynamic> json) => CellInput(
json['previous_output'] == null ? null : OutPoint.fromJson(json['previous_output'] as Map<String, dynamic>),
json['previous_output'] == null
? null
: OutPoint.fromJson(json['previous_output'] as Map<String, dynamic>),
(json['args'] as List)?.map((e) => e as String)?.toList(),
json['since'] as String);

Expand Down
60 changes: 33 additions & 27 deletions lib/ckb-types/item/send_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,37 @@ class SendTransaction {
List<CellOutput> outputs;
int version;

SendTransaction(this.deps, this.inputs, this.outputs, this.version);

factory SendTransaction.fromJson(Map<String, dynamic> json) =>
SendTransaction(
(json['deps'] as List)
?.map((e) => e == null
? null
: OutPoint.fromJson(e as Map<String, dynamic>))
?.toList(),
(json['inputs'] as List)
?.map((e) => e == null
? null
: CellInput.fromJson(e as Map<String, dynamic>))
?.toList(),
(json['outputs'] as List)
?.map((e) => e == null
? null
: CellOutput.fromJson(e as Map<String, dynamic>))
?.toList(),
json['version'] as int);

Map<String, dynamic> toJson() => <String, dynamic>{
'deps': deps,
'inputs': inputs,
'outputs': outputs,
'version': version
};
SendTransaction(this.version, this.deps, this.inputs, this.outputs);

factory SendTransaction.fromJson(Map<String, dynamic> json) => SendTransaction(
json['version'] as int,
(json['deps'] as List)
?.map((e) => e == null ? null : OutPoint.fromJson(e as Map<String, dynamic>))
?.toList(),
(json['inputs'] as List)
?.map((e) => e == null ? null : CellInput.fromJson(e as Map<String, dynamic>))
?.toList(),
(json['outputs'] as List)
?.map((e) => e == null ? null : CellOutput.fromJson(e as Map<String, dynamic>))
?.toList(),
);

Map<String, dynamic> toJson() =>
<String, dynamic>{'version': version, 'deps': deps, 'inputs': inputs, 'outputs': outputs};
}

class SendCellInput {
OutPoint previousOutput;
List<String> args;

SendCellInput(this.previousOutput, this.args);

factory SendCellInput.fromJson(Map<String, dynamic> json) => SendCellInput(
json['previous_output'] == null
? null
: OutPoint.fromJson(json['previous_output'] as Map<String, dynamic>),
(json['args'] as List)?.map((e) => e as String)?.toList());

Map<String, dynamic> toJson() =>
<String, dynamic>{'previous_output': previousOutput.toJson(), 'args': args};
}
17 changes: 0 additions & 17 deletions lib/ckb-types/item/trace_transaction.dart

This file was deleted.

4 changes: 4 additions & 0 deletions lib/ckb-types/item/transaction_with_status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class TransactionWithStatus {
}

class TxStatus {
final String PENDING = 'pending';
final String PROPOSED = 'proposed';
final String COMMITTED = 'committed';

String status;
String blockHash;

Expand Down
2 changes: 0 additions & 2 deletions lib/ckb-types/res_export.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export 'package:ckb_sdk/ckb-types/tip_block_number_res.dart';
export 'package:ckb_sdk/ckb-types/local_node_info_res.dart';
export 'package:ckb_sdk/ckb-types/send_transaction_res.dart';
export 'package:ckb_sdk/ckb-types/block_res.dart';
export 'package:ckb_sdk/ckb-types/trace_transaction.dart';

export 'package:ckb_sdk/ckb-types/item/cell_with_outpoint.dart';
export 'package:ckb_sdk/ckb-types/item/cell_input.dart';
Expand All @@ -31,5 +30,4 @@ export 'package:ckb_sdk/ckb-types/item/uncle_block.dart';
export 'package:ckb_sdk/ckb-types/item/block.dart';
export 'package:ckb_sdk/ckb-types/item/script.dart';
export 'package:ckb_sdk/ckb-types/item/node_info.dart';
export 'package:ckb_sdk/ckb-types/item/trace_transaction.dart';
export 'package:ckb_sdk/ckb-types/item/transaction_with_status.dart';
Loading

0 comments on commit c848d2a

Please sign in to comment.