Description:
Proxy contract enabling upgradeable smart contract patterns. Delegates calls to an implementation contract.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
{{
"language": "Solidity",
"settings": {
"viaIR": true,
"evmVersion": "cancun",
"optimizer": {
"enabled": true,
"runs": 100
},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
},
"remappings": [
"ds-test/=lib/forge-std/lib/ds-test/src/",
"forge-std/=lib/forge-std/src/",
"@openzeppelin-upgradeable/contracts/=lib/openzeppelin-contracts-upgradeable/contracts/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"forge-gas-snapshot/=lib/forge-gas-snapshot/",
"forge-std-1.10.0/=dependencies/forge-std-1.10.0/",
"forge-std/=lib/forge-std/",
"permit2/=lib/permit2/",
"solmate/=lib/solmate/",
"@openzeppelin-upgradeable/contracts/=lib/openzeppelin-contracts-upgradeable/contracts/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"forge-gas-snapshot/=lib/forge-gas-snapshot/",
"forge-std-1.10.0/=dependencies/forge-std-1.10.0/",
"forge-std/=lib/forge-std/",
"permit2/=lib/permit2/",
"solmate/=lib/solmate/"
]
},
"sources": {
"src/TTSwap_Token_Proxy.sol": {
"content": "// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.29;
import {TTSwapError} from "./libraries/L_Error.sol";
import {L_UserConfigLibrary} from "./libraries/L_UserConfig.sol";
import {toTTSwapUINT256} from "./libraries/L_TTSwapUINT256.sol";
/**
* @title TTSwap_Market
* @dev Core market contract for TTSwap protocol that manages goods trading, investing, and staking operations
* @notice This contract implements a decentralized market system with the following key features:
* - Meta good, value goods, and normal goods management
* - Automated market making (AMM) with configurable fees
* - Investment and disinvestment mechanisms
* - Flash loan functionality
* - Commission distribution system
* - ETH or WETH staking integration
*/
contract TTSwap_Token_Proxy {
using L_UserConfigLibrary for uint256;
string internal name;
string internal symbol;
string internal totalSupply;
mapping(address => uint256) internal balanceOf;
mapping(address => mapping(address => uint256)) internal allowance;
mapping(address => uint256) internal nonces;
address internal implementation;
bool internal upgradeable;
address internal usdt;
uint256 internal ttstokenconfig;
uint256 internal stakestate;
uint128 internal left_share = 45_000_000_000_000;
uint128 internal publicsell;
mapping(address => uint256) internal userConfig;
event e_updateUserConfig(address user, uint256 config);
constructor(
address _usdt,
address _dao_admin,
uint256 _ttsconfig,
string memory _name,
string memory _symbol,
address _implementation
) {
usdt = _usdt;
stakestate = toTTSwapUINT256(uint128(block.timestamp), 0);
ttstokenconfig = _ttsconfig;
userConfig[_dao_admin] = userConfig[_dao_admin].setDAOAdmin(true);
name = _name;
symbol = _symbol;
implementation = _implementation;
upgradeable = true;
emit e_updateUserConfig(_dao_admin, userConfig[_dao_admin]);
}
fallback() external payable {
address impl = implementation;
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
if iszero(result) {
revert(0, returndatasize())
}
return(0, returndatasize())
}
}
/// onlydao admin can execute
modifier onlyTokenAdminProxy() {
if (!userConfig[msg.sender].isTokenAdmin() || !upgradeable)
revert TTSwapError(1);
_;
}
/// onlydao admin can execute
modifier onlyTokenOperatorProxy() {
if (!userConfig[msg.sender].isTokenManager() || !upgradeable)
revert TTSwapError(1);
_;
}
function upgrade(address _implementation) external onlyTokenAdminProxy {
implementation = _implementation;
}
function freezeToken() external onlyTokenOperatorProxy {
implementation = address(0);
}
receive() external payable {}
}
"
},
"src/libraries/L_TTSwapUINT256.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.29;
using L_TTSwapUINT256Library for uint256;
/// @notice Converts two uint128 values into a T_BalanceUINT256
/// @param _amount0 The first 128-bit amount
/// @param _amount1 The second 128-bit amount
/// @return balanceDelta The resulting T_BalanceUINT256
function toTTSwapUINT256(uint128 _amount0, uint128 _amount1) pure returns (uint256 balanceDelta) {
assembly ("memory-safe") {
balanceDelta :=
or(shl(128, _amount0), and(0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff, _amount1))
}
}
/// @notice Adds two T_BalanceUINT256 values
/// @param a The first T_BalanceUINT256
/// @param b The second T_BalanceUINT256
/// @return The sum of a and b as a T_BalanceUINT256
function add(uint256 a, uint256 b) pure returns (uint256) {
uint256 res0;
uint256 res1;
uint256 a0;
uint256 a1;
uint256 b0;
uint256 b1;
assembly ("memory-safe") {
a0 := shr(128, a)
a1 := and(0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff, a)
b0 := shr(128, b)
b1 := and(0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff, b)
res0 := add(a0, b0)
res1 := add(a1, b1)
}
require(res0 >= a0 && res0 >= b0 && res1 >= a1 && res1 >= b1 && res1 <type(uint128).max && res0 <type(uint128).max, "TTSwapUINT256: add overflow");
return (res0<<128)+res1;
}
/// @notice Subtracts two T_BalanceUINT256 values
/// @param a The first T_BalanceUINT256
/// @param b The second T_BalanceUINT256
/// @return The difference of a and b as a T_BalanceUINT256
function sub(uint256 a, uint256 b) pure returns (uint256) {
uint256 res0;
uint256 res1;
uint256 a0;
uint256 a1;
uint256 b0;
uint256 b1;
unchecked{
assembly ("memory-safe") {
a0 := shr(128, a)
a1 := and(0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff, a)
b0 := shr(128, b)
b1 := and(0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff, b)
res0 := sub(a0, b0)
res1 := sub(a1, b1)
}}
require(res0 <=a0 && res1<=a1 &&a1>=b1 && a0>=b0, "TTSwapUINT256: sub overflow");
return (res0<<128)+res1;
}
/// @notice Adds the first components and subtracts the second components of two T_BalanceUINT256 values
/// @param a The first T_BalanceUINT256
/// @param b The second T_BalanceUINT256
/// @return The result of (a0 + b0, a1 - b1) as a T_BalanceUINT256
function addsub(uint256 a, uint256 b) pure returns (uint256) {
uint256 res0;
uint256 res1;
uint256 a0;
uint256 a1;
uint256 b0;
uint256 b1;
unchecked{
assembly ("memory-safe") {
a0 := shr(128, a)
a1 := and(0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff, a)
b0 := shr(128, b)
b1 := and(0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff, b)
res0 := add(a0, b0)
res1 := sub(a1, b1)
}}
require(res0 >=a0 && res0>=b0 && res1<=a1 && a1>=b1 && res0<type(uint128).max , "TTSwapUINT256: addsub overflow");
return (res0<<128)+res1;
}
/// @notice Subtracts the first components and adds the second components of two T_BalanceUINT256 values
/// @param a The first T_BalanceUINT256
/// @param b The second T_BalanceUINT256
/// @return The result of (a0 - b0, a1 + b1) as a T_BalanceUINT256
function subadd(uint256 a, uint256 b) pure returns (uint256) {
uint256 res0;
uint256 res1;
uint256 a0;
uint256 a1;
uint256 b0;
uint256 b1;
unchecked{
assembly ("memory-safe") {
a0 := sar(128, a)
a1 := and(0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff, a)
b0 := sar(128, b)
b1 := and(0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff, b)
res0 := sub(a0, b0)
res1 := add(a1, b1)
}}
require(res1 >=a1 && res1>=b1 && res0<=a0 && a0>=b0 && res1<type(uint128).max , "TTSwapUINT256: subadd overflow");
return (res0<<128)+res1;
}
/// @notice Safely converts a uint256 to a uint128
/// @param a The uint256 value to convert
/// @return b converted uint128 value, or 0 if overflow
function toUint128(uint256 a) pure returns (uint128 b) {
b=uint128(a);
require(a==uint256(b) , "TTSwapUINT256: toUint128 overflow");
}
/// @notice Compares the prices of three T_BalanceUINT256 values
/// @param a The first T_BalanceUINT256
/// @param b The second T_BalanceUINT256
/// @param c The third T_BalanceUINT256
/// @return True if the price of a is lower than the prices of b and c, false otherwise
function lowerprice(uint256 a, uint256 b, uint256 c) pure returns (bool) {
return uint256(a.amount0()) * uint256(b.amount1()) * uint256(c.amount1())
> uint256(a.amount1()) * uint256(b.amount0()) * uint256(c.amount0()) ? true : false;
}
/// @notice Performs a multiplication followed by a division
/// @param config The multiplicand
/// @param amount The multiplier
/// @param domitor The divisor
/// @return a The result as a uint128
function mulDiv(uint256 config, uint256 amount, uint256 domitor) pure returns (uint128 a) {
uint256 result;
unchecked {
assembly {
config := mul(config, amount)
result := div(config, domitor)
}
}
return toUint128(result);
}
/// @title L_TTSwapUINT256Library
/// @notice A library for operations on T_BalanceUINT256
library L_TTSwapUINT256Library {
/// @notice Extracts the first 128-bit amount from a T_BalanceUINT256
/// @param balanceDelta The T_BalanceUINT256 to extract from
/// @return _amount0 The extracted first 128-bit amount
function amount0(uint256 balanceDelta) internal pure returns (uint128 _amount0) {
assembly {
_amount0 := shr(128, balanceDelta)
}
}
/// @notice Extracts the second 128-bit amount from a T_BalanceUINT256
/// @param balanceDelta The T_BalanceUINT256 to extract from
/// @return _amount1 The extracted second 128-bit amount
function amount1(uint256 balanceDelta) internal pure returns (uint128 _amount1) {
assembly {
_amount1 := balanceDelta
}
}
/// @notice Extracts the first and second 128-bit amounts from a T_BalanceUINT256
/// @param balanceDelta The T_BalanceUINT256 to extract from
/// @return _amount0 The extracted first 128-bit amount
/// @return _amount1 The extracted second 128-bit amount
function amount01(uint256 balanceDelta) internal pure returns (uint128 _amount0,uint128 _amount1) {
assembly {
_amount0 := shr(128, balanceDelta)
_amount1 := balanceDelta
}
}
/// @notice Calculates amount0 based on a given amount1 and the ratio in balanceDelta
/// @param balanceDelta The T_BalanceUINT256 containing the ratio
/// @param amount1delta The amount1 to base the calculation on
/// @return _amount0 The calculated amount0
function getamount0fromamount1(uint256 balanceDelta, uint128 amount1delta)
internal
pure
returns (uint128 _amount0)
{
return mulDiv(balanceDelta.amount0(), amount1delta, balanceDelta.amount1());
}
/// @notice Calculates amount1 based on a given amount0 and the ratio in balanceDelta
/// @param balanceDelta The T_BalanceUINT256 containing the ratio
/// @param amount0delta The amount0 to base the calculation on
/// @return _amount1 The calculated amount1
function getamount1fromamount0(uint256 balanceDelta, uint128 amount0delta)
internal
pure
returns (uint128 _amount1)
{
return mulDiv(balanceDelta.amount1(), amount0delta, balanceDelta.amount0());
}
}
"
},
"src/libraries/L_UserConfig.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.29;
/// @title Market Configuration Library
/// @notice Library for managing and calculating various fee configurations for a market
library L_UserConfigLibrary {
function isDAOAdmin(uint256 config) internal pure returns(bool a){
return (config&uint256(2**255))>0;
}
function setDAOAdmin(uint256 config,bool a)internal pure returns(uint256 e){
return (config&(~(uint256(2**255))))|(a?uint256(2**255):0);
}
function isTokenAdmin(uint256 config) internal pure returns(bool a){
return (config&uint256(2**254))>0;
}
function setTokenAdmin(uint256 config,bool a)internal pure returns(uint256 e){
return config&~(uint256(2**254))|(a?uint256(2**254):0);
}
function isTokenManager(uint256 config) internal pure returns(bool a){
return (config&uint256(2**253))>0;
}
function setTokenManager(uint256 config,bool a)internal pure returns(uint256 e){
return config&~(uint256(2**253))|(a?uint256(2**253):0);
}
function isMarketAdmin(uint256 config)internal pure returns(bool a){
return (config&uint256(2**252))>0;
}
function setMarketAdmin(uint256 config,bool a)internal pure returns(uint256 e){
return config&~(uint256(2**252))|(a?uint256(2**252):0);
}
function isMarketManager(uint256 config)internal pure returns(bool a){
return (config&uint256(2**251))>0;
}
function setMarketManager(uint256 config,bool a)internal pure returns(uint256 e){
return config&~(uint256(2**251))|(a?uint256(2**251):0);
}
function isCallMintTTS(uint256 config)internal pure returns(bool a){
return (config&uint256(2**250))>0;
}
function setCallMintTTS(uint256 config,bool a)internal pure returns(uint256 e){
return config&~(uint256(2**250))|(a?uint256(2**250):0);
}
function isStakeAdmin(uint256 config)internal pure returns(bool a){
return (config&uint256(2**249))>0;
}
function setStakeAdmin(uint256 config,bool a)internal pure returns(uint256 e){
return config&~(uint256(2**249))|(a?uint256(2**249):0);
}
function isStakeManager(uint256 config)internal pure returns(bool a){
return (config&uint256(2**248))>0;
}
function setStakeManager(uint256 config,bool a)internal pure returns(uint256 e){
return config&~(uint256(2**248))|(a?uint256(2**248):0);
}
function isBan(uint256 config)internal pure returns(bool a){
return (config&uint256(2**160))>0;
}
function setBan(uint256 config,bool a)internal pure returns(uint256 e){
return config&~(uint256(2**160))|(a?uint256(2**160):0);
}
function referral(uint256 config)internal pure returns(address a){
return address(uint160(config));
}
function setReferral(uint256 config,address a)internal pure returns(uint256 e){
return (config&~(uint256(2**160)-1))|uint256(uint160(a));
}
}
"
},
"src/libraries/L_Error.sol": {
"content": "// SPDX-License-Identifier: MIT
pragma solidity 0.8.29;
error TTSwapError(uint256 seq);
"
}
}
}}
Submitted on: 2025-10-15 11:09:39
Comments
Log in to comment.
No comments yet.