|
| 1 | +/* |
| 2 | + Copyright 2018 Set Labs Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +pragma solidity 0.4.24; |
| 18 | + |
| 19 | +import { IERC20 } from "../../lib/IERC20.sol"; |
| 20 | + |
| 21 | + |
| 22 | +/** |
| 23 | + * @title TokenInteract |
| 24 | + * @author Set Protocol |
| 25 | + * |
| 26 | + * This library contains functions for interacting wtih ERC20 tokens, even those not fully compliant. |
| 27 | + * For all functions we will only accept tokens that return a null or true value, any other values will |
| 28 | + * cause the operation to revert. |
| 29 | + */ |
| 30 | +library ERC20Wrapper { |
| 31 | + |
| 32 | + // ============ Constants ============ |
| 33 | + |
| 34 | + string constant INVALID_RETURN_VALUE_TRANSFER = "Transferred token does not return null or true on successful trasnfer."; |
| 35 | + string constant INVALID_RETURN_VALUE_TRANSFERFROM = "Transferred token does not return null or true on successful transferFrom."; |
| 36 | + |
| 37 | + // ============ Internal Functions ============ |
| 38 | + |
| 39 | + function balanceOf( |
| 40 | + address _tokenAddress, |
| 41 | + address _ownerAddress |
| 42 | + ) |
| 43 | + internal |
| 44 | + view |
| 45 | + returns (uint256) |
| 46 | + { |
| 47 | + return IERC20(_tokenAddress).balanceOf(_ownerAddress); |
| 48 | + } |
| 49 | + |
| 50 | + function transfer( |
| 51 | + address _tokenAddress, |
| 52 | + address _to, |
| 53 | + uint256 _quantity |
| 54 | + ) |
| 55 | + internal |
| 56 | + { |
| 57 | + IERC20(_tokenAddress).transfer(_to, _quantity); |
| 58 | + |
| 59 | + require( |
| 60 | + checkSuccess(), |
| 61 | + INVALID_RETURN_VALUE_TRANSFER |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + function transferFrom( |
| 66 | + address _tokenAddress, |
| 67 | + address _from, |
| 68 | + address _to, |
| 69 | + uint256 _quantity |
| 70 | + ) |
| 71 | + internal |
| 72 | + { |
| 73 | + IERC20(_tokenAddress).transferFrom(_from, _to, _quantity); |
| 74 | + |
| 75 | + require( |
| 76 | + checkSuccess(), |
| 77 | + INVALID_RETURN_VALUE_TRANSFERFROM |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + // ============ Private Functions ============ |
| 82 | + |
| 83 | + /** |
| 84 | + * Checks the return value of the previous function up to 32 bytes. Returns true if the previous |
| 85 | + * function returned 0 bytes or 1. |
| 86 | + */ |
| 87 | + function checkSuccess( |
| 88 | + ) |
| 89 | + private |
| 90 | + pure |
| 91 | + returns (bool) |
| 92 | + { |
| 93 | + // default to failure |
| 94 | + uint256 returnValue = 0; |
| 95 | + |
| 96 | + assembly { |
| 97 | + // check number of bytes returned from last function call |
| 98 | + switch returndatasize |
| 99 | + |
| 100 | + // no bytes returned: assume success |
| 101 | + case 0x0 { |
| 102 | + returnValue := 1 |
| 103 | + } |
| 104 | + |
| 105 | + // 32 bytes returned |
| 106 | + case 0x20 { |
| 107 | + // copy 32 bytes into scratch space |
| 108 | + returndatacopy(0x0, 0x0, 0x20) |
| 109 | + |
| 110 | + // load those bytes into returnValue |
| 111 | + returnValue := mload(0x0) |
| 112 | + } |
| 113 | + |
| 114 | + // not sure what was returned: dont mark as success |
| 115 | + default { } |
| 116 | + } |
| 117 | + |
| 118 | + // check if returned value is one or nothing |
| 119 | + return returnValue == 1; |
| 120 | + } |
| 121 | +} |
0 commit comments