Skip to content

Commit

Permalink
Retrun initial deposit amount from _initializeDeposit function
Browse files Browse the repository at this point in the history
The amount may be useful for integrators for validations of the deposit
amount. See example use case in thesis/acre#253 (comment)

It turns out that this version where we read from the storage costs
`117 366` gas. Which is less than an alternative approach:
```
        initialDepositAmount =
            fundingTx
                .outputVector
                .extractOutputAtIndex(reveal.fundingOutputIndex)
                .extractValue() *
            SATOSHI_MULTIPLIER;
```
which costs `117 601` gas.
  • Loading branch information
nkuba committed Feb 27, 2024
1 parent c2d2591 commit ec3e81e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
10 changes: 7 additions & 3 deletions solidity/contracts/integrator/AbstractTBTCDepositor.sol
Original file line number Diff line number Diff line change
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
13 changes: 10 additions & 3 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 Down
4 changes: 3 additions & 1 deletion solidity/test/integrator/AbstractTBTCDepositor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,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 @@ -133,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

0 comments on commit ec3e81e

Please sign in to comment.