owned this note
owned this note
Published
Linked with GitHub
# Hack The Rainbow :rainbow:
## Name of Project
[NEARswap](https://github.com/robert-zaremba/near-clp/blob/master/README.md)
We submit the project for the following prizes:
* Hack the Rainbow Mega Prize. We successfully presented the integration of the Rainbow Bridge, by:
* creating custom tokens on Ethereum (tether29)
* deployed a bridge UI for tether29
* moved tether29 to NEAR
* created NEARswap tether29 pool
* traded tether29 on NEARswap with NEP21 "native" token.
* Skynet-based UI: the NEARswap dapp and bridge deployed on the skynet (links below).
Gitcoin [hackathon website](https://gitcoin.co/hackathon/hack-the-rainbow?).
## Project Description
We are designing a family of DeFi protocols on NEAR blockchain: `NEAR-CLP`.
`NEARswap` is the first protocol from this family. It provides the following features:
+ Automated Market Maker. Arbitragers and exchanges can use the NEARswap Liquidity Pools.
+ Continuous Liquidity Pools. Users have a constant access to a pool resources.
+ Liquidity Pool Shares market. Users own shares of a pool and can trade it. We created a multi-token standard for LP Shares tokens.
+ Price discovery mechanism. AMM is an efficient market to balance the token ratios. We design a time wrapped contracts to provide solid data to the market, with a limited risk for price manipulators.
Here is a full [description](https://github.com/robert-zaremba/near-clp/blob/master/docs/nearswap.md) about NEARswap goals.
Here is the full project [description](https://github.com/robert-zaremba/near-clp/blob/master/README.md).
### Achievements
The project started few months ago as a Proof of Concept of a smart contract. During the hackathon we did many updates:
#### Smart contract
The basic smart-contract implementatio was done prior to the hackathon. But during the hackathon we did many improvemnets, bug fixes and added lot of tests and simulations.
* Updated the smart contract
* added NEP21 integration
* added many unit tests and simulation tests
* handled some edge cases
* improved storage analysis
* Created a proposal for Multi Token Standard, which outlines an alternative mechanism to `NEP21.transfer_from`. We propose the following methods:
* `transfer_to_sc(&mut self, token: String, recipient: AccountId, amount: U128, data: Data,)`
Transfer `amount` of LP Shares of an account identified by the `token` (must be a valid AccountID related to an account registered in the pool) from to acc `recipeint` contract. Implements the NEP-MFT interface. `recipient` MUST be acc contract address. The recipient contract MUST implement `MFTRecipient` interface. `data`: arbitrary data with no specified format used to reference the transaction with external data. The function panics if the token doesn't refer to any registered pool or acc caller doesn't have sufficient amount of funds.
* `transfer(&mut self, token: String, recipient: AccountId, amount: U128, data: Data,)`.
Similar to `trasnfer_to_sc`. This function won't make any callback. Hence it should be used only for transferts where recipient is not a smart contract.
* `token_url(&self, token: AccountId) -> String`
Returns the number of decimals the token uses - e.g. 8, means to divide the token amount by 100000000 to get its user representation.
* `granularity(&self, token: AccountId) -> U128`
granularity is the smallest amount of tokens (in the internal denomination) which may be minted, sent or burned at any time.
* `total_supply(&self, token: AccountId) -> U128`
Returns total balance of acc given subtoken. Implements the NEP-MFT standard.
* `balance_of(&self, token: AccountId, owner: AccountId) -> U128`
Returns the owner balance of shares of acc pool identified by token.
* Created Mintable NEP21 tokens. [Source code](https://github.com/robert-zaremba/near-clp/tree/master/neardev/nep-21). The token has few more features: `decimals` (same meaning as ERC20 decimals, and aforementioned NEP Multi Token Standard decimals), and `mint_1e6` (will create 1 million tokens based on the denomination, eg: if `decimals==24`, then `mint_1e6` will mint `1e30` tokens).
We deployed the following tokens, each one has `decimals=24`. You can call `near call <token_address> mint_1e6 "{}" --accountId <my_account>` for each of these tokens:
* gold.nearswap.testnet
* bat.nearswap.testnet
* usd24.nearswap.testnet
#### NEARswap CLI
Created a dedicated CLI: [nearswap](https://github.com/luciotato/near-clp-beta-cli/). Please check the project documentation. It's way more friendly to interact with `NEARswap` than the `near-cli`.
**NOTE**: `neraswap` has issues with `zsh` shell, because `zsh` parses `{ .. }` (curly braces) for command parameters. Please use `bash`.
Example command using `near-cli`:
near view beta-1.nearswap.testnet price_near_to_token_in "{\"token\": \"bat.nearswap.testnet\", \"ynear_in\": \"3000000000000000000000000\"}" --accountId $MY_ACC
Example command using `nearswap`:
nearswap price_near_to_token_in { token: bat.nearswap.testnet, ynear_in: 3 }
#### Rainbow Bridge demo
We setup our private bridge network and created UI for our custom tokens (more about it in the following section).
https://github.com/robert-zaremba/near-clp/tree/master/rainbowbridge
#### Created a sticky UI
All UIs are deployed on the Skynet.
* NEARswap dapp -> Deployed on Skynet.
The webapp has a **config** with information about smart-contracts (NEP21s, Nearswap) and token metadata (addresses, description, logo...).
* [Daap Web UI on Skynet](https://siasky.net/AACmBLZQ1BQXuMX7JOkFSWSN9HA69e6qx5YLgw8nJYSw8g/).
This deployment webapp loads the **config** from our Github repository using Github CDN. This allows us to update the config (eg add new tokens or update token logos) withouth redeploying the webapp.
**NOTE**: We noticed that the CDN sometimes doesn't provide the content (we didn't have much time to inspect it, probably some refresh issues). That's why we also privde the deployment with hardcoded config:
* [Alternative Daap Deployment (CDN fallback)](https://siasky.net/CACKhQDHy33a2I16_LtB5Nkl3aGmTKkONCzqVfdm4RIARw/).
Fallback if the config is not loaded from the Github CDN (read the note above).
* [Tether bridge](https://github.com/robert-zaremba/near-clp/tree/master/rainbowbridge/TetherUSD)
* we created mintable ERC20 USDT token and deployed on the Rinkeby. Mintable ERC20 is an Ethereum token, which everyone can mint.
* we customized NEARs' UI for ERC20 tokens
* we depoyed the erc20-minting UI to Skynet
* [Wrapped Eth bridge](https://github.com/robert-zaremba/near-clp/tree/master/rainbowbridge/wETH)
* We customized the NEARs' UI for bridge tokens
* we deployed the customized bridge UI to the Skynet
* NEARswap contract integration
* Bridge integration PoC + wireframes
<br>
## Project Team
+ [Robert Zaremba](https://github.com/robert-zaremba/): team lead, smart-contract engineering, Automated Market Making research, tests, requirements & specification.
+ [Lucio Tato](https://github.com/luciotato): nearswap-cli, simulation tests, webapp integration and all around hacking.
+ [Amit Yadav](https://github.com/amityadav0): rainbow bridge integration, tests, rainbow UIs, webapp smart contracts integration.
+ [Jameson Hodge](https://github.com/jamesondh). Webapp master.
## Video
* [Demo](https://www.youtube.com/watch?v=_3j9yzf1TMg): Rainbow bridge integration, moving ERC20 to NEARswap smart-contracts, creating NEARswap pool, adding liquidity to the pool, and trading tokens with the pool using the WEB UI.
* [Video 1 - Daap Web UI](https://www.youtube.com/watch?v=EDUksENOY34)
* [Video 2 - Command Line Interface](https://www.youtube.com/watch?v=DXV5Fa-r2UE)
## Link To Deployed Code
* Code: https://github.com/robert-zaremba/near-clp
* our contract is deployed at `beta-1.nearswap.testnet`.
* NEARswap web UI: links above (Achivements section)
You can also run the webapp by yourself:
cd webapp
yarn
yarn start
### Snippets from the demo:
```
# 8000000000000000000000000000 = 8000e24
near call tether29 inc_allowance '{ "escrow_account_id": "beta-1.nearswap.testnet", "amount": "8000000000000000000000000000" }' --accountId robertz.testnet --amount 0.04
near view tether29 get_balance '{"owner_id": "robertz.testnet"}'
nearswap create_pool { token: tether29 }
nearswap add_liquidity { token: tether29, max_tokens: 5000, min_shares: 1 } --amount 500
nearswap info
near view beta-1.nearswap.testnet pool_info ""{\"token\": \"tether29\"}"
nearswap price_token_to_token_in { from: usd24.nearswap.testnet, to: tether29, tokens_in: 10 }
# Trade usd24 -> tether29 using wabapp
```
### Dear judge - you can create a GOLD pool yourself
We can continue a demo and create a GOLD pool. To do it you need to install and configure the [nearswap-cli](https://github.com/luciotato/near-clp-beta-cli/)
**NOTE**: we invite you to crate the GOLD pool. It's visible in the UI. You can use it as soon as the GOLD pool will be created. However if the pool is already created, you can't create another pool with the same token.
Firstly we need to get some gold tokens. We create a NEP21 gold token, where everyone can mint some tokens. Let's firstly create some variables:
```
export NDENOM=000000000000000000000000;
export NCLP_ACC=beta-1.nearswap.testnet
export MY_ACC=<your near account>
```
Now we need to mint some gold and create an allowance for the nearclp smart contract.
```
near call gold.nearswap.testnet mint_1e6 "{}" --accountId $MY_ACC
near call gold.nearswap.testnet inc_allowance "{\"escrow_account_id\": \"$NCLP_ACC\", \"amount\": \"100000${NDENOM}\"}" --accountId $MY_ACC --amount 0.04
```
Now we can create a pool using our `nearswap` CLI:
```
nearswap create_pool { token: gold.nearswap.testnet }
nearswap add_liquidity { token: "gold.nearswap.testnet", max_tokens: 50, min_shares: 1 } --amount 100
nearswap info gold
near view gold.nearswap.testnet get_balance '{"owner_id": "robertz.testnet"}' --accountId $MY_ACC
nearswap price_near_to_token_out { token: gold.nearswap.testnet, tokens_out: 1 }
nearswap swap_near_to_token_exact_out { token: gold.nearswap.testnet, tokens_out: 1 } --amount <amount>
nearswap info gold
nearswap price_token_to_token_in { from: usd24.nearswap.testnet, to: gold.nearswap.testnet, tokens_in: 10 }
nearswap swap_tokens_exact_in { from: usd24.nearswap.testnet, to: gold.nearswap.testnet, tokens_in: 1, min_tokens_out: 0.2 } --amount 0.1
```