# kchannels - asset swap notes
### Useful links
* http://docs.kchannels.io/
* https://github.com/abridged/collabland-wallet-auth
* Useful [type definitions](https://github.com/abridged/collabland-wallet-auth/blob/master/src/utils/kchannels.js)
* https://www.reddit.com/r/ethereum/comments/i1jjjv/reddit_scaling_bakeoff_submission_abridged/
-----
### Requirements
* JSON RPC service needed to fullfill the asset swap flow
* two points of entry - `generate_quote()` & `fulfill_quote()`
* NodeJS / JSON RPC 2.0 based API service
* Sample client/UI to interact with the service
* OR integrate into the existing dev UI we have seen ?
* Good unit test around the core and complex parts
* Github runner or equivalent as test runner on commit
* Infrastructure
* Hosted on a digital ocean droplet (or similar) somewhere
* Postgresql for data persistance
-----
### Action plan
* **Asset Swap Service**
* **Public methods**
* Ability to query for exchange rate from asset 1 and asset 2
* `generate_quote(asset from, asset to, amount) returns (quote_id, from, to, amount, fee)`
* Stored in DB against channel ID
* Approve quote for swap
* `aprove_asset_swap(quote_id) returns (succsss, receipt...?)`
* Looks up and validates provided quote
* **Internal concerns**
* Persistance of generated quote
* Multiple quotes can be generated for the same swap and user
* Exposure of secure/authenticated JSON API
* Quote logic
* Calculate exchange rate
* How to determine this - going externally?
* Calculate fee
* Are fees dynamic?
* Ensure asset exchange is possible via liquidity depth checks i.e. do I have enough of that asset to do the swap
* Approve asset swap exchange
* Validate quote
* Validate ability to still perform the exchange
* Validate signatures (needs more understanding)
* Perform the actual exchange
* ... ? need to understand how this happens in more detail
* **Domain model changes**
* `ExternalTransactionReference` - addition of source `chain_id`
* `ChannelAsset` - addition of source `chain_id`
* `TransactionValue` - addition of source `chain_id`
* See [Multichain-VOs](#Multichain-VOs) diagram from full relationship strucutre
-----
### Questions
* JSON RPC authentication scheme?
* How to auth the caller?
* * (JY) we'll create wireframes
* What relevance does the asset swap watch tower have?
* Do we care about this at this stage?
* * (JY) Kaman?
* How to detemrine exchange rate?
* 3rd party service vs onchain to price discovery?
* * (JY) DAI -> xDAI, no price discovery needed
* How to handle multiple quotes
* Do quotes expire?
* Can any quote given be redeemed at any time in the future?
* * (JY) only one quote allowed and they do not. expire
* How do zones work in relation to chains?
* Is this is one zone per chain i.e. mainnet = zone 1 | xDAi = zone 2 ?
* Side question - is there a scaling problem with zones as the userbase grows?
* * (JY) Kaman?
* Asset swap fees?
* What logic drives the exchange fee e.g. is it volume based or fixed?
* Do different swaps have different fees?
* Do different chains have different fee e.g. xDAI 0.25% | ETh 1% ?
* * (JY) let's hard code the fee for now
* Error states?
* When asset approval happens the quote is no longer valid?
* liquidty not enough
* quote expired e.g. from one week ago ... is this a thing?
* fee has changed whilst quote is in flight?
* * (JY) quote should never expire or change, only issue is lack of liquidity
* Attack secnarios to worry about?
* I assume this is dramatically reduced is correct signature checks are performed?
* * (JY) will think through this once we have initial implementation (can't think of any attacks at this abstract level)
* How does kchannels deal with things like block reorgs?
* * (JY) Kaman?
-----
### Diagrams
##### Asset Swap POC

##### Asset Swap Service Flow

##### Atomic Asset Exchange

##### Multichain Operation

##### Multichain VOs

##### Phase 1 - Multichain Operation
