How WeTrust Works Under the Hood Part 2: Frontend and Backend Deep Dive

WeTrustLeonD
WeTrust Blog
Published in
5 min readFeb 8, 2017

--

This is the second of a four part series discussing the technology powering the WeTrust platform.

For the Overview in Part 1, click here.

Next week, we’ll discuss security features implemented in the WeTrust platform.

After that, we’ll discuss the decisions guiding how the smart contract was implemented.

In normal operation, the WeTrust front-end GUI communicates with the Smart Contract using 7 different API calls. These API calls can be issued directly with function calls in the web3.js JavaScript library, or with the simple easy-to-use WeTrust GUI interface. Five of these API calls are used by all of the participants in the Trusted Lending Circle, and the other two are used only by the Trusted Lending Circle foreperson. The two functions available only to the Trusted Lending Circle foreperson are:

startRound(): Start a new Trusted Lending Circle round. The first time this is called must be after the predefined start date, and subsequent calls must have at least the specified round period between them, else they will throw.

retrieveSurplus(): One month after the end of a Trusted Lending Circle, after everyone has had an opportunity to collect their funds, if there are any remaining in the contract the foreperson can call this function to retrieve them. This is partially a safeguard against participants being unable to access their accounts, where the foreperson can redistribute the funds; but also ensures that funds aren’t being left in the contract for ever.

In addition, there are five functions used by both the Trusted Lending Circle foreperson and the other members of a Trusted Lending Circle. These are:

contribute(): Allows the user to contribute funds to the Trusted Lending Circle, using the pre-specified cryptocurrency (currently only Ether but other cryptocurrencies will be supported soon!)

bid(): Allows the user to bid in the reverse auction for receiving the current round of Trusted Lending Circle funds

withdraw(): Allows the user to withdraw positive balance funds from the Trusted Lending Circle. This positive balance could be due to receiving the pot for a given Trusted Lending Circle round, receiving discount from someone else winning a round, or from over-contributing to a previous Trusted Lending Circle round.

getParticipantBalance(): Returns the amount the user is in credit to the Trusted Lending Circle (the amount the contract would allow them to withdraw at this time).

getContractNetBalance(): Returns the total contract balance the Trusted Lending Circle participants have access to, minus any fees reserved for WeTrust

In addition to these API calls that are available to WeTrust users, there is a function call that is used only by the WeTrust team during normal operation:

retrieveFees(): This function call allows the WeTrust team to collect the fee (0.3% or less, depending on transaction volume) for providing the platform for the Trusted Lending Circles.

The user can call API functions directly, but in that case they must be aware of their private key, and know many details of how the Ethereum network operates. In order to make participating in Trusted Lending Circles as easy as possible for our users, WeTrust has created a straightforward frontend that even people who don’t understand the details of blockchain can use.

The frontend initiates transactions in which data or funds are sent to the Smart Contract (i.e. contributions, bids, and withdrawals). As the frontend requires a Web3 compatible browser, the easiest way to use it is with the MetaMask chrome plugin, or the Mist browser. Both of these handy tools will require you to sign transactions before they go to the Trusted Lending Circle Smart Contract; MetaMask only requires a click, whereas Mist requires you to enter your password each time.

For an example of the front-end in action, check out this video from WeTrust’s very own Tom Nash:

The backend tries to make as few assumptions about the Trusted Lending Circle state as possible. Most transactions to the contract trigger solidity events, which the backend captures and uses to update the Trusted Lending Circle state. This flow was discussed in Part 1, and the diagram showing the flow is repeated below.

The backend is implemented using express.js, and listens to each smart contract through Web3 event listeners. Whenever a change in the state of a smart contract is detected, the event listener responds by updating the mirrored Smart Contract state in the backend server. The servers themselves are maintained by the WeTrust team and sit in the Microsoft Azure cloud.

A major benefit of the backend server is that it abstracts away many of the complex Solidity method calls needed to check the status of the user account and the smart contract, allowing the user to access their Trusted Lending Circle information much faster. The backend server also allows the foreperson to deploy a smaller contract onto the Ethereum network, since complex retrievals of data from a smart contract are notoriously difficult. Using the backend to deploy a contract allows the system to use an event based model, rather than having to create a number of getter functions. This in turn allows the foreperson to save Ether when deploying the contract by reducing the amount of Ether “gas” needed to deploy the compiled contract.

In addition, the backend will verify any new Trusted Lending Circles that are deployed on through the frontend, to make sure that the deployed Trusted Lending Circle matches the bytecode of WeTrust’s compiled contract. That way, you will know that you are using the latest version of a contract that has been security-audited. In the event that a different contract to WeTrust’s verified contract is deployed, we show a warning to all participants when they view the Trusted Lending Circle. Finally, the backend server allows WeTrust to send email notifications and reminders to Trusted Lending Circle participants, informing them when a new round starts and when they have been outbid.

To learn more about how WeTrust is using the blockchain to innovate with savings, credit, and insurance, follow the WeTrust blog or join our slack here!

--

--