RSKSwap is a decentralized exchange to automatically exchange ERC20 tokens, there is no need to wait for a buyer or seller. It has the advantage of the low costs and security of the RSK network. It also doesn’t require KYC and has no limits for withdrawals or deposits.
In this post I will present a step-by-step guide to create your own token and exchange it in RSKSwap automatically, using smart contracts.
You can read the RSKSwap Documentation to learn more about it.
Here is a summary of the steps we will take in this tutorial:
We will do these steps:
To learn more about Remix and Metamask, go to the tutorial how to use Remix and Metamask to deploy smart contracts on the RSK Testnet .
Metamask
Go to metamask.io and install it.
Remix
To use Remix, visit remix.ethereum.org
Connect MetaMask to RSK testnet
Fill in the fields with the information below:
After configuring it, close the window and select the RSK Testnet.
You can get some Testnet R-BTC at faucet.testnet.rsk.co
Go to remix.ethereum.org
Solidity compiler
In the third button on the left side, click Solidity compiler
It is useful to enable automatic compilation (auto-compile), to compile smart contracts automatically when editing in Remix.
First of all, be sure that you selected the RSK network in Metamask.
Then, in Remix, on the left side, locate the fourth button - Deploy and run transactions..
In Environment, choose Injected Web3
Injected Web3 connects Remix with active accounts in Metamask
A Metamask popup window will open to authorize the connection.
First, select the account and click the Next button. You probably only have one, and in the other window, click the Connect button
In Remix, in Deploy and run transactions, look at the Custom Network.
ChainID 31 was defined at RSK Testnet custom network in Metamask.
Copy and paste this token:
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
contract TokenLaBitconf is ERC20 {
constructor(uint256 initialSupply) public ERC20("Token La Bitconf 2020", "TLB") {
_mint(msg.sender, initialSupply);
}
}
If you enabled auto-compile, the smart contract is already compiled and a green light will appear next to the third button from the left - Solidity compiler.
If you didn't enable it:
In the left side panel, go to the fourth button - Deploy and run transactions.
For now we have only one smart contract, so it is automatically selected in the dropdown.
We are going to create 1000 tokens, with 18 decimal places: 1000000000000000000000
For easy conversions, I recommend you use: eth-converter.com
Click the Deploy button.
It will open a Metamask popup window, to confirm the transaction to create the smart contract TokenLaBitconf.sol
Click on confirm.
At bottom right, we can check the message: creation of TokenLaBitconf pending...
When the transaction was confirmed, copy the token address
For example, mine is:
0x2346dDF3D362472A80cF18Ab288594F3204Fb415
Now we are going to interact with RSKSwap's main smart contract, which is the UniswapV2Router02
RSK - UniswapV2Router02 testnet / mainnet address:
0xf55c496bb1058690DB1401c4b9C19F3f44374961
Interface UniswapV2Router02
We must have an interface with the functions of UniswapV2Router02 to use in this tutorial.
Copy and paste this example:
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
// Source: https://github.com/Think-and-Dev/rskswap-periphery/blob/master/contracts/UniswapV2Router02.sol;
abstract contract InterfaceRouter {
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable virtual returns (uint amountToken, uint amountETH, uint liquidity);
address public WETH;
//Si quiero comprar tokens X, ¿cuándo tengo que enviar tokens Y?
function getAmountsIn(uint amountOut, address[] calldata path)
external view virtual returns (uint[] memory amounts);
function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
external
payable
virtual
returns (uint[] memory amounts);
}
In the right side of At Address button, paste the UniswapV2Router02 address:
Now I have two contracts in the deployed contracts section:
Using the user interface of RSKSwap in app.rskswap, we can do simulations and follow along. Click on Connect to a wallet, choose the Metamask wallet, approve the connection on Metamask pop-up window, then go to Pool.
Do these steps:
You will receive the message No pool found.
Do NOT create the pool here because we will do it programmatically.
To add liquidity to Uniswap, you always need to match it with two pairs.
Let’s create a liquidity pool to exchange our token with tRBTC.
You are the first liquidity provider. The ratio of tokens you add will set the price of this pool.
For example:
We must do 2 steps to add liquidity:
Approve Router to Spent Token
In the token, we will approve RSKSwap in order to use 100 tokens.
0xf55c496bb1058690DB1401c4b9C19F3f44374961
These are the parameters in Approve function:
"0xf55c496bb1058690DB1401c4b9C19F3f44374961","100000000000000000000"
Then click the Approve / Transact button
Add Liquidity using Remix
In the InterfaceRouter interface, expand the addLiquidityETH button.
Complete with the following parameters:
I use the site unixtimestamp to know the current timestamp. To add 5 minutes, use + 300 seconds.
Here is my example:
0x1b1E4115dFD9352c3E2f6EFB289554C5Ee0560CD
These are the parameters in addLiquidityETH function:
"0x2346dDF3D362472A80cF18Ab288594F3204Fb415","100000000000000000000","100000000000000000000","1000000000000000","0x1b1E4115dFD9352c3E2f6EFB289554C5Ee0560CD","1610316260"
BEFORE you press the transact button, fill in the amount of RBTCs (ETHs) to send at the top of the form. We will send 0.001 RBTC (Ether)
Click the button Transact or addLiquidityETH and confirm the transaction on Metamask.
Check the RSKSwap app
In the RSKSwap app, go to Pool, you should probably find your pool now!
The address of the WETH ERC20 token can change from one network to another, but it is saved in a public variable of UniswapV2Router02 contract and we can query it in the interface.
So the WRBTC = WETH is
0x09B6Ca5E4496238a1F176aEA6bB607db96C2286E
.Let's get quotes and prices in RSKSwap programmatically.
If I would like to buy tokens, how many WETH (wRBTC) do I need to send? To know this I will use the getAmountsIn function, with the parameters:
This will calculate the amount you need to provide, so you will provide some RBTCs / ETHs (or wRBTC / wETH, which are automatically converted), since you want to receive an amount of tokens.
Now we would like to have 1 token (1 * 10 ^ 18). What is the price?
Call getAmountsIn function using the parameters
"1000000000000000000",
"0x09B6Ca5E4496238a1F176aEA6bB607db96C2286E",
"0x2346dDF3D362472A80cF18Ab288594F3204Fb415"]
For our example, the result is amounts 10131404313952,1000000000000000000
Which means that:
I would like to buy 1 Token, I must send 0.0010131404313952 RBTC (ETH).
Why aren't you sending exactly 0.001 RBTC (ETH)?
The last step is to execute an exchange. That's pretty easy now that you understand how the Router works.
When you buy tokens with RBTC (ETH), which are probably one of the most common scenarios, you will call the swapETHForExactTokens function, with parameters:
For example, I want to buy 1 Token:
These are the parameters to use in the funcion swapETHForExactTokens:
"1000000000000000000",["0x09B6Ca5E4496238a1F176aEA6bB607db96C2286E","0x2346dDF3D362472A80cF18Ab288594F3204Fb415"],"0x54bE4C3721d2dE76eE0104e53b52Ca8B3A1E27Ca","1610317574"
BEFORE pressing the transact button, fill in the amount of RBTCs (ETHs) to send at the top of the form. We will send 10131404313952 WEI
Click the button Transact or swapETHForExactTokens.
Check the RSKSwap app
In the RSKSwap app, go to the Pool and confirm that you have one less token!
In this tutorial, I have shown you how to create a token and exchange it programmatically, using RSKSwap smart contracts.
I hope this tutorial has been helpful and I appreciate your comments. Share it if you like it :)
Author: Solange Gueiros
Reviewer: Owanate Amachree