RiskProtocol / core-protocol

2 stars 0 forks source link

PoC: CoW or 0x for rebalancing after rebase #44

Open mz7mz7mz7 opened 1 year ago

mz7mz7mz7 commented 1 year ago

Ie user had only token X before rebase. After rebase they would have some token X and some token Y. But let's assume they want to hold only token X. How we can automatically do it for them after the rebase?

Some ideas:

  1. Automatically sell Y. (this would take some time to execute, the transactions will not be atomic, the prices will fluctuate)
  2. Internally offset those who want only X and those who want only Y (no price impact)
  3. ???

@jiokeokwuosa , @Tunji17 -- any other ideas?

Tunji17 commented 1 year ago

@mz7mz7mz7 what reasons will make a user want to hold only one token over the other? It is my understanding that both tokens will have the same pegged price at this point. correct me if i'm wrong

Tunji17 commented 1 year ago

So @mz7mz7mz7 my reasoning behind this is to create some kind of queue where we can partially match risk on tokens with risk off tokens, so a user will opt in to hold only a particular type of token and after every rebase we want to internally offset this for users that have this flag using the queue. but this will depend on demand and supply, so this has to be communicated to the user in one form or the other. like we can encourage them to always trade either of their tokens manually using the exchange but can market this as an experimental feature.

Tunji17 commented 1 year ago

we can also automatically settle open orders with token of users that have this flag. eg User A wants to hold only risk off tokens, and we were able to only partially fill 50% of their risk on tokens, if user B opens an order to sell their risk off token we can automatically match this with user A risk on tokens

Tunji17 commented 1 year ago

POC for matching orders internally on our smart contract and look into 0x creating orders and matching in bulk

mz7mz7mz7 commented 1 year ago

Let's build a prototype/proof of concept with our current smart contracts as a base that would utilize CoW smart orders to execute rebalancing after rebase. Article: https://cow-protocol.medium.com/tutorial-creating-smart-orders-on-cow-protocol-683a5f9f4763 The article is using the old framework, the new one is here: https://github.com/rndlabs/composable-cow Long live coding session on it which was recorded here: https://youtu.be/5lbnMPqfXho Here are the different smart order types that already exist: https://github.com/rndlabs/composable-cow/tree/main/src/types Standard: https://eips.ethereum.org/EIPS/eip-1271 Docs: https://docs.cow.fi/tutorials/how-to-place-erc-1271-smart-contract-orders

We have also shared a Slack channel with the CoW team if we need to raise any questions, it's called 'cow-riskprotocol'.

haidarally commented 1 year ago

0X: @mz7mz7mz7 Can unlimited orders be created by user?

Can fill multiple orders in a single transaction?

mz7mz7mz7 commented 1 year ago

In regards to 2nd point: filling multiple orders in one tx, I was referring to these functions @haidarally : https://etherscan.io/address/0x61935cbdd02287b511119ddb11aeb42f1593b7ef#writeProxyContract 2023-08-02_09-50batch

mz7mz7mz7 commented 1 year ago

And as for both of these points -- can we actually developed simple PoC, in the form of unit test or command line app, whatever is simpler, and just try to do what we discussed:

  1. Assuming our middleman (M) wallet address has sufficient balances of X and Y.
  2. And assuming it has orders from A and B for selling LARGE X->Y and LARGE Y->X respectively.
  3. And the balances of X and Y on A and B are smaller than LARGE.
  4. Execute, in one blockchain transaction, two swaps: X->Y from A to M, and after that Y->X from B to M.
  5. Which would not change the balances on M at all.
  6. And would give A and B only the token they wanted to have.
haidarally commented 1 year ago

@mz7mz7mz7 As discussed, the batchFillLimitOrders method doesnot seem to be present in the v4. Waiting for some clarifications from 0x.

haidarally commented 1 year ago

@mz7mz7mz7 The composable cow frameworks actually works in the following manner

I think for the part where users need to modify their safe, we should be able to permit them to do so on our dApp using thehttps://www.npmjs.com/package/@safe-global/safe-core-sdk or other sdks

Notes: 2nd Audit still in progress For added info: https://forum.cow.fi/t/automated-treasury-management-methodology/1799 https://www.youtube.com/watch?v=5lbnMPqfXho&ab_channel=CoWSwap

haidarally commented 1 year ago

@mz7mz7mz7 As discussed, the batchFillLimitOrders method doesnot seem to be present in the v4. Waiting for some clarifications from 0x. @mz7mz7mz7

0x Poc works PR: https://github.com/RiskProtocol/rebalance-poc-0x/pull/1 Cons: We have to pay the gas fees

Flow: User has to create a very big order( maximum of uint128) , sign the order and submit both objects to us User has to approve the exchange contract. Then we fill the orders as many time as required Same order filled twice below https://polygonscan.com/tx/0xd3b548a4d174f12ef3813740d50c8a0e8c6ca590233a674c59eb0f1f9b2a34ca https://polygonscan.com/tx/0x8936b495fbcddb6cd93250884cfa29be2bd5e737ef580dfe3460740d5ea5a60b

haidarally commented 5 months ago

Cow AMM: image https://discord.com/channels/869166959739170836/1207617790966628414/1219189278769156106

haidarally commented 2 months ago

Updates:

POC

-- Let USDC be RiskOFF -- Let COW be RiskON -- Assume that after every rebase

  1. WrapperX will get rid of RiskOFF (USDC) by sending the totality of it to SafeX
  2. WrapperY will get rid of RiskON (COW) by sending the totality of it to SafeY

Demonstration

  1. As soon as SafeX gets RiskOFF (USDC), it creates an order to sell them
  2. As soon as SafeY gets RiskON(COW), it creates an order to sell them
  3. The recieved tokens are sent directly to their respective wrappers X and Y
mfw78 commented 1 month ago

Hi all, looks like something interesting in the works here. If you need some help from a CoW Protocol side, reach out to us in #tech-talk on discord :smiley: