LKS Foundation

Founder Junior

Technical analysis of the LKSC.finance smart contract

2021-07-19 14:27:01

Technology and operations

Welcome to the third article dedicated to the LKS Liquidity Mining campaign announced in the previous article.

In previous articles we talked about Liquidity Mining from an operational and practical point of view. But how does it really work? How can you check if the platform is working?
This article was created to provide complete transparency on the platform that you can find on LKSC.finance.

First of all, how does Ethereum work?


Before diving into UniSwap and LKSC.finance we need to understand the technology on which they are based: Ethereum. Ethereum is a network of computers that communicate by exchanging messages.

Within those messages we find transactions and data regarding the famous Smart Contracts.
All these messages are then saved forever and will never be deleted again: the famous Blockchain technology.

What are Smart Contracts?


Vitalik Buterin, co-founder of Ethereum recently tweeted that he regretted spreading the term "Smart Contracts", as it does not convey the idea behind this technology. What term does Vitalik prefer? Persistent scripts.

A "script" essentially consists of computer code that some programmer has written. The power of Smart Contracts comes from several characteristics:

  1. they are run not by a computer, not by a server, but by thousands of nodes
  2. nodes do not belong to a private company or to States
  3. nodes are completely anonymous and cannot be stopped by third parties
  4. smart contracts are technically non-censurable


Imagine the Google datacenter but distributed among thousands of people. That is Ethereum, which was born with the aim of providing the world with a global computer.

The LKSC.finance service will also be offered on this global computer today!
When you use the lks.finance website, you will use what is called the "front end" of the application, that is, the interface that the user uses to interact with a service.

This front-end is a modified version of the famous UniSwap, a security standard and above all it allows users to offer a consolidated User Experience in the Ethereum world. This front-end is offered by an Amazon server. Now you are thinking "then it is centralized on Amazon" and the answer is absolutely NO!

Anyone can interact with Ethereum's Smart Contracts, even without this interface: LKSC.finance falls into the category of DApps, i.e. Decentralized Apps!
Let's study its structure.

The infographic you see above represents the architecture of a DApp and you too are represented in the top left where you can find a user's icon!

When you interact with a DApp, then you’re using a DApp Web Browser. In our case, we recommend the use of Metamask, an extension to be installed in your browser that will allow you to interact with the Smart Contracts of the Ethereum Blockchain (or Ethereum clones such as Binance Smart Chain, Polygon, etc.).

Immediately after the browser you find HTML/CSS/Javascript provided by NGINX, a famous WebServer extremely fast and secure.

Our Javascript files in the browser communicate via HTTPS calls with Infura, a service for companies working on the Ethereum Blockchain. This HTTPS call allows you to interact with the Ethereum Blockchain and to perform operations such as transmitting transactions, receiving information on new blocks and much more!

Infura then takes care of communicating with the Ethereum Blockchain through their network of Ethereum nodes, represented in orange in the infographic.

A step forward


Having now understood how Blockchain and Browser (Firefox, Google Chrome, Brave ..) interact, let's see how Smart Contracts on Ethereum work.

The code of Smart Contracts (which are typically written in Solidity, a language similar to Javascript) is not saved inside Ethereum blocks. Inside ETH blocks is saved bytecode, that is the compiled version of the code, the famous machine code that is then executed by the Ethereum Virtual Machine that Ethereum nodes are equipped with, the main component of the Global Computer designed by Vitalik and other pioneers.

LKSC.finance analysis


Let's talk about the Smart Contract behind lks.finance: how was it written? What are its components?

Math/Safemath


First of all, we find the library for mathematical operations necessary to avoid dangerous bugs, such as the famous overflows that during 2017 that gave rise to many thefts of funds deposited by users within Smart Contracts.

This library deserves a dedicated section, because if today we can use the famous DeFi without running into dangerous bugs, it is thanks to this library. Now let's see how Math solves the overflow problem.

You have an overflow when operations are done with numbers too large, so large that they can’t be "saved" in the computer memory leading to a system crash.
Unfortunately, what happens is not a real crash, but the memory cell becomes so large that it returns to zero, 0! A bit like when you count with your hands: once you reach 10, you inevitably go back to zero. How does the Math library help us?

For example, when we add two numbers, it is checked that the result is greater than one of the two addends. Why? Imagine having a computer system that saves data up to 10, then when the number 11 appears the system overflows, reporting 0 as a result.
You have an overflow when one of the two addends (5 + 5 for example) will be greater than the result. Let’s see it.

5 + 5 = 10. Is 10 greater than 5? yes, no overflow problem
5 + 6 = 0 (because our system does not support numbers greater than 10 and it creates an overflow). Is 0 greater than 5? No, developers are warned of the error, an overflow is created! Solidity developers therefore do not use + / * - to perform mathematical operations, but sum, div, mul, sub functions of the Math.sol library

Here is the implementation in Solidity explained for the sum

function add (uint256 a, uint256 b) internal pure returns (uint256)
{
     uint256 c = a + b;
     require (c> = a, "SafeMath: addition overflow");
     return c;
}

IERC20, ERC20 Detailed


These two particular Smart Contracts form a small part of the ERC20 standard. Surely you have read in recent years about ERC20, the interface for fungible tokens like the thousands of Tokens born during the ICO bubble.

ERC20 tokens are a standard and their characteristics can be found in all tokens of that family, including LKSC. For example:
token name: "LKSCOIN"
symbol: "LKSC"
number of decimals that the token has: 8 in the case of LKS, 1.00000000
This industry standard has accelerated the growth of the Ethereum ecosystem, making life easier for developers and users. Without the ERC20s we would not have for example DeFi and all these interoperable systems.

An example to support this thesis comes from the Smart Contract code behind LKSC.finance: we are talking about the IUniswapV2ERC20 interface.

IUniswapV2ERC20


IUniswapV2ERC20 is one of the interfaces of the Uniswap standard and through it an user can guarantee an allowance, that is a "spending permit" from other Smart Contracts.

Let's see the other functions that are part of this interface: we find the same functions as the ERC20 standard, hence the name ERC20!

In this casel, an interface that wraps another interface, that is, "incorporates" it

Address

A library that exposes an important function called isContract (address account) which is used to know if an Ethereum address belongs to a "human" person or to a Smart Contract.

Reentrancy Guard


Prevents a Smart Contract from calling its own functions

Staking


The actual Staking takes place through two Smart Contracts: IStakingRewards and StakingRewards, respectively interface and implementation of the latter.

The most interesting function is definitely getReward (), the one you will use too (without knowing it, of course!) when you will claim the accrued interests. We can see the modifier updateReward, a function that is executed before the body of the function itself, in this case getReward.


function getReward () public nonReentrant updateReward (msg.sender) {
     uint256 reward = rewards [msg.sender];
     if (reward> 0) {
         rewards [msg.sender] = 0;
         rewardsToken.transfer (msg.sender, reward);
         emit RewardPaid (msg.sender, reward);
     }
}

So let's study the behavior of the updateReward function (msg.sender), where msg.sender is the user who is trying to collect interests.

modifier updateReward (address account) {
     rewardPerTokenStored = rewardPerToken ();
     lastUpdateTime = lastTimeRewardApplicable ();
     if (account! = address (0)) {
         rewards [account] = earned (account);
         userRewardPerTokenPaid [account] = rewardPerTokenStored;
     }
     _;
}

You can see in bold a third earned function that takes care of calculating the user's earnings at that time, in order to allow a correct withdrawal of interests!

function earned (address account) public view returns (uint256) {
     return _balances [account] .mul (rewardPerToken (). sub (userRewardPerTokenPaid [account])). div (1e18) .add (rewards [account]);
}

Etherscan, a fundamental tool for Ethereum!


Etherscan is one of the most underrated software. Everybody has used it at least once, to check the status of a transaction or to check the balance of a certain Ethereum address.
For those unfamiliar with Etherscan, a small guide on its interface:
At the top you can find a search bar, through which you can search for addresses, tokens, smart contracts, etc. Just below we find some statistics on the Ethereum network and the latest blocks and transactions mined and broadcasted between nodes.

Now let's try to search for the following Ethereum address: 0x85D725bd75D6b9359fAAc4f72Af7def8F90BC27F

The image you are looking at represents the activity of LKSC.finance!
Highlighted in yellow there is the address you are looking for, followed by some information and finally all the transactions involving the aforementioned address, behind which the Smart Contract acts.

You can see all the transactions, the exact moment in which they were performed, the sender, fees paid by the user and even the name of the action invoked by the user (stake with permit, exit, get reward, etc.) in the "Method" column.

Among the information that you can find just below the address of the contract, click the arrow to expand the menu and there you find all the LKSCs and tokens called "LP Tokens" for "Liquidity Pool Tokens" that were generated by lks.finance during the deposit phase within the Pool (Pool section of the website).

Checking the code


We have almost arrived at the end of this technical paper. Let's study the last section of Etherscan where you can actually check that the code used is the one explained above.

Look for the section "Contract", "code": on Contract source code you can find the the Smart Contract code analyzed before! And that's not all: next to code you can find another item, Read Contract and Write contract.

Click Read and then rewardsToken, that is the Token with which the platform will pay interests. Etherscan will reply with "0xfc4a2cd574bdcc385173f03a6a52cc3b853bb9d4"

Now let's look for this token on Etherscan: https://etherscan.io/address/0xfc4a2cd574bdcc385173f03a6a52cc3b853bb9d4
It is the LKSC token!

And now let's check the Staking address: "0x073f095f0d69f9269bf9ffdb9934f27539ca3cf8" https://etherscan.io/address/0x073f095f0d69f9269bf9ffdb9934f27539ca3cf8

This token is the famous UniSwap LP Token, the one you received when you deposited liquidity within the Pool. You can easily see that this particular token refers to the “USDT-LKSC” pool.

Another useful section is the one that decodes the input parameters, and it answers to the question "how was the Smart Contract configured at its birth?"

There you find the answer through the following parameters:

  1. _rewardsDistribution (address): address of the "manager" of the platform
  2. _rewardsToken (address): address of the token distributed as interest
  3. _stakingToken (address): address of the token to be blocked in the Stake section of lksc.finance

Conclusions


And here we’ve arrived at the end of this rich and complete article. However, even for non-technicians it could be interesting to understand how it works the machine that in recent years some pioneers have invented and other pioneers, including users of lks. finance, have been carrying on! For any further clarification, do not hesitate to ask the LKSCOIN community! 


455