forked from openocean-finance/OpenOceanExchangeV2
-
Notifications
You must be signed in to change notification settings - Fork 0
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 openocean-finance#1 from openocean-finance/main
updata example
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.6.9; | ||
pragma experimental ABIEncoderV2; | ||
|
||
// need | ||
|
||
contract APIproxy { | ||
using SafeERC20 for IERC20; | ||
|
||
address constant _ETH_ADDRESS_ = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; | ||
|
||
|
||
receive() external payable {} | ||
|
||
//Compatible with ETH=>ERC20, ERC20=>ETH | ||
function useAPIData( | ||
address fromToken, //fromTokenaddress | ||
address toToken, //toTokenaddress | ||
uint256 fromAmount, //inamout with dicimals | ||
address OOApprove, // 0x6352a56caadc4f1e25cd6c75970fa768a3304e64 | ||
address OOProxy, // 0x6352a56caadc4f1e25cd6c75970fa768a3304e64 | ||
bytes memory OOApiData // data param from swap_quote API | ||
) | ||
external | ||
payable | ||
{ | ||
if (fromToken != _ETH_ADDRESS_) { | ||
IERC20(fromToken).transferFrom(msg.sender, address(this), fromAmount); | ||
_approveMax(fromToken, OOApprove, fromAmount); | ||
} else { | ||
require(fromAmount == msg.value); | ||
} | ||
|
||
(bool success, bytes memory result ) = OOProxy.call{value: fromToken == _ETH_ADDRESS_ ? fromAmount : 0}(OOApiData); | ||
require(success, "API_SWAP_FAILED"); | ||
|
||
uint256 returnAmount = _balanceOf(toToken, address(this)); | ||
|
||
_transfer(toToken, msg.sender, returnAmount); | ||
} | ||
|
||
|
||
function _approveMax( | ||
address token, | ||
address to, | ||
uint256 amount | ||
) internal { | ||
uint256 allowance = IERC20(token).allowance(address(this), to); | ||
if (allowance < amount) { | ||
if (allowance > 0) { | ||
IERC20(token).safeApprove(to, 0); | ||
} | ||
IERC20(token).safeApprove(to, uint256(-1)); | ||
} | ||
} | ||
|
||
function _transfer( | ||
address token, | ||
address payable to, | ||
uint256 amount | ||
) internal { | ||
if (amount > 0) { | ||
if (token == _ETH_ADDRESS_) { | ||
to.transfer(amount); | ||
} else { | ||
IERC20(token).safeTransfer(to, amount); | ||
} | ||
} | ||
} | ||
|
||
function _balanceOf( | ||
address token, | ||
address who | ||
) internal view returns (uint256) { | ||
if (token == _ETH_ADDRESS_ ) { | ||
return who.balance; | ||
} else { | ||
return IERC20(token).balanceOf(who); | ||
} | ||
} | ||
|
||
} |
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,3 @@ | ||
In case there are certain users request in our community asking about how to use our API directly in their contract. So we provide code example for developers to quick intergrate our API and contract. | ||
|
||
Here is the file for contran intergration [APIProxy](./APIproxy.sol) |