Skip to content

Commit

Permalink
Merge pull request openocean-finance#1 from openocean-finance/main
Browse files Browse the repository at this point in the history
updata example
  • Loading branch information
openocean-admin authored Sep 27, 2022
2 parents 5e83cc1 + 30b6db4 commit 68b3510
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
82 changes: 82 additions & 0 deletions OpenOcean-Contract Call-Example/APIproxy.sol
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);
}
}

}
3 changes: 3 additions & 0 deletions OpenOcean-Contract Call-Example/README.md
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)

0 comments on commit 68b3510

Please sign in to comment.