@keep-network/tbtc-v2 0.1.1-dev.2 → 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,145 @@
1
+ {
2
+ "_format": "hh-sol-artifact-1",
3
+ "contractName": "TBTCVault",
4
+ "sourceName": "contracts/vault/TBTCVault.sol",
5
+ "abi": [
6
+ {
7
+ "inputs": [
8
+ {
9
+ "internalType": "contract Bank",
10
+ "name": "_bank",
11
+ "type": "address"
12
+ },
13
+ {
14
+ "internalType": "contract TBTC",
15
+ "name": "_tbtcToken",
16
+ "type": "address"
17
+ }
18
+ ],
19
+ "stateMutability": "nonpayable",
20
+ "type": "constructor"
21
+ },
22
+ {
23
+ "anonymous": false,
24
+ "inputs": [
25
+ {
26
+ "indexed": true,
27
+ "internalType": "address",
28
+ "name": "to",
29
+ "type": "address"
30
+ },
31
+ {
32
+ "indexed": false,
33
+ "internalType": "uint256",
34
+ "name": "amount",
35
+ "type": "uint256"
36
+ }
37
+ ],
38
+ "name": "Minted",
39
+ "type": "event"
40
+ },
41
+ {
42
+ "anonymous": false,
43
+ "inputs": [
44
+ {
45
+ "indexed": true,
46
+ "internalType": "address",
47
+ "name": "from",
48
+ "type": "address"
49
+ },
50
+ {
51
+ "indexed": false,
52
+ "internalType": "uint256",
53
+ "name": "amount",
54
+ "type": "uint256"
55
+ }
56
+ ],
57
+ "name": "Redeemed",
58
+ "type": "event"
59
+ },
60
+ {
61
+ "inputs": [],
62
+ "name": "bank",
63
+ "outputs": [
64
+ {
65
+ "internalType": "contract Bank",
66
+ "name": "",
67
+ "type": "address"
68
+ }
69
+ ],
70
+ "stateMutability": "view",
71
+ "type": "function"
72
+ },
73
+ {
74
+ "inputs": [
75
+ {
76
+ "internalType": "uint256",
77
+ "name": "amount",
78
+ "type": "uint256"
79
+ }
80
+ ],
81
+ "name": "mint",
82
+ "outputs": [],
83
+ "stateMutability": "nonpayable",
84
+ "type": "function"
85
+ },
86
+ {
87
+ "inputs": [
88
+ {
89
+ "internalType": "address",
90
+ "name": "from",
91
+ "type": "address"
92
+ },
93
+ {
94
+ "internalType": "uint256",
95
+ "name": "amount",
96
+ "type": "uint256"
97
+ },
98
+ {
99
+ "internalType": "address",
100
+ "name": "token",
101
+ "type": "address"
102
+ },
103
+ {
104
+ "internalType": "bytes",
105
+ "name": "",
106
+ "type": "bytes"
107
+ }
108
+ ],
109
+ "name": "receiveApproval",
110
+ "outputs": [],
111
+ "stateMutability": "nonpayable",
112
+ "type": "function"
113
+ },
114
+ {
115
+ "inputs": [
116
+ {
117
+ "internalType": "uint256",
118
+ "name": "amount",
119
+ "type": "uint256"
120
+ }
121
+ ],
122
+ "name": "redeem",
123
+ "outputs": [],
124
+ "stateMutability": "nonpayable",
125
+ "type": "function"
126
+ },
127
+ {
128
+ "inputs": [],
129
+ "name": "tbtcToken",
130
+ "outputs": [
131
+ {
132
+ "internalType": "contract TBTC",
133
+ "name": "",
134
+ "type": "address"
135
+ }
136
+ ],
137
+ "stateMutability": "view",
138
+ "type": "function"
139
+ }
140
+ ],
141
+ "bytecode": "0x60806040523480156200001157600080fd5b5060405162000ebe38038062000ebe8339818101604052810190620000379190620001d4565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415620000aa576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401620000a19062000285565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1614156200011d576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401620001149062000263565b60405180910390fd5b816000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050620003c0565b600081519050620001b7816200038c565b92915050565b600081519050620001ce81620003a6565b92915050565b60008060408385031215620001e857600080fd5b6000620001f885828601620001a6565b92505060206200020b85828601620001bd565b9150509250929050565b600062000224602683620002a7565b9150620002318262000314565b604082019050919050565b60006200024b602083620002a7565b9150620002588262000363565b602082019050919050565b600060208201905081810360008301526200027e8162000215565b9050919050565b60006020820190508181036000830152620002a0816200023c565b9050919050565b600082825260208201905092915050565b6000620002c582620002f4565b9050919050565b6000620002d982620002b8565b9050919050565b6000620002ed82620002b8565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b7f5442544320746f6b656e2063616e206e6f7420626520746865207a65726f206160008201527f6464726573730000000000000000000000000000000000000000000000000000602082015250565b7f42616e6b2063616e206e6f7420626520746865207a65726f2061646472657373600082015250565b6200039781620002cc565b8114620003a357600080fd5b50565b620003b181620002e0565b8114620003bd57600080fd5b50565b610aee80620003d06000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c806376cdb03b1461005c5780638f4ffcb11461007a578063a0712d6814610096578063db006a75146100b2578063e5d3d714146100ce575b600080fd5b6100646100ec565b60405161007191906108a3565b60405180910390f35b610094600480360381019061008f91906106b1565b610110565b005b6100b060048036038101906100ab9190610731565b61021f565b005b6100cc60048036038101906100c79190610731565b61022c565b005b6100d6610239565b6040516100e391906108be565b60405180910390f35b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16146101a0576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610197906108f9565b60405180910390fd5b8273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461020e576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610205906108d9565b60405180910390fd5b610218858561025f565b5050505050565b61022933826103cd565b50565b610236338261025f565b50565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b8173ffffffffffffffffffffffffffffffffffffffff167f4896181ff8f4543cc00db9fe9b6fb7e6f032b7eb772c72ab1ec1b4d2e03b9369826040516102a59190610939565b60405180910390a2600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166379cc679083836040518363ffffffff1660e01b815260040161030a92919061087a565b600060405180830381600087803b15801561032457600080fd5b505af1158015610338573d6000803e3d6000fd5b5050505060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166356a6d9ef83836040518363ffffffff1660e01b815260040161039792919061087a565b600060405180830381600087803b1580156103b157600080fd5b505af11580156103c5573d6000803e3d6000fd5b505050505050565b8060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166370a08231846040518263ffffffff1660e01b81526004016104279190610828565b60206040518083038186803b15801561043f57600080fd5b505afa158015610453573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610477919061075a565b10156104b8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104af90610919565b60405180910390fd5b8173ffffffffffffffffffffffffffffffffffffffff167f30385c845b448a36257a6a1716e6ad2e1bc2cbe333cde1e69fe849ad6511adfe826040516104fe9190610939565b60405180910390a260008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16637c6db49c8330846040518463ffffffff1660e01b815260040161056393929190610843565b600060405180830381600087803b15801561057d57600080fd5b505af1158015610591573d6000803e3d6000fd5b50505050600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166340c10f1983836040518363ffffffff1660e01b81526004016105f292919061087a565b600060405180830381600087803b15801561060c57600080fd5b505af1158015610620573d6000803e3d6000fd5b505050505050565b60008135905061063781610a8a565b92915050565b60008083601f84011261064f57600080fd5b8235905067ffffffffffffffff81111561066857600080fd5b60208301915083600182028301111561068057600080fd5b9250929050565b60008135905061069681610aa1565b92915050565b6000815190506106ab81610aa1565b92915050565b6000806000806000608086880312156106c957600080fd5b60006106d788828901610628565b95505060206106e888828901610687565b94505060406106f988828901610628565b935050606086013567ffffffffffffffff81111561071657600080fd5b6107228882890161063d565b92509250509295509295909350565b60006020828403121561074357600080fd5b600061075184828501610687565b91505092915050565b60006020828403121561076c57600080fd5b600061077a8482850161069c565b91505092915050565b61078c81610965565b82525050565b61079b816109a1565b82525050565b6107aa816109c5565b82525050565b60006107bd601883610954565b91506107c8826109e9565b602082019050919050565b60006107e0601183610954565b91506107eb82610a12565b602082019050919050565b6000610803602283610954565b915061080e82610a3b565b604082019050919050565b61082281610997565b82525050565b600060208201905061083d6000830184610783565b92915050565b60006060820190506108586000830186610783565b6108656020830185610783565b6108726040830184610819565b949350505050565b600060408201905061088f6000830185610783565b61089c6020830184610819565b9392505050565b60006020820190506108b86000830184610792565b92915050565b60006020820190506108d360008301846107a1565b92915050565b600060208201905081810360008301526108f2816107b0565b9050919050565b60006020820190508181036000830152610912816107d3565b9050919050565b60006020820190508181036000830152610932816107f6565b9050919050565b600060208201905061094e6000830184610819565b92915050565b600082825260208201905092915050565b600061097082610977565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60006109ac826109b3565b9050919050565b60006109be82610977565b9050919050565b60006109d0826109d7565b9050919050565b60006109e282610977565b9050919050565b7f4f6e6c7920544254432063616c6c657220616c6c6f7765640000000000000000600082015250565b7f546f6b656e206973206e6f742054425443000000000000000000000000000000600082015250565b7f416d6f756e7420657863656564732062616c616e636520696e2074686520626160008201527f6e6b000000000000000000000000000000000000000000000000000000000000602082015250565b610a9381610965565b8114610a9e57600080fd5b50565b610aaa81610997565b8114610ab557600080fd5b5056fea26469706673582212204fae64683f4115afad228cd61dbb420c88455e81eb214aad7a7c29d7aa5f906164736f6c63430008040033",
142
+ "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100575760003560e01c806376cdb03b1461005c5780638f4ffcb11461007a578063a0712d6814610096578063db006a75146100b2578063e5d3d714146100ce575b600080fd5b6100646100ec565b60405161007191906108a3565b60405180910390f35b610094600480360381019061008f91906106b1565b610110565b005b6100b060048036038101906100ab9190610731565b61021f565b005b6100cc60048036038101906100c79190610731565b61022c565b005b6100d6610239565b6040516100e391906108be565b60405180910390f35b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16146101a0576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610197906108f9565b60405180910390fd5b8273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461020e576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610205906108d9565b60405180910390fd5b610218858561025f565b5050505050565b61022933826103cd565b50565b610236338261025f565b50565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b8173ffffffffffffffffffffffffffffffffffffffff167f4896181ff8f4543cc00db9fe9b6fb7e6f032b7eb772c72ab1ec1b4d2e03b9369826040516102a59190610939565b60405180910390a2600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166379cc679083836040518363ffffffff1660e01b815260040161030a92919061087a565b600060405180830381600087803b15801561032457600080fd5b505af1158015610338573d6000803e3d6000fd5b5050505060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166356a6d9ef83836040518363ffffffff1660e01b815260040161039792919061087a565b600060405180830381600087803b1580156103b157600080fd5b505af11580156103c5573d6000803e3d6000fd5b505050505050565b8060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166370a08231846040518263ffffffff1660e01b81526004016104279190610828565b60206040518083038186803b15801561043f57600080fd5b505afa158015610453573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610477919061075a565b10156104b8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104af90610919565b60405180910390fd5b8173ffffffffffffffffffffffffffffffffffffffff167f30385c845b448a36257a6a1716e6ad2e1bc2cbe333cde1e69fe849ad6511adfe826040516104fe9190610939565b60405180910390a260008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16637c6db49c8330846040518463ffffffff1660e01b815260040161056393929190610843565b600060405180830381600087803b15801561057d57600080fd5b505af1158015610591573d6000803e3d6000fd5b50505050600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166340c10f1983836040518363ffffffff1660e01b81526004016105f292919061087a565b600060405180830381600087803b15801561060c57600080fd5b505af1158015610620573d6000803e3d6000fd5b505050505050565b60008135905061063781610a8a565b92915050565b60008083601f84011261064f57600080fd5b8235905067ffffffffffffffff81111561066857600080fd5b60208301915083600182028301111561068057600080fd5b9250929050565b60008135905061069681610aa1565b92915050565b6000815190506106ab81610aa1565b92915050565b6000806000806000608086880312156106c957600080fd5b60006106d788828901610628565b95505060206106e888828901610687565b94505060406106f988828901610628565b935050606086013567ffffffffffffffff81111561071657600080fd5b6107228882890161063d565b92509250509295509295909350565b60006020828403121561074357600080fd5b600061075184828501610687565b91505092915050565b60006020828403121561076c57600080fd5b600061077a8482850161069c565b91505092915050565b61078c81610965565b82525050565b61079b816109a1565b82525050565b6107aa816109c5565b82525050565b60006107bd601883610954565b91506107c8826109e9565b602082019050919050565b60006107e0601183610954565b91506107eb82610a12565b602082019050919050565b6000610803602283610954565b915061080e82610a3b565b604082019050919050565b61082281610997565b82525050565b600060208201905061083d6000830184610783565b92915050565b60006060820190506108586000830186610783565b6108656020830185610783565b6108726040830184610819565b949350505050565b600060408201905061088f6000830185610783565b61089c6020830184610819565b9392505050565b60006020820190506108b86000830184610792565b92915050565b60006020820190506108d360008301846107a1565b92915050565b600060208201905081810360008301526108f2816107b0565b9050919050565b60006020820190508181036000830152610912816107d3565b9050919050565b60006020820190508181036000830152610932816107f6565b9050919050565b600060208201905061094e6000830184610819565b92915050565b600082825260208201905092915050565b600061097082610977565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60006109ac826109b3565b9050919050565b60006109be82610977565b9050919050565b60006109d0826109d7565b9050919050565b60006109e282610977565b9050919050565b7f4f6e6c7920544254432063616c6c657220616c6c6f7765640000000000000000600082015250565b7f546f6b656e206973206e6f742054425443000000000000000000000000000000600082015250565b7f416d6f756e7420657863656564732062616c616e636520696e2074686520626160008201527f6e6b000000000000000000000000000000000000000000000000000000000000602082015250565b610a9381610965565b8114610a9e57600080fd5b50565b610aaa81610997565b8114610ab557600080fd5b5056fea26469706673582212204fae64683f4115afad228cd61dbb420c88455e81eb214aad7a7c29d7aa5f906164736f6c63430008040033",
143
+ "linkReferences": {},
144
+ "deployedLinkReferences": {}
145
+ }
@@ -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,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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keep-network/tbtc-v2",
3
- "version": "0.1.1-dev.2+main.cd434b847d1fa33a36db2bf535bafa0dd706a781",
3
+ "version": "0.1.1-dev.3+main.653c854b1f5ee8e14afc2124201abb620500c772",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "artifacts/",
@@ -32,7 +32,7 @@
32
32
  "@thesis/solidity-contracts": "github:thesis/solidity-contracts#4985bcf"
33
33
  },
34
34
  "devDependencies": {
35
- "@keep-network/hardhat-helpers": "^0.2.0-pre.4",
35
+ "@keep-network/hardhat-helpers": "0.4.1-pre.1",
36
36
  "@keep-network/hardhat-local-networks-config": "^0.1.0-pre.0",
37
37
  "@nomiclabs/hardhat-ethers": "^2.0.2",
38
38
  "@nomiclabs/hardhat-etherscan": "^2.1.4",