This article covers the entire process of developing a DApp from scratch, from contract writing to deployment and launch. By learning how to build a decentralized library, developers may easily grasp the process of deploying their own decentralized applications on the TRON network.

# **Preparations**

## Install

### Nodej v10+

### TronLink

Install [Tronlink’s Chrome extension](🔗).

# **What Are We Doing**

We are building a decentralized library that contains the following functions:

  • Book borrowing

  • Book browsing

  • Book adding

Download the complete project code from [here](🔗), and run npm install to install dependencies.

# **Data Structure**

Typically, borrowers are concerned with the title, content, availability, and price of the book. On this basis, we design a structure in the contract called Book, which comprises the following properties:

We hope that the library will be able to have an easy way to find every book. To do this, we build a `bookId` attribute and a mapping relationship between `bookId` and `Book`, named `books`.

Additionally, we must keep track of each book's rental information, including the borrower and the start and end dates.

As with Book, construct a structure called `Tracking` to keep track of this data. This structure possesses the following fields:

Similarly, a mapping relationship must be established to manage each rental record:

# **Functions and Events**

We are adding fundamental functions to our library, including:

  • Add a book to the library - `addBook`

  • Borrow a book - `borrowBook`

  • Remove a book from the library - `deleteBook`

## addBook

## borrowBook

## deleteBook

We use two tool methods in the `borrowBook` method: `_sendTRX` and `_createTracking`. We do not wish for users to invoke these APIs. As a result, in accordance with Solidity's standards, we identify them as `internal`, which means they can be accessed only within the contract.

## _sendTRX

## _createTracking

The contract is done, it’s time to deploy it.

# **Deployment and Testing**

We compile and deploy contracts using [TronIDE](🔗). For easier testing, it is advised that the initial deployment utilize TRON's Nile test network.

Nile Test Net

Nile is TRON's pilot test network. Nile will be the first to launch new features that will later be launched on the main network. Please visit the [Nile's official website](🔗) for additional information.

TRX must be staked or burned to pay for energy in the deployment contract. Here you can obtain some test coins, and each address is only valid once.

The logged-in TronLink will automatically connect to TronIDE; however, please ensure that the correct network is selected:


If this is your first time using TronIDE, you must first enable two modules: Solidity Compiler and DEPLOYMENT.


Following activation, the leftmost menu bar should appear as follows:


Create a new contract file, Library.sol, in which you will paste the entire contract code.


The sample contract contained in this article must be compiled by selecting 0.8.0+commit7c2e641 from the Compiler drop-down menu.

The contract that has been successfully compiled is now ready for deployment. To ensure proper deployment, you must enter the relevant feelimit in the location indicated in the figure. Here, enter 1000000000 as the feelimit. Please refer to this page for additional details on feelimit.


Click Accept in the pop-up window to sign for this deployment.


When the contract is successfully deployed, the position indicated in the figure will indicate the methods that this contract is capable of calling.


# **Build the DApp**

To begin, copy the contract address that was previously deployed into the `libraryContractAddress` variable in utils.js.

## **Link the UI to TronLink**

The following step is to link the UI to the TronLink Chrome wallet. TronLink injects the TronWeb object into each browser page, allowing the DApp to communicate with the TRON network.

## **Functions**

After connecting our user interface to TronLink, we need to analyze how the user interface interacts with smart contracts. As a result, a contract object must be created to represent the decentralized library smart contract.

Create the following code in dapp-ui/plugins/utils.js to retrieve the smart contract object and save it to the global variable. Then you can directly interact with the contract via the global variable.

The library should have three fundamental functions:

  • Add a book

  • Browse available books

  • Borrow a book

In index.vue, call setLibraryContract() to initialize the contract object.

## **Add a Book**

To begin, construct an add book form for users to submit information about book rentals. On the back end, it will communicate with the library contract's addBook function.

Add the following code to dapp-ui/components/postAd() bookForm.vue's function:

Add the following code to postBookInfo() of dapp-ui/plugins/utils.js:

## **Browse All Available Books**

The `fetchAllBooks()` function returns the book list, which contains a list of all available books.

Add the following code to dapp-ui/plugins/fetchAllBooks() utils.js's function:

In index.vue, call `fetchAllBooks()` to retrieve book information and show it on the homepage.

## **Borrow a Book**

The user may borrow the book after viewing the book's information. In the book() function of dapp-ui/components/detailsModal.vue, add the following code:

dapp-ui/plugins/utils.js, add the following code to the borrowBook() function:

The development of the library Dapp is done.

# **Run the DApp**

Ascertain that tronLink is logged in before running the following command to start the service:

To view the front-end page, type localhost:3000 into the browser's address bar.


To post book rental information, click the "Rent Your Books" button in the upper right corner. The title of the book, a brief description of the book, and the cost of the book for one day are all included in the material.


After you've completed the form, click the "Submit" button. The information will be passed to the library contract's addBook function, which will generate a transaction that triggers the contract. Then, as illustrated below, a TronLink pop-up box will open, requesting confirmation and signature:



After successfully connecting the transaction to the chain, the following leasing information will be displayed on the page:


Click "View" to view the book's comprehensive details and to select the rental period. To initiate a lease request, click "Lent Now", then the library contract's `borrowBook` function will be called. Additionally, the leasing transaction must be signed and broadcasted to finish it.