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

Implementation of BitcoinRedeemer contract #309

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1680946
Implement Bitcoin Redeemer contract
nkuba Mar 10, 2024
4c3025e
Inherit ERC20Permit in stBTC contract
nkuba Mar 10, 2024
e028a3b
Add reference to BitcoinRedeemer in stBTC
nkuba Mar 10, 2024
ede91a7
Implement stBTC redemption to Bitcoin with signature
nkuba Mar 10, 2024
4b18df7
Add test implementation of TBTC token
nkuba Mar 10, 2024
79a5305
Add deployment script for BitcoinRedeemer contract
nkuba Mar 10, 2024
96c6c0f
Add dependency to @openzeppelin/contracts-upgradeable
nkuba Mar 10, 2024
aca3bd9
Disable slither's reentrancy-event for requestRedemption
nkuba Mar 10, 2024
d58c14a
Upgrade @openzeppelin/hardhat-upgrades dependency
nkuba Mar 10, 2024
29dd099
Add core/cache/ dir to CI upload artifacts
nkuba Mar 10, 2024
a53d360
Add unit tests for redeemToBitcoin function
nkuba Mar 11, 2024
d5090d8
Add unit tests for updateBitcoinRedeemer
nkuba Mar 11, 2024
5ed70fc
Use beforeAfterSnapshotWrapper in updateDispatcher test
nkuba Mar 11, 2024
18a4ac2
Modify TreasuryUpdated event to include old address
nkuba Mar 11, 2024
9123555
Merge remote-tracking branch 'origin/main' into bitcoin-redeemer
nkuba Mar 11, 2024
e75bd73
Rename requestRedemption to redeemSharesAndUnmint
nkuba Mar 13, 2024
0d03792
Add WithdrawToBitcoin function
nkuba Mar 13, 2024
f719271
Add tests for withdrawToBitcoin function
nkuba Mar 13, 2024
2a4dd70
Add ERC20PermitUpgradeable contract
nkuba Mar 13, 2024
e30fca2
Merge remote-tracking branch 'origin/main' into bitcoin-redeemer
nkuba Mar 13, 2024
1660f8d
Revert "Add WithdrawToBitcoin function"
nkuba Mar 13, 2024
cd5e6c3
Revert "Add tests for withdrawToBitcoin function"
nkuba Mar 13, 2024
e9b5092
Implement approveAndCall patter in the vault
nkuba Mar 15, 2024
8e779c2
Merge remote-tracking branch 'origin/main' into bitcoin-redeemer
nkuba Apr 3, 2024
3ea84f8
Add unit tests for redeemer contract
nkuba Apr 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "Add WithdrawToBitcoin function"
This reverts commit 0d03792.
  • Loading branch information
nkuba committed Mar 13, 2024
commit 1660f8d2543bb7d1a1c4a26f195ec23a3642de29
38 changes: 0 additions & 38 deletions core/contracts/BitcoinRedeemer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,44 +75,6 @@ contract BitcoinRedeemer is Initializable {
stbtc = stBTC(_stbtc);
}

/// @notice Initiates the redemption process by exchanging stBTC tokens for
/// tBTC tokens and requesting bridging to Bitcoin.
/// @dev Withdraws tBTC assets and requests redemption of tBTC to Bitcoin via
/// tBTC Bridge.
/// Redemption data in a format expected from `redemptionData` parameter
/// of Bridge's `receiveBalanceApproval`.
/// It uses tBTC token owner which is the TBTCVault contract as spender
/// of tBTC requested for redemption.
/// @dev tBTC Bridge redemption process has a path where request can timeout.
/// It is a scenario that is unlikely to happen with the current Bridge
/// setup. This contract remains upgradable to have flexibility to handle
/// adjustments to tBTC Bridge changes.
/// @param owner The owner of the stBTC tokens.
/// @param tbtcAmount The number of tBTC tokens to withdraw.
/// @param tbtcRedemptionData Additional data required for the tBTC redemption.
/// See `redemptionData` parameter description of `Bridge.requestRedemption`
/// function.
function withdrawAssetsAndUnmint(
address owner,
uint256 tbtcAmount,
bytes calldata tbtcRedemptionData
) public {
uint256 shares = stbtc.withdraw(tbtcAmount, address(this), owner);

// slither-disable-next-line reentrancy-events
emit RedemptionRequested(owner, shares, tbtcAmount);

if (
!tbtcToken.approveAndCall(
tbtcToken.owner(),
tbtcAmount,
tbtcRedemptionData
)
) {
revert ApproveAndCallFailed();
}
}

/// @notice Initiates the redemption process by exchanging stBTC tokens for
/// tBTC tokens and requesting bridging to Bitcoin.
/// @dev Redeems stBTC shares to receive tBTC and requests redemption of tBTC
Expand Down
65 changes: 0 additions & 65 deletions core/contracts/stBTC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ contract stBTC is
/// Maximum total amount of tBTC token held by Acre protocol.
uint256 public maximumTotalAssets;

/// @dev Hash of the type for withdrawing tBTC to Bitcoin with permit.
bytes32 private constant WITHDRAW_TO_BITCOIN_TYPEHASH =
keccak256(
"WithdrawToBitcoin(address owner,uint256 assets,bytes bitcoinOutputScript,uint256 nonce,uint256 deadline)"
);

/// @dev Hash of the type for redeeming stBTC to Bitcoin with permit.
bytes32 private constant REDEEM_TO_BITCOIN_TYPEHASH =
keccak256(
Expand Down Expand Up @@ -239,65 +233,6 @@ contract stBTC is
}
}

/// @notice Redeems stBTC to Bitcoin with permit.
/// @dev After checking the signature the function approves BitcoinRedeemer
/// contract to withdraw tBTC tokens. The BitcoinRedeemer contract
/// withdraws tBTC and requests redemption of tBTC in the tBTC Bridge.
/// @param owner The owner of the stBTC tokens.
/// @param assets The number of tBTC tokens to withdraw.
/// @param tbtcRedemptionData Additional data required for the tBTC redemption
/// by the tBTC Bridge.
/// @param deadline The deadline by which the redemption must occur.
/// @param v The recovery id of the signature.
/// @param r The R value of the signature.
/// @param s The S value of the signature.
function withdrawToBitcoinWithPermit(
address owner,
uint256 assets,
bytes calldata tbtcRedemptionData,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
/* solhint-disable-next-line not-rely-on-time */
if (block.timestamp > deadline) {
revert ERC2612ExpiredSignature(deadline);
}

bytes32 redeemerOutputScriptHash = TbtcRedemption
.extractBitcoinOutputScriptHash(tbtcRedemptionData);

// Use hashing function from Open Zeppelin's ERC20Permit contract.
bytes32 hash = _hashTypedDataV4(
keccak256(
abi.encode(
WITHDRAW_TO_BITCOIN_TYPEHASH,
owner,
assets,
redeemerOutputScriptHash,
_useNonce(owner),
deadline
)
)
);

address signer = ECDSA.recover(hash, v, r, s);

if (signer != owner) {
revert ERC2612InvalidSigner(signer, owner);
}

_approve(owner, address(bitcoinRedeemer), previewWithdraw(assets));

return
bitcoinRedeemer.withdrawAssetsAndUnmint(
owner,
assets,
tbtcRedemptionData
);
}

/// @notice Redeems stBTC to Bitcoin with permit.
/// @dev After checking the signature the function approves BitcoinRedeemer
/// contract to redeem stBTC shares. The BitcoinRedeemer contract
Expand Down