Added safeErc20 lib

This commit is contained in:
Thrilok Kumar 2020-05-18 22:52:14 +05:30
parent 2bb791491a
commit 7da98bfb6d
2 changed files with 21 additions and 85 deletions

View File

@ -1,85 +1,25 @@
pragma solidity ^0.6.0;
import "../../node_modules/@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
// import files from common directory
import { TokenInterface , MemoryInterface, EventInterface} from "../common/interfaces.sol";
import { Stores } from "../common/stores.sol";
import { DSMath } from "../common/math.sol";
interface AccountInterface {
function isAuth(address _user) external view returns (bool);
}
/**
* @title ConnectBasic.
* @dev Connector to deposit/withdraw assets.
*/
interface ERC20Interface {
function allowance(address, address) external view returns (uint);
function balanceOf(address) external view returns (uint);
function approve(address, uint) external;
function transfer(address, uint) external returns (bool);
function transferFrom(address, address, uint) external returns (bool);
}
interface AccountInterface {
function isAuth(address _user) external view returns (bool);
}
interface MemoryInterface {
function getUint(uint _id) external returns (uint _num);
function setUint(uint _id, uint _val) external;
}
interface EventInterface {
function emitEvent(uint _connectorType, uint _connectorID, bytes32 _eventCode, bytes calldata _eventData) external;
}
contract Memory {
/**
* @dev Return InstaMemory Address.
*/
function getMemoryAddr() public pure returns (address) {
return 0x8a5419CfC711B2343c17a6ABf4B2bAFaBb06957F; // InstaMemory Address
}
/**
* @dev Return InstaEvent Address.
*/
function getEventAddr() public pure returns (address) {
return 0x2af7ea6Cb911035f3eb1ED895Cb6692C39ecbA97; // InstaEvent Address
}
/**
* @dev Get Stored Uint Value From InstaMemory.
* @param getId Storage ID.
* @param val if any value.
*/
function getUint(uint getId, uint val) internal returns (uint returnVal) {
returnVal = getId == 0 ? val : MemoryInterface(getMemoryAddr()).getUint(getId);
}
/**
* @dev Store Uint Value In InstaMemory.
* @param setId Storage ID.
* @param val Value To store.
*/
function setUint(uint setId, uint val) internal {
if (setId != 0) MemoryInterface(getMemoryAddr()).setUint(setId, val);
}
/**
* @dev Connector ID and Type.
*/
function connectorID() public pure returns(uint _type, uint _id) {
(_type, _id) = (1, 2);
}
}
contract BasicResolver is Memory {
contract BasicResolver is Stores {
event LogDeposit(address indexed erc20, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogWithdraw(address indexed erc20, uint256 tokenAmt, address indexed to, uint256 getId, uint256 setId);
/**
* @dev ETH Address.
*/
function getEthAddr() public pure returns (address) {
return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
}
using SafeERC20 for IERC20;
/**
* @dev Deposit Assets To Smart Account.
@ -91,9 +31,9 @@ contract BasicResolver is Memory {
function deposit(address erc20, uint tokenAmt, uint getId, uint setId) public payable {
uint amt = getUint(getId, tokenAmt);
if (erc20 != getEthAddr()) {
ERC20Interface token = ERC20Interface(erc20);
IERC20 token = IERC20(erc20);
amt = amt == uint(-1) ? token.balanceOf(msg.sender) : amt;
token.transferFrom(msg.sender, address(this), amt);
token.safeTransferFrom(msg.sender, address(this), amt);
} else {
require(msg.value == amt || amt == uint(-1), "invalid-ether-amount");
amt = msg.value;
@ -104,8 +44,7 @@ contract BasicResolver is Memory {
bytes32 _eventCode = keccak256("LogDeposit(address,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(erc20, amt, getId, setId);
(uint _type, uint _id) = connectorID();
EventInterface(getEventAddr()).emitEvent(_type, _id, _eventCode, _eventParam);
emitEvent(_eventCode, _eventParam);
}
/**
@ -129,9 +68,9 @@ contract BasicResolver is Memory {
amt = amt == uint(-1) ? address(this).balance : amt;
to.transfer(amt);
} else {
ERC20Interface token = ERC20Interface(erc20);
IERC20 token = IERC20(erc20);
amt = amt == uint(-1) ? token.balanceOf(address(this)) : amt;
token.transfer(to, amt);
token.safeTransfer(to, amt);
}
setUint(setId, amt);
@ -139,13 +78,11 @@ contract BasicResolver is Memory {
bytes32 _eventCode = keccak256("LogWithdraw(address,uint256,address,uint256,uint256)");
bytes memory _eventParam = abi.encode(erc20, amt, to, getId, setId);
(uint _type, uint _id) = connectorID();
EventInterface(getEventAddr()).emitEvent(_type, _id, _eventCode, _eventParam);
emitEvent(_eventCode, _eventParam);
}
}
contract ConnectBasic is BasicResolver {
string public constant name = "Basic-v1";
string public constant name = "Basic-v1.1";
}

View File

@ -21,13 +21,13 @@
},
"homepage": "https://github.com/InstaDApp/dsa-connectors#readme",
"dependencies": {
"@openzeppelin/contracts": "^3.0.1",
"@openzeppelin/upgrades": "^2.8.0",
"@truffle/artifactor": "^4.0.45",
"chai": "^4.2.0",
"chalk": "^4.0.0",
"dotenv": "^7.0.0",
"ethereumjs-abi": "^0.6.8",
"minimist": "^1.2.5",
"openzeppelin-test-helpers": "^0.5.1",
"solc": "^0.6.0",
"truffle-assertions": "^0.9.2",
"truffle-hdwallet-provider": "^1.0.17",
@ -35,7 +35,6 @@
"truffle-verify": "^1.0.8"
},
"devDependencies": {
"chai-bn": "^0.2.1",
"sol-merger": "^2.0.1",
"solidity-coverage": "0.5.11",
"solium": "1.2.3"