kuegi / ain

DeFi Blockchain - enabling decentralized finance on Bitcoin
https://defichain.com
MIT License
0 stars 0 forks source link

Implementing the dToken restart #9

Closed kuegi closed 3 weeks ago

kuegi commented 4 months ago

summary

In this issue I will try to document my implementation process of the needed changes for the DFIP to restart the dToken system

video summary of implemented logic:

I summarized all the implemented logic with additional reasoning in this video:

https://youtu.be/MdlbdXfkuY8

tasks to be done

setup

implement

Loan payback

refund futureswaps and cancel auctions

create new tokens and lock

new rpc calls

continuous handling

finish

fixes

notes from implementation

pseudo code from DFIP definition

for all vaults:
  payback DUSD loans with collateral
  payback remaining loans with funds from address
  payback remaining loans by swapping collateral (first DUSD, then other collateral)

convert all loanTokens to new version (like stocksplit, but with no change in price or amount, must also affect the DUSD in collateral), new DUSD is called USDD

create native SC to lock funds in tranches, SC knows funds per address
SC keeps track of release-ratio, starting with 10%
initially 100 tranches (with 10% initial ratio, thats 0.9% per tranche)

for all LM positions in dToken-USDD pools:
  remove 90% of position and lock resulting funds directly into the SC

for all LM positions in USDD-DFI, USDC-USDD, USDT-USDD, EUROC-USDD, XCHF-USDD:
  remove 90% of position, lock resulting USDD directly into the SC, credit other resulting asset to owner

for all vaults:
  withdraw 90% of USDD collateral into SC (for vault owner address)

for all loantoken balances (=dTokens and USDD):
  lock 90% of balances into SC
any transfer EVM->DVM of old dToken or DUSD results in:
  SC.releaseRatio in new dToken/USDD is credited to targetaddress
  1-SC.releaseRatio in new dToken/USDD is locked in SC for targetaddress
on releaseTrancheTx:
  for all entries in SC:
     credit according tranche part of funds to address
     update SC.releaseratio accordingly
//deactivate DUSD loops:
"v0/vaults/dusd-vault/enabled": "false",

//remove old stab fee:
"v0/poolpairs/17/token_a_fee_pct": "0",
"v0/poolpairs/101/token_b_fee_pct": "0",
"v0/poolpairs/102/token_b_fee_pct": "0",
"v0/poolpairs/218/token_b_fee_pct": "0",
"v0/poolpairs/236/token_b_fee_pct": "0",

//activate new system-wide USDD fee

"v0/token/<usdd_id>/dex_in_fee_pct": "0.05",
"v0/token/<usdd_id>/dex_out_fee_pct": "0.05",

learnings

kuegi commented 4 months ago

on my machine, cloning the repo and then calling ./make.sh build installed all necessary things and build succeeded on the first run.

./make.sh test runs all tests

to run only python testfiles that match a filter: ./make.sh test-py --filter feature_evm*