This repository contains smart contracts for Hikaru Dex.
Hikaru finance is DeX platform that enables integration of any imaginable DeX project as long as it follows implements pre-defined interfaces.
This repository contains following:
-
Smart contracts:
-
Tests for smart contracts:
Basic smart-contract system structure:
ββββββββββ ββββββββββββββ Pool creation
β User β βPool Factoryββββββββββββββββββββββββββββββββββ
βββββ¬βββββ β contract β β
β βββ¬ββββββββββ²β β
β request β β β
β to informationβ βChecking β
β perform about β β wether βββββΌββββ
βoperation new β β pool β Pools β
β pools β β is ββ²ββββββ¬β
β β β from calculationβ βcalculation
β β β known request β β result
β β βfactory β β
βββββΌβββββ user request ββΌββββββββββ΄β β β
β Router ββββββββββββββββββββββββΊ Vault βββββββββββββββββββββββββββββββ β
βcontractββββββββββββββββββββββββ contract βββββββββββββββββββββββββββββββββββββ
ββββββββββ user request result ββ¬ββββββββββ²β
β β
transfersβ βbalance
β β info
β β
ββΌββββββββββ΄β
β TRC20 β
β token β
βββββββββββββ
Pool contracts are parameter storage and calculators of the system. They implement required math and store parameters that are specific to the pool.
Pools consist of 3 components:
ββββββββββββββββββββββββ
β Pool's storage β
β β
ββββββββββββββββββββββββ€
ββββββββββββββββββββββββ€
β Pool's math library β
β β
ββββββββββββββββββββββββ€
ββββββββββββββββββββββββ€
β Pool's layer for β
βinteracting with vaultβ
ββββββββββββββββββββββββ
Where:
- Pool's storage -> storage that hold constant parameters that are set during pool creation process and parameters that can be changed (swap fee coefficient, manager address)
- Pool's math library -> library that implements calculations logic
- Pool's layer for interacting with vault -> this layer performs pre-checks, ensures that correct parameters are passed to math library and interacts with vault
All interactions with pool are done in following manner:
Get required paramters ββββββββββββββββ
βββββββββββββββββββββββββββββββββββββΊ β
β βPool's storageβ
β βββββββββββββββββββββββββββββ€ β
β β Return parameters ββββββββββββββββ
β β
βββββββ Calculation request βββ΄ββββββββΌββ
β βββββββββββββββββββββββββββββΊInteractionβ
βVaultβ β β
β βββββββββββββββββββββββββββββ€ Layer β
βββββββ Calculation result βββ²ββββββββ¬ββ
β β Calculate with provided
β β and checked parameters βββββββββββββββ
β ββββββββββββββββββββββββββββββΊ Pool's math β
β β β
ββββββββββββββββββββββββββββββββββββββ€ library β
Return calculation βββββββββββββββ
results
Step-by-step description:
- Valid request is received from Vault smart-contract
- Interaction layer receives request and pre-processes it - checks that parameters are valid and brings them to the right form for math library
- With known parameters math library performs calculations and returns results to interaction layer
- Results are returned to the vault that requested calculations by interaction layer
Pools allow performing operations only through vault. If any other contract/user tries to call contract to perform operation - transaction will fail.
βββββββββββββββ Operation forbidden ββββββ
β Random user βββxxxxxxxxxxxxxxxxxxxβββΊ β
βββββββββββββββ β β
β β
βPoolβ
βββββββββββββββ β β
βCorrespondingβ Operation permitted β β
β Vault βββββββββββββββββββββββββΊ β
βββββββββββββββ ββββββ
Vault contracts are token-storages for pools and are gateways to interact with pools.
Developed vault consists of:
ββββββββββββββββββ
β Vault's storageβ
ββββββββββββββββββ€
ββββββββββββββββββ€
β Pool's balancesβ
β storage β
ββββββββββββββββββ€
ββββββββββββββββββ€
β Layer for poolsβ
β interaction β
ββββββββββββββββββ€
ββββββββββββββββββ€
β Layer for user β
β interaction β
ββββββββββββββββββ€
ββββββββββββββββββ€
β Flashloans β
ββββββββββββββββββ
Where:
- Vault's storage -> stores vault-specific variables
- Pool's balance storage -> stores and modifies pool balances
- Layer for pools interaction -> implements interaction with pools for performing calculations
- Layer for user interaction -> implmenets interaction with users via standart interfaces
- Flashloans -> vault allows users to use this tool using implemented interfaces
Vaults track and store balances of pools, so in vault's token balance consists of pool's token balances:
ββββββββββββββββββββββββββββββ
β βββββββββ β
β β Pool1 β β
β βvirtualβ β
β β token β β
β βbalanceβ β
β βββββββββ€ β
β β Pool2 β β
β βvirtualβ β
β Vault β token β β
β token βbalanceβ β
β balance βββββββββ€ β
β β ..... β β
β βββββββββ€ β
β β PoolN β β
β βvirtualβ β
β β token β β
β βbalanceβ β
β βββββββββ β
ββββββββββββββββββββββββββββββ
Pools in this system have virtual tokens balances, but it may depend on implementation.
While processing operations vaults perform following operations:
- Transfer tokens to/from user
- Calls pool to calculate operation result
- Transfer tokens to/from user (as operation result)
βββββββ Calculation request to pool ββββββ
β βββββββββββββββββββββββββββββββββββΊ β
βVaultβ βPoolβ
β βββββββββββββββββββββββββββββββββββ€ β
ββ¬ββββ²β Calculation result ββββββ
β β
β β token balances βββββββ
β ββββββββββββββββββββββββββββββββββββ€ β
β βERC20β
β transfer operations βtokenβ
ββββββββββββββββββββββββββββββββββββββββΊ β
βββββββ
Vaults can only interact with pools that were created by known factory:
βββββββββ Pool creation
βUnknownβββββββββββββββββββββββββ
βfacotryβ β
βββββββββ β
β
ββββββΌβββββ
βPool fromβ
ββxxxxxxxxxββΊ unknown β
β β factory β
β βββββββββββ
βββββββββ β
β Vault ββββββββ€
βββββ¬ββββ β
β β
Pool β β βββββββββββ
origin β β βPool fromβ
check β βββββββββββββΊ known β
β β factory β
β ββββββ²βββββ
βββββΌββββ β
β Known β Pool creation β
βfactoryβββββββββββββββββββββββββ
βββββββββ
Pool factory contracts are contracts that create new pools and can confirm that pool was deployed by factory.
When new pool is created, factory calls vault to register new pool. During this call vault performs necessary preparations for pool to function correctly.
βββββββββ New pool registration βββββββ
βFactoryββββββββββββββββββββββββββββββΊVaultβ
βββββββββ βββββββ
Router contracts are used as single entry point for users to intract with system, reducing amount of required approvals for tokens and performing required checks.
βββββββββββββββ ββββββββββββββ
β β βPool1 type 1β
ββββΊVault type 1 ββββΊ... β
β β β βPoolN type 1β
β βββββββββββββββ€ ββββββββββββββ€
ββββββββ ββββββββ β βββββββββββββββ€ ββββββββββββββ€
β β β β β β β βPool1 type 2β
β User βββββΊRouterβββΌβββΊVault type 2 ββββΊ... β
β β β β β β β βPoolN type 2β
ββββββββ ββββββββ β βββββββββββββββ€ ββββββββββββββ€
β βββββββββββββββ€ ββββββββββββββ€
β β β βPool1 type 3β
ββββΊVault type 3 ββββΊ... β
β β βPoolN type 3β
βββββββββββββββ ββββββββββββββ
User perspective:
- User sets allowances for tokens to router
- User calls router to perform operation
- Router checks if Vault can perform selected operation using IERC165
- Router transfers tokens from user
- If necessary router performs calculations for operation
- Router calls vault with receiver set to msg.sender so user will receive operation result tokens
Router smart contract: TYqRA6CyCYMTwzZv7hSotiudMRxMHJBZKY
Vault smart contract: TUw7TvNHGZJGMFm3RxUtKxYfBKafVHVFBf
Factory smart contract: TXrsYxtgenb722F8sWJkR8HH3diSBEWtrn
Pool smart contracts:
- TQyKsMAHNNL4fypUJzc8wy9m5zGk5jxcKA
- TWYniMuTc11cW9Sk6gY5NAbKoq7Lpmot3T
- TDe1yuD647gM9bfmwLsZBHS1g1JpWVtJ4V
TRC20 token contracts:
- TApJzmfyYrXd6afvfWLKpqqeBxa7hwS9Cw
- TH9pNgBcqsSV6NNGQr1EHRbnQ29RJQipA1
- TAHLygq9zmpSASCkwvExVgxNfGMwjuqQ1u
- TSF96eswa74vJPpeUuPt9mGdPhs5Ahooi4
There are specific tweaks in system to operate with tokens that implement comission on transfers.
To deal with the problem of difference in passed parameter and real transfer sum, contracts use calculated values as results of token transfers:
- If transferFrom() is called than received amount is calculated as:
receivedAmount = balanceAfter - balanceBefore
- If transfer() is called than transferred amount is calculated as:
transferredAmount = balanceBefore - balanceAfter
You will need to account for this when using tokens with comissions.
balanceOf() βββββββ balanceOf()
ββββββββββββββββ€ERC20βββββββββββββββββ
β βtokenβ β
β ββββ¬βββ β
β β β
β β β
β β β
βββββΌββββ βΌ βββββΌββββ
βInitialβ transfer operation β Final β
β token ββββββββββββββββββββββββββββββΊ token β
βbalanceβ (transferFrom/tranfer) βbalanceβ
βββββ¬ββββ βββββ¬ββββ
β β
β βββββ β
βββββββββββββββββΊ-/+ββββββββββββββββββ
βββ¬ββ
β
βββββββββββΌβββββββββββ
βreceived/transferredβ
β amount of tokens β
ββββββββββββββββββββββ
You can find smart contract error descriptions here: Errors