@keep-network/tbtc-v2 0.1.0 → 0.1.1-dev.3

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.
@@ -0,0 +1,339 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ // ██████████████ ▐████▌ ██████████████
4
+ // ██████████████ ▐████▌ ██████████████
5
+ // ▐████▌ ▐████▌
6
+ // ▐████▌ ▐████▌
7
+ // ██████████████ ▐████▌ ██████████████
8
+ // ██████████████ ▐████▌ ██████████████
9
+ // ▐████▌ ▐████▌
10
+ // ▐████▌ ▐████▌
11
+ // ▐████▌ ▐████▌
12
+ // ▐████▌ ▐████▌
13
+ // ▐████▌ ▐████▌
14
+ // ▐████▌ ▐████▌
15
+ pragma solidity 0.8.4;
16
+
17
+ import "@openzeppelin/contracts/access/Ownable.sol";
18
+
19
+ /// @title Bitcoin Bank
20
+ /// @notice Bank is a central component tracking Bitcoin balances. Balances can
21
+ /// be transferred between holders and holders can approve their
22
+ /// balances to be spent by others. Balances in the Bank are updated for
23
+ /// depositors who deposit their Bitcoin into the Bridge and only the
24
+ /// Bridge can increase balances.
25
+ /// @dev Bank is a governable contract and the Governance can upgrade the Bridge
26
+ /// address.
27
+ contract Bank is Ownable {
28
+ address public bridge;
29
+
30
+ /// @notice The balance of a given account in the Bank. Zero by default.
31
+ mapping(address => uint256) public balanceOf;
32
+
33
+ /// @notice The remaining amount of balance a spender will be
34
+ /// allowed to transfer on behalf of an owner using
35
+ /// `transferBalanceFrom`. Zero by default.
36
+ mapping(address => mapping(address => uint256)) public allowance;
37
+
38
+ /// @notice Returns the current nonce for EIP2612 permission for the
39
+ /// provided balance owner for a replay protection. Used to
40
+ /// construct EIP2612 signature provided to `permit` function.
41
+ mapping(address => uint256) public nonce;
42
+
43
+ uint256 public immutable cachedChainId;
44
+ bytes32 public immutable cachedDomainSeparator;
45
+
46
+ /// @notice Returns EIP2612 Permit message hash. Used to construct EIP2612
47
+ /// signature provided to `permit` function.
48
+ bytes32 public constant PERMIT_TYPEHASH =
49
+ keccak256(
50
+ "Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
51
+ );
52
+
53
+ event BalanceTransferred(
54
+ address indexed from,
55
+ address indexed to,
56
+ uint256 amount
57
+ );
58
+
59
+ event BalanceApproved(
60
+ address indexed owner,
61
+ address indexed spender,
62
+ uint256 amount
63
+ );
64
+
65
+ event BalanceIncreased(address indexed owner, uint256 amount);
66
+
67
+ event BalanceDecreased(address indexed owner, uint256 amount);
68
+
69
+ event BridgeUpdated(address newBridge);
70
+
71
+ modifier onlyBridge() {
72
+ require(msg.sender == address(bridge), "Caller is not the bridge");
73
+ _;
74
+ }
75
+
76
+ constructor() {
77
+ cachedChainId = block.chainid;
78
+ cachedDomainSeparator = buildDomainSeparator();
79
+ }
80
+
81
+ /// @notice Allows the Governance to upgrade the Bridge address.
82
+ /// @dev The function does not implement any governance delay and does not
83
+ /// check the status of the Bridge. The Governance implementation needs
84
+ /// to ensure all requirements for the upgrade are satisfied before
85
+ /// executing this function.
86
+ function updateBridge(address _bridge) external onlyOwner {
87
+ require(_bridge != address(0), "Bridge address must not be 0x0");
88
+ bridge = _bridge;
89
+ emit BridgeUpdated(_bridge);
90
+ }
91
+
92
+ /// @notice Moves the given `amount` of balance from the caller to
93
+ /// `recipient`.
94
+ /// @dev Requirements:
95
+ /// - `recipient` cannot be the zero address,
96
+ /// - the caller must have a balance of at least `amount`.
97
+ function transferBalance(address recipient, uint256 amount) external {
98
+ _transferBalance(msg.sender, recipient, amount);
99
+ }
100
+
101
+ /// @notice Sets `amount` as the allowance of `spender` over the caller's
102
+ /// balance.
103
+ /// @dev If the `amount` is set to `type(uint256).max` then
104
+ /// `transferBalanceFrom` will not reduce an allowance.
105
+ /// Beware that changing an allowance with this function brings the
106
+ /// risk that someone may use both the old and the new allowance by
107
+ /// unfortunate transaction ordering. Please use
108
+ /// `increaseBalanceAllowance` and `decreaseBalanceAllowance` to
109
+ /// eliminate the risk.
110
+ function approveBalance(address spender, uint256 amount) external {
111
+ _approveBalance(msg.sender, spender, amount);
112
+ }
113
+
114
+ /// @notice Atomically increases the balance allowance granted to `spender`
115
+ /// by the caller by the given `addedValue`.
116
+ function increaseBalanceAllowance(address spender, uint256 addedValue)
117
+ external
118
+ {
119
+ _approveBalance(
120
+ msg.sender,
121
+ spender,
122
+ allowance[msg.sender][spender] + addedValue
123
+ );
124
+ }
125
+
126
+ /// @notice Atomically decreases the balance allowance granted to `spender`
127
+ /// by the caller by the given `subtractedValue`.
128
+ function decreaseBalanceAllowance(address spender, uint256 subtractedValue)
129
+ external
130
+ {
131
+ uint256 currentAllowance = allowance[msg.sender][spender];
132
+ require(
133
+ currentAllowance >= subtractedValue,
134
+ "Can not decrease balance allowance below zero"
135
+ );
136
+ unchecked {
137
+ _approveBalance(
138
+ msg.sender,
139
+ spender,
140
+ currentAllowance - subtractedValue
141
+ );
142
+ }
143
+ }
144
+
145
+ /// @notice Moves `amount` of balance from `spender` to `recipient` using the
146
+ /// allowance mechanism. `amount` is then deducted from the caller's
147
+ /// allowance unless the allowance was made for `type(uint256).max`.
148
+ /// @dev Requirements:
149
+ /// - `recipient` cannot be the zero address,
150
+ /// - `spender` must have a balance of at least `amount`,
151
+ /// - the caller must have allowance for `spender`'s balance of at
152
+ /// least `amount`.
153
+ function transferBalanceFrom(
154
+ address spender,
155
+ address recipient,
156
+ uint256 amount
157
+ ) external {
158
+ uint256 currentAllowance = allowance[spender][msg.sender];
159
+ if (currentAllowance != type(uint256).max) {
160
+ require(
161
+ currentAllowance >= amount,
162
+ "Transfer amount exceeds allowance"
163
+ );
164
+ unchecked {
165
+ _approveBalance(spender, msg.sender, currentAllowance - amount);
166
+ }
167
+ }
168
+ _transferBalance(spender, recipient, amount);
169
+ }
170
+
171
+ /// @notice EIP2612 approval made with secp256k1 signature.
172
+ /// Users can authorize a transfer of their balance with a signature
173
+ /// conforming EIP712 standard, rather than an on-chain transaction
174
+ /// from their address. Anyone can submit this signature on the
175
+ /// user's behalf by calling the permit function, paying gas fees,
176
+ /// and possibly performing other actions in the same transaction.
177
+ /// @dev The deadline argument can be set to `type(uint256).max to create
178
+ /// permits that effectively never expire. If the `amount` is set
179
+ /// to `type(uint256).max` then `transferBalanceFrom` will not
180
+ /// reduce an allowance. Beware that changing an allowance with this
181
+ /// function brings the risk that someone may use both the old and the
182
+ /// new allowance by unfortunate transaction ordering. Please use
183
+ /// `increaseBalanceAllowance` and `decreaseBalanceAllowance` to
184
+ /// eliminate the risk.
185
+ function permit(
186
+ address owner,
187
+ address spender,
188
+ uint256 amount,
189
+ uint256 deadline,
190
+ uint8 v,
191
+ bytes32 r,
192
+ bytes32 s
193
+ ) external {
194
+ /* solhint-disable-next-line not-rely-on-time */
195
+ require(deadline >= block.timestamp, "Permission expired");
196
+
197
+ // Validate `s` and `v` values for a malleability concern described in EIP2.
198
+ // Only signatures with `s` value in the lower half of the secp256k1
199
+ // curve's order and `v` value of 27 or 28 are considered valid.
200
+ require(
201
+ uint256(s) <=
202
+ 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
203
+ "Invalid signature 's' value"
204
+ );
205
+ require(v == 27 || v == 28, "Invalid signature 'v' value");
206
+
207
+ bytes32 digest =
208
+ keccak256(
209
+ abi.encodePacked(
210
+ "\x19\x01",
211
+ DOMAIN_SEPARATOR(),
212
+ keccak256(
213
+ abi.encode(
214
+ PERMIT_TYPEHASH,
215
+ owner,
216
+ spender,
217
+ amount,
218
+ nonce[owner]++,
219
+ deadline
220
+ )
221
+ )
222
+ )
223
+ );
224
+ address recoveredAddress = ecrecover(digest, v, r, s);
225
+ require(
226
+ recoveredAddress != address(0) && recoveredAddress == owner,
227
+ "Invalid signature"
228
+ );
229
+ _approveBalance(owner, spender, amount);
230
+ }
231
+
232
+ /// @notice Increases balances of the provided `recipients` by the provided
233
+ /// `amounts`. Can only be called by the Bridge.
234
+ /// @dev This function fails if the lengths of the arrays are not the same.
235
+ function increaseBalances(
236
+ address[] calldata recipients,
237
+ uint256[] calldata amounts
238
+ ) external onlyBridge {
239
+ require(
240
+ recipients.length == amounts.length,
241
+ "Arrays must have the same length"
242
+ );
243
+ for (uint256 i = 0; i < recipients.length; i++) {
244
+ _increaseBalance(recipients[i], amounts[i]);
245
+ }
246
+ }
247
+
248
+ /// @notice Increases balance of the provided `recipient` by the provided
249
+ /// `amount`. Can only be called by the Bridge.
250
+ function increaseBalance(address recipient, uint256 amount)
251
+ external
252
+ onlyBridge
253
+ {
254
+ _increaseBalance(recipient, amount);
255
+ }
256
+
257
+ /// @notice Decreases caller's balance by the provided `amount`. There is no
258
+ /// way to restore the balance so do not call this function unless
259
+ /// you really know what you are doing!
260
+ function decreaseBalance(uint256 amount) external {
261
+ balanceOf[msg.sender] -= amount;
262
+ emit BalanceDecreased(msg.sender, amount);
263
+ }
264
+
265
+ /// @notice Returns hash of EIP712 Domain struct with `TBTC Bank` as
266
+ /// a signing domain and Bank contract as a verifying contract.
267
+ /// Used to construct EIP2612 signature provided to `permit`
268
+ /// function.
269
+ /* solhint-disable-next-line func-name-mixedcase */
270
+ function DOMAIN_SEPARATOR() public view returns (bytes32) {
271
+ // As explained in EIP-2612, if the DOMAIN_SEPARATOR contains the
272
+ // chainId and is defined at contract deployment instead of
273
+ // reconstructed for every signature, there is a risk of possible replay
274
+ // attacks between chains in the event of a future chain split.
275
+ // To address this issue, we check the cached chain ID against the
276
+ // current one and in case they are different, we build domain separator
277
+ // from scratch.
278
+ if (block.chainid == cachedChainId) {
279
+ return cachedDomainSeparator;
280
+ } else {
281
+ return buildDomainSeparator();
282
+ }
283
+ }
284
+
285
+ function _increaseBalance(address recipient, uint256 amount) internal {
286
+ require(
287
+ recipient != address(this),
288
+ "Can not increase balance for Bank"
289
+ );
290
+ balanceOf[recipient] += amount;
291
+ emit BalanceIncreased(recipient, amount);
292
+ }
293
+
294
+ function _transferBalance(
295
+ address spender,
296
+ address recipient,
297
+ uint256 amount
298
+ ) private {
299
+ require(
300
+ recipient != address(0),
301
+ "Can not transfer to the zero address"
302
+ );
303
+ require(
304
+ recipient != address(this),
305
+ "Can not transfer to the Bank address"
306
+ );
307
+
308
+ uint256 spenderBalance = balanceOf[spender];
309
+ require(spenderBalance >= amount, "Transfer amount exceeds balance");
310
+ unchecked {balanceOf[spender] = spenderBalance - amount;}
311
+ balanceOf[recipient] += amount;
312
+ emit BalanceTransferred(spender, recipient, amount);
313
+ }
314
+
315
+ function _approveBalance(
316
+ address owner,
317
+ address spender,
318
+ uint256 amount
319
+ ) private {
320
+ require(spender != address(0), "Can not approve to the zero address");
321
+ allowance[owner][spender] = amount;
322
+ emit BalanceApproved(owner, spender, amount);
323
+ }
324
+
325
+ function buildDomainSeparator() private view returns (bytes32) {
326
+ return
327
+ keccak256(
328
+ abi.encode(
329
+ keccak256(
330
+ "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
331
+ ),
332
+ keccak256(bytes("TBTC Bank")),
333
+ keccak256(bytes("1")),
334
+ block.chainid,
335
+ address(this)
336
+ )
337
+ );
338
+ }
339
+ }
@@ -0,0 +1,161 @@
1
+ pragma solidity 0.8.4;
2
+
3
+ /// @title BTC Bridge
4
+ /// @notice Bridge manages BTC deposit and redemption and is increasing and
5
+ /// decreasing balances in the Bank as a result of BTC deposit and
6
+ /// redemption operations.
7
+ ///
8
+ /// Depositors send BTC funds to the most-recently-created-wallet of the
9
+ /// bridge using pay-to-script-hash (P2SH) which contains hashed
10
+ /// information about the depositor’s minting Ethereum address. Then,
11
+ /// the depositor reveals their desired Ethereum minting address to the
12
+ /// Ethereum chain. The Bridge listens for these sorts of messages and
13
+ /// when it gets one, it checks the Bitcoin network to make sure the
14
+ /// funds line up. If they do, the off-chain wallet may decide to pick
15
+ /// this transaction for sweeping, and when the sweep operation is
16
+ /// confirmed on the Bitcoin network, the wallet informs the Bridge
17
+ /// about the sweep increasing appropriate balances in the Bank.
18
+ /// @dev Bridge is an upgradeable component of the Bank.
19
+ contract Bridge {
20
+ struct DepositInfo {
21
+ uint64 amount;
22
+ address vault;
23
+ uint32 revealedAt;
24
+ }
25
+
26
+ /// @notice Collection of all unswept deposits indexed by
27
+ /// keccak256(fundingTxHash | fundingOutputIndex | depositorAddress).
28
+ /// This mapping may contain valid and invalid deposits and the
29
+ /// wallet is responsible for validating them before attempting to
30
+ /// execute a sweep.
31
+ mapping(uint256 => DepositInfo) public unswept;
32
+
33
+ event DepositRevealed(
34
+ uint256 depositId,
35
+ bytes32 fundingTxHash,
36
+ uint8 fundingOutputIndex,
37
+ address depositor,
38
+ uint64 blindingFactor,
39
+ bytes refundPubKey,
40
+ uint64 amount,
41
+ address vault
42
+ );
43
+
44
+ /// @notice Used by the depositor to reveal information about their P2SH
45
+ /// Bitcoin deposit to the Bridge on Ethereum chain. The off-chain
46
+ /// wallet listens for revealed deposit events and may decide to
47
+ /// include the revealed deposit in the next executed sweep.
48
+ /// Information about the Bitcoin deposit can be revealed before or
49
+ /// after the Bitcoin transaction with P2SH deposit is mined on the
50
+ /// Bitcoin chain.
51
+ /// @param fundingTxHash The BTC transaction hash containing BTC P2SH
52
+ /// deposit funding transaction
53
+ /// @param fundingOutputIndex The index of the transaction output in the
54
+ /// funding TX with P2SH deposit, max 256
55
+ /// @param blindingFactor The blinding factor used in the BTC P2SH deposit,
56
+ /// max 2^64
57
+ /// @param refundPubKey The refund pub key used in the BTC P2SH deposit
58
+ /// @param amount The amount locked in the BTC P2SH deposit
59
+ /// @param vault Bank vault to which the swept deposit should be routed
60
+ /// @dev Requirements:
61
+ /// - `msg.sender` must be the Ethereum address used in the P2SH BTC deposit,
62
+ /// - `blindingFactor` must be the blinding factor used in the P2SH BTC deposit,
63
+ /// - `refundPubKey` must be the refund pub key used in the P2SH BTC deposit,
64
+ /// - `amount` must be the same as locked in the P2SH BTC deposit,
65
+ /// - BTC deposit for the given `fundingTxHash`, `fundingOutputIndex`
66
+ /// can be revealed by `msg.sender` only one time.
67
+ ///
68
+ /// If any of these requirements is not met, the wallet _must_ refuse
69
+ /// to sweep the deposit and the depositor has to wait until the
70
+ /// deposit script unlocks to receive their BTC back.
71
+ function revealDeposit(
72
+ bytes32 fundingTxHash,
73
+ uint8 fundingOutputIndex,
74
+ uint64 blindingFactor,
75
+ bytes calldata refundPubKey,
76
+ uint64 amount,
77
+ address vault
78
+ ) external {
79
+ uint256 depositId =
80
+ uint256(
81
+ keccak256(
82
+ abi.encode(fundingTxHash, fundingOutputIndex, msg.sender)
83
+ )
84
+ );
85
+
86
+ DepositInfo storage deposit = unswept[depositId];
87
+ require(deposit.revealedAt == 0, "Deposit already revealed");
88
+
89
+ deposit.amount = amount;
90
+ deposit.vault = vault;
91
+ /* solhint-disable-next-line not-rely-on-time */
92
+ deposit.revealedAt = uint32(block.timestamp);
93
+
94
+ emit DepositRevealed(
95
+ depositId,
96
+ fundingTxHash,
97
+ fundingOutputIndex,
98
+ msg.sender,
99
+ blindingFactor,
100
+ refundPubKey,
101
+ amount,
102
+ vault
103
+ );
104
+ }
105
+
106
+ /// @notice Used by the wallet to prove the BTC deposit sweep transaction
107
+ /// and to update Bank balances accordingly. Sweep is only accepted
108
+ /// if it satisfies SPV proof.
109
+ ///
110
+ /// The function is performing Bank balance updates by first
111
+ /// computing the Bitcoin fee for the sweep transaction. The fee is
112
+ /// divided evenly between all swept deposits. Each depositor
113
+ /// receives a balance in the bank equal to the amount they have
114
+ /// declared during the reveal transaction, minus their fee share.
115
+ ///
116
+ /// It is possible to prove the given sweep only one time.
117
+ /// @param txVersion Transaction version number (4-byte LE)
118
+ /// @param txInputVector All transaction inputs prepended by the number of
119
+ /// inputs encoded as a VarInt, max 0xFC(252) inputs
120
+ /// @param txOutput Single sweep transaction output
121
+ /// @param txLocktime Final 4 bytes of the transaction
122
+ /// @param merkleProof The merkle proof of transaction inclusion in a block
123
+ /// @param txIndexInBlock Transaction index in the block (0-indexed)
124
+ /// @param bitcoinHeaders Single bytestring of 80-byte bitcoin headers,
125
+ /// lowest height first
126
+ function sweep(
127
+ bytes4 txVersion,
128
+ bytes memory txInputVector,
129
+ bytes memory txOutput,
130
+ bytes4 txLocktime,
131
+ bytes memory merkleProof,
132
+ uint256 txIndexInBlock,
133
+ bytes memory bitcoinHeaders
134
+ ) external {
135
+ // TODO We need to read `fundingTxHash`, `fundingOutputIndex` and
136
+ // P2SH script depositor address from `txInputVector`.
137
+ // We then hash them to obtain deposit identifier and read
138
+ // DepositInfo. From DepositInfo we know what amount was declared
139
+ // by the depositor in their reveal transaction and we use that
140
+ // amount to update their Bank balance, minus fee.
141
+ //
142
+ // TODO We need to validate if the sum in the output minus the
143
+ // amount from the previous wallet balance input minus fees is
144
+ // equal to the amount by which Bank balances were increased.
145
+ //
146
+ // TODO We need to validate txOutput to see if the balance was not
147
+ // transferred away from the wallet before increasing balances in
148
+ // the bank.
149
+ //
150
+ // TODO Delete deposit from unswept mapping or mark it as swept
151
+ // depending on the gas costs. Alternativly, do not allow to
152
+ // use the same TX input vector twice. Sweep should be provable
153
+ // only one time.
154
+ }
155
+
156
+ // TODO It is possible a malicious wallet can sweep deposits that can not
157
+ // be later proved on Ethereum. For example, a deposit with
158
+ // an incorrect amount revealed. We need to provide a function for honest
159
+ // depositors, next to sweep, to prove their swept balances on Ethereum
160
+ // selectively, based on deposits they have earlier received.
161
+ }
@@ -0,0 +1,104 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ // ██████████████ ▐████▌ ██████████████
4
+ // ██████████████ ▐████▌ ██████████████
5
+ // ▐████▌ ▐████▌
6
+ // ▐████▌ ▐████▌
7
+ // ██████████████ ▐████▌ ██████████████
8
+ // ██████████████ ▐████▌ ██████████████
9
+ // ▐████▌ ▐████▌
10
+ // ▐████▌ ▐████▌
11
+ // ▐████▌ ▐████▌
12
+ // ▐████▌ ▐████▌
13
+ // ▐████▌ ▐████▌
14
+ // ▐████▌ ▐████▌
15
+
16
+ pragma solidity 0.8.4;
17
+
18
+ import "../bank/Bank.sol";
19
+ import "../token/TBTC.sol";
20
+
21
+ /// @title TBTC application vault
22
+ /// @notice TBTC is a fully Bitcoin-backed ERC-20 token pegged to the price of
23
+ /// Bitcoin. It facilitates Bitcoin holders to act on the Ethereum
24
+ /// blockchain and access the decentralized finance (DeFi) ecosystem.
25
+ /// TBTC Vault mints and redeems TBTC based on Bitcoin balances in the
26
+ /// Bank.
27
+ /// @dev TBTC Vault is the owner of TBTC token contract and is the only contract
28
+ /// minting the token.
29
+ contract TBTCVault {
30
+ Bank public bank;
31
+ TBTC public tbtcToken;
32
+
33
+ event Minted(address indexed to, uint256 amount);
34
+
35
+ event Redeemed(address indexed from, uint256 amount);
36
+
37
+ constructor(Bank _bank, TBTC _tbtcToken) {
38
+ require(
39
+ address(_bank) != address(0),
40
+ "Bank can not be the zero address"
41
+ );
42
+
43
+ require(
44
+ address(_tbtcToken) != address(0),
45
+ "TBTC token can not be the zero address"
46
+ );
47
+
48
+ bank = _bank;
49
+ tbtcToken = _tbtcToken;
50
+ }
51
+
52
+ /// @notice Transfers the given `amount` of the Bank balance from caller
53
+ /// to TBTC Vault, and mints `amount` of TBTC to the caller.
54
+ /// @dev TBTC Vault must have an allowance for caller's balance in the Bank
55
+ /// for at least `amount`.
56
+ /// @param amount Amount of TBTC to mint
57
+ function mint(uint256 amount) external {
58
+ _mint(msg.sender, amount);
59
+ }
60
+
61
+ /// @notice Burns `amount` of TBTC from the caller's account and transfers
62
+ /// `amount` back to the caller's balance in the Bank.
63
+ /// @dev Caller must have at least `amount` of TBTC approved to
64
+ /// TBTC Vault.
65
+ /// @param amount Amount of TBTC to redeem
66
+ function redeem(uint256 amount) external {
67
+ _redeem(msg.sender, amount);
68
+ }
69
+
70
+ /// @notice Burns `amount` of TBTC from the caller's account and transfers
71
+ /// `amount` back to the caller's balance in the Bank.
72
+ /// @dev This function is doing the same as `redeem` but it allows to
73
+ /// execute redemption without an additional approval transaction.
74
+ /// The function can be called only via `approveAndCall` of TBTC token.
75
+ /// @param from TBTC token holder executing redemption
76
+ /// @param amount Amount of TBTC to redeem
77
+ /// @param token TBTC token address
78
+ function receiveApproval(
79
+ address from,
80
+ uint256 amount,
81
+ address token,
82
+ bytes calldata
83
+ ) external {
84
+ require(token == address(tbtcToken), "Token is not TBTC");
85
+ require(msg.sender == token, "Only TBTC caller allowed");
86
+ _redeem(from, amount);
87
+ }
88
+
89
+ function _mint(address minter, uint256 amount) internal {
90
+ require(
91
+ bank.balanceOf(minter) >= amount,
92
+ "Amount exceeds balance in the bank"
93
+ );
94
+ emit Minted(minter, amount);
95
+ bank.transferBalanceFrom(minter, address(this), amount);
96
+ tbtcToken.mint(minter, amount);
97
+ }
98
+
99
+ function _redeem(address redeemer, uint256 amount) internal {
100
+ emit Redeemed(redeemer, amount);
101
+ tbtcToken.burnFrom(redeemer, amount);
102
+ bank.transferBalance(redeemer, amount);
103
+ }
104
+ }
@@ -13,7 +13,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
13
13
  } else if (hre.network.name !== "hardhat") {
14
14
  throw new Error("deployed TBTCToken contract not found")
15
15
  } else {
16
- log(`deploying TBTCToken stub`)
16
+ log("deploying TBTCToken stub")
17
17
 
18
18
  await deployments.deploy("TBTCToken", {
19
19
  contract: "TestERC20",
@@ -6,10 +6,17 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
6
6
  const { deploy } = deployments
7
7
  const { deployer } = await getNamedAccounts()
8
8
 
9
- await deploy("TBTC", {
9
+ const TBTC = await deploy("TBTC", {
10
10
  from: deployer,
11
11
  log: true,
12
12
  })
13
+
14
+ if (hre.network.tags.tenderly) {
15
+ await hre.tenderly.verify({
16
+ name: "TBTC",
17
+ address: TBTC.address,
18
+ })
19
+ }
13
20
  }
14
21
 
15
22
  export default func
@@ -22,6 +22,13 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
22
22
  VendingMachine.address,
23
23
  deployer
24
24
  )
25
+
26
+ if (hre.network.tags.tenderly) {
27
+ await hre.tenderly.verify({
28
+ name: "VendingMachine",
29
+ address: VendingMachine.address,
30
+ })
31
+ }
25
32
  }
26
33
 
27
34
  export default func