@keep-network/tbtc-v2 0.1.1-dev.2 → 0.1.1-dev.6
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.
- package/artifacts/TBTC.json +10 -10
- package/artifacts/TBTCToken.json +10 -10
- package/artifacts/VendingMachine.json +11 -11
- package/artifacts/solcInputs/c4fd2c31cc58f5fe0cc586dd84a84b60.json +125 -0
- package/build/contracts/GovernanceUtils.sol/GovernanceUtils.dbg.json +1 -1
- package/build/contracts/bank/Bank.sol/Bank.dbg.json +4 -0
- package/build/contracts/bank/Bank.sol/Bank.json +519 -0
- package/build/contracts/bridge/Bridge.sol/Bridge.dbg.json +1 -1
- package/build/contracts/bridge/Bridge.sol/Bridge.json +116 -71
- package/build/contracts/bridge/VendingMachine.sol/VendingMachine.dbg.json +1 -1
- package/build/contracts/token/TBTC.sol/TBTC.dbg.json +1 -1
- package/build/contracts/vault/IVault.sol/IVault.dbg.json +4 -0
- package/build/contracts/vault/IVault.sol/IVault.json +29 -0
- package/build/contracts/vault/TBTCVault.sol/TBTCVault.dbg.json +4 -0
- package/build/contracts/vault/TBTCVault.sol/TBTCVault.json +163 -0
- package/contracts/bank/Bank.sol +374 -0
- package/contracts/bridge/Bridge.sol +228 -71
- package/contracts/vault/IVault.sol +38 -0
- package/contracts/vault/TBTCVault.sol +128 -0
- package/package.json +4 -3
- package/artifacts/solcInputs/02b9e185d8beb23545e98201d474fc6b.json +0 -107
|
@@ -1,12 +1,31 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
// ██████████████ ▐████▌ ██████████████
|
|
4
|
+
// ██████████████ ▐████▌ ██████████████
|
|
5
|
+
// ▐████▌ ▐████▌
|
|
6
|
+
// ▐████▌ ▐████▌
|
|
7
|
+
// ██████████████ ▐████▌ ██████████████
|
|
8
|
+
// ██████████████ ▐████▌ ██████████████
|
|
9
|
+
// ▐████▌ ▐████▌
|
|
10
|
+
// ▐████▌ ▐████▌
|
|
11
|
+
// ▐████▌ ▐████▌
|
|
12
|
+
// ▐████▌ ▐████▌
|
|
13
|
+
// ▐████▌ ▐████▌
|
|
14
|
+
// ▐████▌ ▐████▌
|
|
15
|
+
|
|
1
16
|
pragma solidity 0.8.4;
|
|
2
17
|
|
|
18
|
+
import {BTCUtils} from "@keep-network/bitcoin-spv-sol/contracts/BTCUtils.sol";
|
|
19
|
+
import {BytesLib} from "@keep-network/bitcoin-spv-sol/contracts/BytesLib.sol";
|
|
20
|
+
|
|
3
21
|
/// @title BTC Bridge
|
|
4
22
|
/// @notice Bridge manages BTC deposit and redemption and is increasing and
|
|
5
23
|
/// decreasing balances in the Bank as a result of BTC deposit and
|
|
6
24
|
/// redemption operations.
|
|
7
25
|
///
|
|
8
26
|
/// Depositors send BTC funds to the most-recently-created-wallet of the
|
|
9
|
-
/// bridge using pay-to-script-hash (P2SH)
|
|
27
|
+
/// bridge using pay-to-script-hash (P2SH) or
|
|
28
|
+
/// pay-to-witness-script-hash (P2WSH) which contains hashed
|
|
10
29
|
/// information about the depositor’s minting Ethereum address. Then,
|
|
11
30
|
/// the depositor reveals their desired Ethereum minting address to the
|
|
12
31
|
/// Ethereum chain. The Bridge listens for these sorts of messages and
|
|
@@ -17,89 +36,234 @@ pragma solidity 0.8.4;
|
|
|
17
36
|
/// about the sweep increasing appropriate balances in the Bank.
|
|
18
37
|
/// @dev Bridge is an upgradeable component of the Bank.
|
|
19
38
|
contract Bridge {
|
|
20
|
-
|
|
21
|
-
|
|
39
|
+
using BTCUtils for bytes;
|
|
40
|
+
using BytesLib for bytes;
|
|
41
|
+
|
|
42
|
+
/// @notice Represents Bitcoin transaction data as described in:
|
|
43
|
+
/// https://developer.bitcoin.org/reference/transactions.html#raw-transaction-format
|
|
44
|
+
struct TxInfo {
|
|
45
|
+
// Transaction version number (4-byte LE).
|
|
46
|
+
bytes4 version;
|
|
47
|
+
// All transaction inputs prepended by the number of inputs encoded
|
|
48
|
+
// as a compactSize uint. Single vector item looks as follows:
|
|
49
|
+
// https://developer.bitcoin.org/reference/transactions.html#txin-a-transaction-input-non-coinbase
|
|
50
|
+
// though SegWit inputs don't contain the signature script (scriptSig).
|
|
51
|
+
// All encoded input transaction hashes are little-endian.
|
|
52
|
+
bytes inputVector;
|
|
53
|
+
// All transaction outputs prepended by the number of outputs encoded
|
|
54
|
+
// as a compactSize uint. Single vector item looks as follows:
|
|
55
|
+
// https://developer.bitcoin.org/reference/transactions.html#txout-a-transaction-output
|
|
56
|
+
bytes outputVector;
|
|
57
|
+
// Transaction locktime (4-byte LE).
|
|
58
|
+
bytes4 locktime;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// @notice Represents data which must be revealed by the depositor during
|
|
62
|
+
/// deposit reveal.
|
|
63
|
+
struct RevealInfo {
|
|
64
|
+
// Index of the funding output belonging to the funding transaction.
|
|
65
|
+
uint8 fundingOutputIndex;
|
|
66
|
+
// Ethereum depositor address.
|
|
67
|
+
address depositor;
|
|
68
|
+
// The blinding factor as 8 bytes. Byte endianness doesn't matter
|
|
69
|
+
// as this factor is not interpreted as uint.
|
|
70
|
+
bytes8 blindingFactor;
|
|
71
|
+
// The compressed Bitcoin public key (33 bytes and 02 or 03 prefix)
|
|
72
|
+
// of the deposit's wallet hashed in the HASH160 Bitcoin opcode style.
|
|
73
|
+
bytes20 walletPubKeyHash;
|
|
74
|
+
// The compressed Bitcoin public key (33 bytes and 02 or 03 prefix)
|
|
75
|
+
// that can be used to make the deposit refund after the refund
|
|
76
|
+
// locktime passes. Hashed in the HASH160 Bitcoin opcode style.
|
|
77
|
+
bytes20 refundPubKeyHash;
|
|
78
|
+
// The refund locktime (4-byte LE). Interpreted according to locktime
|
|
79
|
+
// parsing rules described in:
|
|
80
|
+
// https://developer.bitcoin.org/devguide/transactions.html#locktime-and-sequence-number
|
|
81
|
+
// and used with OP_CHECKLOCKTIMEVERIFY opcode as described in:
|
|
82
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki
|
|
83
|
+
bytes4 refundLocktime;
|
|
84
|
+
// Address of the tBTC vault.
|
|
22
85
|
address vault;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/// @notice Represents tBTC deposit data.
|
|
89
|
+
struct DepositInfo {
|
|
90
|
+
// Ethereum depositor address.
|
|
91
|
+
address depositor;
|
|
92
|
+
// Deposit amount in satoshi (8-byte LE). For example:
|
|
93
|
+
// 0.0001 BTC = 10000 satoshi = 0x1027000000000000
|
|
94
|
+
bytes8 amount;
|
|
95
|
+
// UNIX timestamp the deposit was revealed at.
|
|
23
96
|
uint32 revealedAt;
|
|
97
|
+
// Address of the tBTC vault.
|
|
98
|
+
address vault;
|
|
24
99
|
}
|
|
25
100
|
|
|
26
101
|
/// @notice Collection of all unswept deposits indexed by
|
|
27
|
-
/// keccak256(fundingTxHash | fundingOutputIndex
|
|
102
|
+
/// keccak256(fundingTxHash | fundingOutputIndex).
|
|
103
|
+
/// The fundingTxHash is LE bytes32 and fundingOutputIndex an uint8.
|
|
28
104
|
/// This mapping may contain valid and invalid deposits and the
|
|
29
105
|
/// wallet is responsible for validating them before attempting to
|
|
30
106
|
/// execute a sweep.
|
|
107
|
+
///
|
|
108
|
+
/// TODO: Explore the possibility of storing just a hash of DepositInfo.
|
|
31
109
|
mapping(uint256 => DepositInfo) public unswept;
|
|
32
110
|
|
|
33
111
|
event DepositRevealed(
|
|
34
|
-
uint256 depositId,
|
|
35
112
|
bytes32 fundingTxHash,
|
|
36
113
|
uint8 fundingOutputIndex,
|
|
37
114
|
address depositor,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
115
|
+
bytes8 blindingFactor,
|
|
116
|
+
bytes20 walletPubKeyHash,
|
|
117
|
+
bytes20 refundPubKeyHash,
|
|
118
|
+
bytes4 refundLocktime
|
|
42
119
|
);
|
|
43
120
|
|
|
44
|
-
/// @notice Used by the depositor to reveal information about their
|
|
121
|
+
/// @notice Used by the depositor to reveal information about their P2(W)SH
|
|
45
122
|
/// Bitcoin deposit to the Bridge on Ethereum chain. The off-chain
|
|
46
123
|
/// wallet listens for revealed deposit events and may decide to
|
|
47
124
|
/// include the revealed deposit in the next executed sweep.
|
|
48
125
|
/// Information about the Bitcoin deposit can be revealed before or
|
|
49
|
-
/// after the Bitcoin transaction with
|
|
50
|
-
/// Bitcoin chain.
|
|
51
|
-
///
|
|
52
|
-
///
|
|
53
|
-
/// @param
|
|
54
|
-
///
|
|
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
|
|
126
|
+
/// after the Bitcoin transaction with P2(W)SH deposit is mined on
|
|
127
|
+
/// the Bitcoin chain. Worth noting the gas cost of this function
|
|
128
|
+
/// scales with the number of P2(W)SH transaction inputs and
|
|
129
|
+
/// outputs.
|
|
130
|
+
/// @param fundingTx Bitcoin funding transaction data.
|
|
131
|
+
/// @param reveal Deposit reveal data.
|
|
60
132
|
/// @dev Requirements:
|
|
61
|
-
/// - `
|
|
62
|
-
///
|
|
63
|
-
/// - `
|
|
64
|
-
///
|
|
133
|
+
/// - `reveal.fundingOutputIndex` must point to the actual P2(W)SH
|
|
134
|
+
/// output of the BTC deposit transaction
|
|
135
|
+
/// - `reveal.depositor` must be the Ethereum address used in the
|
|
136
|
+
/// P2(W)SH BTC deposit transaction,
|
|
137
|
+
/// - `reveal.blindingFactor` must be the blinding factor used in the
|
|
138
|
+
/// P2(W)SH BTC deposit transaction,
|
|
139
|
+
/// - `reveal.walletPubKeyHash` must be the wallet pub key hash used in
|
|
140
|
+
/// the P2(W)SH BTC deposit transaction,
|
|
141
|
+
/// - `reveal.refundPubKeyHash` must be the refund pub key hash used in
|
|
142
|
+
/// the P2(W)SH BTC deposit transaction,
|
|
143
|
+
/// - `reveal.refundLocktime` must be the refund locktime used in the
|
|
144
|
+
/// P2(W)SH BTC deposit transaction,
|
|
65
145
|
/// - BTC deposit for the given `fundingTxHash`, `fundingOutputIndex`
|
|
66
|
-
/// can be revealed
|
|
146
|
+
/// can be revealed only one time.
|
|
67
147
|
///
|
|
68
148
|
/// If any of these requirements is not met, the wallet _must_ refuse
|
|
69
149
|
/// to sweep the deposit and the depositor has to wait until the
|
|
70
150
|
/// deposit script unlocks to receive their BTC back.
|
|
71
151
|
function revealDeposit(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
uint64 blindingFactor,
|
|
75
|
-
bytes calldata refundPubKey,
|
|
76
|
-
uint64 amount,
|
|
77
|
-
address vault
|
|
152
|
+
TxInfo calldata fundingTx,
|
|
153
|
+
RevealInfo calldata reveal
|
|
78
154
|
) external {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
155
|
+
bytes memory expectedScript =
|
|
156
|
+
abi.encodePacked(
|
|
157
|
+
hex"14", // Byte length of depositor Ethereum address.
|
|
158
|
+
reveal.depositor,
|
|
159
|
+
hex"75", // OP_DROP
|
|
160
|
+
hex"08", // Byte length of blinding factor value.
|
|
161
|
+
reveal.blindingFactor,
|
|
162
|
+
hex"75", // OP_DROP
|
|
163
|
+
hex"76", // OP_DUP
|
|
164
|
+
hex"a9", // OP_HASH160
|
|
165
|
+
hex"14", // Byte length of a compressed Bitcoin public key hash.
|
|
166
|
+
reveal.walletPubKeyHash,
|
|
167
|
+
hex"87", // OP_EQUAL
|
|
168
|
+
hex"63", // OP_IF
|
|
169
|
+
hex"ac", // OP_CHECKSIG
|
|
170
|
+
hex"67", // OP_ELSE
|
|
171
|
+
hex"76", // OP_DUP
|
|
172
|
+
hex"a9", // OP_HASH160
|
|
173
|
+
hex"14", // Byte length of a compressed Bitcoin public key hash.
|
|
174
|
+
reveal.refundPubKeyHash,
|
|
175
|
+
hex"88", // OP_EQUALVERIFY
|
|
176
|
+
hex"04", // Byte length of refund locktime value.
|
|
177
|
+
reveal.refundLocktime,
|
|
178
|
+
hex"b1", // OP_CHECKLOCKTIMEVERIFY
|
|
179
|
+
hex"75", // OP_DROP
|
|
180
|
+
hex"ac", // OP_CHECKSIG
|
|
181
|
+
hex"68" // OP_ENDIF
|
|
84
182
|
);
|
|
85
183
|
|
|
86
|
-
|
|
184
|
+
bytes memory fundingOutput =
|
|
185
|
+
fundingTx.outputVector.extractOutputAtIndex(
|
|
186
|
+
reveal.fundingOutputIndex
|
|
187
|
+
);
|
|
188
|
+
bytes memory fundingOutputHash = fundingOutput.extractHash();
|
|
189
|
+
|
|
190
|
+
if (fundingOutputHash.length == 20) {
|
|
191
|
+
// A 20-byte output hash is used by P2SH. That hash is constructed
|
|
192
|
+
// by applying OP_HASH160 on the locking script. A 20-byte output
|
|
193
|
+
// hash is used as well by P2PKH and P2WPKH (OP_HASH160 on the
|
|
194
|
+
// public key). However, since we compare the actual output hash
|
|
195
|
+
// with an expected locking script hash, this check will succeed only
|
|
196
|
+
// for P2SH transaction type with expected script hash value. For
|
|
197
|
+
// P2PKH and P2WPKH, it will fail on the output hash comparison with
|
|
198
|
+
// the expected locking script hash.
|
|
199
|
+
require(
|
|
200
|
+
keccak256(fundingOutputHash) ==
|
|
201
|
+
keccak256(expectedScript.hash160()),
|
|
202
|
+
"Wrong 20-byte script hash"
|
|
203
|
+
);
|
|
204
|
+
} else if (fundingOutputHash.length == 32) {
|
|
205
|
+
// A 32-byte output hash is used by P2WSH. That hash is constructed
|
|
206
|
+
// by applying OP_HASH256 on the locking script.
|
|
207
|
+
require(
|
|
208
|
+
fundingOutputHash.toBytes32() == expectedScript.hash256(),
|
|
209
|
+
"Wrong 32-byte script hash"
|
|
210
|
+
);
|
|
211
|
+
} else {
|
|
212
|
+
revert("Wrong script hash length");
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Resulting TX hash is in native Bitcoin little-endian format.
|
|
216
|
+
bytes32 fundingTxHash =
|
|
217
|
+
abi
|
|
218
|
+
.encodePacked(
|
|
219
|
+
fundingTx
|
|
220
|
+
.version,
|
|
221
|
+
fundingTx
|
|
222
|
+
.inputVector,
|
|
223
|
+
fundingTx
|
|
224
|
+
.outputVector,
|
|
225
|
+
fundingTx
|
|
226
|
+
.locktime
|
|
227
|
+
)
|
|
228
|
+
.hash256();
|
|
229
|
+
|
|
230
|
+
DepositInfo storage deposit =
|
|
231
|
+
unswept[
|
|
232
|
+
uint256(
|
|
233
|
+
keccak256(
|
|
234
|
+
abi.encodePacked(
|
|
235
|
+
fundingTxHash,
|
|
236
|
+
reveal.fundingOutputIndex
|
|
237
|
+
)
|
|
238
|
+
)
|
|
239
|
+
)
|
|
240
|
+
];
|
|
87
241
|
require(deposit.revealedAt == 0, "Deposit already revealed");
|
|
88
242
|
|
|
89
|
-
|
|
90
|
-
|
|
243
|
+
bytes8 fundingOutputAmount;
|
|
244
|
+
/* solhint-disable-next-line no-inline-assembly */
|
|
245
|
+
assembly {
|
|
246
|
+
// First 8 bytes (little-endian) of the funding output represents
|
|
247
|
+
// its value. To take the value, we need to jump over the first
|
|
248
|
+
// word determining the array length, load the array, and trim it
|
|
249
|
+
// by putting it to a bytes8.
|
|
250
|
+
fundingOutputAmount := mload(add(fundingOutput, 32))
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
deposit.amount = fundingOutputAmount;
|
|
254
|
+
deposit.depositor = reveal.depositor;
|
|
91
255
|
/* solhint-disable-next-line not-rely-on-time */
|
|
92
256
|
deposit.revealedAt = uint32(block.timestamp);
|
|
257
|
+
deposit.vault = reveal.vault;
|
|
93
258
|
|
|
94
259
|
emit DepositRevealed(
|
|
95
|
-
depositId,
|
|
96
260
|
fundingTxHash,
|
|
97
|
-
fundingOutputIndex,
|
|
98
|
-
|
|
99
|
-
blindingFactor,
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
261
|
+
reveal.fundingOutputIndex,
|
|
262
|
+
reveal.depositor,
|
|
263
|
+
reveal.blindingFactor,
|
|
264
|
+
reveal.walletPubKeyHash,
|
|
265
|
+
reveal.refundPubKeyHash,
|
|
266
|
+
reveal.refundLocktime
|
|
103
267
|
);
|
|
104
268
|
}
|
|
105
269
|
|
|
@@ -110,45 +274,37 @@ contract Bridge {
|
|
|
110
274
|
/// The function is performing Bank balance updates by first
|
|
111
275
|
/// computing the Bitcoin fee for the sweep transaction. The fee is
|
|
112
276
|
/// divided evenly between all swept deposits. Each depositor
|
|
113
|
-
/// receives a balance in the bank equal to the amount
|
|
114
|
-
///
|
|
277
|
+
/// receives a balance in the bank equal to the amount inferred
|
|
278
|
+
/// during the reveal transaction, minus their fee share.
|
|
115
279
|
///
|
|
116
280
|
/// It is possible to prove the given sweep only one time.
|
|
117
|
-
/// @param
|
|
118
|
-
/// @param
|
|
119
|
-
///
|
|
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)
|
|
281
|
+
/// @param sweepTx Bitcoin sweep transaction data.
|
|
282
|
+
/// @param merkleProof The merkle proof of transaction inclusion in a block.
|
|
283
|
+
/// @param txIndexInBlock Transaction index in the block (0-indexed).
|
|
124
284
|
/// @param bitcoinHeaders Single bytestring of 80-byte bitcoin headers,
|
|
125
|
-
/// lowest height first
|
|
285
|
+
/// lowest height first.
|
|
126
286
|
function sweep(
|
|
127
|
-
|
|
128
|
-
bytes memory txInputVector,
|
|
129
|
-
bytes memory txOutput,
|
|
130
|
-
bytes4 txLocktime,
|
|
287
|
+
TxInfo calldata sweepTx,
|
|
131
288
|
bytes memory merkleProof,
|
|
132
289
|
uint256 txIndexInBlock,
|
|
133
290
|
bytes memory bitcoinHeaders
|
|
134
291
|
) external {
|
|
135
|
-
// TODO We need to read `fundingTxHash`, `fundingOutputIndex`
|
|
136
|
-
//
|
|
137
|
-
//
|
|
138
|
-
//
|
|
139
|
-
//
|
|
140
|
-
// amount to update their Bank balance, minus fee.
|
|
292
|
+
// TODO We need to read `fundingTxHash`, `fundingOutputIndex` from
|
|
293
|
+
// `sweepTx.inputVector`. We then hash them to obtain deposit
|
|
294
|
+
// identifier and read DepositInfo. From DepositInfo we know what
|
|
295
|
+
// amount was inferred during deposit reveal transaction and we
|
|
296
|
+
// use that amount to update their Bank balance, minus fee.
|
|
141
297
|
//
|
|
142
298
|
// TODO We need to validate if the sum in the output minus the
|
|
143
299
|
// amount from the previous wallet balance input minus fees is
|
|
144
300
|
// equal to the amount by which Bank balances were increased.
|
|
145
301
|
//
|
|
146
|
-
// TODO We need to validate
|
|
147
|
-
// transferred away from the wallet before increasing
|
|
148
|
-
// the bank.
|
|
302
|
+
// TODO We need to validate `sweepTx.outputVector` to see if the balance
|
|
303
|
+
// was not transferred away from the wallet before increasing
|
|
304
|
+
// balances in the bank.
|
|
149
305
|
//
|
|
150
306
|
// TODO Delete deposit from unswept mapping or mark it as swept
|
|
151
|
-
// depending on the gas costs.
|
|
307
|
+
// depending on the gas costs. Alternatively, do not allow to
|
|
152
308
|
// use the same TX input vector twice. Sweep should be provable
|
|
153
309
|
// only one time.
|
|
154
310
|
}
|
|
@@ -158,4 +314,5 @@ contract Bridge {
|
|
|
158
314
|
// an incorrect amount revealed. We need to provide a function for honest
|
|
159
315
|
// depositors, next to sweep, to prove their swept balances on Ethereum
|
|
160
316
|
// selectively, based on deposits they have earlier received.
|
|
317
|
+
// (UPDATE PR #90: Is it still the case since amounts are inferred?)
|
|
161
318
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
/// @title Bank Vault interface
|
|
19
|
+
/// @notice `IVault` is an interface for a smart contract consuming Bank
|
|
20
|
+
/// balances allowing the smart contract to receive Bank balances right
|
|
21
|
+
/// after sweeping the deposit by the Bridge. This method allows the
|
|
22
|
+
/// depositor to route their deposit revealed to the Bridge to the
|
|
23
|
+
/// particular smart contract in the same transaction the deposit is
|
|
24
|
+
/// revealed. This way, the depositor does not have to execute
|
|
25
|
+
/// additional transaction after the deposit gets swept by the Bridge.
|
|
26
|
+
interface IVault {
|
|
27
|
+
/// @notice Called by the Bank in `increaseBalanceAndCall` function after
|
|
28
|
+
/// increasing the balance in the Bank for the vault.
|
|
29
|
+
/// @param depositors Addresses of depositors whose deposits have been swept
|
|
30
|
+
/// @param depositedAmounts Amounts deposited by individual depositors and
|
|
31
|
+
/// swept
|
|
32
|
+
/// @dev The implementation must ensure this function can only be called
|
|
33
|
+
/// by the Bank.
|
|
34
|
+
function onBalanceIncreased(
|
|
35
|
+
address[] calldata depositors,
|
|
36
|
+
uint256[] calldata depositedAmounts
|
|
37
|
+
) external;
|
|
38
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
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 "./IVault.sol";
|
|
19
|
+
import "../bank/Bank.sol";
|
|
20
|
+
import "../token/TBTC.sol";
|
|
21
|
+
|
|
22
|
+
/// @title TBTC application vault
|
|
23
|
+
/// @notice TBTC is a fully Bitcoin-backed ERC-20 token pegged to the price of
|
|
24
|
+
/// Bitcoin. It facilitates Bitcoin holders to act on the Ethereum
|
|
25
|
+
/// blockchain and access the decentralized finance (DeFi) ecosystem.
|
|
26
|
+
/// TBTC Vault mints and redeems TBTC based on Bitcoin balances in the
|
|
27
|
+
/// Bank.
|
|
28
|
+
/// @dev TBTC Vault is the owner of TBTC token contract and is the only contract
|
|
29
|
+
/// minting the token.
|
|
30
|
+
contract TBTCVault is IVault {
|
|
31
|
+
Bank public bank;
|
|
32
|
+
TBTC public tbtcToken;
|
|
33
|
+
|
|
34
|
+
event Minted(address indexed to, uint256 amount);
|
|
35
|
+
|
|
36
|
+
event Redeemed(address indexed from, uint256 amount);
|
|
37
|
+
|
|
38
|
+
modifier onlyBank() {
|
|
39
|
+
require(msg.sender == address(bank), "Caller is not the Bank");
|
|
40
|
+
_;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
constructor(Bank _bank, TBTC _tbtcToken) {
|
|
44
|
+
require(
|
|
45
|
+
address(_bank) != address(0),
|
|
46
|
+
"Bank can not be the zero address"
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
require(
|
|
50
|
+
address(_tbtcToken) != address(0),
|
|
51
|
+
"TBTC token can not be the zero address"
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
bank = _bank;
|
|
55
|
+
tbtcToken = _tbtcToken;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/// @notice Transfers the given `amount` of the Bank balance from caller
|
|
59
|
+
/// to TBTC Vault, and mints `amount` of TBTC to the caller.
|
|
60
|
+
/// @dev TBTC Vault must have an allowance for caller's balance in the Bank
|
|
61
|
+
/// for at least `amount`.
|
|
62
|
+
/// @param amount Amount of TBTC to mint
|
|
63
|
+
function mint(uint256 amount) external {
|
|
64
|
+
address minter = msg.sender;
|
|
65
|
+
require(
|
|
66
|
+
bank.balanceOf(minter) >= amount,
|
|
67
|
+
"Amount exceeds balance in the bank"
|
|
68
|
+
);
|
|
69
|
+
_mint(minter, amount);
|
|
70
|
+
bank.transferBalanceFrom(minter, address(this), amount);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/// @notice Mints the same amount of TBTC as the deposited amount for each
|
|
74
|
+
/// depositor in the array. Can only be called by the Bank after the
|
|
75
|
+
/// Bridge swept deposits and Bank increased balance for the
|
|
76
|
+
/// vault.
|
|
77
|
+
/// @dev Fails if `depositors` array is empty. Expects the length of
|
|
78
|
+
/// `depositors` and `depositedAmounts` is the same.
|
|
79
|
+
function onBalanceIncreased(
|
|
80
|
+
address[] calldata depositors,
|
|
81
|
+
uint256[] calldata depositedAmounts
|
|
82
|
+
) external override onlyBank {
|
|
83
|
+
require(depositors.length != 0, "No depositors specified");
|
|
84
|
+
for (uint256 i = 0; i < depositors.length; i++) {
|
|
85
|
+
_mint(depositors[i], depositedAmounts[i]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// @notice Burns `amount` of TBTC from the caller's account and transfers
|
|
90
|
+
/// `amount` back to the caller's balance in the Bank.
|
|
91
|
+
/// @dev Caller must have at least `amount` of TBTC approved to
|
|
92
|
+
/// TBTC Vault.
|
|
93
|
+
/// @param amount Amount of TBTC to redeem
|
|
94
|
+
function redeem(uint256 amount) external {
|
|
95
|
+
_redeem(msg.sender, amount);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// @notice Burns `amount` of TBTC from the caller's account and transfers
|
|
99
|
+
/// `amount` back to the caller's balance in the Bank.
|
|
100
|
+
/// @dev This function is doing the same as `redeem` but it allows to
|
|
101
|
+
/// execute redemption without an additional approval transaction.
|
|
102
|
+
/// The function can be called only via `approveAndCall` of TBTC token.
|
|
103
|
+
/// @param from TBTC token holder executing redemption
|
|
104
|
+
/// @param amount Amount of TBTC to redeem
|
|
105
|
+
/// @param token TBTC token address
|
|
106
|
+
function receiveApproval(
|
|
107
|
+
address from,
|
|
108
|
+
uint256 amount,
|
|
109
|
+
address token,
|
|
110
|
+
bytes calldata
|
|
111
|
+
) external {
|
|
112
|
+
require(token == address(tbtcToken), "Token is not TBTC");
|
|
113
|
+
require(msg.sender == token, "Only TBTC caller allowed");
|
|
114
|
+
_redeem(from, amount);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// slither-disable-next-line calls-loop
|
|
118
|
+
function _mint(address minter, uint256 amount) internal {
|
|
119
|
+
emit Minted(minter, amount);
|
|
120
|
+
tbtcToken.mint(minter, amount);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function _redeem(address redeemer, uint256 amount) internal {
|
|
124
|
+
emit Redeemed(redeemer, amount);
|
|
125
|
+
tbtcToken.burnFrom(redeemer, amount);
|
|
126
|
+
bank.transferBalance(redeemer, amount);
|
|
127
|
+
}
|
|
128
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keep-network/tbtc-v2",
|
|
3
|
-
"version": "0.1.1-dev.
|
|
3
|
+
"version": "0.1.1-dev.6+main.68e9da2c1b826725378fc2c33c2067b6c024a26e",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"artifacts/",
|
|
@@ -26,13 +26,14 @@
|
|
|
26
26
|
"prepublishOnly": "./scripts/prepare-artifacts.sh --network $npm_config_network"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@keep-network/
|
|
29
|
+
"@keep-network/bitcoin-spv-sol": "3.1.0-solc-0.8",
|
|
30
|
+
"@keep-network/tbtc": ">1.1.2-dev <1.1.2-pre",
|
|
30
31
|
"@openzeppelin/contracts": "^4.1.0",
|
|
31
32
|
"@tenderly/hardhat-tenderly": "^1.0.12",
|
|
32
33
|
"@thesis/solidity-contracts": "github:thesis/solidity-contracts#4985bcf"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
|
-
"@keep-network/hardhat-helpers": "
|
|
36
|
+
"@keep-network/hardhat-helpers": "0.4.1-pre.1",
|
|
36
37
|
"@keep-network/hardhat-local-networks-config": "^0.1.0-pre.0",
|
|
37
38
|
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
|
38
39
|
"@nomiclabs/hardhat-etherscan": "^2.1.4",
|