Add events

This commit is contained in:
Mubaris NK 2021-04-04 10:12:41 +05:30
parent 00ab0f7bff
commit 1d2725f36a
No known key found for this signature in database
GPG Key ID: 9AC09AD0F8D68561
6 changed files with 128 additions and 32 deletions

View File

@ -0,0 +1,13 @@
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
contract Events {
event LogMigrateAaveV2(
address indexed owner,
address[] supplyTokens,
address[] borrowTokens,
uint[] supplyAmts,
uint[] variableBorrowAmts,
uint[] stableBorrowAmts
);
}

View File

@ -4,8 +4,9 @@ pragma experimental ABIEncoderV2;
import { TokenInterface, AccountInterface } from "../../common/interfaces.sol";
import { AaveInterface, ReceiverInterface, AaveData } from "./interfaces.sol";
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
contract AaveMigratorResolver is Helpers {
contract AaveMigratorResolver is Helpers, Events {
ReceiverInterface public immutable receiver;
constructor(address _receiver) {

View File

@ -0,0 +1,16 @@
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
contract Events {
event LogDeposit(
address owner,
address[] tokens,
uint[] amts
);
event LogWithdraw(
address owner,
address[] tokens,
uint[] amts
);
}

View File

@ -6,8 +6,9 @@ import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { DSMath } from "../../common/math.sol";
import { AccountInterface } from "./interfaces.sol";
import { Events } from "./events.sol";
contract MigrateResolver is DSMath {
contract MigrateResolver is DSMath, Events {
using SafeERC20 for IERC20;
struct AaveData {
@ -20,25 +21,15 @@ contract MigrateResolver is DSMath {
address[] borrowTokens;
}
uint private lastStateId;
mapping (address => AaveData) public positions;
mapping(address => mapping(address => uint)) deposits;
function _migratePosition(address owner) internal {
AaveData storage data = positions[owner];
for (uint i = 0; i < data.supplyTokens.length; i++) {
IERC20(data.supplyTokens[i]).safeTransfer(data.targetDsa, data.supplyAmts[i]);
}
AccountInterface(data.targetDsa).migrateAave(owner);
data.isFinal = true;
}
function deposit(address[] calldata tokens, uint[] calldata amts) external {
uint _length = tokens.length;
require(_length == amts.length, "invalid-length");
uint[] memory _amts = new uint[](_length);
for (uint256 i = 0; i < _length; i++) {
address _token = tokens[i];
@ -47,13 +38,18 @@ contract MigrateResolver is DSMath {
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
deposits[msg.sender][_token] = _amt;
_amts[i] = _amt;
}
emit LogDeposit(msg.sender, tokens, _amts);
}
function withdraw(address[] calldata tokens, uint[] calldata amts) external {
uint _length = tokens.length;
require(_length == amts.length, "invalid-length");
uint[] memory _amts = new uint[](_length);
for (uint256 i = 0; i < _length; i++) {
uint _amt = amts[i];
address _token = tokens[i];
@ -66,6 +62,44 @@ contract MigrateResolver is DSMath {
IERC20(_token).safeTransfer(msg.sender, _amt);
deposits[msg.sender][_token] = sub(maxAmt, _amt);
_amts[i] = _amt;
}
emit LogWithdraw(msg.sender, tokens, _amts);
}
}
contract AaveV2Migrator is MigrateResolver {
using SafeERC20 for IERC20;
uint private lastStateId;
function _migratePosition(address owner) internal {
AaveData storage data = positions[owner];
for (uint i = 0; i < data.supplyTokens.length; i++) {
IERC20(data.supplyTokens[i]).safeTransfer(data.targetDsa, data.supplyAmts[i]);
}
AccountInterface(data.targetDsa).migrateAave(owner);
data.isFinal = true;
}
function getPosition(address owner) public view returns (AaveData memory data) {
data = positions[owner];
}
function canMigrate(address owner) public view returns (bool can) {
can = true;
AaveData memory data = getPosition(owner);
for (uint i = 0; i < data.supplyTokens.length; i++) {
IERC20 token = IERC20(data.supplyTokens[i]);
if (token.balanceOf(address(this)) < data.supplyAmts[i]) {
can = false;
}
}
}
@ -87,21 +121,4 @@ contract MigrateResolver is DSMath {
_migratePosition(owner);
}
function getPosition(address owner) public view returns (AaveData memory data) {
data = positions[owner];
}
function canMigrate(address owner) public view returns (bool can) {
can = true;
AaveData memory data = getPosition(owner);
for (uint i = 0; i < data.supplyTokens.length; i++) {
IERC20 token = IERC20(data.supplyTokens[i]);
if (token.balanceOf(address(this)) < data.supplyAmts[i]) {
can = false;
}
}
}
}

View File

@ -0,0 +1,26 @@
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
contract Events {
event LogDeposit(
address owner,
address[] tokens,
uint[] amts
);
event LogWithdraw(
address owner,
address[] tokens,
uint[] amts
);
event LogAaveV2Migrate(
address indexed user,
address indexed targetDsa,
address[] supplyTokens,
address[] borrowTokens,
uint[] supplyAmts,
uint[] variableBorrowAmts,
uint[] stableBorrowAmts
);
}

View File

@ -7,8 +7,9 @@ import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { TokenInterface } from "../../common/interfaces.sol";
import { Helpers } from "./helpers.sol";
import { AaveInterface, ATokenInterface } from "./interfaces.sol";
import { Events } from "./events.sol";
contract LiquidityResolver is Helpers {
contract LiquidityResolver is Helpers, Events {
using SafeERC20 for IERC20;
mapping(address => mapping(address => uint)) deposits;
@ -17,6 +18,8 @@ contract LiquidityResolver is Helpers {
uint _length = tokens.length;
require(_length == amts.length, "invalid-length");
uint[] memory _amts = new uint[](_length);
for (uint256 i = 0; i < _length; i++) {
uint _amt;
address _token = tokens[i];
@ -31,14 +34,20 @@ contract LiquidityResolver is Helpers {
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
}
_amts[i] = _amt;
deposits[msg.sender][_token] = _amt;
}
emit LogDeposit(msg.sender, tokens, _amts);
}
function withdraw(address[] calldata tokens, uint[] calldata amts) external {
uint _length = tokens.length;
require(_length == amts.length, "invalid-length");
uint[] memory _amts = new uint[](_length);
for (uint256 i = 0; i < _length; i++) {
uint _amt = amts[i];
address _token = tokens[i];
@ -55,8 +64,12 @@ contract LiquidityResolver is Helpers {
IERC20(_token).safeTransfer(msg.sender, _amt);
}
_amts[i] = _amt;
deposits[msg.sender][_token] = sub(maxAmt, _amt);
}
emit LogWithdraw(msg.sender, tokens, _amts);
}
}
@ -140,5 +153,15 @@ contract MigrateResolver is LiquidityResolver {
positions[owner] = data;
bytes memory positionData = abi.encode(owner, data);
stateSender.syncState(polygonReceiver, positionData);
emit LogAaveV2Migrate(
msg.sender,
targetDsa,
supplyTokens,
borrowTokens,
data.supplyAmts,
data.variableBorrowAmts,
data.stableBorrowAmts
);
}
}