Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

register contracts to csr, low level call for deposit for asdUSDC #6

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions contracts/asd/asdRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {ICrocSwapDex, ICrocImpact} from "../ambient/CrocInterfaces.sol";
import {ASDUSDC} from "./asdUSDC.sol";
import {CErc20Interface} from "../clm/CTokenInterfaces.sol";
import {WCANTO} from "../clm/WCANTO.sol";
import {Turnstile} from "../clm/Turnstile.sol";

/**
* @title ASDRouter
Expand Down Expand Up @@ -49,9 +50,14 @@ contract ASDRouter is IOAppComposer {
crocImpactAddress = _crocImpactAddress;
asdUSDC = _asdUSDCAddress;
ASDUSDC(_asdUSDCAddress).approve(crocSwapAddress, type(uint).max);
if (block.chainid == 7700 || block.chainid == 7701) {
// Register CSR on Canto main- and testnet
Turnstile turnstile = Turnstile(0xEcf044C5B4b867CFda001101c617eCd347095B44);
turnstile.register(msg.sender);
}
}

function _getNoteAddress() internal returns (address) {
function _getNoteAddress() internal view returns (address) {
return CErc20Interface(cNoteAddress).underlying();
}

Expand Down Expand Up @@ -89,8 +95,12 @@ contract ASDRouter is IOAppComposer {
}

/* deposit oft to receive asdUSDC for swapping */
IERC20(_from).approve(asdUSDC, amountLD);
uint amountUSDC = ASDUSDC(asdUSDC).deposit(_from, amountLD);
(bool successOFTDeposit, uint amountUSDC) = _depositOFTForASDUSDC(_from, amountLD);
if (!successOFTDeposit) {
// return tokens to the refund address on canto
_refundToken(_guid, _from, payload._cantoRefundAddress, amountLD, msg.value, "unsuccessful deposit");
return;
}

/* swap tokens for $NOTE (check minAmount for slippage) */
(uint amountNote, bool successfulSwap) = _swapOFTForNote(asdUSDC, amountUSDC, payload._minAmountASD);
Expand All @@ -103,12 +113,12 @@ contract ASDRouter is IOAppComposer {
}

/* deposit $NOTE to the correct asd vault to receive ASD tokens */
(bool successfulDeposit, string memory reason) = _depositNoteToASDVault(payload._cantoAsdAddress, amountNote);
(bool successfulDeposit, ) = _depositNoteToASDVault(payload._cantoAsdAddress, amountNote);

// check if deposit was successful
if (!successfulDeposit) {
// return $NOTE to the refund address on canto since OFT was swapped already
_refundToken(_guid, _getNoteAddress(), payload._cantoRefundAddress, amountNote, msg.value, reason);
_refundToken(_guid, _getNoteAddress(), payload._cantoRefundAddress, amountNote, msg.value, "unsuccessful deposit to ASD vault");
return;
}

Expand Down Expand Up @@ -144,6 +154,10 @@ contract ASDRouter is IOAppComposer {
// just transfer the ASD tokens to the destination receiver
emit ASDSent(_guid, _payload._dstReceiver, _payload._cantoAsdAddress, _amount, _payload._dstLzEid, false);
ASDOFT(_payload._cantoAsdAddress).transfer(_payload._dstReceiver, _amount);
// msg.value might have been sent, refund this since it is not needed
if (msg.value > 0) {
_refundToken(_guid, address(0), _payload._cantoRefundAddress, 0, msg.value, "no msg.value needed: canto is destination chain");
}
} else {
// use Layer Zero oapp to send ASD tokens to the destination receiver on the destination chain

Expand All @@ -168,6 +182,12 @@ contract ASDRouter is IOAppComposer {
_refundToken(_guid, _payload._cantoAsdAddress, _payload._cantoRefundAddress, _amount, msg.value, string(data));
return;
}
// check to make sure all msg.value was used, refund if not
uint leftOverNativeFee = msg.value - _payload._feeForSend;
if (leftOverNativeFee > 0) {
// refund difference
_refundToken(_guid, address(0), _payload._cantoRefundAddress, 0, leftOverNativeFee, "too much msg.value sent for lz fee");
}
emit ASDSent(_guid, _payload._dstReceiver, _payload._cantoAsdAddress, _amount, _payload._dstLzEid, true);
}
}
Expand All @@ -184,7 +204,10 @@ contract ASDRouter is IOAppComposer {
// emit event
emit TokenRefund(_guid, _tokenAddress, _refundAddress, _amount, _nativeAmount, _reason);
// transfer tokens to refund address
IERC20(_tokenAddress).transfer(_refundAddress, _amount);
// check if zero address (might just be refunding native tokens)
if (_tokenAddress != address(0)) {
IERC20(_tokenAddress).transfer(_refundAddress, _amount);
}
// transfer native tokens to refund address and check that this value is less than or equal to msg.value
if (_nativeAmount > 0 && _nativeAmount <= msg.value) {
(bool sent, ) = payable(_refundAddress).call{value: _nativeAmount}("");
Expand All @@ -196,6 +219,15 @@ contract ASDRouter is IOAppComposer {
}
}

function _depositOFTForASDUSDC(address _oftVersion, uint _amount) internal returns (bool, uint) {
IERC20(_oftVersion).approve(asdUSDC, _amount);
(bool success, bytes memory amountBytes) = asdUSDC.call(abi.encodeWithSelector(ASDUSDC.deposit.selector, _oftVersion, _amount));
if (!success) {
return (false, 0);
}
return (true, uint(bytes32(amountBytes)));
}

/**
* @notice deposits $NOTE to the correct asd vault to receive ASD tokens
* @param _asdVault The address of the ASD vault to deposit to
Expand Down
9 changes: 8 additions & 1 deletion contracts/asd/asdUSDC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.22;
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {Turnstile} from "../clm/Turnstile.sol";

/**
* @title ASDUSDC
Expand All @@ -15,7 +16,13 @@ contract ASDUSDC is ERC20, Ownable {
event Deposit(address _version, uint _amount);
event Withdrawal(address _version, uint _amount);

constructor() ERC20("ASD USDC", "asdUSDC") {}
constructor() ERC20("ASD USDC", "asdUSDC") {
if (block.chainid == 7700 || block.chainid == 7701) {
// Register CSR on Canto main- and testnet
Turnstile turnstile = Turnstile(0xEcf044C5B4b867CFda001101c617eCd347095B44);
turnstile.register(msg.sender);
}
}

/**
* @notice Add a USDC version to the whitelist
Expand Down