Swap own Fungible Token at Ref.finance on Near Protocol

In this article, we start from liquidity pools basics and at the end you have your own one, and everyone will be able to swap your token at Ref.finance

Den
Near Protocol Development
6 min readJun 5, 2022

--

Introduction

Hi, readers 👋

Did you ever ask yourself a question — what I actually could do with this Fungible Token ❓

Basically, there are a lot of things you could do, but today we’re going to discover the basics — let’s add our newly created Fungible Token on DeFi platform Ref Finance to allow others to exchange our token

If you still don’t have your own Fungible Token — follow here to create one

Fundamentals of Fungible Token swapping

There are a few fundamentals we should know about before we start

If you’re already aware of these basics — feel free to skip this block and move down to the actual guide

What is the wNear token?

We will be using this token further in this guide, so you may ask what is it?

Ref Finance uses wNear to allow you to trade on their platform
Basically, it’s the usual Fungible Token, but it has the one distinction — you always can buy/sell it at the rate of 1 wNear = 1 Near

Its contract has few additional functions near_deposit & near_withdraw — which actually perform this exchange for us

What is a liquidity pool?

A DeFi liquidity pool is a smart contract that locks tokens to ensure liquidity for those tokens on a decentralized exchange. Users who provide tokens to the smart contract are called liquidity providers

DeFi liquidity pools emerged as an innovative and automated way of solving the liquidity challenge on decentralized exchanges. They replace the traditional order book model used by centralized crypto exchanges, which was lifted directly from the established financial markets

In this model, the exchange serves as a marketplace where buyers and sellers come together and agree on prices for assets based on the relative supply and demand. However, this model depends on there being enough buyers and sellers to create liquidity. Therefore, the market makers’ role exists to ensure that there is always someone to meet the demand, effectively keeping prices fair by contributing to liquidity

The basic model has proved to be ineffective for a decentralized exchange. Ethereum’s gas fees and slow block time make it unattractive to market makers, resulting in low liquidity for DEXs’ attempt to replicate the order book model. Therefore, liquidity pools have become the go-to solution of choice in decentralized finance, offering continuous, automated liquidity for decentralized trading platforms

👀 The simplest version of a DeFi liquidity pool holds two tokens in a smart contract to form a trading pair

Let’s use Near(wNear) and Near’s USD stable-coin(USN) as an example, and to make it simple, the price of wNear can be equal to 10 USN. Liquidity providers contribute an equal value of wNear and USN to the pool, so someone depositing 1 wNear would have to match it with 10 USN

The liquidity in the pool means that when someone wants to trade wNear for USN, they can do so based on the funds deposited, rather than waiting for a counterparty to come along to match their trade

Liquidity providers are incentivized for their contribution with rewards. When they make a deposit, they receive a new token representing their stake, called a pool token. In this example, the pool token would be USNWNEAR.

The share of trading fees paid by users who use the pool to swap tokens is distributed automatically to all liquidity providers proportionate to their stake size. So if the trading fees for the USN-WNEAR pool are 0.3% and a liquidity provider has contributed 10% of the pool, they’re entitled to 10% of 0.3% of the total value of all trades

When a user wants to withdraw their stake in the liquidity pool, they burn their pool tokens and can withdraw their stake

Guide

Let’s start, we’ll be creating a trading pair wNear-FTNEAR (FTNEAR is my Fungible Token title)

❗️ This article assumes that you already own some FTNEAR or any other token you want to create trading pair with

Some preparation before we go — define the env variables for further usage

In my case account address is ‘denbite.testnet’ and token address is ‘token.denbite.testnet’, but you should type here your own

1️⃣ The first thing we have to do is to deposit some Near to cover storage fees on the exchange’s side

✅️ The expected terminal output is:

2️⃣ The next step is to buy wNear

⚠️ For this example, I’m buying 19 tokens, but you could purchase any other amount

✅️ The expected terminal output is:

3️⃣ Let’s register our token on the Ref.finance exchange, so they are aware of our Fungible Token contract

✅️ The expected terminal output is:

4️⃣ For now, we have done everything needed to create a liquidity pool for the trading pair FTNEAR-wNear

⚠️ Fee is set to 30, which is actually 0.3%

✅️ The expected terminal output is:

5️⃣ Define variables with amounts of FTNEAR & wNear tokens to send to our created liquidity pool

⚠️ You could use here any amounts you owned

6️⃣ Before we send tokens, the exchange’s account should has enough prepaid storage at your FT contract, otherwise — contract will panic when trying to send tokens

✅️ The expected terminal output is:

7️⃣ Sending our tokens to Ref.finance to add them as liquidity next

⚠️ I’d like to make a point on that we use 'ft_transfer_call' , because after the successful transfer Ref.finance should also change balance on its side

✅️ The expected terminal output is:

✅️ The expected terminal output is:

8️⃣ Adding these tokens to the liquidity pool

⚠️ Make sure you’re providing amounts in the same order as you have been creating the liquidity pool tokens

✅️ The expected terminal output is:

Finally let’s open Ref.finance and review eveything went smoothly

Great, we’ve done it 🎉

As you can see, out token is available for swapping which was the goal of this guide

At this point, you know how to create own liquidity pool on the Near Protocol, which only are our first steps in learning crypto tokenomics 👀

Stay tuned and don’t miss the next chapter soon 😇

If you’ve found this article useful, reach over to the 👏 button and hit it as many times as you have enjoyed reading this post. Your responses are also highly appreciated. You could also find me on the Twitter

If there are left some questions and you need help with that — feel free to ask me on Twitter as well 👨‍💻

--

--

Den
Near Protocol Development

Follow me on Twitter https://twitter.com/captaincatd #web3 #NearProtocol #blockchain #development Hit FOLLOW ⤵