Given the recent rise of the Avalanche blockchain I decided to write a short tutorial on how to interact with Dapps on Avalanche programmatically. Luckily, Avalanche is EVM compatible and those who are familiar with ethereum development (be it onchain contract writing or web3 interactions with deployed contracts) should have a fairly easy transition to dealing with Avalanche. The code for this example is available here: https://github.com/codista/AvaTrading.
In this article I’ll go over the main differences/things to consider when working with Avalanche.
Due to the similarities between Avalanche and Ethereum, it is possible to use common ethereum environments (Truffle, Hardhat) for Ava development. My example is based on Hardhat, and frankly the only adjustments required were in setting up the right networks on the hardhat config file. A good resource to get started with setting up hardhat for avalanche is this repository on Ava’s account: https://github.com/ava-labs/avalanche-smart-contract-quickstart (which I used myself for the config file modifications). Avalanche offers a (currently) free API service equivalent to the likes of Infura, Alchemy and other paid Node access providers on Ethereum. The API address for mainnet is https://api.avax-test.network and for mainnet: https://api.avax.network . These APIs enable interaction with all three Avax chains (C, X and P) which are described in the following section.
C-Chain, X-Chain and P-Chain
One of the more notable differences between Ava and Ethereum is the split chain model of Ava. On Avalanche there are separate chains for smart contracts (C-Chain), Asset transfers (X-Chain) and governance/staking (P-chain). For most Dapp related purposes the relevant chain is C-chain, which is the one that’s most similar to Ethereum. The X-Chain is required for transfers of Avax (Ava’s native token) from onramp exchanges.
The Etherescan equivalent in Ava is called Avalanceh Explorer (https://cchain.explorer.avax.network/) and offers a fairly similar set of features. You can lookup contracts, addresses and transactions, interact with contracts and find their ABIs and source code where available in the same way as in Etherscan.
Metamask can be used to access and interact with tokens on the C-Chain (setup is similar to any other custom network i.e. BSC etc.). Ava’s proprietary online wallet (https://wallet.avax.network/) is required for transfers between the different chains (P,X,C), staking etc.
The second is Avash (https://docs.avax.network/build/tools/avash) which is a shell execution utility to deploy and manage Avalanche nodes.
The Arbitrage Example
The code demonstrates a basic arbitrage bot checking for arbitrage opportunities between the main Avalanche ecosystem dexes: Pangolin (Link) and TraderJoe (link). It can be configured to work with any two pairs that exist on both dexes and with minimal effort, with other Ava dexes as well. Luckily, most Ava dexes follow the same design principles as Uniswap (a polite way of saying “clones of”) and the example code leverages this fact to use a generic Router contract for quotes or swaps.
Note that some of the data (i.e. price of the two tokens in terms of Avax) are hardcoded and should be manually updated (the information can be retrieved relatively easily with CoinMarketCap js APIs such as “coinmarketcap-api”.
Another disclaimer is that this code was tested on a mainnet fork only, not on mainnet (for lack of time mainly, as the cost of transacting with Avalanche mainnet at this stage is still marginal). While a mainnet fork gives a realistic as possible glimpse of how the code would behave on mainnet, there are always surprises when deploying on mainnet which should be taken into account.
I hope this helps kickstart some Avalanche projects and intend to expand this example over time.