Created
June 10, 2023 02:56
-
-
Save jackygu2006/cbe88c5b6038505548cff90b8a473c67 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.18+commit.87f61d96.js&optimize=false&runs=200&gist=
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.0; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "./String.sol"; | |
import "./interfaces/IInscriptionFactory.sol"; | |
// This is common token interface, get balance of owner's token by ERC20/ERC721/ERC1155. | |
interface ICommonToken { | |
function balanceOf(address owner) external returns(uint256); | |
function allowance(address owner, address spender) external returns(uint256); | |
function transferFrom(address sender, address recipient, uint256 amount) external; | |
function symbol() external returns(string memory); | |
} | |
contract Conversation is Ownable { | |
mapping(address => mapping(uint8 => string)) public names; // tick => position => name | |
mapping(address => mapping(uint8 => string)) public logos; // tick => position => logo url | |
mapping(address => mapping(uint8 => string)) public urls; // tick => position => url | |
mapping(address => mapping(uint8 => uint256)) public rates; // tick => position => rate | |
mapping(address => mapping(uint8 => address)) public sellers; // tick => position => seller | |
mapping(address => mapping(uint8 => uint256)) public lastUpdate;// tick => position => timestamp | |
string[] public prefixUrls = ["https://www.puppy.chat"]; | |
address public serviceAddress = 0x5F73c285D62659090f33484B9e166b0749C03044; | |
uint8 public maxPosition = 3; | |
uint8 public versions = 2; | |
uint256 public commission = 250; | |
uint256 public frozenTime = 12 * 3600; | |
mapping(uint8 => address) public availabeInscriptionFactoryAddresses; | |
constructor() { | |
updateAvailabeInscriptionFactoryAddresses(0x16dabfAD608EDAC8bC77ab92a89f66E44AF6EdfA, 0); | |
} | |
function setLink( | |
address token, | |
uint8 pos, | |
string memory name, | |
string memory logo, | |
string memory url, | |
uint256 amount, | |
uint256 newRate | |
) external { | |
string memory tick = ICommonToken(token).symbol(); | |
require(lastUpdate[token][pos] + frozenTime < block.timestamp, "in frozen time"); | |
require(pos < maxPosition, "position bigger than maxPosition"); | |
require(token != address(0x0), "token is not ferc20"); | |
require(newRate <= amount, "new rate smaller than amount"); | |
tick = String.toLower(tick); | |
require(amount > rates[token][pos], "must higher than current price"); | |
require(checkUrl(url), "url is illegal"); | |
require(ICommonToken(token).balanceOf(msg.sender) >= amount, "balance not enough"); | |
require(ICommonToken(token).allowance(msg.sender, address(this)) >= amount, "allowance not enough"); | |
// Send the balance of seller's offer and amount to service address | |
uint256 _commission = rates[token][pos] * commission / 10000; | |
if(amount - rates[token][pos] > 0) ICommonToken(token).transferFrom(msg.sender, serviceAddress, amount - rates[token][pos] + _commission); | |
// Send the seller's offer amount to seller | |
if(rates[token][pos] > 0) ICommonToken(token).transferFrom(msg.sender, sellers[token][pos], rates[token][pos] - _commission); | |
// update position data | |
names[token][pos] = name; | |
logos[token][pos] = logo; | |
urls[token][pos] = url; | |
rates[token][pos] = newRate; | |
sellers[token][pos] = msg.sender; | |
lastUpdate[token][pos] = block.timestamp; | |
} | |
function getLink(address token) public view returns( | |
string[] memory names_, | |
string[] memory logo_, | |
string[] memory urls_, | |
uint256[] memory rates_, | |
address[] memory sellers_, | |
uint256[] memory lastUpdate_ | |
) { | |
names_ = new string[](maxPosition); | |
logo_ = new string[](maxPosition); | |
urls_ = new string[](maxPosition); | |
rates_ = new uint256[](maxPosition); | |
sellers_ = new address[](maxPosition); | |
lastUpdate_ = new uint256[](maxPosition); | |
for(uint8 i = 0; i < maxPosition; i++) { | |
names_[i] = names[token][i]; | |
logo_[i] = logos[token][i]; | |
urls_[i] = urls[token][i]; | |
rates_[i] = rates[token][i]; | |
sellers_[i] = sellers[token][i]; | |
lastUpdate_[i] = lastUpdate[token][i]; | |
} | |
} | |
function checkUrl(string memory url) public view returns(bool re) { | |
re = false; | |
for(uint256 i = 0; i < prefixUrls.length; i++) { | |
if(String.strlen(url) < String.strlen(prefixUrls[i])) continue; | |
re = String.compareStrings(String.substring(url, 0, String.strlen(prefixUrls[i])), prefixUrls[i]); | |
} | |
} | |
function getAddress(string memory tick) public view returns(address re) { | |
re = address(0x0); | |
for(uint8 i = 0; i < versions; i++) { | |
if(IInscriptionFactory(availabeInscriptionFactoryAddresses[i]).getIncriptionIdByTick(tick) > 0) { | |
(IInscriptionFactory.Token memory token, ) = IInscriptionFactory(availabeInscriptionFactoryAddresses[i]).getIncriptionByTick(tick); | |
re = token.addr; | |
break; | |
} | |
} | |
} | |
function updatePrefixUrl(string memory _prefixUrl) public onlyOwner { | |
prefixUrls.push(_prefixUrl); | |
} | |
function updateServiceAddress(address addr) public onlyOwner { | |
serviceAddress = addr; | |
} | |
function updateMaxPosition(uint8 num) public onlyOwner { | |
maxPosition = num; | |
} | |
function updateAvailabeInscriptionFactoryAddresses(address addr, uint8 version) public onlyOwner { | |
require(addr != address(0x0)); | |
availabeInscriptionFactoryAddresses[version] = addr; | |
} | |
function updateFrozenTime(uint256 _seconds) public onlyOwner { | |
frozenTime = _seconds; | |
} | |
function updateFactoryVersions(uint8 _versions) public onlyOwner { | |
versions = _versions; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment