Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Breaking] feat: self-redeem #702

Merged
merged 2 commits into from Sep 13, 2022
Merged

[Breaking] feat: self-redeem #702

merged 2 commits into from Sep 13, 2022

Conversation

sander2
Copy link
Member

@sander2 sander2 commented Aug 18, 2022

(User story copied from notion)

User Story

As a Vault

I want to instantly redeem iBTC or kBTC

So that I

  1. Can reuse my collateral atomically
  2. Don’t have to send a BTC transaction to myself wasting BTC tx fees
  3. And have the BTC in my wallet available right away (technically already the case since we don’t have theft reporting anymore)

Background

When vaults redeem with themselves, they basically burn iBTC, unlock BTC locked with them, and free collateral. This is a three step process at the moment:

  • request_redeem schedules the redeem to be processed and gives the vault a BTC address from its own wallet to transfer BTC from its other addresses
  • the vault client listens for this request_redeem event and then sends the BTC to the specified address (but all in the same wallet - it’s just shifting UTXOs around)
  • once the BTC transaction has enough confirmations, the vault client calls execute_redeem which burns the locked iBTC and frees the vault collateral

This process takes at least 1 hour on main net due to the 6 required BTC confirmations.

Advantages

By making redeem atomic for vaults, the time to execute redeems is reduced to 12-36 seconds (blocks until the parachains is finalized).

Collateral for vaults is directly unlocked allowing vaults to:

  1. Instantly increase their collateral rate in case they need to (not in 1 hour)
  2. Atomically execute actions with their collateral.

Note that the user still pays the 0.5% redeem fees unless it redeems the full amount left in the vault

@sander2 sander2 added the needs-democracy Needs a referendum to be enacted label Aug 18, 2022
Comment on lines +366 to +386
pub(crate) fn execute<T: Config>(vault_id: DefaultVaultId<T>, amount_wrapped: Amount<T>) -> DispatchResult {
// ensure that vault is not liquidated and not banned
ext::vault_registry::ensure_not_banned::<T>(&vault_id)?;

// for self-redeem, dustAmount is effectively 1 satoshi
ensure!(!amount_wrapped.is_zero(), Error::<T>::AmountBelowDustAmount);

let (fees, consumed_issued_tokens) = calculate_token_amounts::<T>(&vault_id, &amount_wrapped)?;

take_user_tokens::<T>(&vault_id.account_id, &consumed_issued_tokens, &fees)?;

update_vault_tokens::<T>(&vault_id, &consumed_issued_tokens)?;

Pallet::<T>::deposit_event(Event::<T>::SelfRedeem {
vault_id,
amount: consumed_issued_tokens.amount(),
fee: fees.amount(),
});

Ok(())
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original code looked surprisingly complex for such a simple function, so this was a bit of an experiment to attempt to make the code easier to read. I like how clear this function is now, although I did end up paying the price in increased verbosity.

@sander2 sander2 added this to the 1.19 milestone Sep 7, 2022
Copy link
Member

@gregdhill gregdhill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blocking concerns but may be worth adding some integration tests, what do you think @sander2?

// for self-redeem, dustAmount is effectively 1 satoshi
ensure!(!amount_wrapped.is_zero(), Error::<T>::AmountBelowDustAmount);

let (fees, consumed_issued_tokens) = calculate_token_amounts::<T>(&vault_id, &amount_wrapped)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A potentially nice refactoring from a concept I came across whilst digging into Bitcoin Core is to bundle this tuple into something similar to the COutput and then calculate the amount to burn using GetEffectiveValue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only used within this module (and only once at that), so I don't think that refactoring is worth it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah just mentioning it because I thought it was pretty ;)


#[pallet::weight(<T as Config>::WeightInfo::self_redeem())]
#[transactional]
pub fn self_redeem(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we bundle this into the existing redeem flow? I guess having a separate extrinsic is cleaner but not sure if there were other considerations in your design here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that would be very clean. The existing request_redeem has a btc address and a vault account id, both of which are not used for self_redeem. And it's very different in operation in that no request is placed into storage at all (UI will have to handle this differently).

@sander2
Copy link
Member Author

sander2 commented Sep 13, 2022

No blocking concerns but may be worth adding some integration tests, what do you think @sander2?

What do you mean? There are integration tests, see test_redeem.rs?

@gregdhill gregdhill merged commit 0597988 into master Sep 13, 2022
@gregdhill gregdhill deleted the feat/self-redeem branch September 13, 2022 16:10
@gregdhill
Copy link
Member

No blocking concerns but may be worth adding some integration tests, what do you think @sander2?

What do you mean? There are integration tests, see test_redeem.rs?

Yes you are right, sorry I must have missed those - trying to do too many things at once

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-democracy Needs a referendum to be enacted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants