Skip to content

Connect to a Public Blockchain

The aim of this tutorial is to transform the Demo App to let it connect to an external Ethereum Geth client connected to the Ropsten public test net provided by Chainstack. We thank them for their help and generosity! Chainstack offers a rich suite of services that are complimentary to Platform 6. These two services are all you need to build a full-fledged decentralized application leveraging blockchain technology.

Step 1: Install a P6 Core instance and the Demo App

Before embarking on this journey, please make sure to carefully follow the previous tutorial Learn with the Demo App to get familiarized with the Demo App.

Before proceeding any further, you need to have an instance of P6 Core running with the Demo App. Make sure the Demo App is working properly by searching for transactions as shown below:

Search for Transactions

Step 2: Create a wallet and credit it on Ropsten

In this step we are going to create a wallet for test purposes. This wallet needs to be credited on Ropsten. Once credited, it will be used to deploy the Demo App’s smart contract and issue transactions on Ropsten.

First, go to the Scripts menu and create a new script called GenerateWallet, fill the description field as you wish. Then in the edit pane, paste the following code:

def walletPassword = "ADummyPassword"
def walletJson = p6.web3ethereum.generateNewCredential(walletPassword, true)
println(walletJson)

Warning

Do not change the password above because it’s used across the Demo App. Evidently this is bad practice, always store your secrets in a vault for an application in production. The Demo App is, as its name implies, only for demo purposes.

Save and run the script, it should output the created wallet JSON as shown below:

Generate Wallet Script

Save the JSON string in a text file on your file system.

The next step will consist in crediting the wallet you’ve just created. For that purpose, make sure the Metamask browser plugin is installed in your browser.

Once installed, click on Metamask’s logo then go to the top right menu and click on Import Account. Select the JSON File type and upload the text file you created previously as shown below:

Import Wallet JSON File in Metamask

Once created, select the new account to be used in subsequent transactions.

Account Selection in Metamask

And most importantly, select the Ropsten network in Metamask as shown below:

Network Selection in Metamask

To credit your wallet, you need to go to https://faucet.metamask.io/ then click on request 1 ether from faucet. At the bottom of the page a transaction id should appear in the transactions section, click on it to follow the execution of your transaction on Etherscan. After roughly a minute or two, the status should be Success.

Account Balance Verification in Etherscan

You can also verify that your wallet was credited in Metamask.

Account Balance Verification in Metamask

Now we are all set to switch the Demo App from using the locally running demo blockchain node to the public Ropsten network!

Step 3: Update the Demo App’s configuration

Go the Configuration menu on the left, and first edit the ethClientURL by clicking on the Edit button. Set the value to https://nd-080-674-678.p2pify.com. This is the URL of a Geth client running in Chainstack’s cloud connected to Ropsten.

Also, edit the demoWallet config entry with the JSON you saved in a text file earlier in the tutorial as shown below:

Edit App Configuration

Then, add 2 new configuration entries with the following values:

Name Value
chainstackUsername objective-lalande
chainstackPassword skied-tall-strung-brute-header-briar

The end result should look like this:

Modified App Configuration

Step 4: Update the Demo App’s scripts

Next we are going to update scripts to read the newly introduced configuration entries.

Go to the Scripts menu and edit the RFQSmartContractHelper script.

Warning

Be careful, there are 2 script resources associated to this script, select the second one on the left.

RFQSmartContractHelper Script

Replace lines 40 and 41 with the code below and save:

    def ethClientURL = context.p6.appconfig.get('ethClientURL')
    def chainstackUsername = context.p6.appconfig.get('', 'chainstackUsername')
    def chainstackPassword = context.p6.appconfig.get('', 'chainstackPassword')
    this.web3j = context.p6.web3ethereum.build(ethClientURL, chainstackUsername, chainstackPassword)

Next, you need to edit the PostInstallApplication script. Replace line 18 with the code below and save:

def ethClientURL = p6.appconfig.get('ethClientURL')
def chainstackUsername = p6.appconfig.get('', 'chainstackUsername')
def chainstackPassword = p6.appconfig.get('', 'chainstackPassword')
def web3j =  p6.web3ethereum.build(ethClientURL, chainstackUsername, chainstackPassword)

Finally, restart the Scripts service by clicking on Stop then Start in the top right corner of the screen as shown below:

Restart Service

Step 5: Update the Demo App’s routes

Go to the Routes menu and edit the BlockchainEventHandler route.

In the addRoutes segment replace line 1 with:

from("web3j://" + ethClientURL + "?address=" + contractAddress + "&operation=ETH_LOG_FLOWABLE"
            + "&username=" + chainstackUsername + "&password=" + chainstackPassword)

Then, in the preProcessingStatement segment after line 12 add:

def chainstackUsername = p6.appconfig.get('', 'chainstackUsername')
def chainstackPassword = p6.appconfig.get('', 'chainstackPassword')

Save the route and go to the Scripts menu.

Step 6: Switch to Ropsten

Go to the Scripts menu and run the modified PostInstallApplication script. It will deploy the Demo App’s smart contract on Ropsten using your fresh demo wallet.

This will take a few minutes so be patient. Once the deployment transaction is confirmed on the blockchain, the script will update the configuration entry contractAddress with the address of the newly deployed smart contract.

Therefore, you need to go to the Routes service and restart the BlockchainEventHandler route to let it load the correct smart contract address from the configuration.

Step 7: Turn off the Demo blockchain container

At this stage, your Demo App is running on the Ropsten test net! Therefore, you can safely shutdown the demobc container by running:

docker stop demobc

The Demo App automatically creates new transactions every 10 minutes. However, you can manually create new ones by running the CreateRFQTransaction script. The execution is obviously much slower than with the local demo blockchain.

You can then search for your newly created transaction in the Transactions menu. Don’t hesitate to play with the Workflow Tasks which will generate transactions on the blockchain too.

As you play with the Demo App and create new transactions, you can monitor your Ether consumption in Metamask.