@keep-network/tbtc-v2 1.6.0-dev.15 → 1.6.0-dev.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/artifacts/BLS.json +1 -1
  2. package/artifacts/Bank.json +3 -3
  3. package/artifacts/BeaconAuthorization.json +1 -1
  4. package/artifacts/BeaconDkg.json +1 -1
  5. package/artifacts/BeaconDkgValidator.json +1 -1
  6. package/artifacts/BeaconInactivity.json +1 -1
  7. package/artifacts/BeaconSortitionPool.json +3 -3
  8. package/artifacts/Bridge.json +5 -5
  9. package/artifacts/BridgeGovernance.json +2 -2
  10. package/artifacts/BridgeGovernanceParameters.json +2 -2
  11. package/artifacts/Deposit.json +2 -2
  12. package/artifacts/DepositSweep.json +2 -2
  13. package/artifacts/DonationVault.json +3 -3
  14. package/artifacts/EcdsaDkgValidator.json +1 -1
  15. package/artifacts/EcdsaInactivity.json +1 -1
  16. package/artifacts/EcdsaSortitionPool.json +3 -3
  17. package/artifacts/Fraud.json +2 -2
  18. package/artifacts/LightRelay.json +18 -18
  19. package/artifacts/LightRelayMaintainerProxy.json +8 -8
  20. package/artifacts/MaintainerProxy.json +19 -19
  21. package/artifacts/MovingFunds.json +2 -2
  22. package/artifacts/NuCypherToken.json +2 -2
  23. package/artifacts/RandomBeacon.json +2 -2
  24. package/artifacts/RandomBeaconChaosnet.json +2 -2
  25. package/artifacts/RandomBeaconGovernance.json +2 -2
  26. package/artifacts/Redemption.json +2 -2
  27. package/artifacts/ReimbursementPool.json +2 -2
  28. package/artifacts/T.json +2 -2
  29. package/artifacts/TBTC.json +3 -3
  30. package/artifacts/TBTCToken.json +3 -3
  31. package/artifacts/TBTCVault.json +23 -23
  32. package/artifacts/TokenStaking.json +1 -1
  33. package/artifacts/TokenholderGovernor.json +9 -9
  34. package/artifacts/TokenholderTimelock.json +8 -8
  35. package/artifacts/VendingMachine.json +3 -3
  36. package/artifacts/VendingMachineNuCypher.json +1 -1
  37. package/artifacts/VendingMachineV2.json +3 -3
  38. package/artifacts/VendingMachineV3.json +3 -3
  39. package/artifacts/WalletProposalValidator.json +2 -2
  40. package/artifacts/WalletRegistry.json +5 -5
  41. package/artifacts/WalletRegistryGovernance.json +2 -2
  42. package/artifacts/Wallets.json +2 -2
  43. package/artifacts/solcInputs/{1788e81ec23cfa014c2c13441f296723.json → d86f3b9c8cafe2e77604a4fc30b88b09.json} +12 -0
  44. package/build/contracts/GovernanceUtils.sol/GovernanceUtils.dbg.json +1 -1
  45. package/build/contracts/bank/Bank.sol/Bank.dbg.json +1 -1
  46. package/build/contracts/bank/IReceiveBalanceApproval.sol/IReceiveBalanceApproval.dbg.json +1 -1
  47. package/build/contracts/bridge/BitcoinTx.sol/BitcoinTx.dbg.json +1 -1
  48. package/build/contracts/bridge/Bridge.sol/Bridge.dbg.json +1 -1
  49. package/build/contracts/bridge/BridgeGovernanceParameters.sol/BridgeGovernanceParameters.dbg.json +1 -1
  50. package/build/contracts/bridge/BridgeState.sol/BridgeState.dbg.json +1 -1
  51. package/build/contracts/bridge/Deposit.sol/Deposit.dbg.json +1 -1
  52. package/build/contracts/bridge/DepositSweep.sol/DepositSweep.dbg.json +1 -1
  53. package/build/contracts/bridge/EcdsaLib.sol/EcdsaLib.dbg.json +1 -1
  54. package/build/contracts/bridge/Fraud.sol/Fraud.dbg.json +1 -1
  55. package/build/contracts/bridge/Heartbeat.sol/Heartbeat.dbg.json +1 -1
  56. package/build/contracts/bridge/IRelay.sol/IRelay.dbg.json +1 -1
  57. package/build/contracts/bridge/MovingFunds.sol/MovingFunds.dbg.json +1 -1
  58. package/build/contracts/bridge/Redemption.sol/OutboundTx.dbg.json +1 -1
  59. package/build/contracts/bridge/Redemption.sol/Redemption.dbg.json +1 -1
  60. package/build/contracts/bridge/VendingMachine.sol/VendingMachine.dbg.json +1 -1
  61. package/build/contracts/bridge/VendingMachineV2.sol/VendingMachineV2.dbg.json +1 -1
  62. package/build/contracts/bridge/VendingMachineV3.sol/VendingMachineV3.dbg.json +1 -1
  63. package/build/contracts/bridge/WalletProposalValidator.sol/WalletProposalValidator.dbg.json +1 -1
  64. package/build/contracts/bridge/Wallets.sol/Wallets.dbg.json +1 -1
  65. package/build/contracts/integrator/AbstractTBTCDepositor.sol/AbstractTBTCDepositor.dbg.json +4 -0
  66. package/build/contracts/integrator/AbstractTBTCDepositor.sol/AbstractTBTCDepositor.json +113 -0
  67. package/build/contracts/integrator/IBridge.sol/IBridge.dbg.json +4 -0
  68. package/build/contracts/integrator/IBridge.sol/IBridge.json +172 -0
  69. package/build/contracts/integrator/IBridge.sol/IBridgeTypes.dbg.json +4 -0
  70. package/build/contracts/integrator/IBridge.sol/IBridgeTypes.json +10 -0
  71. package/build/contracts/integrator/ITBTCVault.sol/ITBTCVault.dbg.json +4 -0
  72. package/build/contracts/integrator/ITBTCVault.sol/ITBTCVault.json +48 -0
  73. package/build/contracts/l2/L2TBTC.sol/L2TBTC.dbg.json +1 -1
  74. package/build/contracts/l2/L2WormholeGateway.sol/IWormholeTokenBridge.dbg.json +1 -1
  75. package/build/contracts/l2/L2WormholeGateway.sol/L2WormholeGateway.dbg.json +1 -1
  76. package/build/contracts/maintainer/MaintainerProxy.sol/MaintainerProxy.dbg.json +1 -1
  77. package/build/contracts/relay/LightRelay.sol/ILightRelay.dbg.json +1 -1
  78. package/build/contracts/relay/LightRelay.sol/LightRelay.dbg.json +1 -1
  79. package/build/contracts/relay/LightRelay.sol/RelayUtils.dbg.json +1 -1
  80. package/build/contracts/relay/LightRelayMaintainerProxy.sol/LightRelayMaintainerProxy.dbg.json +1 -1
  81. package/build/contracts/token/TBTC.sol/TBTC.dbg.json +1 -1
  82. package/build/contracts/vault/DonationVault.sol/DonationVault.dbg.json +1 -1
  83. package/build/contracts/vault/IVault.sol/IVault.dbg.json +1 -1
  84. package/build/contracts/vault/TBTCOptimisticMinting.sol/TBTCOptimisticMinting.dbg.json +1 -1
  85. package/build/contracts/vault/TBTCVault.sol/TBTCVault.dbg.json +1 -1
  86. package/contracts/integrator/AbstractTBTCDepositor.sol +321 -0
  87. package/contracts/integrator/IBridge.sol +80 -0
  88. package/contracts/integrator/ITBTCVault.sol +28 -0
  89. package/export/artifacts/@keep-network/bitcoin-spv-sol/contracts/BTCUtils.sol/BTCUtils.json +20 -20
  90. package/export/artifacts/@keep-network/ecdsa/contracts/EcdsaDkgValidator.sol/EcdsaDkgValidator.json +1986 -1986
  91. package/export/artifacts/@keep-network/ecdsa/contracts/libraries/EcdsaDkg.sol/EcdsaDkg.json +20 -20
  92. package/export/artifacts/@keep-network/ecdsa/contracts/libraries/EcdsaInactivity.sol/EcdsaInactivity.json +1836 -1836
  93. package/export/artifacts/@keep-network/random-beacon/contracts/ReimbursementPool.sol/ReimbursementPool.json +545 -545
  94. package/export/artifacts/@keep-network/sortition-pools/contracts/Chaosnet.sol/Chaosnet.json +506 -506
  95. package/export/artifacts/@keep-network/sortition-pools/contracts/Rewards.sol/Rewards.json +23 -23
  96. package/export/artifacts/@keep-network/sortition-pools/contracts/SortitionPool.sol/SortitionPool.json +2456 -2456
  97. package/export/artifacts/@keep-network/sortition-pools/contracts/SortitionTree.sol/SortitionTree.json +465 -465
  98. package/export/artifacts/@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol/ERC1967Proxy.json +568 -568
  99. package/export/artifacts/@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol/ProxyAdmin.json +603 -603
  100. package/export/artifacts/@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol/TransparentUpgradeableProxy.json +1001 -1001
  101. package/export/artifacts/@openzeppelin/contracts/token/ERC721/ERC721.sol/ERC721.json +1877 -1877
  102. package/export/artifacts/@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol/ERC20Upgradeable.json +654 -654
  103. package/export/artifacts/@thesis/solidity-contracts/contracts/token/ERC20WithPermit.sol/ERC20WithPermit.json +2534 -2534
  104. package/export/artifacts/@thesis/solidity-contracts/contracts/token/MisfundRecovery.sol/MisfundRecovery.json +736 -736
  105. package/export/artifacts/contracts/bank/Bank.sol/Bank.json +1872 -1872
  106. package/export/artifacts/contracts/bridge/Bridge.sol/Bridge.json +7234 -7234
  107. package/export/artifacts/contracts/bridge/VendingMachine.sol/VendingMachine.json +1257 -1257
  108. package/export/artifacts/contracts/bridge/VendingMachineV2.sol/VendingMachineV2.json +972 -972
  109. package/export/artifacts/contracts/bridge/VendingMachineV3.sol/VendingMachineV3.json +1122 -1122
  110. package/export/artifacts/contracts/bridge/WalletProposalValidator.sol/WalletProposalValidator.json +5028 -5028
  111. package/export/artifacts/contracts/l2/L2TBTC.sol/L2TBTC.json +3002 -3002
  112. package/export/artifacts/contracts/l2/L2WormholeGateway.sol/L2WormholeGateway.json +2490 -2490
  113. package/export/artifacts/contracts/maintainer/MaintainerProxy.sol/MaintainerProxy.json +2456 -2456
  114. package/export/artifacts/contracts/relay/LightRelay.sol/LightRelay.json +1922 -1922
  115. package/export/artifacts/contracts/relay/LightRelayMaintainerProxy.sol/LightRelayMaintainerProxy.json +1013 -1013
  116. package/export/artifacts/contracts/test/BankStub.sol/BankStub.json +1874 -1874
  117. package/export/artifacts/contracts/test/BridgeStub.sol/BridgeStub.json +8259 -8259
  118. package/export/artifacts/contracts/test/HeartbeatStub.sol/HeartbeatStub.json +130 -130
  119. package/export/artifacts/contracts/test/LightRelayStub.sol/LightRelayStub.json +2047 -2047
  120. package/export/artifacts/contracts/test/ReceiveApprovalStub.sol/ReceiveApprovalStub.json +354 -354
  121. package/export/artifacts/contracts/test/SepoliaLightRelay.sol/SepoliaLightRelay.json +1924 -1924
  122. package/export/artifacts/contracts/test/SystemTestRelay.sol/SystemTestRelay.json +568 -568
  123. package/export/artifacts/contracts/test/TestBitcoinTx.sol/TestBitcoinTx.json +1249 -1249
  124. package/export/artifacts/contracts/test/TestERC20.sol/TestERC20.json +2291 -2291
  125. package/export/artifacts/contracts/test/TestERC721.sol/TestERC721.json +1698 -1698
  126. package/export/artifacts/contracts/test/TestEcdsaLib.sol/TestEcdsaLib.json +189 -189
  127. package/export/artifacts/contracts/test/TestTBTCDepositor.sol/MockBridge.json +6631 -0
  128. package/export/artifacts/contracts/test/TestTBTCDepositor.sol/MockTBTCVault.json +1523 -0
  129. package/export/artifacts/contracts/test/TestTBTCDepositor.sol/TestTBTCDepositor.json +11156 -0
  130. package/export/artifacts/contracts/test/WormholeBridgeStub.sol/WormholeBridgeStub.json +1447 -1447
  131. package/export/artifacts/contracts/token/TBTC.sol/TBTC.json +2847 -2847
  132. package/export/artifacts/contracts/vault/DonationVault.sol/DonationVault.json +858 -858
  133. package/export/artifacts/contracts/vault/TBTCVault.sol/TBTCVault.json +3489 -3489
  134. package/export/typechain/AbstractTBTCDepositor.js +2 -0
  135. package/export/typechain/IBridge.js +2 -0
  136. package/export/typechain/ITBTCVault.js +2 -0
  137. package/export/typechain/MockBridge.js +2 -0
  138. package/export/typechain/MockTBTCVault.js +2 -0
  139. package/export/typechain/TestTBTCDepositor.js +2 -0
  140. package/export/typechain/factories/AbstractTBTCDepositor__factory.js +124 -0
  141. package/export/typechain/factories/IBridge__factory.js +183 -0
  142. package/export/typechain/factories/ITBTCVault__factory.js +59 -0
  143. package/export/typechain/factories/MockBridge__factory.js +303 -0
  144. package/export/typechain/factories/MockTBTCVault__factory.js +166 -0
  145. package/export/typechain/factories/TestTBTCDepositor__factory.js +362 -0
  146. package/export/typechain/index.js +14 -1
  147. package/package.json +1 -1
@@ -0,0 +1,48 @@
1
+ {
2
+ "_format": "hh-sol-artifact-1",
3
+ "contractName": "ITBTCVault",
4
+ "sourceName": "contracts/integrator/ITBTCVault.sol",
5
+ "abi": [
6
+ {
7
+ "inputs": [],
8
+ "name": "optimisticMintingFeeDivisor",
9
+ "outputs": [
10
+ {
11
+ "internalType": "uint32",
12
+ "name": "",
13
+ "type": "uint32"
14
+ }
15
+ ],
16
+ "stateMutability": "view",
17
+ "type": "function"
18
+ },
19
+ {
20
+ "inputs": [
21
+ {
22
+ "internalType": "uint256",
23
+ "name": "depositKey",
24
+ "type": "uint256"
25
+ }
26
+ ],
27
+ "name": "optimisticMintingRequests",
28
+ "outputs": [
29
+ {
30
+ "internalType": "uint64",
31
+ "name": "requestedAt",
32
+ "type": "uint64"
33
+ },
34
+ {
35
+ "internalType": "uint64",
36
+ "name": "finalizedAt",
37
+ "type": "uint64"
38
+ }
39
+ ],
40
+ "stateMutability": "nonpayable",
41
+ "type": "function"
42
+ }
43
+ ],
44
+ "bytecode": "0x",
45
+ "deployedBytecode": "0x",
46
+ "linkReferences": {},
47
+ "deployedLinkReferences": {}
48
+ }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/b5f1e8cf3b3e0a9bb2a81d4dff3fdda7.json"
3
+ "buildInfo": "../../../build-info/fd90cfb34a89b5cd7ef101e64705ab37.json"
4
4
  }
@@ -0,0 +1,321 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+
3
+ // ██████████████ ▐████▌ ██████████████
4
+ // ██████████████ ▐████▌ ██████████████
5
+ // ▐████▌ ▐████▌
6
+ // ▐████▌ ▐████▌
7
+ // ██████████████ ▐████▌ ██████████████
8
+ // ██████████████ ▐████▌ ██████████████
9
+ // ▐████▌ ▐████▌
10
+ // ▐████▌ ▐████▌
11
+ // ▐████▌ ▐████▌
12
+ // ▐████▌ ▐████▌
13
+ // ▐████▌ ▐████▌
14
+ // ▐████▌ ▐████▌
15
+
16
+ pragma solidity ^0.8.0;
17
+
18
+ import {BTCUtils} from "@keep-network/bitcoin-spv-sol/contracts/BTCUtils.sol";
19
+
20
+ import "./IBridge.sol";
21
+ import "./ITBTCVault.sol";
22
+
23
+ /// @title Abstract AbstractTBTCDepositor contract.
24
+ /// @notice This abstract contract is meant to facilitate integration of protocols
25
+ /// aiming to use tBTC as an underlying Bitcoin bridge.
26
+ ///
27
+ /// Such an integrator is supposed to:
28
+ /// - Create a child contract inheriting from this abstract contract
29
+ /// - Call the `__AbstractTBTCDepositor_initialize` initializer function
30
+ /// - Use the `_initializeDeposit` and `_finalizeDeposit` as part of their
31
+ /// business logic in order to initialize and finalize deposits.
32
+ ///
33
+ /// @dev Example usage:
34
+ /// ```
35
+ /// // Example upgradeable integrator contract.
36
+ /// contract ExampleTBTCIntegrator is AbstractTBTCDepositor, Initializable {
37
+ /// /// @custom:oz-upgrades-unsafe-allow constructor
38
+ /// constructor() {
39
+ /// // Prevents the contract from being initialized again.
40
+ /// _disableInitializers();
41
+ /// }
42
+ ///
43
+ /// function initialize(
44
+ /// address _bridge,
45
+ /// address _tbtcVault
46
+ /// ) external initializer {
47
+ /// __AbstractTBTCDepositor_initialize(_bridge, _tbtcVault);
48
+ /// }
49
+ ///
50
+ /// function startProcess(
51
+ /// IBridgeTypes.BitcoinTxInfo calldata fundingTx,
52
+ /// IBridgeTypes.DepositRevealInfo calldata reveal
53
+ /// ) external {
54
+ /// // Embed necessary context as extra data.
55
+ /// bytes32 extraData = ...;
56
+ ///
57
+ /// uint256 depositKey = _initializeDeposit(
58
+ /// fundingTx,
59
+ /// reveal,
60
+ /// extraData
61
+ /// );
62
+ ///
63
+ /// // Use the depositKey to track the process.
64
+ /// }
65
+ ///
66
+ /// function finalizeProcess(uint256 depositKey) external {
67
+ /// (
68
+ /// uint256 initialDepositAmount,
69
+ /// uint256 tbtcAmount,
70
+ /// bytes32 extraData
71
+ /// ) = _finalizeDeposit(depositKey);
72
+ ///
73
+ /// // Do something with the minted TBTC using context
74
+ /// // embedded in the extraData.
75
+ /// }
76
+ /// }
77
+ abstract contract AbstractTBTCDepositor {
78
+ using BTCUtils for bytes;
79
+
80
+ /// @notice Multiplier to convert satoshi to TBTC token units.
81
+ uint256 public constant SATOSHI_MULTIPLIER = 10**10;
82
+
83
+ /// @notice Bridge contract address.
84
+ IBridge public bridge;
85
+ /// @notice TBTCVault contract address.
86
+ ITBTCVault public tbtcVault;
87
+ /// @notice Mapping holding information about pending deposits that have
88
+ /// been initialized but not finalized yet. If the deposit is not
89
+ /// in this mapping it means it has already been finalized or it
90
+ /// has not been initialized yet.
91
+ mapping(uint256 => bool) public pendingDeposits;
92
+
93
+ // Reserved storage space that allows adding more variables without affecting
94
+ // the storage layout of the child contracts. The convention from OpenZeppelin
95
+ // suggests the storage space should add up to 50 slots. If more variables are
96
+ // added in the upcoming versions one need to reduce the array size accordingly.
97
+ // See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
98
+ // slither-disable-next-line unused-state
99
+ uint256[47] private __gap;
100
+
101
+ event DepositInitialized(uint256 indexed depositKey, uint32 initializedAt);
102
+
103
+ event DepositFinalized(
104
+ uint256 indexed depositKey,
105
+ uint256 tbtcAmount,
106
+ uint32 finalizedAt
107
+ );
108
+
109
+ /// @notice Initializes the contract. MUST BE CALLED from the child
110
+ /// contract initializer.
111
+ // slither-disable-next-line dead-code
112
+ function __AbstractTBTCDepositor_initialize(
113
+ address _bridge,
114
+ address _tbtcVault
115
+ ) internal {
116
+ require(
117
+ address(bridge) == address(0) && address(tbtcVault) == address(0),
118
+ "AbstractTBTCDepositor already initialized"
119
+ );
120
+
121
+ require(_bridge != address(0), "Bridge address cannot be zero");
122
+ require(_tbtcVault != address(0), "TBTCVault address cannot be zero");
123
+
124
+ bridge = IBridge(_bridge);
125
+ tbtcVault = ITBTCVault(_tbtcVault);
126
+ }
127
+
128
+ /// @notice Initializes a deposit by revealing it to the Bridge.
129
+ /// @param fundingTx Bitcoin funding transaction data, see `IBridgeTypes.BitcoinTxInfo`.
130
+ /// @param reveal Deposit reveal data, see `IBridgeTypes.DepositRevealInfo` struct.
131
+ /// @param extraData 32-byte deposit extra data.
132
+ /// @return depositKey Deposit key computed as
133
+ /// `keccak256(fundingTxHash | reveal.fundingOutputIndex)`. This
134
+ /// key can be used to refer to the deposit in the Bridge and
135
+ /// TBTCVault contracts.
136
+ /// @dev Requirements:
137
+ /// - The revealed vault address must match the TBTCVault address,
138
+ /// - All requirements from {Bridge#revealDepositWithExtraData}
139
+ /// function must be met.
140
+ // slither-disable-next-line dead-code
141
+ function _initializeDeposit(
142
+ IBridgeTypes.BitcoinTxInfo calldata fundingTx,
143
+ IBridgeTypes.DepositRevealInfo calldata reveal,
144
+ bytes32 extraData
145
+ ) internal returns (uint256) {
146
+ require(reveal.vault == address(tbtcVault), "Vault address mismatch");
147
+
148
+ uint256 depositKey = _calculateDepositKey(
149
+ _calculateBitcoinTxHash(fundingTx),
150
+ reveal.fundingOutputIndex
151
+ );
152
+
153
+ pendingDeposits[depositKey] = true;
154
+
155
+ emit DepositInitialized(
156
+ depositKey,
157
+ /* solhint-disable-next-line not-rely-on-time */
158
+ uint32(block.timestamp)
159
+ );
160
+
161
+ // The Bridge does not allow to reveal the same deposit twice and
162
+ // revealed deposits stay there forever. The transaction will revert
163
+ // if the deposit has already been revealed so, there is no need to do
164
+ // an explicit check here.
165
+ bridge.revealDepositWithExtraData(fundingTx, reveal, extraData);
166
+
167
+ return depositKey;
168
+ }
169
+
170
+ /// @notice Finalizes a deposit by calculating the amount of TBTC minted
171
+ /// for the deposit
172
+ /// @param depositKey Deposit key identifying the deposit.
173
+ /// @return initialDepositAmount Amount of funding transaction deposit. In
174
+ /// TBTC token decimals precision.
175
+ /// @return tbtcAmount Approximate amount of TBTC minted for the deposit. In
176
+ /// TBTC token decimals precision.
177
+ /// @return extraData 32-byte deposit extra data.
178
+ /// @dev Requirements:
179
+ /// - The deposit must be initialized but not finalized
180
+ /// (in the context of this contract) yet.
181
+ /// - The deposit must be finalized on the Bridge side. That means the
182
+ /// deposit must be either swept or optimistically minted.
183
+ /// @dev IMPORTANT NOTE: The tbtcAmount returned by this function is an
184
+ /// approximation. See documentation of the `calculateTbtcAmount`
185
+ /// responsible for calculating this value for more details.
186
+ // slither-disable-next-line dead-code
187
+ function _finalizeDeposit(uint256 depositKey)
188
+ internal
189
+ returns (
190
+ uint256 initialDepositAmount,
191
+ uint256 tbtcAmount,
192
+ bytes32 extraData
193
+ )
194
+ {
195
+ require(pendingDeposits[depositKey], "Deposit not initialized");
196
+
197
+ IBridgeTypes.DepositRequest memory deposit = bridge.deposits(
198
+ depositKey
199
+ );
200
+ (, uint64 finalizedAt) = tbtcVault.optimisticMintingRequests(
201
+ depositKey
202
+ );
203
+
204
+ require(
205
+ deposit.sweptAt != 0 || finalizedAt != 0,
206
+ "Deposit not finalized by the bridge"
207
+ );
208
+
209
+ // We can safely delete the deposit from the pending deposits mapping.
210
+ // This deposit cannot be initialized again because the bridge does not
211
+ // allow to reveal the same deposit twice. Deleting the deposit from
212
+ // the mapping will also prevent the finalizeDeposit function from
213
+ // being called again for the same deposit.
214
+ // slither-disable-next-line reentrancy-no-eth
215
+ delete pendingDeposits[depositKey];
216
+
217
+ initialDepositAmount = deposit.amount * SATOSHI_MULTIPLIER;
218
+
219
+ tbtcAmount = _calculateTbtcAmount(deposit.amount, deposit.treasuryFee);
220
+
221
+ // slither-disable-next-line reentrancy-events
222
+ emit DepositFinalized(
223
+ depositKey,
224
+ tbtcAmount,
225
+ /* solhint-disable-next-line not-rely-on-time */
226
+ uint32(block.timestamp)
227
+ );
228
+
229
+ extraData = deposit.extraData;
230
+ }
231
+
232
+ /// @notice Calculates the amount of TBTC minted for the deposit.
233
+ /// @param depositAmountSat Deposit amount in satoshi (1e8 precision).
234
+ /// This is the actual amount deposited by the deposit creator, i.e.
235
+ /// the gross amount the Bridge's fees are cut from.
236
+ /// @param depositTreasuryFeeSat Deposit treasury fee in satoshi (1e8 precision).
237
+ /// This is an accurate value of the treasury fee that was actually
238
+ /// cut upon minting.
239
+ /// @return tbtcAmount Approximate amount of TBTC minted for the deposit.
240
+ /// @dev IMPORTANT NOTE: The tbtcAmount returned by this function may
241
+ /// not correspond to the actual amount of TBTC minted for the deposit.
242
+ /// Although the treasury fee cut upon minting is known precisely,
243
+ /// this is not the case for the optimistic minting fee and the Bitcoin
244
+ /// transaction fee. To overcome that problem, this function just takes
245
+ /// the current maximum allowed values of both fees, at the moment of deposit
246
+ /// finalization. For the great majority of the deposits, such an
247
+ /// algorithm will return a tbtcAmount slightly lesser than the
248
+ /// actual amount of TBTC minted for the deposit. This will cause
249
+ /// some TBTC to be left in the contract and ensure there is enough
250
+ /// liquidity to finalize the deposit. However, in some rare cases,
251
+ /// where the actual values of those fees change between the deposit
252
+ /// minting and finalization, the tbtcAmount returned by this function
253
+ /// may be greater than the actual amount of TBTC minted for the deposit.
254
+ /// If this happens and the reserve coming from previous deposits
255
+ /// leftovers does not provide enough liquidity, the deposit will have
256
+ /// to wait for finalization until the reserve is refilled by subsequent
257
+ /// deposits or a manual top-up. The integrator is responsible for
258
+ /// handling such cases.
259
+ // slither-disable-next-line dead-code
260
+ function _calculateTbtcAmount(
261
+ uint64 depositAmountSat,
262
+ uint64 depositTreasuryFeeSat
263
+ ) internal view virtual returns (uint256) {
264
+ // Both deposit amount and treasury fee are in the 1e8 satoshi precision.
265
+ // We need to convert them to the 1e18 TBTC precision.
266
+ uint256 amountSubTreasury = (depositAmountSat - depositTreasuryFeeSat) *
267
+ SATOSHI_MULTIPLIER;
268
+
269
+ uint256 omFeeDivisor = tbtcVault.optimisticMintingFeeDivisor();
270
+ uint256 omFee = omFeeDivisor > 0
271
+ ? (amountSubTreasury / omFeeDivisor)
272
+ : 0;
273
+
274
+ // The deposit transaction max fee is in the 1e8 satoshi precision.
275
+ // We need to convert them to the 1e18 TBTC precision.
276
+ (, , uint64 depositTxMaxFee, ) = bridge.depositParameters();
277
+ uint256 txMaxFee = depositTxMaxFee * SATOSHI_MULTIPLIER;
278
+
279
+ return amountSubTreasury - omFee - txMaxFee;
280
+ }
281
+
282
+ /// @notice Calculates the deposit key for the given funding transaction
283
+ /// hash and funding output index.
284
+ /// @param fundingTxHash Funding transaction hash.
285
+ /// @param fundingOutputIndex Funding output index.
286
+ /// @return depositKey Deposit key computed as
287
+ /// `keccak256(fundingTxHash | reveal.fundingOutputIndex)`. This
288
+ /// key can be used to refer to the deposit in the Bridge and
289
+ /// TBTCVault contracts.
290
+ // slither-disable-next-line dead-code
291
+ function _calculateDepositKey(
292
+ bytes32 fundingTxHash,
293
+ uint32 fundingOutputIndex
294
+ ) internal pure returns (uint256) {
295
+ return
296
+ uint256(
297
+ keccak256(abi.encodePacked(fundingTxHash, fundingOutputIndex))
298
+ );
299
+ }
300
+
301
+ /// @notice Calculates the Bitcoin transaction hash for the given Bitcoin
302
+ /// transaction data.
303
+ /// @param txInfo Bitcoin transaction data, see `IBridgeTypes.BitcoinTxInfo` struct.
304
+ /// @return txHash Bitcoin transaction hash.
305
+ // slither-disable-next-line dead-code
306
+ function _calculateBitcoinTxHash(IBridgeTypes.BitcoinTxInfo calldata txInfo)
307
+ internal
308
+ view
309
+ returns (bytes32)
310
+ {
311
+ return
312
+ abi
313
+ .encodePacked(
314
+ txInfo.version,
315
+ txInfo.inputVector,
316
+ txInfo.outputVector,
317
+ txInfo.locktime
318
+ )
319
+ .hash256View();
320
+ }
321
+ }
@@ -0,0 +1,80 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+
3
+ // ██████████████ ▐████▌ ██████████████
4
+ // ██████████████ ▐████▌ ██████████████
5
+ // ▐████▌ ▐████▌
6
+ // ▐████▌ ▐████▌
7
+ // ██████████████ ▐████▌ ██████████████
8
+ // ██████████████ ▐████▌ ██████████████
9
+ // ▐████▌ ▐████▌
10
+ // ▐████▌ ▐████▌
11
+ // ▐████▌ ▐████▌
12
+ // ▐████▌ ▐████▌
13
+ // ▐████▌ ▐████▌
14
+ // ▐████▌ ▐████▌
15
+
16
+ pragma solidity ^0.8.0;
17
+
18
+ /// @notice Namespace which groups all types relevant to the IBridge interface.
19
+ /// @dev This is a mirror of the real types used in the Bridge contract.
20
+ /// This way, the `integrator` subpackage does not need to import
21
+ /// anything from the `bridge` subpackage and explicitly depend on it.
22
+ /// This simplifies the dependency graph for integrators.
23
+ library IBridgeTypes {
24
+ /// @dev See bridge/BitcoinTx.sol#Info
25
+ struct BitcoinTxInfo {
26
+ bytes4 version;
27
+ bytes inputVector;
28
+ bytes outputVector;
29
+ bytes4 locktime;
30
+ }
31
+
32
+ /// @dev See bridge/Deposit.sol#DepositRevealInfo
33
+ struct DepositRevealInfo {
34
+ uint32 fundingOutputIndex;
35
+ bytes8 blindingFactor;
36
+ bytes20 walletPubKeyHash;
37
+ bytes20 refundPubKeyHash;
38
+ bytes4 refundLocktime;
39
+ address vault;
40
+ }
41
+
42
+ /// @dev See bridge/Deposit.sol#DepositRequest
43
+ struct DepositRequest {
44
+ address depositor;
45
+ uint64 amount;
46
+ uint32 revealedAt;
47
+ address vault;
48
+ uint64 treasuryFee;
49
+ uint32 sweptAt;
50
+ bytes32 extraData;
51
+ }
52
+ }
53
+
54
+ /// @notice Interface of the Bridge contract.
55
+ /// @dev See bridge/Bridge.sol
56
+ interface IBridge {
57
+ /// @dev See {Bridge#revealDepositWithExtraData}
58
+ function revealDepositWithExtraData(
59
+ IBridgeTypes.BitcoinTxInfo calldata fundingTx,
60
+ IBridgeTypes.DepositRevealInfo calldata reveal,
61
+ bytes32 extraData
62
+ ) external;
63
+
64
+ /// @dev See {Bridge#deposits}
65
+ function deposits(uint256 depositKey)
66
+ external
67
+ view
68
+ returns (IBridgeTypes.DepositRequest memory);
69
+
70
+ /// @dev See {Bridge#depositParameters}
71
+ function depositParameters()
72
+ external
73
+ view
74
+ returns (
75
+ uint64 depositDustThreshold,
76
+ uint64 depositTreasuryFeeDivisor,
77
+ uint64 depositTxMaxFee,
78
+ uint32 depositRevealAheadPeriod
79
+ );
80
+ }
@@ -0,0 +1,28 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+
3
+ // ██████████████ ▐████▌ ██████████████
4
+ // ██████████████ ▐████▌ ██████████████
5
+ // ▐████▌ ▐████▌
6
+ // ▐████▌ ▐████▌
7
+ // ██████████████ ▐████▌ ██████████████
8
+ // ██████████████ ▐████▌ ██████████████
9
+ // ▐████▌ ▐████▌
10
+ // ▐████▌ ▐████▌
11
+ // ▐████▌ ▐████▌
12
+ // ▐████▌ ▐████▌
13
+ // ▐████▌ ▐████▌
14
+ // ▐████▌ ▐████▌
15
+
16
+ pragma solidity ^0.8.0;
17
+
18
+ /// @notice Interface of the TBTCVault contract.
19
+ /// @dev See vault/TBTCVault.sol
20
+ interface ITBTCVault {
21
+ /// @dev See {TBTCVault#optimisticMintingRequests}
22
+ function optimisticMintingRequests(uint256 depositKey)
23
+ external
24
+ returns (uint64 requestedAt, uint64 finalizedAt);
25
+
26
+ /// @dev See {TBTCVault#optimisticMintingFeeDivisor}
27
+ function optimisticMintingFeeDivisor() external view returns (uint32);
28
+ }