Skip to content

Instantly share code, notes, and snippets.

@jackygu2006
Created June 9, 2023 05:24
Show Gist options
  • Save jackygu2006/1a156f0460bdcade6a64871b4ca10d81 to your computer and use it in GitHub Desktop.
Save jackygu2006/1a156f0460bdcade6a64871b4ca10d81 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=
// 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;
}
contract Conversation is Ownable {
mapping(string => mapping(uint8 => string)) public names; // tick => position => name
mapping(string => mapping(uint8 => string)) public urls; // tick => position => url
mapping(string => mapping(uint8 => uint256)) public rates; // tick => position => rate
mapping(string => mapping(uint8 => address)) public sellers; // tick => position => seller
string public prefixUrl = "https://www.puppy.chat";
address public serviceAddress = 0x5F73c285D62659090f33484B9e166b0749C03044;
uint8 public maxPosition = 3;
address[] public availabeInscriptionFactoryAddresses;
constructor() {
updateAvailabeInscriptionFactoryAddresses(0x16dabfAD608EDAC8bC77ab92a89f66E44AF6EdfA);
}
function setLink(
string memory tick,
string memory name,
uint8 pos,
string memory url,
uint256 amount,
uint256 newRate
) external {
require(pos < maxPosition, "position bigger than maxPosition");
address token = this.getAddress(tick);
require(token != address(0x0), "token is not ferc20");
require(newRate <= amount, "new rate smaller than amount");
tick = String.toLower(tick);
require(amount > rates[tick][pos], "must higher than current price");
require(this.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
if(amount - rates[tick][pos] > 0) ICommonToken(token).transferFrom(msg.sender, serviceAddress, amount - rates[tick][pos]);
// Send the seller's offer amount to seller
if(rates[tick][pos] > 0) ICommonToken(token).transferFrom(msg.sender, sellers[tick][pos], rates[tick][pos]);
// update position data
urls[tick][pos] = url;
rates[tick][pos] = newRate;
sellers[tick][pos] = msg.sender;
names[tick][pos] = name;
}
function getLink(string memory tick, uint8 pos) external view returns(string memory, uint256, address ) {
return (urls[tick][pos], rates[tick][pos], sellers[tick][pos]);
}
function checkUrl(string memory url) external view returns(bool) {
if(String.strlen(url) < String.strlen(prefixUrl)) return false;
return String.compareStrings(String.substring(url, 0, String.strlen(prefixUrl)), prefixUrl);
}
function getAddress(string memory tick) external view returns(address re) {
re = address(0x0);
for(uint32 i = 0; i < availabeInscriptionFactoryAddresses.length; 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) external onlyOwner {
prefixUrl = _prefixUrl;
}
function updateServiceAddress(address addr) external onlyOwner {
serviceAddress = addr;
}
function updateMaxPosition(uint8 num) external onlyOwner {
maxPosition = num;
}
function updateAvailabeInscriptionFactoryAddresses(address addr) public onlyOwner {
require(addr != address(0x0));
availabeInscriptionFactoryAddresses.push(addr);
}
function test() external {
this.setLink(
"ferc",
unicode"我的社区",
0,
"https://www.puppy.chat/vpPgCNezcnIxvWyjYEvc",
10000000000000000000,
10000000000000000000
);
// return ICommonToken(0x092cE3205406f060489165e2E09cD84D9Cb71627).inscriptionFactory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment