-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarFactory.sol
76 lines (63 loc) · 2.29 KB
/
CarFactory.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "./interfaces/ICarMarket.sol";
import "./interfaces/ICarToken.sol";
/**
* @title CarFactory
* @author Jelo
* @notice This is a contract that handles crucial changes in the car company.
* It also gives out flashloans to existing customers of the car company.
*/
contract CarFactory {
// -- States --
address private _owner;
address private carFactory;
ICarToken private carToken;
ICarMarket public carMarket;
/**
* @notice Sets the car Market and car token during deployment.
* @param _carMarket The exchange where car trades take place.
* @param _carToken The token used to purchase cars.
*/
constructor(address _carMarket, address _carToken) {
carToken = ICarToken(_carToken);
carMarket = ICarMarket(_carMarket);
}
/**
* @notice Gives out flashLoan to an existing customer.
* @param _amount The amount to be borrowed.
*/
function flashLoan(uint256 _amount) external {
//checks if the address has purchased a car previously.
require(
carMarket.isExistingCustomer(msg.sender),
"Not existing customer"
);
//fetches the balance of the carFactory before loaning out.
uint balanceBefore = carToken.balanceOf(carFactory);
//check if there is enough amount in the contract to borrow.
require(balanceBefore >= _amount, "Amount not available");
//transfers the amount to be borrowed to the borrower
carToken.transfer(msg.sender, _amount);
(bool success, ) = msg.sender.call(
abi.encodeWithSignature("receivedCarToken(address)", address(this))
);
require(success, "Call to target failed");
//fetches the balance of the carFactory after loaning out.
uint balanceAfter = carToken.balanceOf(carFactory);
//ensures that the Loan has been paid
require(balanceAfter >= balanceBefore, "Loan not paid in full");
}
/**
* @dev Returns the car market
*/
function getCarMarket() external view returns (ICarMarket) {
return carMarket;
}
/**
* @dev Returns the car token
*/
function getCarToken() external view returns (ICarToken) {
return carToken;
}
}