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 install
to install dependencies. -
Customize package.json. Basically, you need to change 5 parameters of run task from
scripts
section--chain-id
is chain ID of Ethereum chain you work with.--amount-in
amount of input token you want to swap. Change according to your balance – there is no check--token-in-address
smart contract address of input token--token-out-address
smart contract address of output token--wallet-address
your 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-comments
sections 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_URL
with URL from Alchemy,WALLET_PRIVATE_KEY
by 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-matic
for 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.