I have the following code that adds some asset quantity to a user, transfers some of it to another user and then queries their asset balances:
public class App {
private static final Ed25519Sha3 crypto = new Ed25519Sha3();
private static int getAccountBalance(IrohaAPI api, String user, KeyPair userKeypair, String asset) {
QueryAPI qapi = new QueryAPI(api, user, userKeypair);
List<AccountAsset> assets = qapi.getAccountAssets(user, 1, asset)
.getAccountAssetsList();
Optional<AccountAsset> assetOptional = assets.stream()
.filter(a -> a.getAssetId().equals(asset))
.findFirst();
return assetOptional.map(a -> Integer.parseInt(a.getBalance()))
.orElse(0);
}
public static void main(String[] args) {
String adminPriv = "", adminPub = "", testPriv = "", testPub = "";
try {
adminPriv= new String(Files.readAllBytes(
Paths.get("/home/george/Misc/iroha/example/[email protected]")
));
adminPub= new String(Files.readAllBytes(
Paths.get("/home/george/Misc/iroha/example/[email protected]")
));
testPriv= new String(Files.readAllBytes(
Paths.get("/home/george/Misc/iroha/example/[email protected]")
));
testPub= new String(Files.readAllBytes(
Paths.get("/home/george/Misc/iroha/example/[email protected]")
));
} catch (IOException err) {
System.err.println("Failed reading keys: " + err.getMessage());
System.exit(-1);
}
KeyPair adminKeypair = Ed25519Sha3.keyPairFromBytes(
parseHexBinary(adminPriv),
parseHexBinary(adminPub)
);
KeyPair testKeypair = Ed25519Sha3.keyPairFromBytes(
parseHexBinary(testPriv),
parseHexBinary(testPub)
);
TransactionOuterClass.Transaction tx = Transaction.builder("admin@test")
.addAssetQuantity("coin#test", "100.00")
.transferAsset(
"admin@test",
"test@test",
"coin#test",
"Reason",
"50.00"
)
.sign(adminKeypair)
.build();
TransactionStatusObserver txObserver = TransactionStatusObserver.builder()
.onTransactionFailed(ftx -> System.out.println(String.format(
"Transaction %s failed: %s",
ftx.getTxHash(),
ftx.getErrOrCmdName()
))
)
.onError(err -> System.out.println("Failed: " + err))
.onTransactionCommitted(ctx -> {
System.out.println(ctx.getTxHash());
System.out.println("Committed");
})
.onComplete(() -> System.out.println("Completed"))
.build();
IrohaAPI api = new IrohaAPI("0.0.0.0", 50051);
api.transaction(tx)
.blockingSubscribe(txObserver);
int adminBalance = getAccountBalance(api, "admin@test", adminKeypair, "coin#test");
int testBalance = getAccountBalance(api, "test@test", testKeypair, "coin#test");
System.out.println(Integer.toString(adminBalance));
System.out.println(Integer.toString(testBalance));
}
}
As you can see from the code, I'm using the default configuration files together with the latest master branch version of Iroha, which I start in the following way:
build/bin/irohad --config example/config.sample --genesis_block example/genesis.block --keypair_name example/node0 --overwrite_ledger
At the moment I have a hard time figuring out why the queries fail validation. The initial transaction for creating and transferring the asset succeeds and I've also tried to use the Python bindings on similar code and I had no problems with that.