Swapping tokens on EVM-compatible blockchains: Mainnet, Polygon, testnets Goerli, Rinkeby. You can read blog post on this tutorial with detailed explanation.
To run this example:
-
npm installto install dependencies. -
Customize package.json. Basically, you need to change 5 parameters of run task from
scriptssection--chain-idis chain ID of Ethereum chain you work with.--amount-inamount of input token you want to swap. Change according to your balance – there is no check--token-in-addresssmart contract address of input token--token-out-addresssmart contract address of output token--wallet-addressyour actual wallet address – replace[YOUR_WALLET_ADDRESS].
In scripts sections you can see sample configurations for few networks (
start-matic, start-rinkeby, start-goerli). Inscripts-commentssections you'll find descriptions of each configuration. -
Create .env file and put up there the following code according to network you gonna use:
API_URL = "API_URL from https://www.alchemy.com" PRIVATE_KEY = "WALLET_PRIVATE_KEY"Replace
API_URLwith URL from Alchemy,WALLET_PRIVATE_KEYby your wallet' private key (you can get it from Metamask).(Don't publish these keys anywhere in public!)
-
Run configuration for certain network, e.g.
npm run start-maticfor Matic (Polygon) network. Please note the tutorial is for demostration purposes and it misses lots of checks.
Happy hacking!
If you have a question or something does not work, you can ask in comment section in my blog or post an issue.