Skip to content

Commit

Permalink
Merge commit '666b834a3038dd3d4b78523e57c91b4dae51dd15'
Browse files Browse the repository at this point in the history
  • Loading branch information
janaakhterov committed Mar 1, 2022
2 parents 412ff9a + 666b834 commit ba43c46
Show file tree
Hide file tree
Showing 31 changed files with 764 additions and 122 deletions.
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v2.9.0-beta.1

### Added

* `owner` field to `*Allowance` classes.
* `Executable.[set|get]GrpcDeadline()`

### Fixed

* `AccountAllowanceAdjustTransaction` now deserializes correctly with `Transaction.fromBytes()`

## v2.8.0

### Added
Expand Down Expand Up @@ -39,6 +50,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `StorageChange`
* New response codes.
* `ChunkedTransaction.[set|get]ChunkSize()`, and changed default chunk size for `FileAppendTransaction` to 2048.
* `AccountAllowance[Adjust|Approve]Transaction`
* `AccountInfo.[hbar|token|tokenNft]Allowances`
* `[Hbar|Token|TokenNft]Allowance`
* `[Hbar|Token|TokenNft]Allowance`
* `TransferTransaction.set[Hbar|Token|TokenNft]TransferApproval()`

### Fixed

Expand All @@ -53,7 +69,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* Support for regenerating transaction IDs on demand if a request
* Support for regenerating transaction IDs on demand if a request
responsed with `TRANSACITON_EXPIRED`

## v2.7.0-beta.1
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ Select _one_ of the following depending on your target platform.

```groovy
// Android, Corda DJVM, Java 7+
implementation 'com.hedera.hashgraph:sdk-jdk7:2.8.0'
implementation 'com.hedera.hashgraph:sdk-jdk7:2.9.0-beta.1'
// Java 9+, Kotlin
implementation 'com.hedera.hashgraph:sdk:2.8.0'
implementation 'com.hedera.hashgraph:sdk:2.9.0-beta.1'
```

Select _one_ of the following to provide the gRPC implementation.
Expand Down Expand Up @@ -61,14 +61,14 @@ Select _one_ of the following depending on your target platform.
<dependency>
<groupId>com.hedera.hashgraph</groupId>
<artifactId>sdk-jdk7</artifactId>
<version>2.8.0</version>
<version>2.9.0-beta.1</version>
</dependency>

<!-- Java 9+, Kotlin -->
<dependency>
<groupId>com.hedera.hashgraph</groupId>
<artifactId>sdk</artifactId>
<version>2.8.0</version>
<version>2.9.0-beta.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "com.hedera.hashgraph"
version = "2.8.0"
version = "2.9.0-beta.1"
description = "Hedera™ Hashgraph SDK for Java"

configurations {
Expand Down
91 changes: 91 additions & 0 deletions sdk/src/integrationTest/java/AccountAllowanceIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import com.hedera.hashgraph.sdk.AccountAllowanceApproveTransaction;
import com.hedera.hashgraph.sdk.AccountBalanceQuery;
import com.hedera.hashgraph.sdk.AccountCreateTransaction;
import com.hedera.hashgraph.sdk.AccountDeleteTransaction;
import com.hedera.hashgraph.sdk.AccountInfoQuery;
import com.hedera.hashgraph.sdk.Hbar;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.Status;
import com.hedera.hashgraph.sdk.TransactionId;
import com.hedera.hashgraph.sdk.TransactionRecordQuery;
import com.hedera.hashgraph.sdk.TransferTransaction;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AccountAllowanceIntegrationTest {
@Test
@Disabled
@DisplayName("Can spend hbar allowance")
// TODO: this currently is not passing, and I don't know why.
void canSpendHbarAllowance() throws Throwable {
var testEnv = new IntegrationTestEnv(1);

var aliceKey = PrivateKey.generateED25519();
var aliceId = new AccountCreateTransaction()
.setKey(aliceKey)
.setInitialBalance(new Hbar(10))
.execute(testEnv.client)
.getReceipt(testEnv.client)
.accountId;

var bobKey = PrivateKey.generateED25519();
var bobId = new AccountCreateTransaction()
.setKey(bobKey)
.setInitialBalance(new Hbar(10))
.execute(testEnv.client)
.getReceipt(testEnv.client)
.accountId;

Objects.requireNonNull(aliceId);
Objects.requireNonNull(bobId);

var allowanceTx = new AccountAllowanceApproveTransaction()
.addHbarAllowance(aliceId, new Hbar(10))
.setTransactionId(TransactionId.generate(bobId))
.freezeWith(testEnv.client)
.sign(bobKey);
System.out.println(allowanceTx);
allowanceTx
.execute(testEnv.client)
.getReceipt(testEnv.client);

var transferTx = new TransferTransaction()
.addHbarTransfer(testEnv.operatorId, new Hbar(5))
.addHbarTransfer(bobId, new Hbar(5).negated())
.setHbarTransferApproval(bobId, true)
.setTransactionId(TransactionId.generate(aliceId))
.freezeWith(testEnv.client)
.sign(aliceKey);
System.out.println(transferTx);
var transferRecord = transferTx
.execute(testEnv.client)
.getRecord(testEnv.client);

var transferFound = false;
System.out.println(transferRecord);
for (var transfer : transferRecord.transfers) {
if (transfer.accountId.equals(testEnv.operatorId) && transfer.amount.equals(new Hbar(5))) {
transferFound = true;
break;
}
}
assertTrue(transferFound);

new AccountDeleteTransaction()
.setAccountId(bobId)
.freezeWith(testEnv.client)
.sign(bobKey)
.execute(testEnv.client)
.getReceipt(testEnv.client);

testEnv.close(aliceId, aliceKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public AccountAllowanceAdjustTransaction() {
}

private void initFromTransactionBody() {
var body = sourceTransactionBody.getCryptoApproveAllowance();
var body = sourceTransactionBody.getCryptoAdjustAllowance();
for (var allowanceProto : body.getCryptoAllowancesList()) {
hbarAllowances.add(HbarAllowance.fromProtobuf(allowanceProto));
}
Expand All @@ -59,6 +59,7 @@ private void initFromTransactionBody() {

public AccountAllowanceAdjustTransaction addHbarAllowance(AccountId spenderAccountId, Hbar amount) {
hbarAllowances.add(new HbarAllowance(
null,
Objects.requireNonNull(spenderAccountId),
Objects.requireNonNull(amount)
));
Expand All @@ -72,6 +73,7 @@ public List<HbarAllowance> getHbarAllowances() {
public AccountAllowanceAdjustTransaction addTokenAllowance(TokenId tokenId, AccountId spenderAccountId, long amount) {
tokenAllowances.add(new TokenAllowance(
Objects.requireNonNull(tokenId),
null,
Objects.requireNonNull(spenderAccountId),
amount
));
Expand Down Expand Up @@ -99,7 +101,7 @@ private List<Long> getNftSerials(AccountId spenderAccountId, TokenId tokenId) {

private List<Long> newNftSerials(AccountId spenderAccountId, TokenId tokenId, Map<TokenId, Integer> innerMap) {
innerMap.put(tokenId, nftAllowances.size());
TokenNftAllowance newAllowance = new TokenNftAllowance(tokenId, spenderAccountId, new ArrayList<>());
TokenNftAllowance newAllowance = new TokenNftAllowance(tokenId, null, spenderAccountId, new ArrayList<>());
nftAllowances.add(newAllowance);
return newAllowance.serialNumbers;
}
Expand All @@ -110,7 +112,7 @@ public AccountAllowanceAdjustTransaction addTokenNftAllowance(NftId nftId, Accou
}

public AccountAllowanceAdjustTransaction addAllTokenNftAllowance(TokenId tokenId, AccountId spenderAccountId) {
nftAllowances.add(new TokenNftAllowance(tokenId, spenderAccountId, null));
nftAllowances.add(new TokenNftAllowance(tokenId, null, spenderAccountId, null));
return this;
}

Expand All @@ -129,14 +131,19 @@ MethodDescriptor<com.hedera.hashgraph.sdk.proto.Transaction, TransactionResponse

CryptoAdjustAllowanceTransactionBody.Builder build() {
var builder = CryptoAdjustAllowanceTransactionBody.newBuilder();

@Nullable
AccountId ownerAccountId = (transactionIds.size() > 0 && transactionIds.get(0) != null) ?
transactionIds.get(0).accountId : null;

for (var allowance : hbarAllowances) {
builder.addCryptoAllowances(allowance.toProtobuf());
builder.addCryptoAllowances(allowance.withOwner(ownerAccountId).toProtobuf());
}
for (var allowance : tokenAllowances) {
builder.addTokenAllowances(allowance.toProtobuf());
builder.addTokenAllowances(allowance.withOwner(ownerAccountId).toProtobuf());
}
for (var allowance : nftAllowances) {
builder.addNftAllowances(allowance.toProtobuf());
builder.addNftAllowances(allowance.withOwner(ownerAccountId).toProtobuf());
}
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.hedera.hashgraph.sdk.proto.TransactionResponse;
import io.grpc.MethodDescriptor;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -56,6 +57,7 @@ private void initFromTransactionBody() {

public AccountAllowanceApproveTransaction addHbarAllowance(AccountId spenderAccountId, Hbar amount) {
hbarAllowances.add(new HbarAllowance(
null,
Objects.requireNonNull(spenderAccountId),
Objects.requireNonNull(amount)
));
Expand All @@ -69,6 +71,7 @@ public List<HbarAllowance> getHbarAllowances() {
public AccountAllowanceApproveTransaction addTokenAllowance(TokenId tokenId, AccountId spenderAccountId, long amount) {
tokenAllowances.add(new TokenAllowance(
Objects.requireNonNull(tokenId),
null,
Objects.requireNonNull(spenderAccountId),
amount
));
Expand Down Expand Up @@ -96,7 +99,7 @@ private List<Long> getNftSerials(AccountId spenderAccountId, TokenId tokenId) {

private List<Long> newNftSerials(AccountId spenderAccountId, TokenId tokenId, Map<TokenId, Integer> innerMap) {
innerMap.put(tokenId, nftAllowances.size());
TokenNftAllowance newAllowance = new TokenNftAllowance(tokenId, spenderAccountId, new ArrayList<>());
TokenNftAllowance newAllowance = new TokenNftAllowance(tokenId, null,spenderAccountId, new ArrayList<>());
nftAllowances.add(newAllowance);
return newAllowance.serialNumbers;
}
Expand All @@ -107,7 +110,7 @@ public AccountAllowanceApproveTransaction addTokenNftAllowance(NftId nftId, Acco
}

public AccountAllowanceApproveTransaction addAllTokenNftAllowance(TokenId tokenId, AccountId spenderAccountId) {
nftAllowances.add(new TokenNftAllowance(tokenId, spenderAccountId, null));
nftAllowances.add(new TokenNftAllowance(tokenId, null, spenderAccountId, null));
return this;
}

Expand All @@ -126,14 +129,19 @@ MethodDescriptor<com.hedera.hashgraph.sdk.proto.Transaction, TransactionResponse

CryptoApproveAllowanceTransactionBody.Builder build() {
var builder = CryptoApproveAllowanceTransactionBody.newBuilder();

@Nullable
AccountId ownerAccountId = (transactionIds.size() > 0 && transactionIds.get(0) != null) ?
transactionIds.get(0).accountId : null;

for (var allowance : hbarAllowances) {
builder.addCryptoAllowances(allowance.toProtobuf());
builder.addCryptoAllowances(allowance.withOwner(ownerAccountId).toProtobuf());
}
for (var allowance : tokenAllowances) {
builder.addTokenAllowances(allowance.toProtobuf());
builder.addTokenAllowances(allowance.withOwner(ownerAccountId).toProtobuf());
}
for (var allowance : nftAllowances) {
builder.addNftAllowances(allowance.toProtobuf());
builder.addNftAllowances(allowance.withOwner(ownerAccountId).toProtobuf());
}
return builder;
}
Expand Down
12 changes: 7 additions & 5 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public final class Client implements AutoCloseable, WithPing, WithPingAll {
static final int DEFAULT_MAX_ATTEMPTS = 10;
static final Duration DEFAULT_MAX_BACKOFF = Duration.ofSeconds(8L);
static final Duration DEFAULT_MIN_BACKOFF = Duration.ofMillis(250L);
static final Duration DEFAULT_MAX_NODE_BACKOFF = Duration.ofHours(1L);
static final Duration DEFAULT_MIN_NODE_BACKOFF = Duration.ofSeconds(8L);
static final Duration DEFAULT_CLOSE_TIMEOUT = Duration.ofSeconds(30L);
static final Duration DEFAULT_REQUEST_TIMEOUT = Duration.ofMinutes(2L);

Expand Down Expand Up @@ -74,7 +76,7 @@ public final class Client implements AutoCloseable, WithPing, WithPingAll {
this.mirrorNetwork = mirrorNetwork;
}

private static ExecutorService createExecutor() {
static ExecutorService createExecutor() {
var threadFactory = new ThreadFactoryBuilder()
.setNameFormat("hedera-sdk-%d")
.setDaemon(true)
Expand Down Expand Up @@ -620,7 +622,7 @@ public synchronized Client setNodeWaitTime(Duration nodeWaitTime) {
* @return
*/
public synchronized Duration getNodeMinBackoff() {
return network.getMinBackoff();
return network.getMinNodeBackoff();
}

/**
Expand All @@ -630,7 +632,7 @@ public synchronized Duration getNodeMinBackoff() {
* @return
*/
public synchronized Client setNodeMinBackoff(Duration minBackoff) {
network.setMinBackoff(minBackoff);
network.setMinNodeBackoff(minBackoff);
return this;
}

Expand All @@ -640,7 +642,7 @@ public synchronized Client setNodeMinBackoff(Duration minBackoff) {
* @return
*/
public synchronized Duration getNodeMaxBackoff() {
return network.getMaxBackoff();
return network.getMaxNodeBackoff();
}

/**
Expand All @@ -650,7 +652,7 @@ public synchronized Duration getNodeMaxBackoff() {
* @return
*/
public synchronized Client setNodeMaxBackoff(Duration maxBackoff) {
network.setMaxBackoff(maxBackoff);
network.setMaxNodeBackoff(maxBackoff);
return this;
}

Expand Down
Loading

0 comments on commit ba43c46

Please sign in to comment.