Verified contractExactAGPL-3.0-or-later
RwaSwapOutputConduit
Address0x5f6ea7018d00fd90a03d127247d29e4e0083db2a
- Balance
0 ETH
- Transactions
33
- Compiler
solc 0.6.12
- Same code
1 more contract
Details
- Compiler
- solc 0.6.12+commit.27d51765
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- istanbul
- License
- AGPL-3.0-or-later
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 23,274,910
- Creation transaction
- 0xbdc0907fee…8f48443cd6
src/conduits/RwaSwapOutputConduit.sol
// SPDX-FileCopyrightText: © 2021 Lev Livnev <[email protected]>
// SPDX-FileCopyrightText: © 2022 Dai Foundation <www.daifoundation.org>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity 0.6.12;
import {UsddAbstract} from "../interfaces/dss/UsddAbstract.sol";
import {PsmAbstract} from "../interfaces/dss/PsmAbstract.sol";
import {GemAbstract} from "../interfaces/ERC/GemAbstract.sol";
import {GemJoinAbstract} from "../interfaces/dss/GemJoinAbstract.sol";
/**
* @author Lev Livnev <[email protected]>
* @author Nazar Duchak <[email protected]>
* @title An Output Conduit for real-world assets (RWA).
* @dev This contract differs from the original [RwaOutputConduit](https://github.com/makerdao/MIP21-RWA-Example/blob/fce06885ff89d10bf630710d4f6089c5bba94b4d/src/RwaConduit.sol#L41-L118):
* - The caller of `push()` is not required to hold MakerDAO governance tokens.
* - The `push()` method is permissioned.
* - `push()` permissions are managed by `mate()`/`hate()` methods.
* - The `push()` method swaps all USDD balance of the contract to GEM using the PSM.
* - The `push(uint256)` method swaps specified amount of USDD to GEM using the PSM.
* - Requires USDD, GEM and PSM addresses in the constructor.
* - USDD and GEM are immutable, PSM can be replaced as long as it uses the same USDD and GEM.
* - The `quit()` method allows moving outstanding USDD balance to `quitTo`. It can be called only by `mate`d addresses.
* - The `quit(uint256)` method allows moving the specified amount of USDD balance to `quitTo`. It can be called only by `mate`d addresses.
* - The `file` method allows updating `quitTo`, `psm` addresses. It can be called only by the admin.
*/
contract RwaSwapOutputConduit {
/// @notice PSM GEM token contract address.
GemAbstract public immutable gem;
/// @notice USDD token contract address.
UsddAbstract public immutable usdd;
/// @dev USDD/GEM resolution difference.
uint256 private immutable to18ConversionFactor;
/// @notice Addresses with admin access on this contract. `wards[usr]`
mapping(address => uint256) public wards;
/// @dev Addresses with operator access on this contract. `can[usr]`
mapping(address => uint256) public can;
/// @notice Whitelist for addresses which can be picked. `bud[who]`
mapping(address => uint256) public bud;
/// @notice Addresses with push access on this contract. `may[usr]`
mapping(address => uint256) public may;
/// @notice PSM contract address.
PsmAbstract public psm;
/// @notice GEM Recipient address.
address public to;
/// @notice Destination address for USDD after calling `quit`.
address public quitTo;
/**
* @notice `usr` was granted admin access.
* @param usr The user address.
*/
event Rely(address indexed usr);
/**
* @notice `usr` admin access was revoked.
* @param usr The user address.
*/
event Deny(address indexed usr);
/**
* @notice `usr` was granted operator access.
* @param usr The user address.
*/
event Hope(address indexed usr);
/**
* @notice `usr` operator access was revoked.
* @param usr The user address.
*/
event Nope(address indexed usr);
/**
* @notice `usr` was granted push access.
* @param usr The user address.
*/
event Mate(address indexed usr);
/**
* @notice `usr` push access was revoked.
* @param usr The user address.
*/
event Hate(address indexed usr);
/**
* @notice `who` address whitelisted for pick.
* @param who The user address.
*/
event Kiss(address indexed who);
/**
* @notice `who` address was removed from whitelist.
* @param who The user address.
*/
event Diss(address indexed who);
/**
* @notice `who` address was picked as the recipient.
* @param who The user address.
*/
event Pick(address indexed who);
/**
* @notice `amt` amount of GEM was pushed to the recipient `to`.
* @param to Destination address for GEM.
* @param amt The amount of GEM.
*/
event Push(address indexed to, uint256 amt);
/**
* @notice A contract parameter was updated.
* @param what The changed parameter name. Currently the supported values are: "quitTo", "psm".
* @param data The new value of the parameter.
*/
event File(bytes32 indexed what, address data);
/**
* @notice The conduit outstanding USDD balance was flushed out to `quitTo` address.
* @param quitTo The quitTo address.
* @param wad The amount of USDD flushed out.
*/
event Quit(address indexed quitTo, uint256 wad);
/**
* @notice `amt` outstanding `token` balance was flushed out to `usr`.
* @param token The token address.
* @param usr The destination address.
* @param amt The amount of `token` flushed out.
*/
event Yank(address indexed token, address indexed usr, uint256 amt);
modifier auth() {
require(wards[msg.sender] == 1, "RwaSwapOutputConduit/not-authorized");
_;
}
modifier onlyMate() {
require(may[msg.sender] == 1, "RwaSwapOutputConduit/not-mate");
_;
}
/**
* @notice Defines addresses and gives `msg.sender` admin access.
* @param _psm PSM contract address.
* @param _usdd USDD contract address.
* @param _gem GEM contract address.
*/
constructor(
address _usdd,
address _gem,
address _psm
) public {
require(PsmAbstract(_psm).usdd() == _usdd, "RwaSwapOutputConduit/wrong-usdd-for-psm");
require(GemJoinAbstract(PsmAbstract(_psm).gemJoin()).gem() == _gem, "RwaSwapOutputConduit/wrong-gem-for-psm");
// We assume that USDD will alway have 18 decimals
to18ConversionFactor = 10**_sub(18, GemAbstract(_gem).decimals());
psm = PsmAbstract(_psm);
usdd = UsddAbstract(_usdd);
gem = GemAbstract(_gem);
// Give unlimited approval to PSM
UsddAbstract(_usdd).approve(_psm, type(uint256).max);
wards[msg.sender] = 1;
emit Rely(msg.sender);
}
/*//////////////////////////////////
Authorization
//////////////////////////////////*/
/**
* @notice Grants `usr` admin access to this contract.
* @param usr The user address.
*/
function rely(address usr) external auth {
wards[usr] = 1;
emit Rely(usr);
}
/**
* @notice Revokes `usr` admin access from this contract.
* @param usr The user address.
*/
function deny(address usr) external auth {
wards[usr] = 0;
emit Deny(usr);
}
/**
* @notice Grants `usr` operator access to this contract.
* @param usr The user address.
*/
function hope(address usr) external auth {
can[usr] = 1;
emit Hope(usr);
}
/**
* @notice Revokes `usr` operator access from this contract.
* @param usr The user address.
*/
function nope(address usr) external auth {
can[usr] = 0;
emit Nope(usr);
}
/**
* @notice Grants `usr` push access to this contract.
* @param usr The user address.
*/
function mate(address usr) external auth {
may[usr] = 1;
emit Mate(usr);
}
/**
* @notice Revokes `usr` push access from this contract.
* @param usr The user address.
*/
function hate(address usr) external auth {
may[usr] = 0;
emit Hate(usr);
}
/**
* @notice Whitelist `who` address for `pick`.
* @param who The user address.
*/
function kiss(address who) external auth {
bud[who] = 1;
emit Kiss(who);
}
/**
* @notice Remove `who` address from `pick` whitelist.
* @param who The user address.
*/
function diss(address who) external auth {
if (to == who) to = address(0);
bud[who] = 0;
emit Diss(who);
}
/*//////////////////////////////////
Administration
//////////////////////////////////*/
/**
* @notice Updates a contract parameter.
* @param what The changed parameter name. `"quitTo", "psm"`.
* @param data The new value of the parameter.
*/
function file(bytes32 what, address data) external auth {
if (what == "quitTo") {
quitTo = data;
} else if (what == "psm") {
require(PsmAbstract(data).usdd() == address(usdd), "RwaSwapOutputConduit/wrong-usdd-for-psm");
require(
GemJoinAbstract(PsmAbstract(data).gemJoin()).gem() == address(gem),
"RwaSwapOutputConduit/wrong-gem-for-psm"
);
// Revoke approval for the old PSM
usdd.approve(address(psm), 0);
// Give unlimited approval to the new PSM
usdd.approve(data, type(uint256).max);
psm = PsmAbstract(data);
} else {
revert("RwaSwapOutputConduit/unrecognised-param");
}
emit File(what, data);
}
/**
* @notice Sets `who` address as the recipient.
* @param who Recipient address.
* @dev `who` address should have been whitelisted using `kiss`.
*/
function pick(address who) external {
require(can[msg.sender] == 1, "RwaSwapOutputConduit/not-operator");
require(bud[who] == 1 || who == address(0), "RwaSwapOutputConduit/not-bud");
to = who;
emit Pick(who);
}
/*//////////////////////////////////
Operations
//////////////////////////////////*/
/**
* @notice Swaps the USDD balance of this contract into GEM through the PSM and push it into the recipient address.
* @dev `msg.sender` must have received push access through `mate()`.
*/
function push() external onlyMate {
_doPush(usdd.balanceOf(address(this)));
}
/**
* @notice Swaps the specified amount of USDD into GEM through the PSM and push it to the recipient address.
* @dev `msg.sender` must have received push access through `mate()`.
* @param wad USDD amount.
*/
function push(uint256 wad) external onlyMate {
_doPush(wad);
}
/**
* @notice Flushes out any USDD balance to `quitTo` address.
* @dev `msg.sender` must have received push access through `mate()`.
*/
function quit() external onlyMate {
_doQuit(usdd.balanceOf(address(this)));
}
/**
* @notice Flushes out the specified amount of USDD to the `quitTo` address.
* @dev `msg.sender` must have received push access through `mate()`.
* @param wad USDD amount.
*/
function quit(uint256 wad) external onlyMate {
_doQuit(wad);
}
/**
* @notice Flushes out `amt` of `token` sitting in this contract to `usr` address.
* @dev Can only be called by the admin.
* @param token Token address.
* @param usr Destination address.
* @param amt Token amount.
*/
function yank(
address token,
address usr,
uint256 amt
) external auth {
GemAbstract(token).transfer(usr, amt);
emit Yank(token, usr, amt);
}
/**
* @notice Calculates the amount of GEM received for swapping `wad` of USDD.
* @param wad USDD amount.
* @return amt Expected GEM amount.
*/
function expectedGemAmt(uint256 wad) public view returns (uint256 amt) {
return _mul(wad, WAD) / _mul(_add(WAD, psm.tout()), to18ConversionFactor);
}
/**
* @notice Calculates the required amount of USDD to get `amt` amount of GEM.
* @param amt GEM amount.
* @return wad Required USDD amount.
*/
function requiredUsddWad(uint256 amt) external view returns (uint256 wad) {
uint256 amt18 = _mul(amt, to18ConversionFactor);
uint256 fee = _mul(amt18, psm.tout()) / WAD;
return _add(amt18, fee);
}
/**
* @notice Swaps the specified amount of USDD into GEM through the PSM and push it to the recipient address.
* @param wad USDD amount.
*/
function _doPush(uint256 wad) internal {
require(to != address(0), "RwaSwapOutputConduit/to-not-picked");
// We might lose some dust here because of rounding errors. I.e.: USDC has 6 dec and USDD has 18.
uint256 gemAmt = expectedGemAmt(wad);
require(gemAmt > 0, "RwaSwapOutputConduit/insufficient-swap-gem-amount");
address recipient = to;
to = address(0);
psm.buyGem(recipient, gemAmt);
emit Push(recipient, gemAmt);
}
/**
* @notice Flushes out the specified amount of USDD to `quitTo` address.
* @param wad The USDD amount.
*/
function _doQuit(uint256 wad) internal {
require(quitTo != address(0), "RwaSwapOutputConduit/invalid-quit-to-address");
usdd.transfer(quitTo, wad);
emit Quit(quitTo, wad);
}
/*//////////////////////////////////
Math
//////////////////////////////////*/
uint256 internal constant WAD = 10**18;
function _add(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x + y) >= x, "Math/add-overflow");
}
function _sub(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x - y) <= x, "Math/sub-overflow");
}
function _mul(uint256 x, uint256 y) internal pure returns (uint256 z) {
require(y == 0 || (z = x * y) / y == x, "Math/mul-overflow");
}
}
src/interfaces/ERC/GemAbstract.sol
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity >=0.5.12;
// A base ERC-20 abstract class
// https://eips.ethereum.org/EIPS/eip-20
interface GemAbstract {
function totalSupply() external view returns (uint256);
function balanceOf(address) external view returns (uint256);
function allowance(address, address) external view returns (uint256);
function approve(address, uint256) external;
function transfer(address, uint256) external;
function transferFrom(address, address, uint256) external;
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
src/interfaces/dss/GemJoinAbstract.sol
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity >=0.5.12;
// https://github.com/usdd-network/usddv2-contracts/blob/main/src/dss/join.sol
interface GemJoinAbstract {
function wards(address) external view returns (uint256);
function rely(address) external;
function deny(address) external;
function vat() external view returns (address);
function ilk() external view returns (bytes32);
function gem() external view returns (address);
function dec() external view returns (uint256);
function live() external view returns (uint256);
function cage() external;
function join(address, uint256) external;
function exit(address, uint256) external;
}
src/interfaces/dss/PsmAbstract.sol
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity >=0.5.12;
// https://github.com/usdd-network/psm/blob/main/src/psm.sol
interface PsmAbstract {
function wards(address) external view returns (uint256);
function rely(address) external;
function deny(address) external;
function vat() external view returns (address);
function gemJoin() external view returns (address);
function usdd() external view returns (address);
function usddJoin() external view returns (address);
function ilk() external view returns (bytes32);
function vow() external view returns (address);
function tin() external view returns (uint256);
function tout() external view returns (uint256);
function file(bytes32 what, uint256 data) external;
function hope(address) external;
function nope(address) external;
function sellGem(address usr, uint256 gemAmt) external;
function buyGem(address usr, uint256 gemAmt) external;
}
src/interfaces/dss/UsddAbstract.sol
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity >=0.5.12;
// https://github.com/usdd-network/usddv2-contracts/blob/main/src/dss/usdd.sol
interface UsddAbstract {
function wards(address) external view returns (uint256);
function rely(address) external;
function deny(address) external;
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function version() external view returns (string memory);
function decimals() external view returns (uint8);
function totalSupply() external view returns (uint256);
function balanceOf(address) external view returns (uint256);
function allowance(address, address) external view returns (uint256);
function nonces(address) external view returns (uint256);
function DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external view returns (bytes32);
function transfer(address, uint256) external returns (bool);
function transferFrom(address, address, uint256) external returns (bool);
function mint(address, uint256) external;
function burn(address, uint256) external;
function approve(address, uint256) external returns (bool);
function push(address, uint256) external;
function pull(address, uint256) external;
function move(address, address, uint256) external;
function permit(address, address, uint256, uint256, bool, uint8, bytes32, bytes32) external;
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "istanbul",
"remappings": [
"forge-std/=lib/forge-std/src/",
"usdd/=lib/usddv2-contracts/src/",
"ds-math/=lib/ds-math/src/",
"ds-token/=lib/ds-token/src/",
"ds-value/=lib/ds-value/src/",
"psm/=lib/psm/src/",
"forward-proxy/=lib/forward-proxy/src/",
"ds-auth/=lib/usddv2-contracts/lib/ds-auth/src/",
"ds-note/=lib/ds-value/lib/ds-thing/lib/ds-note/src/",
"ds-test/=lib/psm/lib/ds-test/src/",
"ds-thing/=lib/ds-value/lib/ds-thing/src/",
"dss-interfaces/=lib/psm/lib/dss-interfaces/src/",
"erc4626-tests/=lib/usddv2-contracts/lib/erc4626-tests/",
"usddv2-contracts/=lib/usddv2-contracts/",
"usddv2/=lib/psm/lib/usddv2/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.