forked from calvinheath/wheat-v1-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNativeBridge.sol
53 lines (45 loc) · 1.59 KB
/
NativeBridge.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
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.6.0;
import { Transfers } from "./modules/Transfers.sol";
import { Wrapping } from "./modules/Wrapping.sol";
interface IStrategy
{
function reserveToken() external view returns (address _reserveToken);
function deposit(uint256 _amount) external;
function withdraw(uint256 _shares) external;
}
contract NativeBridge
{
address public immutable nativeToken;
constructor (address _nativeToken) public
{
nativeToken = _nativeToken;
}
function deposit(address _strategyToken) public payable
{
address _from = msg.sender;
uint256 _amount = msg.value;
address _reserveToken = IStrategy(_strategyToken).reserveToken();
require(_reserveToken == nativeToken, "unsupported operation");
Wrapping._wrap(_reserveToken, _amount);
Transfers._approveFunds(_reserveToken, _strategyToken, _amount);
IStrategy(_strategyToken).deposit(_amount);
uint256 _shares = Transfers._getBalance(_strategyToken);
Transfers._pushFunds(_strategyToken, _from, _shares);
}
function withdraw(address _strategyToken, uint256 _shares) public
{
address payable _from = msg.sender;
address _reserveToken = IStrategy(_strategyToken).reserveToken();
require(_reserveToken == nativeToken, "unsupported operation");
Transfers._pullFunds(_strategyToken, _from, _shares);
IStrategy(_strategyToken).withdraw(_shares);
uint256 _amount = Transfers._getBalance(_reserveToken);
Wrapping._unwrap(_reserveToken, _amount);
_from.transfer(_amount);
}
receive() external payable
{
require(msg.sender == nativeToken, "not allowed"); // not to be used directly
}
}