Skip to content

Instantly share code, notes, and snippets.

@helderjnpinto
Created May 11, 2022 15:13
Show Gist options
  • Save helderjnpinto/4e192b7199b471cca69b49744d555fbe to your computer and use it in GitHub Desktop.
Save helderjnpinto/4e192b7199b471cca69b49744d555fbe to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";
abstract contract AUSD {
/**
* @dev Returns the cost of withdraw
* @param sig is the withdraw method signature
* @param amount Amount to withdraw
* @return Returns cost.
*/
function estimateCosts(bytes4 sig, uint256 amount) external virtual view returns (uint256);
event Deposit(
address indexed to,
uint256 amount,
bytes32 indexed ref,
bytes32 indexed custodial
);
event Withdraw(
address indexed from,
uint256 amount,
bytes32 indexed ref,
bytes32 indexed feeType
);
event WithdrawUSDC(
address indexed from,
uint256 amount,
string targetAccount,
int256 chainId,
bytes32 indexed feeType
);
function getGasManager() external virtual pure returns (address);
function version() external virtual pure returns (uint256);
function balanceOf(address account)
external
virtual
view
returns (uint256 balance_);
function balanceOfAndSymbol(address account)
external
virtual
view
returns (uint256 balance_, string memory symbol_);
function withdrawWireUS(uint256 amount, bytes32 ref)
external
virtual
returns (bool);
function withdrawWireInt(uint256 amount, bytes32 ref)
external
virtual
returns (bool);
function withdrawAchUS(uint256 amount, bytes32 ref) external virtual returns (bool);
/**
* Withdraw method of type USDC
* @dev Destroys `amount` tokens from `account` as msg.sender, reducing the
* total supply for USD and USDC in custodial contract.
* @param amount How much to withdraw from sender balance.
* @param targetAccount Target chain address
* @param chainId USDC chain id for target account address
* Emits a `WithdrawUSDC` event with the `from`, `amount`, `targetAccount`, `chainId`, `feeType`.
* Emits a `Transfer` event with `to` set to the zero address.
*
* Requirements
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
* - `chainId` must be a valid chain id for usdc
* - `targeAccount` will be validated by the oracle
* @return bool
*/
function withdrawToUSDC(
uint256 amount,
string calldata targetAccount,
int256 chainId
) external virtual returns (bool);
function checkPermissionsWithdrawalToUSDC(address account)
external
virtual
view
returns (bool);
function getUSDCmaxWithdrawAllowed() external virtual view returns (uint256);
function getUSDCmaxPoolPercentage() external virtual view returns (uint256);
}
contract ContractWallet {
using SafeMath for uint256;
AUSD constant public USD = AUSD(0x0000000000000000000000000000000000002070);
constructor() {
}
bytes4 constant public SignatureWithdrawalWireInt = 0xb0288ee4;
bytes4 constant public SignatureWithdrawalAchUS = 0x0fddef0f;
bytes4 constant public SignatureWithdrawalWireUS = 0xda2eaba0;
bytes4 constant public SignatureWithdrawalUSDC = 0x5856ebd1;
// TODO: missing passing the type of withdrawal
function _requiredFeesForValue(bytes4 sig, uint256 amountToWithdraw) internal virtual view returns(uint256 requiredAdditionalFees) {
return USD.estimateCosts(sig, amountToWithdraw);
}
function calcTotalUSDC(uint256 amountToWithdraw) public view returns(uint256 total) {
total = _requiredFeesForValue(SignatureWithdrawalUSDC, amountToWithdraw).add(amountToWithdraw);
}
/*
* withdrawToUSDC used to Sender send USD as total value to withdrawal
* @param amount amount to withdrawal without the fee for USDC
* @param targetAccount target chain address
* @param chainId USDC chain id for target account address (1 ETHEREUM)
*/
function withdrawToUSDC(
uint256 amount,
string calldata targetAccount,
int256 chainId
) external payable returns (bool) {
uint256 value = msg.value;
require(value == calcTotalUSDC(amount), "CONTRACT_WALLET: required to pay maybe missing the fee");
return USD.withdrawToUSDC(amount, targetAccount, chainId);
}
function calcTotalWireUS(uint256 amountToWithdraw) public view returns(uint256 total) {
total = _requiredFeesForValue(SignatureWithdrawalWireInt, amountToWithdraw).add(amountToWithdraw);
}
function withdrawWireUS(
uint256 amount
) external payable returns (bool) {
uint256 value = msg.value;
require(value == calcTotalWireUS(amount), "CONTRACT_WALLET: required to pay maybe missing the fee");
return USD.withdrawWireUS(amount, keccak256(abi.encodePacked("REFERENCE FOR PUBLIC MINT API EXAMPLE")));
}
function destruct() public {
selfdestruct(payable(msg.sender));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment