# 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).
## 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:
* 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,)`.
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 a customized CLI for nearswap and near-clp:
* https://github.com/luciotato/near-clp-beta-cli/
* Run private bridge network
* https://github.com/robert-zaremba/near-clp/tree/master/rainbowbridge
* Created a sticky UI
* Skynet integration
* [NEARswap dapp](https://siasky.net/AACmBLZQ1BQXuMX7JOkFSWSN9HA69e6qx5YLgw8nJYSw8g/)
note: sometimes we noticed problems with the page when the config is not loaded. We store a config in the github repository and we load it through CDN. The config has information about smart-contract addresses (NEP21s, Nearswap).
* [Tether bridge](https://github.com/robert-zaremba/near-clp/tree/master/rainbowbridge/TetherUSD)
* [wrapped Eth bridge](https://github.com/robert-zaremba/near-clp/tree/master/rainbowbridge/wETH)
* NEARswap contract integration
* sourcing pool contracts from a repository config
* 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.
+ [Amit Yadav](https://github.com/amityadav0): rainbow bridge tests, rainbow UIs, webapp integration.
+ [Jameson Hodge](https://github.com/jamesondh). Webapp UI master.
<br>
## 2 Minute Video*
https://www.youtube.com/watch?v=EDUksENOY34
## Link To Deployed Code*
* Demo video https://www.youtube.com/watch?v=EDUksENOY34
* Code: https://github.com/robert-zaremba/near-clp
* [Deployed Daap](https://siasky.net/AACmBLZQ1BQXuMX7JOkFSWSN9HA69e6qx5YLgw8nJYSw8g/)
* [Alternative Daap Deployment (CDN fallback)](https://siasky.net/CACKhQDHy33a2I16_LtB5Nkl3aGmTKkONCzqVfdm4RIARw/)
* If none of those work (problems with CDN, as explained above), then you can run and build it locally:
cd webapp
yarn
yarn start
* our contract is deployed at `beta-1.nearswap.testnet`.
### 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
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 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
```