-
Notifications
You must be signed in to change notification settings - Fork 47
/
LoanTokenLogicWrbtc.sol
151 lines (137 loc) · 5.79 KB
/
LoanTokenLogicWrbtc.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
// SPDX-License-Identifier: MIT
pragma solidity 0.5.17;
pragma experimental ABIEncoderV2;
import "../../LoanTokenLogicStandard.sol";
contract LoanTokenLogicWrbtc is LoanTokenLogicStandard {
/**
* @notice This function is MANDATORY, which will be called by LoanTokenLogicBeacon and be registered.
* Every new public function, the signature needs to be included in this function.
*
* @dev This function will return the list of function signature in this contract that are available for public call
* Then this function will be called by LoanTokenLogicBeacon, and the function signatures will be registred in LoanTokenLogicBeacon.
* @dev To save the gas we can just directly return the list of function signature from this pure function.
* The other workaround (fancy way) is we can create a storage for the list of the function signature, and then we can store each function signature to that storage from the constructor.
* Then, in this function we just need to return that storage variable.
*
* @return The list of function signatures (bytes4[])
*/
function getListFunctionSignatures()
external
pure
returns (bytes4[] memory functionSignatures, bytes32 moduleName)
{
bytes4[] memory res = new bytes4[](28);
// Loan Token Logic Standard, Trade & Borrow
res[0] = this.borrow.selector;
res[1] = this.marginTrade.selector;
res[2] = this.marginTradeAffiliate.selector;
res[3] = this.transfer.selector;
res[4] = this.transferFrom.selector;
res[5] = this.profitOf.selector;
res[6] = this.tokenPrice.selector;
res[7] = this.checkpointPrice.selector;
res[8] = this.marketLiquidity.selector;
res[9] = this.avgBorrowInterestRate.selector;
res[10] = this.borrowInterestRate.selector;
res[11] = this.nextBorrowInterestRate.selector;
res[12] = this.supplyInterestRate.selector;
res[13] = this.nextSupplyInterestRate.selector;
res[14] = this.totalSupplyInterestRate.selector;
res[15] = this.totalAssetBorrow.selector;
res[16] = this.totalAssetSupply.selector;
res[17] = this.getMaxEscrowAmount.selector;
res[18] = this.assetBalanceOf.selector;
res[19] = this.getEstimatedMarginDetails.selector;
res[20] = this.getDepositAmountForBorrow.selector;
res[21] = this.getBorrowAmountForDeposit.selector;
res[22] = this.checkPriceDivergence.selector;
res[23] = this.calculateSupplyInterestRate.selector;
// Advanced Token
res[24] = this.approve.selector;
// Advanced Token Storage
res[25] = this.totalSupply.selector;
res[26] = this.balanceOf.selector;
res[27] = this.allowance.selector;
return (res, stringToBytes32("LoanTokenLogicWrbtc"));
}
/**
* @dev internal override functions
* @dev Put all of internal override function dedicated to the loanTokenWrtbc module here
* e.g: _verifyTransfers will override the implementation of _verifyTransfers in loanTokenLogicSplit
*/
/**
* @notice Handle transfers prior to adding newPrincipal to loanTokenSent.
*
* @param collateralTokenAddress The address of the collateral token.
* @param sentAddresses The struct which contains addresses of
* - lender
* - borrower
* - receiver
* - manager
*
* @param sentAmounts The struct which contains uint256 of:
* - interestRate
* - newPrincipal
* - interestInitialAmount
* - loanTokenSent
* - collateralTokenSent
*
* @param withdrawalAmount The amount to withdraw.
*
* @return msgValue The amount of value sent.
* */
function _verifyTransfers(
address collateralTokenAddress,
MarginTradeStructHelpers.SentAddresses memory sentAddresses,
MarginTradeStructHelpers.SentAmounts memory sentAmounts,
uint256 withdrawalAmount
) internal returns (uint256 msgValue) {
address _wrbtcToken = wrbtcTokenAddress;
address _loanTokenAddress = _wrbtcToken;
address receiver = sentAddresses.receiver;
uint256 newPrincipal = sentAmounts.newPrincipal;
uint256 loanTokenSent = sentAmounts.loanTokenSent;
uint256 collateralTokenSent = sentAmounts.collateralTokenSent;
require(_loanTokenAddress != collateralTokenAddress, "26");
msgValue = msg.value;
if (withdrawalAmount != 0) {
/// withdrawOnOpen == true
IWrbtcERC20(_wrbtcToken).withdraw(withdrawalAmount);
Address.sendValue(receiver, withdrawalAmount);
if (newPrincipal > withdrawalAmount) {
_safeTransfer(
_loanTokenAddress,
sovrynContractAddress,
newPrincipal - withdrawalAmount,
""
);
}
} else {
_safeTransfer(_loanTokenAddress, sovrynContractAddress, newPrincipal, "27");
}
if (collateralTokenSent != 0) {
_safeTransferFrom(
collateralTokenAddress,
msg.sender,
sovrynContractAddress,
collateralTokenSent,
"28"
);
}
if (loanTokenSent != 0) {
if (msgValue != 0 && msgValue >= loanTokenSent) {
IWrbtc(_wrbtcToken).deposit.value(loanTokenSent)();
_safeTransfer(_loanTokenAddress, sovrynContractAddress, loanTokenSent, "29");
msgValue -= loanTokenSent;
} else {
_safeTransferFrom(
_loanTokenAddress,
msg.sender,
sovrynContractAddress,
loanTokenSent,
"29"
);
}
}
}
}