-
Notifications
You must be signed in to change notification settings - Fork 0
/
file1
121 lines (105 loc) · 3.99 KB
/
file1
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
pragma solidity 0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract BPADVault is Ownable, ReentrancyGuard {
using SafeMath for uint256;
struct Items {
address tokenAddress;
address withdrawalAddress;
address sender;
uint256 tokenAmount;
uint256 unlockTime;
uint256 lockTime;
bool withdrawn;
uint128 purpose;
}
uint256 public depositId;
uint256[] public allDepositIds;
mapping(address => uint256[]) public depositsByWithdrawalAddress;
mapping(uint256 => Items) public lockedToken;
mapping(address => mapping(address => uint256)) public walletTokenBalance;
// event LogWithdrawal(address SentToAddress, uint256 AmountTransferred);
event LogTokensLocked(
address indexed tokenAddress,
address indexed sender,
uint256 amount,
uint256 unlockTime,
uint256 depositId
);
event LogTokensWithdrawn(
address indexed tokenAddress,
address indexed receiver,
uint256 amount,
uint256 depositId
);
uint256 public bnbFee = 0.01 ether;
uint256 public vestingDiscount = 5;
uint256 public totalBnbFees = 0;
uint256 public remainingBnbFees = 0;
function lockTokens(
address _tokenAddress,
address _withdrawalAddress,
uint256 _amount,
uint256 _unlockTime,
uint128 _purpose
) external payable returns (uint256 _id) {
require(_amount > 0);
require(_unlockTime < 10000000000);
require(_unlockTime > block.timestamp);
require(msg.value >= bnbFee, "fees missing");
totalBnbFees = totalBnbFees.add(msg.value);
remainingBnbFees = remainingBnbFees.add(msg.value);
walletTokenBalance[_tokenAddress][
_withdrawalAddress
] = walletTokenBalance[_tokenAddress][_withdrawalAddress].add(_amount);
_id = ++depositId;
lockedToken[_id].tokenAddress = _tokenAddress;
lockedToken[_id].sender = msg.sender;
lockedToken[_id].withdrawalAddress = _withdrawalAddress;
lockedToken[_id].tokenAmount = _amount;
lockedToken[_id].unlockTime = _unlockTime;
lockedToken[_id].lockTime = block.timestamp;
lockedToken[_id].withdrawn = false;
lockedToken[_id].purpose = _purpose;
allDepositIds.push(_id);
depositsByWithdrawalAddress[_withdrawalAddress].push(_id);
require(
IERC20(_tokenAddress).transferFrom(
msg.sender,
address(this),
_amount
)
);
emit LogTokensLocked(
_tokenAddress,
msg.sender,
_amount,
_unlockTime,
depositId
);
}
function withdrawTokens(uint256 _id) public {
require(block.timestamp >= lockedToken[_id].unlockTime);
require(msg.sender == lockedToken[_id].withdrawalAddress);
require(!lockedToken[_id].withdrawn);
lockedToken[_id].withdrawn = true;
address withdrawalAddress = lockedToken[_id].withdrawalAddress;
address tokenAddress = lockedToken[_id].tokenAddress;
uint256 amount = lockedToken[_id].tokenAmount;
walletTokenBalance[tokenAddress][msg.sender] = walletTokenBalance[
tokenAddress
][msg.sender]
.sub(amount);
uint256 j;
uint256 arrLength =
depositsByWithdrawalAddress[withdrawalAddress].length;
for (j = 0; j < arrLength; j++) {
if (depositsByWithdrawalAddress[withdrawalAddress][j] == _id) {
depositsByWithdrawalAddress[withdrawalAddress][
j
] = depositsByWithdrawalAddress[withdrawalAddress][
arrLength - 1
];
depositsByWithdrawalAddress[withdrawalAddress].pop();