-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAbond_Token.sol
237 lines (204 loc) · 7.3 KB
/
Abond_Token.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// SPDX-License-Identifier: MIT
pragma solidity 0.8.22;
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20BurnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {State} from "../interface/IAbond.sol";
import "../lib/Colors.sol";
/**
* @title ABONDToken contract
* @author Autonomint
* @notice The ABOND token contract
* - has all ERC20 functions
*/
contract ABONDToken is
Initializable,
ERC20Upgradeable,
ERC20BurnableUpgradeable,
ERC20PausableUpgradeable,
UUPSUpgradeable,
OwnableUpgradeable
{
mapping(address user => State) public userStates; // ABOND state of this user
mapping(address user => mapping(uint64 index => State)) public userStatesAtDeposits; // ABOND state at each deposits
uint128 PRECISION;
address private borrowingContract; // borrowing contract address
/**
* @dev initialize function to initialize the contract
*/
function initialize() public initializer {
// Initialize the ERC20 token
__ERC20_init("ABOND Token", "ABOND");
// Initialize the token as burnable
__ERC20Burnable_init();
// Initialize the token as pausable
__ERC20Pausable_init();
// Initialize the owner of the contract
__Ownable_init(msg.sender);
// Initialize the proxy
__UUPSUpgradeable_init();
PRECISION = 1e18;
}
function _authorizeUpgrade(
address newImplementation
) internal override onlyOwner {}
/**
* @dev modifier to check whether the msg.sender is borrowing contract
*/
modifier onlyBorrowingContract() {
require(msg.sender == borrowingContract,"This function can only called by borrowing contract");
_;
}
/**
* @dev Function to check if an address is a contract
* @param account address to check whether the address is an contract address or EOA
*/
function isContract(address account) internal view returns (bool) {
return account.code.length > 0;
}
function pause() public onlyOwner {
_pause();
}
function unpause() public onlyOwner {
_unpause();
}
/**
* @dev mints ABOND token to the user
* @param to Address to mint
* @param index Withdraw positions index in borrowing
* @param amount amount of ABOND to mint
*/
function mint(
address to,
uint64 index,
uint256 amount
) public onlyBorrowingContract returns (bool) {
// check the to address is non zero address
require(to != address(0), "Invalid User");
// get the initial(deposit) state and current state
State memory fromState = userStatesAtDeposits[to][index];
State memory toState = userStates[to];
// update the from state
fromState.ethBacked = (fromState.ethBacked * PRECISION) / uint128(amount);
// update the to state
toState = Colors._credit(fromState, toState, uint128(amount));
userStatesAtDeposits[to][index] = fromState;
userStates[to] = toState;
// mint abond
_mint(to, amount);
return true;
}
/**
* transfer abond from msg.sender to, to address
* @param to address of the recepient
* @param value abond amount to transfer
*/
function transfer(
address to,
uint256 value
) public override returns (bool) {
// check the input params are non zero
require(msg.sender != address(0) && to != address(0), "Invalid User");
// get the sender and receiver state
State memory fromState = userStates[msg.sender];
State memory toState = userStates[to];
// check sender has enough abond to transfer
require(fromState.aBondBalance >= value, "Insufficient aBond balance");
// update receiver state
toState = Colors._credit(fromState, toState, uint128(value));
userStates[to] = toState;
// update sender state
fromState = Colors._debit(fromState, uint128(value));
userStates[msg.sender] = fromState;
// transfer abond
super.transfer(to, value);
return true;
}
/**
* transfer abond from spender to, to address
* @param from address of the abond owner
* @param to address of the recepient
* @param value abond amount to transfer
*/
function transferFrom(
address from,
address to,
uint256 value
) public override returns (bool) {
// check the input params are non zero
require(from != address(0) && to != address(0), "Invalid User");
// get the sender and receiver state
State memory fromState = userStates[from];
State memory toState = userStates[to];
// update receiver state
toState = Colors._credit(fromState, toState, uint128(value));
userStates[to] = toState;
// update sender state
fromState = Colors._debit(fromState, uint128(value));
userStates[msg.sender] = fromState;
// transfer abond
super.transferFrom(from, to, value);
return true;
}
function burnFromUser(
address to,
uint256 amount
) public onlyBorrowingContract returns (bool) {
//get the state
State memory state = userStates[to];
// update the state
state = Colors._debit(state, uint128(amount));
userStates[to] = state;
// burn abond
burnFrom(to, amount);
return true;
}
function burn(uint256 value) public override onlyBorrowingContract {
super.burn(value);
}
function burnFrom(
address account,
uint256 value
) public override onlyBorrowingContract {
super.burnFrom(account, value);
}
function _update(
address from,
address to,
uint256 value
) internal override(ERC20Upgradeable, ERC20PausableUpgradeable) {
super._update(from, to, value);
}
/**
* @dev set the borrowing contract
* @param _address Borrowing contract address
*/
function setBorrowingContract(address _address) external onlyOwner {
require(_address != address(0) && isContract(_address),"Input address is invalid");
borrowingContract = _address;
}
/**
* @dev sets the abond data for the borrower during deposit
* @param user address of the borrower
* @param index index
* @param ethBacked eth backed for abond
* @param cumulativeRate ext protocol cumulative rate
*/
function setAbondData(
address user,
uint64 index,
uint128 ethBacked,
uint128 cumulativeRate
) external onlyBorrowingContract {
// get the state
State memory state = userStatesAtDeposits[user][index];
// assign cr and eth backed, since we dont know abond amount during deposit
state.cumulativeRate = cumulativeRate;
state.ethBacked = ethBacked;
// update the state
userStatesAtDeposits[user][index] = state;
}
}