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

One-transaction TBTC to BTC #302

Merged
merged 7 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
2 changes: 1 addition & 1 deletion solidity/contracts/bank/Bank.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ contract Bank is Ownable {
function approveBalanceAndCall(
address spender,
uint256 amount,
bytes memory extraData
bytes calldata extraData
) external {
_approveBalance(msg.sender, spender, amount);
IReceiveBalanceApproval(spender).receiveBalanceApproval(
Expand Down
4 changes: 2 additions & 2 deletions solidity/contracts/bank/IReceiveBalanceApproval.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface IReceiveBalanceApproval {
/// @param amount The amount of the Bank balance approved by the owner
/// to be used by the contract.
/// @param extraData The `extraData` passed to `Bank.approveBalanceAndCall`.
// @dev The implementation must ensure this function can only be called
/// @dev The implementation must ensure this function can only be called
/// by the Bank. The Bank does _not_ guarantee that the `amount`
/// approved by the `owner` currently exists on their balance. That is,
/// the `owner` could approve more balance than they currently have.
Expand All @@ -40,6 +40,6 @@ interface IReceiveBalanceApproval {
function receiveBalanceApproval(
address owner,
uint256 amount,
bytes memory extraData
bytes calldata extraData
) external;
}
56 changes: 47 additions & 9 deletions solidity/contracts/vault/TBTCVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ contract TBTCVault is IVault, Governable {
function receiveBalanceApproval(
address owner,
uint256 amount,
bytes memory
bytes calldata
) external override onlyBank {
require(
bank.balanceOf(owner) >= amount,
Expand All @@ -136,7 +136,7 @@ contract TBTCVault is IVault, Governable {
}
}

/// @notice Burns `amount` of TBTC from the caller's account and transfers
/// @notice Burns `amount` of TBTC from the caller's balance and transfers
/// `amount` back to the caller's balance in the Bank.
/// @dev Caller must have at least `amount` of TBTC approved to
/// TBTC Vault.
Expand All @@ -145,23 +145,51 @@ contract TBTCVault is IVault, Governable {
_unmint(msg.sender, amount);
}

/// @notice Burns `amount` of TBTC from the caller's account and transfers
/// `amount` back to the caller's balance in the Bank.
/// @dev This function is doing the same as `unmint` but it allows to
/// execute unminting without an additional approval transaction.
/// The function can be called only via `approveAndCall` of TBTC token.
/// @notice Burns `amount` of TBTC from the caller's balance and transfers
/// `amount` of Bank balance to the Bridge requesting redemption
/// based on the provided `redemptionData`.
/// @dev Caller must have at least `amount` of TBTC approved to
/// TBTC Vault.
/// @param amount Amount of TBTC to unmint and request to redeem in Bridge.
/// @param redemptionData Redemption data in a format expected from
/// `redemptionData` parameter of Bridge's `receiveBalanceApproval`
/// function.
function unmintAndRedeem(uint256 amount, bytes calldata redemptionData)
external
{
_unmintAndRedeem(msg.sender, amount, redemptionData);
}

/// @notice Burns `amount` of TBTC from the caller's balance. If `extraData`
/// is empty, transfers `amount` back to the caller's balance in the
/// Bank. If `extraData` is not empty, requests redemption in the
/// Bridge using the `extraData` as a `redemptionData` parameter to
/// Bridge's `receiveBalanceApproval` function.
/// @dev This function is doing the same as `unmint` or `unmintAndRedeem`
/// (depending on `extraData` parameter) but it allows to execute
/// unminting without a separate approval transaction. The function can
/// be called only via `approveAndCall` of TBTC token.
/// @param from TBTC token holder executing unminting.
/// @param amount Amount of TBTC to unmint.
/// @param token TBTC token address.
/// @param extraData Redemption data in a format expected from
/// `redemptionData` parameter of Bridge's `receiveBalanceApproval`
/// function. If empty, `receiveApproval` is not requesting a
/// redemption of Bank balance but is instead performing just TBTC
/// unminting to a Bank balance.
function receiveApproval(
address from,
uint256 amount,
address token,
bytes calldata
bytes calldata extraData
) external {
require(token == address(tbtcToken), "Token is not TBTC");
require(msg.sender == token, "Only TBTC caller allowed");
_unmint(from, amount);
if (extraData.length == 0) {
_unmint(from, amount);
} else {
_unmintAndRedeem(from, amount, extraData);
}
}

// slither-disable-next-line calls-loop
Expand All @@ -175,4 +203,14 @@ contract TBTCVault is IVault, Governable {
tbtcToken.burnFrom(unminter, amount);
bank.transferBalance(unminter, amount);
}

function _unmintAndRedeem(
address redeemer,
uint256 amount,
bytes calldata redemptionData
) internal {
emit Unminted(redeemer, amount);
tbtcToken.burnFrom(redeemer, amount);
bank.approveBalanceAndCall(bank.bridge(), amount, redemptionData);
}
}
10 changes: 5 additions & 5 deletions solidity/test/bridge/VendingMachine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
to1ePrecision,
getBlockTime,
} from "../helpers/contract-test-helpers"
import vendingMachineFixture from "../fixtures/vendingMachine"
import bridgeFixture from "../fixtures/bridge"

const ZERO_ADDRESS = ethers.constants.AddressZero

Expand Down Expand Up @@ -41,10 +41,10 @@ describe("VendingMachine", () => {
thirdParty,
] = await helpers.signers.getUnnamedSigners()

// eslint-disable-next-line @typescript-eslint/no-extra-semi
;({ tbtcV1, tbtcV2, vendingMachine } = await waffle.loadFixture(
vendingMachineFixture
))
await waffle.loadFixture(bridgeFixture)
tbtcV1 = await helpers.contracts.getContract("TBTCToken")
tbtcV2 = await helpers.contracts.getContract("TBTC")
vendingMachine = await helpers.contracts.getContract("VendingMachine")

await tbtcV1
.connect(deployer)
Expand Down
8 changes: 8 additions & 0 deletions solidity/test/fixtures/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {
BridgeStub,
IWalletRegistry,
TestRelay,
TBTC,
TBTCVault,
} from "../../typechain"

/**
Expand All @@ -19,6 +21,10 @@ export default async function bridgeFixture() {
await helpers.signers.getNamedSigners()
const [thirdParty] = await helpers.signers.getUnnamedSigners()

const tbtc: TBTC = await helpers.contracts.getContract("TBTC")

const tbtcVault: TBTCVault = await helpers.contracts.getContract("TBTCVault")
lukasz-zimnoch marked this conversation as resolved.
Show resolved Hide resolved

const bank: Bank & BankStub = await helpers.contracts.getContract("Bank")

const bridge: Bridge & BridgeStub = await helpers.contracts.getContract(
Expand Down Expand Up @@ -58,6 +64,8 @@ export default async function bridgeFixture() {
governance,
thirdParty,
treasury,
tbtc,
tbtcVault,
bank,
relay,
walletRegistry,
Expand Down
20 changes: 0 additions & 20 deletions solidity/test/fixtures/vendingMachine.ts

This file was deleted.

Loading