Complete the example
Let's add the remaining parts of our client to test different functions.
We start by defining the rest of our Book Review and Transaction (Tx) model:
type BookReview = {
book: Book;
reviewer_name: string;
};
type TxOp = {
op_name: string;
op_args: any[];
};
type TxBody = {
tx_rid: Buffer;
ops: TxOp[];
signers: Buffer[];
};
type Tx = {
tx_body: TxBody;
signatures: Buffer[];
};
We define functions to query for entities and some helper functions to read input from user:
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function getInput(query: string): Promise<string> {
return new Promise((resolve) => {
rl.question(query, (answer) => {
resolve(answer);
});
});
}
const getReviewsForBook = async (isbn: string) => {
const bookList = await client.query<BookReview[]>("get_all_reviews_for_book", { isbn: isbn });
console.log("Book review list\n", bookList);
};
const getAllTransactions = async () => {
await client.query<Tx[]>("get_transactions", {}).then((transactions) => {
transactions.forEach((tx) => {
console.log("TxRID:", tx.tx_body.tx_rid.toString("hex"));
tx.tx_body.ops.forEach((op) => {
console.log("OpName:", op.op_name);
console.log("OpArgs:", op.op_args);
});
});
});
const transactions = (await client.query("get_transactions", {})) as Tx[];
transactions.forEach((tx) => {
console.log("TxRID:", tx.tx_body.tx_rid.toString("hex"));
tx.tx_body.ops.forEach((op) => {
console.log("OpName:", op.op_name);
console.log("OpArgs:", op.op_args);
});
});
};
and finally our complete main function where we in this code example have added some more functions to show queries and transactions
async function main() {
client = await createClient({
nodeUrlPool: "http://localhost:7740",
blockchainRid: blockchainRID,
});
console.log("Creating a new book transactions");
await client.signAndSendUniqueTransaction(
{ name: "create_book", args: ["ISBN1", "Chromia 101", "John Doe"] },
bookKeeperSignatureProvider
);
await getInput("Transaction comitted!\npress any key to continue...");
console.log("Let's fetch and view all books currently in the node");
await getAllBooks();
await getInput("Press any key to continue...");
console.log("We can now add a second book");
await client.signAndSendUniqueTransaction(
{ name: "create_book", args: ["ISBN2", "Rell 101", "Jane Doe"] },
bookKeeperSignatureProvider
);
await getInput("Transaction comitted, press any key to continue...");
console.log("Let's fetch and view all books currently in the node");
await getAllBooks();
await getInput("Press any key to continue...");
console.log("We can now add two reviews for the book with ISBN = ISBN2");
await client.signAndSendUniqueTransaction(
{
name: "create_book_review",
args: ["ISBN2", "Bob Doe", "This is a great book!", 5],
},
bookKeeperSignatureProvider
);
await client.signAndSendUniqueTransaction(
{
name: "create_book_review",
args: ["ISBN2", "Charlie Doe", "It was ok!", 3],
},
bookKeeperSignatureProvider
);
await getInput("Transaction comitted, press any key to continue...");
console.log("Let's fetch and view all books currently in the node");
await getReviewsForBook("ISBN2");
await getInput("Press any key to continue...");
console.log("Now lets look at all transaction that have been commited to the blockchain");
await getAllTransactions();
await getInput("Press any key to continue...");
}
Now we have a fully functional example to showcase how simple it is to implement a front-end client using Chromia and Postchain-client