{"id":20433105,"url":"https://github.com/anataliocs/avax-dex-example","last_synced_at":"2026-05-16T06:38:36.819Z","repository":{"id":82966458,"uuid":"545547441","full_name":"anataliocs/avax-dex-example","owner":"anataliocs","description":"Create a minimal DEX, approve the contract, bootstrap liquidity, deposit to the LP and swap an ERC-20 token for ETH.","archived":false,"fork":false,"pushed_at":"2022-10-06T03:25:33.000Z","size":23880,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-05T06:20:22.949Z","etag":null,"topics":["javascript","solidity","truffle"],"latest_commit_sha":null,"homepage":"https://infura.io","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anataliocs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-04T15:03:12.000Z","updated_at":"2023-10-11T18:02:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"4b579525-c150-4936-97c2-7aaaf987be15","html_url":"https://github.com/anataliocs/avax-dex-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/anataliocs/avax-dex-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anataliocs%2Favax-dex-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anataliocs%2Favax-dex-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anataliocs%2Favax-dex-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anataliocs%2Favax-dex-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anataliocs","download_url":"https://codeload.github.com/anataliocs/avax-dex-example/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anataliocs%2Favax-dex-example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33092859,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-16T04:41:52.686Z","status":"ssl_error","status_checked_at":"2026-05-16T04:41:52.009Z","response_time":115,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","solidity","truffle"],"created_at":"2024-11-15T08:17:43.298Z","updated_at":"2026-05-16T06:38:36.804Z","avatar_url":"https://github.com/anataliocs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Avalanche DEX example\n\nAvalanche DEX Example\n\n- [Sign up for a free account with Infura](https://infura.io/register?utm_source=github\u0026utm_medium=devcommunity\u0026utm_campaign=2022_Jul_devrel-sample-projects_content_content).\n- [Getting Started with Infura](https://blog.infura.io/post/getting-started-with-infura-28e41844cc89)\n\n## Module 1: Setup\n\nSetting up your local development environment.\n\n### Prerequisites\n\nBasic Setup:\n\n- [NodeJS](https://nodejs.org/en/) version 16 or above\n- [Visual Studio Code](https://code.visualstudio.com/)\n- [Git Bash](https://git-scm.com/downloads)\n- [Bash on VS Code](https://www.shanebart.com/set-default-vscode-terminal/)\n\nAdd env config:\n\n```bash\nnpx yarn add dotenv\n```\n\nAdd the Infura credentials:\n\n```text\n# Secret recovery phrase - NEVER EVER SHARE\nMNEMONIC= Add your 12 word secret phrase to access your assets on Ethereum. Never share these! Wrap in qoutations.\n\n# Infura Project details\nINFURA_PROJECT_ID= Add your secret here. (No qoutations)\nINFURA_PROJECT_SECRET=Add your ID here. (No quotations)\n\n```\n\n### MetaMask Setup\n\nFirst, install MetaMask on your browser. If you are already a MetaMask user, it's suggested to create a new browser profile for development purposes and install a separate instance of MetaMask.\n\nSee this article to create a [new Chrome profile](https://pureinfotech.com/add-new-user-profiles-google-chrome/), [Firefox](https://www.wikihow.com/Create-a-Firefox-Profile) or [Brave Browser](https://community.brave.com/t/brave-multi-user-accounts/119425).\n\nGo to [https://metamask.io/download/](https://metamask.io/download/)] and choose your browser.\n\nSet up your wallet. **Remember to save your secret recovery phrase in a secure location**. Due to how blockchains are created, the secret recovery phrase CAN NOT be reset. Since MetaMask is a non-custodial wallet, they do not hold a copy for you.\n\n### Infura Setup\n\nNext, you will need to [set up a free account with Infura](https://infura.io/register?utm_source=github\u0026utm_medium=devcommunity\u0026utm_campaign=2022_Jul_devrel-sample-projects_content_content).\n\n![infura-signup.png](img/infura-signup.png)\n\nNext, select a project. We will create two projects.\n\nFirst, select Ethereum project.\n\n![create-new-project.png](img/create-new-project.png)\n\nChoose the Rinkeby Test Network. It' easier to view test net NFTs on this network via OpenSea.\n\n![eth-creds-infura-rinkeby](img/eth-creds-infura-rinkeby.png)\n\nAccess your credentials. The project ID can be akin to your username, and the project secret a password.\n\n![eth-creds-infura](img/eth-creds-infura.png)\n\nNext, create a new project and choose IPFS.\nYou will save both these credentials into an .env file.\n\n![ipfs-creds-infura](img/ipfs-creds-infura.png)\n\n\n#### Install hdwallet-provider\n\nNext, let's add the hierarchical deterministic wallet (HD Wallet). `hdwallet-provider` is a separate package that holds our keys and signs transactions for addresses derived from a 12 or 24-word mnemonic.\n\nNote: that Infura does not manage your private keys. So, it cannot sign transactions on your behalf.\n\n```bash\nyarn add @truffle/hdwallet-provider\n```\n\n#### OpenZeppelin Contracts\n\nNext up, let's add the OpenZeppelin Contracts. OpenZeppelin is a library for secure smart contract development. It allows developers to build on a solid foundation of community-vetted code.\n\nThis is important because smart contracts can hold enormous amounts of value and are immutable.\n\n```bash\nnpx yarn add @openzeppelin/contracts\n```\n\n#### Truffle config\n\nNow, let's configure our Truffle set up. This will allow us to connect Truffle to Infura and access the Ethereum Network.\n\nThis particular network, `Rinkeby`, is test network. Test networks are used to deploy contract for testing for free. This allows developers to experiment with contracts conditions that mirror the Ethereum Mainnet where Ether is worth real money.\n\nOpen `truffle.config.js` and modify `truffle-config.js` with the following code:\n\n```javascript\n// add at the top of truffle-config.js\n\nrequire(\"dotenv\").config(); // allows usage of .env file to store secrets\nconst HDWalletProvider = require('@truffle/hdwallet-provider'); // holds secret mnemonic for your Ethereum address\nconst infuraURL = 'https://rinkeby.infura.io/v3/' + INFURA_PROJECT_ID; // end point to join network\nconst mnemonic = process.env.MNEMONIC;\n\n//...\n// inside networks value\nnetworks: {\n    rinkeby: {\n      provider: () =\u003e new HDWalletProvider(mnemonic, infuraURL),\n      network_id: 4, // Rinkeby's id\n      gas: 5500000, // Rinkeby has a lower block limit than mainnet\n      confirmations: 2, // # of confs to wait between deployments. (default: 0)\n      timeoutBlocks: 200, // # of blocks before a deployment times out  (minimum/default: 50)\n      skipDryRun: true, // Skip dry run before migrations? (default: false for public nets )\n    },\n},\n//...\n// inside compilers\n compilers: {\n    solc: {\n      version: '0.8.13'\n    }\n};\n```\n\n#### Fund Deployment Account\n\nGo to [faucet.avax.network](https://faucet.avax.network/) and add your address to claim Fuji AVAX.\n\n## Module 3: Deployment\n\nNow we can deploy our contract to Fuji Avalanche test network!\n\n```bash\ntruffle migrate --network fuji\n```\n\n### Update .env file\n\nLet's update our `.env` file:\n\n```text\n# Address of the deployed smart contract\nCONTRACT_ADDRESS=0x47DC746F41c5dB584e5A6ccf15c2c161560cD0F7\n```\n\n## Module 4: Interacting with the DEX\n\nNow let's work with our deployed dex and ERC-20 token contracts to:\n- Approve the contract\n- Bootstrap liquidity\n- Deposit to the Liquidity Pool(LP)\n- Swap our token for ETH\n\n#### Approve the DEX contract\n\nRun the following script to approve the ERC-20 token on the DEX.\n\n```bash\nnpx truffle exec scripts/approveContract.js --network fuji\n```\n\nThis script will create a web3.js Contract object for the deployed ERC-20 token contract, `AvaxTestToken.sol` that calls the approve() function `contractToken.methods.approve` \nand approves the deployed Dex contract to spend the ERC-20 token, Avax Test Token, in your wallet, up to the specified allowance.  This should emit an event which is\nthen logged out to console.\n\n```bash\nEvent: Approval\nOwner: 0xdf997dd8d5ecb45f4568bEF6791B0E59c2A51886\nSpender: 0x726eD59088fcB5874d2d0cB4e458D326755CCF27\nValue: 10000\n\nAvaxTestToken balance: 99987.434999733274405172\nDex Allowance: 10000\n```\n\n#### Bootstrap liquidity on the DEX contract\n\nRun the following script to bootstrap the DEX with an initial pool of liquidity.\n\n```bash\nnpx truffle exec scripts/initDex.js --network \n```\n\nThis script will create a web3.js Contract object that calls the `payable` init() function `contractDex.methods.init` on the deployed Dex contract, \n`Dex.sol` and bootstraps the Dex contract with initial liquidity consisting of 1-to-1 ERC-20 token, Avax Test Token, and ETH.  This should emit an event which is\nthen logged out to console.\n\n```\nTransaction Hash: 0x3613dcb9531c469ab2d1a7bd8d0888ef1a09377cb62576f24d2586cf2c7e905e\n\nDex Liquidity: 1\n```\n\n#### Deposit to the LP on the DEX contract\n\nRun the following script to deposit your token pair TAVAX/ETH to the DEX LP.\n\n```bash\nnpx truffle exec scripts/depositLP.js --network \n```\n\nThis script will create a web3.js Contract object that calls the `payable` deposit() function `contractDex.methods.deposit` on the deployed Dex contract,\n`Dex.sol` and deposits to the existing TAVAX/ETH LP with a 1-to-1 ratio of ERC-20 token, Avax Test Token, and ETH.  This should emit a `LiquidityProvided`\nevent which is then logged out to console.\n\n```\nEvent: LiquidityProvided\nLiquidity Provider: 0xdf997dd8d5ecb45f4568bEF6791B0E59c2A51886\nTAVAX Deposited: 1.251485871078270025 TAVAX\nETH Deposited: 1 ETH\n\nDex Liquidity: 13.754457613234810075\n```\n\n#### Swap ETH for TAVAX\n\nRun the following script to swap ETH for TAVAX using the DEX.\n\n```bash\nnpx truffle exec scripts/swapEthForTavax.js --network \n```\n\nThis script will create a web3.js Contract object that calls the `payable` ethToToken() function `contractDex.methods.ethToToken` on the deployed Dex contract,\n`Dex.sol` and uses the existing TAVAX/ETH LP to swap ETH for the ERC-20 token, Avax Test Token.  This should emit a `EthToTokenSwap`\nevent which is then logged out to console.\n\n```\nEvent: EthToTokenSwap\nSwapper: 0xdf997dd8d5ecb45f4568bEF6791B0E59c2A51886\nTx Details: Eth to Balloons\nETH Input: 1\nToken Output: 1.437382812323980889\n\nDex Liquidity: 13.754457613234810075\n\nETH Balance(After Swap): 975.941305858221052478\nAvaxTestToken balance(After Swap): 99984.154906454427853183\n```\n\nCongrats! You have just created a liquidity pool(LP) in a dex, deposited into a token/ETH pair on the dex and swapped your token for ETH and vice versa!  You have taken some\nbig steps in your DeFi developer journey\n\n## Next Steps\n\n[Getting Started with Infura](https://testnet.snowtrace.io/)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanataliocs%2Favax-dex-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanataliocs%2Favax-dex-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanataliocs%2Favax-dex-example/lists"}