Fullstack dApp Integration
Learn how to write a full stack decentralized React application utilizing a Secret smart contract and Secret.js
Last updated
Learn how to write a full stack decentralized React application utilizing a Secret smart contract and Secret.js
Last updated
Yao's Millionaires' problem is a secure multi-party computation problem introduced in 1982 by computer scientist and computational theorist Andrew Yao. The problem discusses two millionaires, Alice and Bob, who are interested in knowing which of them is richer without revealing their actual wealth.
The Secret smart contract we will be working with demonstrates an example implementation that allows two millionaires to submit their net worth and determine who is richer, without revealing their actual net worth.
This is the source code for the full stack application, and you can use the live dApp at this web address on Secret testnet.
To interact with the dApp, you will need to have the Secret Testnet (pulsar-3) configured with your Keplr wallet and also fund it with testnet tokens. Learn how to configure and fund your keplr wallet here!
In this demo you will learn how to integrate the Secret Millionaire contract with a front end designed in React using Secret.Js. Let's get started!
This tutorial assumes that you've already learned how to compile, upload, and instantiate a Secret smart contract to the Secret testnet and now you will learn how to connect your instantiated smart contract to a frontend library, namely, React.js.
Excluding the instantiation message, the Secret Millionaire smart contract is capable of executing three messages:
Submit net worth
Reset net worth
Query net worth
Thus, we need to design our frontend in an intuitive manner that will allow the user to execute these messages. By the end of this tutorial you will learn how to do the following:
Integrate Keplr wallet with React.js
Use secret.js to execute multiple transactions simultaneously (submit and reset net worth)
Use secret.js to query the updated Secret Millionaire smart contract
In order to execute the Secret millionaire contract, users must first be able to connect to their Keplr wallet. To see a generalized approach to connecting to Keplr wallet with Secret.js, you can review the Secret.js docs here. However, for our application you will learn how to connect to Keplr with React.js so that the user's wallet can control every page of your app.
Before we proceed, review the ConnectWallet(
)
function:
This function awaits SetupKeplr()
, an asynchronous function which enables Secret Network on Keplr, retrieves the user's account information from Keplr, creates a Secret Network client with this information, and then sets the secretjs
instance and secretAddress
with these details so that we can then share this data with the rest of our application. Notice that when we establish the Secret Network client with Secret.js we are also specifying the url
and chainId
of the client, which in this case is the url
+ chainId
for Secret testnet:
In fewer than 100 lines of code, you have the functionality to connect and disconnect a Keplr wallet and can now use this data in every other part of any dApp you choose to create.
See the completed file here. CreateContext() is the React.js hook that allows us to share the Secret Network client (aka the user's wallet address) throughout the entire application.
Now that a user can connect to our front end, let's write some functions with Secret.js that execute the submit_net_worth()
and reset_net_worth()
functions.
The Secret Millionaire contract is designed such that when a millionaire's net worth is submitted, the contract saves the first two entries and then returns an error for subsequent attempts until a reset. The reset operation resets the contract's state back to its initial condition, where no millionaires are registered.
Because the Secret smart contract requires two millionaires to be submitted at any given time, it would be an improved UI experience if the user could submit two millionaires simultaneously, rather than having to execute two separate transactions. We are able to implement this functionality with Secret.js using MsgExecuteContract and the broadcast
method:
This function takes in two objects, each representing a millionaire, and sends their name and net worth to the Millionaire smart contract using the broadcast
method of the secretjs
library. This method accepts an array of transactions to send to the blockchain and an options object. In this case, the options object specifies a gas limit of 300,000, which is the maximum amount of computational work the transactions are allowed to perform before they are halted. Now let's use React.js to fire this function every time a user clicks on a button.
The handleSubmit
function is a handler for a form submit event in our React application. This function takes user inputs (names and net worths of two millionaires), submits this data to the blockchain, queries the richer millionaire, and updates the UI accordingly:
e.preventDefault();
: This line stops the default form submission event in a web page (which would typically reload the page).
setMillionaire1({ name: name1, networth: networth1 });
and setMillionaire2({ name: name2, networth: networth2 });
: These two lines update the states of Millionaire1
and Millionaire2
respectively. setMillionaire1
and setMillionaire2
are state-setting functions from the useState React hook.
await submit_net_worth({ name: name1, networth: networth1 }, { name: name2, networth: networth2 });
: This line calls the submit_net_worth
function described above, passing two objects containing the names and net worths of two millionaires. It then waits for the function to finish.
await query_net_worth(myQuery);
: This line calls the query_net_worth
function and waits for the function to finish.
setRicherModalOpen(true);
and setShowRicherButton(false);
: These two lines update the states controlling whether a modal and a button are displayed in the UI. They hide a button and show a modal that presents the result of the query_net_worth
function.
alert("Please approve the transaction in keplr.");
: If any error is thrown during the execution of the function, it is caught, and an alert is shown to the user instructing them to approve the transaction in Keplr.
resetSubmit
resets the smart contract state back to 0:
Lastly, we need to be able to query the result of submitted transaction, namely, which millionaire is richer. This function queries the Millionaire contract to get information about who is richer among the previously submitted millionaires, stores the result in the myQuery
array, and then we can display information stored in myQuery
in our front end.
First, we use the queryContract
method of the compute
module from secretjs
library to send a query to the Millionaire contract. This method takes an object as its argument with the following properties:
contract_address
: The address of the smart contract to which the query is being sent.
query
: The query message to be sent to the contract. In this case, it's calling the who_is_richer
method of the contract, which returns information about the richer of the two millionaires.
code_hash
: The code hash of the contract to ensure that the contract code hasn't been tampered with.
The result of this query is stored in the query
variable.
The result of the query is then pushed onto the myQuery
array. This array stores the results of all queries made so far, and we can display this on our React front end.
You now have all of the tools you need to create a decentralized full stack application on Secret Network. In this tutorial you learned how to write React.js functions to connect to Keplr wallet, submit simultaneous transactions and reset net worth data, and query a Secret smart contract for the wealthier party. If you have further questions as you continue to develop on Secret Network, please join the weekly Monday developer call on Discord at 1pmET.