dsa-connectors/contracts/mainnet/connectors/basic/main.sol

82 lines
3.1 KiB
Solidity
Raw Normal View History

2021-02-08 15:18:32 +00:00
pragma solidity ^0.7.0;
/**
* @title Basic.
* @dev Deposit & Withdraw from DSA.
*/
2021-02-08 15:18:32 +00:00
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { DSMath } from "../../common/math.sol";
import { Basic } from "../../common/basic.sol";
import { Events } from "./events.sol";
abstract contract BasicResolver is Events, DSMath, Basic {
using SafeERC20 for IERC20;
/**
* @dev Deposit Assets To Smart Account.
2021-08-25 19:36:59 +00:00
* @notice Deposit a token to DSA.
2021-08-25 19:55:29 +00:00
* @param token The address of the token to deposit.<br>(For <b>ETH</b>: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE and need to pass `value` parameter equal to `amt` in cast function ```dsa.cast({..., value: amt})```.<br>For <b>ERC20</b>: Need to give allowance prior casting spells.)
2021-03-24 12:23:36 +00:00
* @param amt The amount of tokens to deposit. (For max: `uint256(-1)` (Not valid for ETH))
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
2021-02-08 15:18:32 +00:00
*/
function deposit(
2021-03-20 20:28:28 +00:00
address token,
uint256 amt,
uint256 getId,
uint256 setId
2021-02-08 15:18:32 +00:00
) public payable returns (string memory _eventName, bytes memory _eventParam) {
2021-03-20 20:28:28 +00:00
uint _amt = getUint(getId, amt);
if (token != ethAddr) {
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(msg.sender) : _amt;
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
2021-02-08 15:18:32 +00:00
} else {
2021-03-20 20:28:28 +00:00
require(msg.value == _amt || _amt == uint(-1), "invalid-ether-amount");
_amt = msg.value;
2021-02-08 15:18:32 +00:00
}
2021-03-20 20:28:28 +00:00
setUint(setId, _amt);
2021-02-08 15:18:32 +00:00
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
2021-03-20 20:28:28 +00:00
_eventParam = abi.encode(token, _amt, getId, setId);
2021-02-08 15:18:32 +00:00
}
/**
2021-03-24 12:23:36 +00:00
* @dev Withdraw Assets from Smart Account
* @notice Withdraw a token from DSA
* @param token The address of the token to withdraw. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of tokens to withdraw. (For max: `uint256(-1)`)
* @param to The address to receive the token upon withdrawal
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
2021-02-08 15:18:32 +00:00
*/
function withdraw(
2021-03-20 20:28:28 +00:00
address token,
uint amt,
2021-02-08 15:18:32 +00:00
address payable to,
uint getId,
uint setId
) public payable returns (string memory _eventName, bytes memory _eventParam) {
2021-03-20 20:28:28 +00:00
uint _amt = getUint(getId, amt);
if (token == ethAddr) {
_amt = _amt == uint(-1) ? address(this).balance : _amt;
to.call{value: _amt}("");
2021-02-08 15:18:32 +00:00
} else {
2021-03-20 20:28:28 +00:00
IERC20 tokenContract = IERC20(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
tokenContract.safeTransfer(to, _amt);
2021-02-08 15:18:32 +00:00
}
2021-03-20 20:28:28 +00:00
setUint(setId, _amt);
2021-02-08 15:18:32 +00:00
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
2021-03-20 20:28:28 +00:00
_eventParam = abi.encode(token, _amt, to, getId, setId);
2021-02-08 15:18:32 +00:00
}
}
2021-03-15 12:26:22 +00:00
contract ConnectV2Basic is BasicResolver {
2021-03-20 20:28:28 +00:00
string constant public name = "Basic-v1";
2021-02-08 15:18:32 +00:00
}