Last active
March 22, 2023 14:56
-
-
Save z0r0z/3a8a569bc211b48114d39017d0ecf4ea to your computer and use it in GitHub Desktop.
We do a lil OTC, inspired by https://twitter.com/0xngmi/status/1638537074318909442?s=20
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.4; | |
import "https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC721.sol"; | |
import "https://github.com/transmissions11/solmate/blob/main/src/utils/SafeTransferLib.sol"; | |
struct OTC { | |
address to; | |
ERC20 tkn0; | |
uint256 tkn0amt; | |
ERC20 tkn1; | |
uint256 tkn1amt; | |
uint256 deadline; | |
} | |
contract LilOTC is ERC721("OTC", "OTC") { | |
using SafeTransferLib for ERC20; | |
event Created(OTC otc); | |
event Settled(uint256 indexed id); | |
uint256 public count; | |
mapping(uint256 => OTC) public otcs; | |
// CREATE | |
function create(OTC calldata otc) public payable virtual returns (uint256 id) { | |
unchecked { | |
otcs[id = ++count] = otc; | |
} | |
_mint(msg.sender, id); | |
otc.tkn0.safeTransferFrom(msg.sender, address(this), otc.tkn0amt); | |
emit Created(otc); | |
} | |
// SETTLE | |
function settle(uint256 id) public payable virtual { | |
OTC storage otc = otcs[id]; | |
if (block.timestamp < otc.deadline) { | |
otc.tkn1.safeTransferFrom(ownerOf(id), otc.to, otc.tkn1amt); | |
} else { | |
otc.tkn0.safeTransfer(otc.to, otc.tkn0amt); | |
} | |
delete otcs[id]; | |
emit Settled(id); | |
} | |
// METADATA | |
function tokenURI(uint256) public view override virtual returns (string memory) { | |
return ""; // placeholder | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment