// SPDX-License-Identifier: DG
pragma solidity ^0.8.9;
import "./ERC20.sol";
interface DGToken {
function balanceOf(
address _account
)
external
view
returns (uint256);
function transfer(
address _recipient,
uint256 _amount
)
external
returns (bool);
function transferFrom(
address _sender,
address _recipient,
uint256 _amount
)
external
returns (bool);
}
contract DGTownHall is ERC20("Decentral Games Governance", "xDG") {
DGToken public immutable DG;
constructor(
address _tokenAddress
) {
DG = DGToken(
_tokenAddress
);
}
function stepInside(
uint256 _DGAmount
)
external
{
uint256 DGTotal = innerSupply();
uint256 xDGTotal = totalSupply();
DGTotal == 0 || xDGTotal == 0
? _mint(msg.sender, _DGAmount)
: _mint(msg.sender, _DGAmount * xDGTotal / DGTotal);
DG.transferFrom(
msg.sender,
address(this),
_DGAmount
);
}
function stepOutside(
uint256 _xDGAmount
)
external
{
uint256 transferAmount = _xDGAmount
* innerSupply()
/ totalSupply();
_burn(
msg.sender,
_xDGAmount
);
DG.transfer(
msg.sender,
transferAmount
);
}
function DGAmount(
address _account
)
external
view
returns (uint256)
{
return balanceOf(_account)
* innerSupply()
/ totalSupply();
}
function outsidAmount(
uint256 _xDGAmount
)
external
view
returns (uint256 _DGAmount)
{
return _xDGAmount
* innerSupply()
/ totalSupply();
}
function insideAmount(
uint256 _DGAmount
)
external
view
returns (uint256 _xDGAmount)
{
uint256 xDGTotal = totalSupply();
uint256 DGTotal = innerSupply();
return xDGTotal == 0 || DGTotal == 0
? _DGAmount
: _DGAmount * xDGTotal / DGTotal;
}
function innerSupply()
public
view
returns (uint256)
{
return DG.balanceOf(address(this));
}
}
// SPDX-License-Identifier: ---DG----
pragma solidity ^0.8.9;
contract ERC20 {
string private _name;
string private _symbol;
uint8 private _decimals;
uint256 private _totalSupply;
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
mapping(address => uint) public nonces;
bytes32 public DOMAIN_SEPARATOR;
bytes32 public constant PERMIT_TYPEHASH = keccak256(
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
);
event Transfer(
address indexed _from,
address indexed _to,
uint256 _value
);
event Approval(
address indexed _owner,
address indexed _spender,
uint256 _value
);
constructor(
string memory _entryname,
string memory _entrysymbol
) {
_name = _entryname;
_symbol = _entrysymbol;
_decimals = 18;
}
function name()
public
view
returns (string memory)
{
return _name;
}
function symbol()
public
view
returns (string memory)
{
return _symbol;
}
function decimals()
public
view
returns (uint8)
{
return _decimals;
}
function totalSupply()
public
view
returns (uint256)
{
return _totalSupply;
}
function balanceOf(
address _account
)
public
view
returns (uint256)
{
return _balances[_account];
}
function transfer(
address _recipient,
uint256 _amount
)
external
returns (bool)
{
_transfer(
msg.sender,
_recipient,
_amount
);
return true;
}
function allowance(
address _owner,
address _spender
)
external
view
returns (uint256)
{
return _allowances[_owner][_spender];
}
function approve(
address _spender,
uint256 _amount
)
external
returns (bool)
{
_approve(
msg.sender,
_spender,
_amount
);
return true;
}
function transferFrom(
address _sender,
address _recipient,
uint256 _amount
)
public
returns (bool)
{
_approve(
_sender,
msg.sender,
_allowances[_sender][msg.sender] - _amount
);
_transfer(
_sender,
_recipient,
_amount
);
return true;
}
function permit(
address _owner,
address _spender,
uint256 _value,
uint256 _deadline,
uint8 _v,
bytes32 _r,
bytes32 _s
)
external
{
require(
_deadline >= block.timestamp,
'ERC20: PERMIT_CALL_EXPIRED'
);
bytes32 digest = keccak256(
abi.encodePacked(
'\x19\x01',
DOMAIN_SEPARATOR,
keccak256(
abi.encode(
PERMIT_TYPEHASH,
_owner,
_spender,
_value,
nonces[_owner]++,
_deadline
)
)
)
);
address recoveredAddress = ecrecover(
digest,
_v,
_r,
_s
);
require(
recoveredAddress != address(0) &&
recoveredAddress == _owner,
'INVALID_SIGNATURE'
);
_approve(
_owner,
_spender,
_value
);
}
function _transfer(
address _sender,
address _recipient,
uint256 _amount
)
internal
{
_balances[_sender] =
_balances[_sender] - _amount;
_balances[_recipient] =
_balances[_recipient] + _amount;
emit Transfer(
_sender,
_recipient,
_amount
);
}
function _mint(
address _account,
uint256 _amount
)
internal
{
_totalSupply =
_totalSupply + _amount;
unchecked {
_balances[_account] =
_balances[_account] + _amount;
}
emit Transfer(
address(0),
_account,
_amount
);
}
function _burn(
address _account,
uint256 _amount
)
internal
{
_balances[_account] =
_balances[_account] - _amount;
unchecked {
_totalSupply =
_totalSupply - _amount;
}
emit Transfer(
_account,
address(0),
_amount
);
}
function _approve(
address _owner,
address _spender,
uint256 _amount
)
internal
{
_allowances[_owner][_spender] = _amount;
emit Approval(
_owner,
_spender,
_amount
);
}
}
{
"compilationTarget": {
"DGTownHall.sol": "DGTownHall"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_tokenAddress","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"DG","outputs":[{"internalType":"contract DGToken","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"DGAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"DOMAIN_SEPARATOR","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PERMIT_TYPEHASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"address","name":"_spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"innerSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_DGAmount","type":"uint256"}],"name":"insideAmount","outputs":[{"internalType":"uint256","name":"_xDGAmount","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"nonces","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_xDGAmount","type":"uint256"}],"name":"outsidAmount","outputs":[{"internalType":"uint256","name":"_DGAmount","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"},{"internalType":"uint8","name":"_v","type":"uint8"},{"internalType":"bytes32","name":"_r","type":"bytes32"},{"internalType":"bytes32","name":"_s","type":"bytes32"}],"name":"permit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_DGAmount","type":"uint256"}],"name":"stepInside","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_xDGAmount","type":"uint256"}],"name":"stepOutside","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_recipient","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_sender","type":"address"},{"internalType":"address","name":"_recipient","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]