|
1 | 1 | Minter Android Blockchain API SDK
|
2 | 2 | =================================
|
3 | 3 | [ ](https://bintray.com/minterteam/android/minter-android-blockchain-testnet/_latestVersion)
|
4 |
| -[](https://opensource.org/licenses/MIT) |
| 4 | +[](LICENSE.txt) |
5 | 5 |
|
6 | 6 |
|
7 | 7 | Minter blockchain sdk library
|
@@ -36,18 +36,141 @@ dependencies {
|
36 | 36 | }
|
37 | 37 | ```
|
38 | 38 |
|
39 |
| -## Basic Usage |
40 |
| -### Initialize it |
41 |
| -```java |
| 39 | +## Usage / Examples |
| 40 | +### 1. Initialize SDK |
42 | 41 |
|
| 42 | +Use our nodes |
| 43 | +```java |
43 | 44 | MinterBlockChainApi.initialize();
|
44 | 45 | ```
|
45 | 46 |
|
46 |
| -### Usage |
47 |
| -Look at [tests](src/tests/java/network/minter/blockchain) to see how to create and sign transactions |
| 47 | +Or you can pass you own |
| 48 | +```java |
| 49 | +MinterBlockChainApi.initialize("https://your-node.local"); |
| 50 | +``` |
| 51 | + |
| 52 | +### 2. Creating and signing transactions |
| 53 | + |
| 54 | +Transactions API uses **Builder** pattern, so it so easy to handle it. |
| 55 | + |
| 56 | +All transactions requires a valid **nonce** value. Nonce - is a number of transaction. To get valid transaction number, you should get current number via `BlockChainAccountRepository#getTransactionCount` and increment it: |
| 57 | + |
| 58 | +```java |
| 59 | +// init object with your Minter address |
| 60 | +MinterAddress myAddress = new MinterAddress("Mxccc3fc91a3d47dc1ee26d62611a09831f0214d62"); |
| 61 | + |
| 62 | +// get account repository from SDK singleton object |
| 63 | +BlockChainAccountRepository repo = MinterBlockChainApi.getInstance().account(); |
| 64 | + |
| 65 | +// send request |
| 66 | +repo.getTransactionCount(myAddress).enqueue(new Callback<BCResult<CountableData>>() { |
| 67 | + @Override |
| 68 | + public void onResponse(Call<BCResult<CountableData>> call, Response<BCResult<CountableData>> response) { |
| 69 | + BigInteger txCount = response.body().result.count; |
| 70 | + |
| 71 | + // use this incremented value as nonce to your transaction |
| 72 | + BigInteger nonce = txCount.add(new BigInteger("1")); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onFailure(Call<BCResult<CountableData>> call, Throwable t) { |
| 77 | + } |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +#### 2.1 Create "Send" transaction |
| 82 | +Calculate nonce (see above) |
| 83 | +```java |
| 84 | +// Calculated nonce |
| 85 | +BigInteger nonce = ... |
| 86 | +``` |
| 87 | + |
| 88 | +Create your private key to sign tx with it: |
| 89 | +```java |
| 90 | +PrivateKey privateKey = new PrivateKey("4c9a495b52aeaa839e53c3eb2f2d6650d892277bde58a24bb6a396f2bb31aa37"); |
| 91 | +``` |
| 92 | + |
| 93 | +or if you know only mnemonic phrase, you can do this: |
| 94 | +```java |
| 95 | +final PrivateKey privateKey = PrivateKey.fromMnemonic("your phrase must contains twenty words et cetera ..."); |
| 96 | +``` |
| 97 | + |
| 98 | +Create transaction builder and build transaction: |
| 99 | +```java |
| 100 | +Transaction tx = new Transaction.Builder(new BigInteger("1")) |
| 101 | + // optional: available for all transactions, but not useful for some transactions |
| 102 | + .setGasCoin("MNT") |
| 103 | + // here you should select what transaction you are trying to create, builder will select exact type |
| 104 | + .sendCoin() |
| 105 | + // required: coin to send |
| 106 | + .setCoin(coin) |
| 107 | + // required: value to send |
| 108 | + .setValue(10D) |
| 109 | + // required: recipient address |
| 110 | + .setTo(toAddress) |
| 111 | + // finally, build object |
| 112 | + .build(); |
| 113 | +``` |
| 114 | + |
| 115 | +Sign transaction using your private key |
| 116 | +```java |
| 117 | +TransactionSign sign = tx.sign(privateKey); |
| 118 | + |
| 119 | +// get transaction hash - this hash you'll send to blockchain |
| 120 | +String signedTransaction = sign.getTxSign(); |
| 121 | +``` |
| 122 | + |
| 123 | + |
| 124 | +So, it's easy, isn't? :) |
| 125 | + |
| 126 | +For more transaction types see `OperationType` and class `Transaction.Builder` |
| 127 | + |
| 128 | +Now we'll send transaction to blockchain. |
| 129 | + |
| 130 | +#### 2.2 Send "send" transaction to the Minter blockchain |
| 131 | + |
| 132 | +To send transaction to blockchain, we need to get `BlockChainAccountRepository` from `MinterBlockChainApi` |
| 133 | + |
| 134 | +```java |
| 135 | +BlockChainAccountRepository accountRepo = MinterBlockChainApi.getInstance().account(); |
| 136 | +``` |
| 137 | + |
| 138 | +To send transaction, you just need to call http request |
| 139 | +```java |
| 140 | +TransactionSign sign = ... |
| 141 | +accountRepo.sendTransaction(sign).enqueue(new Callback<BCResult<TransactionSendResult>>() { |
| 142 | + @Override |
| 143 | + public void onResponse(Call<BCResult<TransactionSendResult>> call, Response<BCResult<TransactionSendResult>> response) { |
| 144 | + // handle send result |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void onFailure(Call<BCResult<TransactionSendResult>> call, Throwable t) { |
| 149 | + // handle send error |
| 150 | + } |
| 151 | +}) |
| 152 | +``` |
| 153 | + |
| 154 | +That's all! |
| 155 | + |
| 156 | + |
| 157 | +## Documentation |
| 158 | + |
| 159 | +Javadoc available in code and in *.jar file at the bintray |
| 160 | + |
| 161 | +## Build |
| 162 | +TODO |
| 163 | + |
| 164 | +## Tests |
| 165 | +TODO |
| 166 | + |
| 167 | +## Changelog |
| 168 | + |
| 169 | +See [Release notes](RELEASE.md) |
| 170 | + |
| 171 | + |
| 172 | +## License |
48 | 173 |
|
49 |
| -## Docs |
50 |
| -TODO (tests and javadocs available for now) |
| 174 | +This software is released under the [MIT](LICENSE.txt) License. |
51 | 175 |
|
52 |
| -# Build |
53 |
| -TODO |
| 176 | +© 2018 MinterTeam < [email protected]>, All rights reserved. |
0 commit comments