How To Create CRC20 Token Swap Coinex Smart Chain – PART 1


Swap is one of the most widely used methods in the cryptocurrency world, if you are a dex user like pancakeswap , uniswap, then you are familiar with the term swap token. TokenSwap is a method of exchanging native tokens or coins with other tokens in a blockchain and without the intervention of other parties. Swaps are executed on the blockchain completely, with no humans intervening in the swap process.

Basically swap is the easiest and most preferred method by traders to exchange their assets for other assets. Swap tokens run on the blockchain and use smart contracts so that the swap function can run perfectly, generally token swaps run on blockchains that support smart contracts, such as ethereum or the coinex smart chain blockchain. Swaps cannot run on blockchains like bitcoin, litecoin, monero, because those blockchains are not “smart contract platforms”.

To run TokenSwap perfectly you need a “smart contract platform” that has high performance and high transaction speed, and has a low tx fee. Why should it be? Because some traders don’t want to make transactions with high fees and wait for long tx validation. They want speed and cheapness, because they usually do a lot of transactions on the blockchain.

In order for the token swap to run properly and perfectly, we recommend that you use the coinex smart chain blockchain, the blockchain has high performance, uses the PoS protocol that is environmentally friendly and has a high TPS. To make a swap transaction on the coinex smart chain, it only takes you 3-5 seconds and costs less than $0.005 , in contrast to ethereum which has to pay $3-5 per transaction and has to wait 10-30 minutes for the swap process to complete.

In this article we will provide a tutorial, how to create a tokenswap on the coinex smart chain blockchain. The token swap uses an opensource smart contract from openzepplin, and you are free to take or modify the smart contract

 

Several things need to be prepared, to make CRC20 Token Swap Coinex Smart Chain

1# Metamask Wallet

Metamask is the most popular evm wallet today, this wallet is available in several versions, starting from the android version, IOS, firefox browser and chrome browser, safari, etc. You can use 1 metamask wallet for multiple blockchains, such as ethereum, binance, and coinex smart chain. To use multiple blockchains in 1 wallet, you need to set the RPC according to the blockchain you want. The following is the RPC blockchain coinex smart chain

RPC URL : https://rpc.coinex.net
Network Name : Coinex Smart Chain
ChainID : 52
Symbol : CET
Block Explorer : https://www.coinex.net

 

2# Coin CET (coin native coinex smart chain)

CET is a native coin of the coinex smart chain blockchain, CET coins are used to pay for transactions on the network, for example if you send crc20 tokens, or swap or trade, you need a small amount of coins to pay the transaction fee. To get coin cet you can buy it on the coinex exchange. After buying a coin cet, immediately send it to your metamask wallet.

 

3# CRC20 Token Smart Contract

This smart contract is used to create crc20 tokens on the coinex smart chain blockchain


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Token is ERC20 {
constructor(
string memory name_,
string memory symbol_,
uint256 totalSupply_,
address recipient_
) ERC20(name_, symbol_) {
_mint(recipient_, totalSupply_);
}
}

 

4# TokenSwap Smart Contract

This is a smart contract swap, which functions to exchange crc20 tokens with other crc20 tokens. In this smart contract we will create 3 tokens (Token A, Token B and Token C), each token can be exchanged or swapped with other tokens. For example, you can swap token A to token B, or swap token A to C or vice versa. If you are proficient in the field of coding solidity, you can customize the smart contact.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.6;;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./Token.sol";

contract Swapper {
IERC20 public tokenA;
IERC20 public tokenB;
IERC20 public tokenC;

constructor() {
tokenA = new Token("tokenA", "TKNA", 1_000_000 * 1E18, msg.sender);

tokenB = new Token("tokenB", "TKNB", 1_000_000 * 1E18, msg.sender);

tokenC = new Token("tokenC", "TKNC", 1_000_000 * 1E18, address(this));
}

function swap(address token_, uint256 amount) external {
_swap(address(token_), amount);
}

function unswap(address token_, uint256 amount) external {
_unswap(address(token_), amount);
}

function swapAforC(uint256 amount) external {
_swap(address(tokenA), amount);
}

function swapBforC(uint256 amount) external {
_swap(address(tokenB), amount);
}

function unswapCforA(uint256 amount) external {
_unswap(address(tokenA), amount);
}

function unswapCforB(uint256 amount) external {
_unswap(address(tokenB), amount);
}

function _swap(address token_, uint256 amount) private {
require(
(IERC20(token_).allowance(msg.sender, address(this)) >= amount),
"ERC20: transfer amount exceeds allowance"
);
require(
tokenC.balanceOf(address(this)) >= amount,
"ERC20: transfer amount exceeds balance"
);
require(
IERC20(token_).transferFrom(msg.sender, address(this), amount),
"ERC20: Error"
);
require(tokenC.transfer(msg.sender, amount), "Error");
}


function _unswap(address token_, uint256 amount) private {
require(
(tokenC.allowance(msg.sender, address(this)) >= amount),
"ERC20: transfer amount exceeds allowance"
);
require(
IERC20(token_).balanceOf(address(this)) >= amount,
"ERC20: transfer amount exceeds balance"
);
require(
tokenC.transferFrom(msg.sender, address(this), amount),
"ERC20: Error"
);
require(IERC20(token_).transfer(msg.sender, amount), "Error");
}
}

 

How to Create CRC20 Token Swap Coinex Smart Chain

1# Open Remix IDE CSC – ide.coinex.net

Just like the ide ethereum remix, the csc ide remix is used to deploy smart contracts on the coinex smart chain blockchain, the features are exactly the same, because it is a fork of the ide ethereum remix.

  • Create a Token.sol file and paste the crc20 smart contract above
  • Compile the smart contract with a compiler that matches the smart contract, set enable compression to 200 and make sure the smart contract compile runs properly and without errors, after successful compilation, a green check mark will appear on the left.
  • Create a Swap.sol file and paste the smart contract token swap above, and compile it, just like you complie smart contract crc20

 

The following is information on the crc20 token that we will create, you can change the token name, supply amount and token symbol

constructor() {
tokenA = new Token("tokenA", "TKNA", 1_000_000 * 1E18, msg.sender);

tokenB = new Token("tokenB", "TKNB", 1_000_000 * 1E18, msg.sender);

tokenC = new Token("tokenC", "TKNC", 1_000_000 * 1E18, address(this));
}

 

this is a function for swap which you can later use to swap tokens, this function you can link with reactjs application or you can interact contract in explorer

  • Swap token A to token C
  • Swap token B to token C
  • UnSwap token C to token A
  • UnSwap dari token C to token B
function swapAforC(uint256 amount) external {
_swap(address(tokenA), amount);
}

function swapBforC(uint256 amount) external {
_swap(address(tokenB), amount);
}

function unswapCforA(uint256 amount) external {
_unswap(address(tokenA), amount);
}

function unswapCforB(uint256 amount) external {
_unswap(address(tokenB), amount);
}

For the swap value or comparison of these tokens, 1 to 1 , for example 1 token A is worth 1 token B/C .

Continue to PART 2


Alif Fahmi

hi , I'm Alif, I'm a blockchain & cryptocurrency lover, I love writing & learning, my job is web developer & crypto trader