Skip to content

Instantly share code, notes, and snippets.

@z0r0z
Last active March 22, 2023 14:56
Show Gist options
  • Save z0r0z/3a8a569bc211b48114d39017d0ecf4ea to your computer and use it in GitHub Desktop.
Save z0r0z/3a8a569bc211b48114d39017d0ecf4ea to your computer and use it in GitHub Desktop.
// 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