28
loading...
This website collects cookies to deliver better user experience
daml new quickstart --template quickstart-java
. This command generates a new quick-start application. As a third option, pull the source code, however you will still need to deploy a bSQL instance and modify the connection parameters appropriately.IouMain.java
and make changes to pom.xml
to resolve conflicts.CREATE
DATABASE iou;
USE
iou;
CREATE BLOCKCHAIN contracts TRADITIONAL (
id UINT64 PRIMARY,
unique_identifier STRING PACKED,
issuer STRING PACKED,
owner STRING PACKED,
currency STRING PACKED,
amount FLOAT32
);
pom.xml
file and resolve any conflicting dependencies.pom.xml
will not work. Because both the Daml Application and the JDBC use different versions of protocol buffers, they must be resolved via the <dependency management>
field in the pom.xml
file. I highly recommend replacing the current pom.xml
file with the example provided instead of doing this manually.Utils.java
file in the com.daml.quickstart.iou
directory and copying the following code. This class has a single method called connect()
.package com.daml.quickstart.iou;
import java.sql.Connection;
import java.sql.DriverManager;
public class Utils {
public Connection connect() {
Connection c;
try {
// Remove brackets when specifying info
c = DriverManager.getConnection("jdbc:mdb://{your public bSQL IP address}:5461/iou?user={your bSQL username}&password={your bSQL password}");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
return null;
}
System.out.println("succesfully connected to bSQL!");
return c;
}
}
line 9
will be unique to your instance. The following information is needed:Utils.connect()
; a connection to the database in the instance is returned. This connection can then be used to send data to the contracts blockchain. The rest of the work is done in iouMain.java
.static void addContract(Connection c, Iou.Contract contract, Long id) throws SQLException {
try {
Statement stmt = c.createStatement();
String sql = String.format(
"INSERT iou.contracts VALUES(%d, \"%s\", \"%s\", \"%s\", \"%s\", %f);",
id,
contract.id.contractId,
contract.data.issuer,
contract.data.owner,
contract.data.currency,
contract.data.amount);
stmt.execute(sql);
c.commit();
} catch (SQLException e) {
e.printStackTrace();
System.exit(1);
}
}
static void archiveContract(Connection c, ArchivedEvent archivedEvent, Long id) throws SQLException {
try {
Statement stmt = c.createStatement();
String sql = String.format(
"DISCONTINUE iou.contracts (id) VALUES (%d)",
id);
stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
System.exit(1);
}
}
iouMain.java
at the beginning of the main method, the DamlLedgerClient is built and the connection is established. The code you need to add is on lines 8–9 in the snippet below. This establishes a connection to the bSQL instance.// Create a client object to access services on the ledger.
DamlLedgerClient client = DamlLedgerClient.newBuilder(ledgerhost, ledgerport).build();
// Connects to the ledger and runs initial validation.
client.connect();
// Establishes a connection to the bSQL instance
Utils u = new Utils();
Connection c = u.connect();
addContract
and archiveContract
methods in our application stream, effectively sending information to the bSQL instance every time a contract is added or archived.addContract
after the in-memory maps are updated.archiveContract
after the maps are updated.
Disposable ignore =
client
.getTransactionsClient()
.getTransactions(acsOffset.get(), iouFilter, true)
.forEach(
t -> {
for (Event event : t.getEvents()) {
if (event instanceof CreatedEvent) {
CreatedEvent createdEvent = (CreatedEvent) event;
long id = idCounter.getAndIncrement();
Iou.Contract contract = Iou.Contract.fromCreatedEvent(createdEvent);
contracts.put(id, contract.data);
idMap.put(id, contract.id);
try {
addContract(c, contract, id);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
} else if (event instanceof ArchivedEvent) {
ArchivedEvent archivedEvent = (ArchivedEvent) event;
long id =
idMap.inverse().get(new Iou.ContractId(archivedEvent.getContractId()));
contracts.remove(id);
idMap.remove(id);
try {
archiveContract(c, archivedEvent, id);
} catch (SQLException e) {
e.printStackTrace();
System.exit(1);
}
}
}
});
quickstart
directorydaml build
in terminal to generate the .dar file.
daml codegen java
mvn compile
daml sandbox .daml/dist/quickstart-0.0.1.dar
to start the the sandbox
mvn exec:java@run-quickstart
Utils.java
daml script - dar .daml/dist/quickstart-0.0.1.dar - script-name Main:initialize - ledger-host localhost - ledger-port 6865 - static-time
daml navigator server
Iou:Iou
. Issue yourself one AliceCoin by filling out the template like below and hitting "Submit".SELECT * FROM contracts WHERE id = 1;
when no records are returned.SELECT *, sys_timestamp FROM LIFETIME contracts WHERE id = 1;
this provides us with the following records:sys_timestamp
column, a built in column for all blockchains, we can note the time this contract was archived.SELECT * FROM contracts WHERE currency = "AliceCoin";
.USE master;
CHECK VALIDITY;
iou
database, run a read digest command.USE iou;
READ DIGEST iou;
SELECT owner, COUNT(*) FROM LIFETIME contracts
WHERE NOT ISNULL(owner)
GROUP BY owner;
SET TRANSACTION QUERY TIME "2021–07–28 19:27:51.131868043";
. This sets the scope of the current state back to the time specified, all queries I run after this transaction will interact with the current state as if it was at this time period.