Skip to content

Commit

Permalink
Merge branch 'main' into optimistic-redemption-1
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-zimnoch authored Feb 29, 2024
2 parents 2556c28 + 9e047d1 commit b6aef91
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
24 changes: 20 additions & 4 deletions solidity/contracts/integrator/AbstractTBTCDepositor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import "./ITBTCVault.sol";
/// // Embed necessary context as extra data.
/// bytes32 extraData = ...;
///
/// uint256 depositKey = _initializeDeposit(
/// (uint256 depositKey, uint256 initialDepositAmount) = _initializeDeposit(
/// fundingTx,
/// reveal,
/// extraData
Expand Down Expand Up @@ -123,6 +123,8 @@ abstract contract AbstractTBTCDepositor {
/// `keccak256(fundingTxHash | reveal.fundingOutputIndex)`. This
/// key can be used to refer to the deposit in the Bridge and
/// TBTCVault contracts.
/// @return initialDepositAmount Amount of funding transaction deposit. In
/// TBTC token decimals precision.
/// @dev Requirements:
/// - The revealed vault address must match the TBTCVault address,
/// - All requirements from {Bridge#revealDepositWithExtraData}
Expand All @@ -134,10 +136,10 @@ abstract contract AbstractTBTCDepositor {
IBridgeTypes.BitcoinTxInfo calldata fundingTx,
IBridgeTypes.DepositRevealInfo calldata reveal,
bytes32 extraData
) internal returns (uint256) {
) internal returns (uint256 depositKey, uint256 initialDepositAmount) {
require(reveal.vault == address(tbtcVault), "Vault address mismatch");

uint256 depositKey = _calculateDepositKey(
depositKey = _calculateDepositKey(
_calculateBitcoinTxHash(fundingTx),
reveal.fundingOutputIndex
);
Expand All @@ -148,7 +150,9 @@ abstract contract AbstractTBTCDepositor {
// an explicit check here.
bridge.revealDepositWithExtraData(fundingTx, reveal, extraData);

return depositKey;
initialDepositAmount =
bridge.deposits(depositKey).amount *
SATOSHI_MULTIPLIER;
}

/// @notice Finalizes a deposit by calculating the amount of TBTC minted
Expand Down Expand Up @@ -289,4 +293,16 @@ abstract contract AbstractTBTCDepositor {
)
.hash256View();
}

/// @notice Returns minimum deposit amount.
/// @return Minimum deposit amount. In TBTC token decimals precision.
// slither-disable-next-line dead-code
function _minDepositAmount() internal view returns (uint256) {
// Read tBTC Bridge Deposit Dust Threshold in satoshi precision.
(uint64 bridgeDepositDustThresholdSat, , , ) = bridge
.depositParameters();

// Convert tBTC Bridge Deposit Dust Threshold to TBTC token precision.
return bridgeDepositDustThresholdSat * SATOSHI_MULTIPLIER;
}
}
26 changes: 21 additions & 5 deletions solidity/contracts/test/TestTBTCDepositor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import "../integrator/ITBTCVault.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract TestTBTCDepositor is AbstractTBTCDepositor {
event InitializeDepositReturned(uint256 depositKey);
event InitializeDepositReturned(
uint256 depositKey,
uint256 initialDepositAmount
);

event FinalizeDepositReturned(
uint256 initialDepositAmount,
Expand All @@ -28,8 +31,12 @@ contract TestTBTCDepositor is AbstractTBTCDepositor {
IBridgeTypes.DepositRevealInfo calldata reveal,
bytes32 extraData
) external {
uint256 depositKey = _initializeDeposit(fundingTx, reveal, extraData);
emit InitializeDepositReturned(depositKey);
(uint256 depositKey, uint256 initialDepositAmount) = _initializeDeposit(
fundingTx,
reveal,
extraData
);
emit InitializeDepositReturned(depositKey, initialDepositAmount);
}

function finalizeDepositPublic(uint256 depositKey) external {
Expand All @@ -51,13 +58,18 @@ contract TestTBTCDepositor is AbstractTBTCDepositor {
) external view returns (uint256) {
return _calculateTbtcAmount(depositAmountSat, depositTreasuryFeeSat);
}

function minDepositAmountPublic() external view returns (uint256) {
return _minDepositAmount();
}
}

contract MockBridge is IBridge {
using BTCUtils for bytes;

mapping(uint256 => IBridgeTypes.DepositRequest) internal _deposits;

uint64 internal _depositDustThreshold = 1000000; // 1000000 satoshi = 0.01 BTC
uint64 internal _depositTreasuryFeeDivisor = 50; // 1/50 == 100 bps == 2% == 0.02
uint64 internal _depositTxMaxFee = 1000; // 1000 satoshi = 0.00001 BTC

Expand Down Expand Up @@ -137,12 +149,16 @@ contract MockBridge is IBridge {
uint32 depositRevealAheadPeriod
)
{
depositDustThreshold = 0;
depositTreasuryFeeDivisor = 0;
depositDustThreshold = _depositDustThreshold;
depositTreasuryFeeDivisor = _depositTreasuryFeeDivisor;
depositTxMaxFee = _depositTxMaxFee;
depositRevealAheadPeriod = 0;
}

function setDepositDustThreshold(uint64 value) external {
_depositDustThreshold = value;
}

function setDepositTreasuryFeeDivisor(uint64 value) external {
_depositTreasuryFeeDivisor = value;
}
Expand Down
25 changes: 23 additions & 2 deletions solidity/test/integrator/AbstractTBTCDepositor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
import { to1ePrecision } from "../helpers/contract-test-helpers"

const { createSnapshot, restoreSnapshot } = helpers.snapshot
const { lastBlockTime } = helpers.time

const loadFixture = (vault: string) => ({
fundingTx: {
Expand Down Expand Up @@ -109,6 +108,8 @@ describe("AbstractTBTCDepositor", () => {
})

context("when deposit is accepted by the Bridge", () => {
const expectedInitialDepositAmount = to1ePrecision(10000, 10)

let tx: ContractTransaction

before(async () => {
Expand All @@ -134,7 +135,7 @@ describe("AbstractTBTCDepositor", () => {
it("should return proper values", async () => {
await expect(tx)
.to.emit(depositor, "InitializeDepositReturned")
.withArgs(fixture.expectedDepositKey)
.withArgs(fixture.expectedDepositKey, expectedInitialDepositAmount)
})
})
})
Expand Down Expand Up @@ -438,4 +439,24 @@ describe("AbstractTBTCDepositor", () => {
})
})
})

describe("_minDepositAmount", () => {
before(async () => {
await createSnapshot()

// Set deposit dust threshold to 0.1 BTC.
await bridge.setDepositDustThreshold(1000000)
})

after(async () => {
await restoreSnapshot()
})

it("returns value in TBTC token precision", async () => {
// 1000000 sat * 1e10 TBTC
expect(await depositor.minDepositAmountPublic()).to.be.equal(
to1ePrecision(1000000, 10)
)
})
})
})

0 comments on commit b6aef91

Please sign in to comment.