This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from SetProtocol/brian/require_successful_tran…
…sfer Brian/require successful transfer
- Loading branch information
Showing
11 changed files
with
610 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
Copyright 2018 Set Labs Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
pragma solidity 0.4.24; | ||
|
||
import { IERC20 } from "../../lib/IERC20.sol"; | ||
|
||
|
||
/** | ||
* @title TokenInteract | ||
* @author Set Protocol | ||
* | ||
* This library contains functions for interacting wtih ERC20 tokens, even those not fully compliant. | ||
* For all functions we will only accept tokens that return a null or true value, any other values will | ||
* cause the operation to revert. | ||
*/ | ||
library ERC20Wrapper { | ||
|
||
// ============ Constants ============ | ||
|
||
string constant INVALID_RETURN_VALUE_TRANSFER = "Transferred token does not return null or true on successful trasnfer."; | ||
string constant INVALID_RETURN_VALUE_TRANSFERFROM = "Transferred token does not return null or true on successful transferFrom."; | ||
|
||
// ============ Internal Functions ============ | ||
|
||
function balanceOf( | ||
address _tokenAddress, | ||
address _ownerAddress | ||
) | ||
internal | ||
view | ||
returns (uint256) | ||
{ | ||
return IERC20(_tokenAddress).balanceOf(_ownerAddress); | ||
} | ||
|
||
function transfer( | ||
address _tokenAddress, | ||
address _to, | ||
uint256 _quantity | ||
) | ||
internal | ||
{ | ||
IERC20(_tokenAddress).transfer(_to, _quantity); | ||
|
||
require( | ||
checkSuccess(), | ||
INVALID_RETURN_VALUE_TRANSFER | ||
); | ||
} | ||
|
||
function transferFrom( | ||
address _tokenAddress, | ||
address _from, | ||
address _to, | ||
uint256 _quantity | ||
) | ||
internal | ||
{ | ||
IERC20(_tokenAddress).transferFrom(_from, _to, _quantity); | ||
|
||
require( | ||
checkSuccess(), | ||
INVALID_RETURN_VALUE_TRANSFERFROM | ||
); | ||
} | ||
|
||
// ============ Private Functions ============ | ||
|
||
/** | ||
* Checks the return value of the previous function up to 32 bytes. Returns true if the previous | ||
* function returned 0 bytes or 1. | ||
*/ | ||
function checkSuccess( | ||
) | ||
private | ||
pure | ||
returns (bool) | ||
{ | ||
// default to failure | ||
uint256 returnValue = 0; | ||
|
||
assembly { | ||
// check number of bytes returned from last function call | ||
switch returndatasize | ||
|
||
// no bytes returned: assume success | ||
case 0x0 { | ||
returnValue := 1 | ||
} | ||
|
||
// 32 bytes returned | ||
case 0x20 { | ||
// copy 32 bytes into scratch space | ||
returndatacopy(0x0, 0x0, 0x20) | ||
|
||
// load those bytes into returnValue | ||
returnValue := mload(0x0) | ||
} | ||
|
||
// not sure what was returned: dont mark as success | ||
default { } | ||
} | ||
|
||
// check if returned value is one or nothing | ||
return returnValue == 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright 2018 Set Labs Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
pragma solidity 0.4.24; | ||
|
||
|
||
/** | ||
* @title GeneralERC20 | ||
* @author Set Protocol | ||
* | ||
* Interface for using ERC20 Tokens. This interface is needed to interact with tokens that are not | ||
* fully ERC20 compliant and return something other than true on successful transfers. | ||
*/ | ||
interface IERC20 { | ||
function balanceOf( | ||
address _owner | ||
) | ||
external | ||
view | ||
returns (uint256); | ||
|
||
function transfer( | ||
address _to, | ||
uint256 _quantity | ||
) | ||
external; | ||
|
||
function transferFrom( | ||
address _from, | ||
address _to, | ||
uint256 _quantity | ||
) | ||
external; | ||
} |
Oops, something went wrong.