Lachain amToken handler
1
// pragma solidity 0.6.4;
2
pragma experimental ABIEncoderV2;
3
import "../interfaces/IDepositExecute.sol";
4
import "../interfaces/IERC20.sol";
5
import "../interfaces/ILendingPool.sol";
6
import "../interfaces/IBridge.sol";
7
import "./HandlerHelpers.sol";
8
import "../ERC20Safe.sol";
9
import "../ExampleToken.sol";
10
import "../utils/Ownable.sol";
11
/**
12
@title Handles ERC20 deposits and deposit executions.
13
@notice This contract is intended to be used with the Bridge contract.
14
*/
15
contract AmTokenHandler is IDepositExecute, HandlerHelpers, Ownable, ERC20Safe {
16
address public constant AAVE_POLYGON_LP_ADDR = 0x8dFf5E27EA6b7AC08EbFdf9eB090F32ee9a30fcf;
17
address public constant AAVE_MUMBAI_LP_ADDR = 0x9198F13B08E299d85E096929fA9781A1E3d5d827;
18
bytes8 public constant LACHAIN_ID = 0x0000000000000019;
19
bytes8 public constant LACHAIN_TESTNET_ID = 0x00000000574c4131;
20
address public aaveHandler;
21
address public erc20Handler;
22
struct DepositRecord {
23
address _tokenAddress;
24
bytes8 _destinationChainID;
25
bytes32 _resourceID;
26
address _destinationRecipientAddress;
27
address _depositer;
28
uint256 _amount;
29
}
30
mapping(bytes8 => mapping(uint64 => DepositRecord)) public _depositRecords;
31
mapping(bytes32 => bytes32) public amTokenResources;
32
event AAVEWithdrawDone(
33
address indexed recipientAddress,
34
uint256 amount,
35
bytes32 indexed resourceID
36
);
37
/**
38
@param bridgeAddress Contract address of previously deployed Bridge.
39
*/
40
constructor(
41
address bridgeAddress
42
) public {
43
_bridgeAddress = bridgeAddress;
44
}
45
function setAaveHandler(address _aaveHandler) external onlyOwner {
46
aaveHandler = _aaveHandler;
47
}
48
function setErc20Handler(address _erc20Handler) external onlyOwner {
49
erc20Handler = _erc20Handler;
50
}
51
function setAmTokenResources(bytes32 amTokenResourceId, bytes32 aaveTokenResourceId) external onlyOwner {
52
amTokenResources[amTokenResourceId] = aaveTokenResourceId;
53
}
54
function adminChangeBridgeAddress(address newBridgeAddress) external onlyOwner {
55
_bridgeAddress = newBridgeAddress;
56
}
57
function getDepositRecord(uint64 depositNonce, bytes8 destId)
58
external
59
view
60
returns (DepositRecord memory)
61
{
62
return _depositRecords[destId][depositNonce];
63
}
64
function deposit(
65
bytes32 resourceID,
66
bytes8 destinationChainID,
67
uint64 depositNonce,
68
address depositer,
69
address recipientAddress,
70
uint256 amount,
71
bytes calldata params
72
) external override onlyBridge returns (address) {
73
address tokenAddress = _resourceIDToTokenContractAddress[resourceID];
74
require(
75
_contractWhitelist[tokenAddress],
76
"provided tokenAddress is not whitelisted"
77
);
78
if (_burnList[tokenAddress]) {
79
burnERC20(tokenAddress, depositer, amount);
80
} else {
81
lockERC20(tokenAddress, depositer, address(this), amount);
82
}
83
_depositRecords[destinationChainID][depositNonce] = DepositRecord(
84
tokenAddress,
85
destinationChainID,
86
resourceID,
87
recipientAddress,
88
depositer,
89
amount
90
);
91
return (tokenAddress);
92
}
93
function executeProposal(
94
bytes32 resourceID,
95
address recipientAddress,
96
uint256 amount,
97
bytes calldata params
98
) external override onlyBridge {
99
address tokenAddress = _resourceIDToTokenContractAddress[amTokenResources[resourceID]];
100
uint256 amountToWithdraw = ILendingPool(AAVE_MUMBAI_LP_ADDR).withdraw(tokenAddress, amount, erc20Handler);
101
IBridge(_bridgeAddress).internalDeposit(LACHAIN_TESTNET_ID, amTokenResources[resourceID], amountToWithdraw, recipientAddress);
102
emit AAVEWithdrawDone(
103
recipientAddress,
104
amountToWithdraw,
105
resourceID
106
);
107
}
108
/**
109
@notice Used to manually release ERC20 tokens from ERC20Safe.
110
@param tokenAddress Address of token contract to release.
111
@param recipient Address to release tokens to.
112
@param amount The amount of ERC20 tokens to release.
113
*/
114
function withdraw(
115
address tokenAddress,
116
address recipient,
117
uint256 amount
118
) external override onlyBridge {
119
releaseERC20(tokenAddress, recipient, amount);
120
}
121
function getAddressFromResourceId(bytes32 resourceID) external override view returns (address) {
122
return _resourceIDToTokenContractAddress[resourceID];
123
}
124
}
Copied!
Copy link