Custom Contract
Here is a example usage of the Farming Extension, extracted from the template of the Covenants frontend that can be found here. It implements an owner-based logic where there is a host wallet that rules the writable functions (e.g. to create new setups).
The constructor cannot be used to link the extension to the farming contract (because the extension address is needed to create it). However, the farming contract supports a lazy initialization approach, allowing it to pass an ABI-encoded method payload during farming contract creation. For example, this template reports the optional init(bool byMint, address host, address treasury) method to specify initialization arguments.
init is optional because the only mandatory functions are transferTo(uint256 amount) public and backToYou(uint256 amount) payable public, because their method signatures are used in the farming Contract. However, of course, their bodies can be changed to follow your logic.
1
contract FarmExtension is IFarmExtension {
2
3
// wallet who has control on the extension and treasury
4
address internal _host;
5
address internal _treasury;
6
// address of the farm main contract linked to this extension
7
address internal _farmMainContract;
8
// the reward token address linked to this extension
9
address internal _rewardTokenAddress;
10
// whether the token is by mint or by reserve
11
bool internal _byMint;
12
13
/** MODIFIERS */
14
15
/** @dev farmMainOnly modifier used to check for unauthorized transfers. */
16
modifier farmMainOnly() {
17
require(msg.sender == _farmMainContract, "Unauthorized");
18
_;
19
}
20
21
/** @dev hostOnly modifier used to check for unauthorized edits. */
22
modifier hostOnly() {
23
require(msg.sender == _host, "Unauthorized");
24
_;
25
}
26
27
/** PUBLIC METHODS */
28
29
receive() external payable {
30
require(_farmMainContract != address(0) && _rewardTokenAddress == address(0), "ETH not allowed");
31
}
32
33
function init(bool byMint, address host, address treasury) public virtual override {
34
require(_farmMainContract == address(0), "Already init");
35
require((_host = host) != address(0), "blank host");
36
_rewardTokenAddress = IFarmMain(_farmMainContract = msg.sender)._rewardTokenAddress();
37
_byMint = byMint;
38
_treasury = treasury != address(0) ? treasury : host;
39
}
40
41
function data() view public virtual override returns(address farmMainContract, bool byMint, address host, address treasury, address rewardTokenAddress) {
42
return (_farmMainContract, _byMint, _host, _treasury, _rewardTokenAddress);
43
}
44
45
/** @dev method used to update the extension host.
46
* @param host new host address.
47
*/
48
function setHost(address host) public virtual override hostOnly {
49
_host = host;
50
}
51
52
/** @dev method used to update the extension treasury.
53
* @param treasury new treasury address.
54
*/
55
function setTreasury(address treasury) public virtual override hostOnly {
56
_treasury = treasury;
57
}
58
59
60
/** @dev this function calls the farm main contract with the given address and sets the given farming setups.
61
* @param farmingSetups array containing all the farming setups.
62
*/
63
function setFarmingSetups(FarmingSetupConfiguration[] memory farmingSetups) public virtual override hostOnly {
64
IFarmMain(_farmMainContract).setFarmingSetups(farmingSetups);
65
}
66
67
/** @dev transfers the input amount to the caller liquidity mining contract.
68
* @param amount amount of erc20 to transfer or mint.
69
*/
70
function transferTo(uint256 amount) public virtual override farmMainOnly {
71
if(_rewardTokenAddress != address(0)) {
72
return _byMint ? _mintAndTransfer(_rewardTokenAddress, _farmMainContract, amount) : _safeTransfer(_rewardTokenAddress, _farmMainContract, amount);
73
}
74
(bool result, ) = _farmMainContract.call{value:amount}("");
75
require(result, "ETH transfer failed.");
76
}
77
78
/** @dev transfers the input amount from the caller liquidity mining contract to the extension.
79
* @param amount amount of erc20 to transfer back or burn.
80
*/
81
function backToYou(uint256 amount) payable public virtual override farmMainOnly {
82
if(_rewardTokenAddress != address(0)) {
83
_safeTransferFrom(_rewardTokenAddress, msg.sender, _byMint ? address(this) : _treasury, amount);
84
if(_byMint) {
85
_burn(_rewardTokenAddress, amount);
86
}
87
} else {
88
require(msg.value == amount, "invalid sent amount");
89
if(_treasury != address(this)) {
90
(bool result, ) = _treasury.call{value:amount}("");
91
require(result, "ETH transfer failed.");
92
}
93
}
94
}
95
96
/** INTERNAL METHODS */
97
98
function _mintAndTransfer(address erc20TokenAddress, address recipient, uint256 value) internal virtual {
99
IERC20Mintable(erc20TokenAddress).mint(recipient, value);
100
}
101
102
function _burn(address erc20TokenAddress, uint256 value) internal virtual {
103
IERC20Mintable(erc20TokenAddress).burn(msg.sender, value);
104
}
105
106
/** @dev function used to safely approve ERC20 transfers.
107
* @param erc20TokenAddress address of the token to approve.
108
* @param to receiver of the approval.
109
* @param value amount to approve for.
110
*/
111
function _safeApprove(address erc20TokenAddress, address to, uint256 value) internal virtual {
112
bytes memory returnData = _call(erc20TokenAddress, abi.encodeWithSelector(IERC20(erc20TokenAddress).approve.selector, to, value));
113
require(returnData.length == 0 || abi.decode(returnData, (bool)), 'APPROVE_FAILED');
114
}
115
116
/** @dev function used to safe transfer ERC20 tokens.
117
* @param erc20TokenAddress address of the token to transfer.
118
* @param to receiver of the tokens.
119
* @param value amount of tokens to transfer.
120
*/
121
function _safeTransfer(address erc20TokenAddress, address to, uint256 value) internal virtual {
122
bytes memory returnData = _call(erc20TokenAddress, abi.encodeWithSelector(IERC20(erc20TokenAddress).transfer.selector, to, value));
123
require(returnData.length == 0 || abi.decode(returnData, (bool)), 'TRANSFER_FAILED');
124
}
125
126
/** @dev this function safely transfers the given ERC20 value from an address to another.
127
* @param erc20TokenAddress erc20 token address.
128
* @param from address from.
129
* @param to address to.
130
* @param value amount to transfer.
131
*/
132
function _safeTransferFrom(address erc20TokenAddress, address from, address to, uint256 value) internal virtual {
133
bytes memory returnData = _call(erc20TokenAddress, abi.encodeWithSelector(IERC20(erc20TokenAddress).transferFrom.selector, from, to, value));
134
require(returnData.length == 0 || abi.decode(returnData, (bool)), 'TRANSFERFROM_FAILED');
135
}
136
137
/** @dev calls the contract at the given location using the given payload and returns the returnData.
138
* @param location location to call.
139
* @param payload call payload.
140
* @return returnData call return data.
141
*/
142
function _call(address location, bytes memory payload) private returns(bytes memory returnData) {
143
assembly {
144
let result := call(gas(), location, 0, add(payload, 0x20), mload(payload), 0, 0)
145
let size := returndatasize()
146
returnData := mload(0x40)
147
mstore(returnData, size)
148
let returnDataPayloadStart := add(returnData, 0x20)
149
returndatacopy(returnDataPayloadStart, 0, size)
150
mstore(0x40, add(returnDataPayloadStart, size))
151
switch result case 0 {revert(returnDataPayloadStart, size)}
152
}
153
}
154
}
Copied!
Copy link