How to use xDai in your Dapp
Deploying and onboarding users to xDai to avoid the high gas costs
Gas costs are exploding again, ETH2.0 is still too far away and people are now looking at layer 2 solutions. Here's a good overview of existing layer 2 projects: https://github.com/Awesome-Layer-2/awesome-layer-2. Today we will take a closer look at xDai as a solution for your Dapp.
What are sidechains?
Sidechains are generally EVM-compatible, separate blockchains with their own consensus models. xDai is moving towards a Proof-of-Stake mechanism using their own STAKE token on the Ethereum mainnet. Sidechains are designed to easily allow transfer of value and arbitrary state with the Ethereum chain.
+ allow you to run fully functioning smart contracts
+ arbitrary data exchange with mainchain
- security relies on small set of nodes in the sidechain
- not suitable for extremely high-value applications
Why xDai
You may have heard about xDai especially given the high current gas prices. It's a working mainnet deployed sidechain focusing on simplicity and using the stable coin DAI as underlying main currency. The current implementation is still a PoA (proof of authority) chain with selected and trusted, public validators based in the United States.
The biggest advantages of xDai are the 3 S:
1. Simplicity
At its core the functionality is very easy to use.
- Need xDai? Send Dai to bridge.
- Deploy contracts? Just set up the Poanetwork endpoint.
2. Stable
Since we are using xDai as native currency, this will be used to pay for all gas costs as well. In contrast to ETH, this means we have a stable base currency to pay the very low transactions costs.
Win win.
3. Support
Most of the times tools only support the Ethereum mainnet and maybe 1-2 testnets. However with xDai, you get support for example from:
TheGraph: An extremely useful tool to query blockchain data, see my previous tutorial here.
Blocknative: Transaction status service.
AnyBlock: Blockchain analytics service.
Existing projects on xDai
- Gnosis: xDai and Gnosis started a partnership just days ago. You can find their core protocol and multisigs deployed on xDai.
- KickBack: Event Management and RSVP accountability
- HoneySwap: Uniswap v2 Fork
- RealityCards: NFTs for prediction voting
How to deploy to xDai
Deploying to the xDai sidechain is very simple. If you're using Truffle, simply add the xDai network configuration like this:
xDai: {
provider: () => new HDWalletProvider(mnemonic, 'https://xdai.poanetwork.dev'),
network_id: 100,
confirmations: 2,
gas: 500000,
gasPrice: 1000000000
skipDryRun: true
},
}
You'll also need funds in the sidechain of course, see further below on how to use the bridge.
Wrapped xDai
Onboarding users
MetaMask
To interact with the side chain, users need to add the xDai network to their MetaMask. Click on the network selection in MetaMask and then choose 'Custom RPC'. For the current testnet, use the configuration of
- RPC URL:
https://xdai.poanetwork.dev
- ChainID:
100
- Symbol:
xDai
- Block Explorer URL:
https://blockscout.com/poa/xdai
Value transfer from and to sidechain
1. Getting the native xDai
In most cases using only this system will be enough. To move DAI between sidechain and Ethereum mainnet, there is a bridging system that is very easy to use. As a Dapp developer, you may choose between
- Forwarding users to the Bridge UI available at https://dai-bridge.poa.network/.
- Or recommended: Manually adding a bridge system to your Dapp. It's super simple, just send Dai to the bridge contract on Ethereum (
0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359
) or for the way back send xDai to the bridge contract on xDai (0x4aa42145Aa6Ebf72e164C9bBC74fbD3788045016
). And you can properly integrate this into your UI.
2. Moving ERC-20 and other tokens
To move any tokens between sidechain and Ethereum mainnet, there is a bridging system that is very easy to use. There were still warnings about it being experimental at the time of this writing, so use with care. As a Dapp developer, you may choose between
- Forwarding users to the OmniBridge UI available at https://xdai-omnibridge.web.app/.
- Manually calling the contracts. This is a bit more involved, but worth it for the best UX.
3. Arbitrary data transfer
Of course you can also transfer arbitrary data. There is naturally no UI for this, you will need to call the contracts directly.
The Future with POS
xDai is transitioning towards POS. They implemented a POSDAO system which is using a DAO to find consensus and using Randao for its randomness. Randao is essentially a commit-reveal scheme, see here. You can find the complete POSDAO whitepaper at https://forum.poa.network/t/posdao-white-paper/2208. The next phase of staking is currently actively in development. You can now start staking your STAKE at https://easy-staking.xdaichain.com/.
Given the underlying Randao, you will also be able to use this to get on-chain randomness. Once again I wouldn't use this for billion dollar projects, see the reasoning here for last-revealer problem. But it's nevertheless a great addition for when it's good enough.
Layer-2 solutions
With the rise of gas prices we will see more and more support for layer 2. It will be up to us developers to actually drive adoption and make this a reality. We have stayed comfortably on layer 1 together for too long.
Have you used xDai before? Maybe even on Mainnet? There are also grants opening again soon that you might want to check out. Or what other layer-2 solutions did you look at or would be interested to hear about next? Please let me know in the comments.
Solidity Developer