Skip to content

Instantly share code, notes, and snippets.

@ColonelJ
Last active March 28, 2023 18:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ColonelJ/38b08b3086f12aa4b2b18ad9b3623a6a to your computer and use it in GitHub Desktop.
Save ColonelJ/38b08b3086f12aa4b2b18ad9b3623a6a to your computer and use it in GitHub Desktop.
Tronpix smart contract
pragma solidity ^0.5.0;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*
* NOTE: This is a feature of the next version of OpenZeppelin Contracts.
* @dev Get it via `npm install @openzeppelin/contracts@next`.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
* NOTE: This is a feature of the next version of OpenZeppelin Contracts.
* @dev Get it via `npm install @openzeppelin/contracts@next`.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*
* NOTE: This is a feature of the next version of OpenZeppelin Contracts.
* @dev Get it via `npm install @openzeppelin/contracts@next`.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
pragma solidity ^0.5.4;
import "SafeMath.sol";
contract Tronpix {
using SafeMath for uint;
struct Plot {
address plot_owner;
uint[10] pixel_data;
string url;
}
uint constant PLOT_COLS = 150;
uint constant PLOT_ROWS = 600;
trcToken constant TOKEN_ID = 1000513;
address public owner;
address public moderator;
uint public token_price;
uint public owned_plots;
Plot[PLOT_COLS][PLOT_ROWS] plots;
event Buy(
uint indexed x,
uint indexed y,
address indexed plot_owner,
uint[10] pixel_data,
string url
);
event Update(
uint indexed x,
uint indexed y,
uint[10] pixel_data,
string url
);
event Sell(
uint indexed x,
uint indexed y
);
constructor(uint initial_token_price) public {
owner = msg.sender;
token_price = initial_token_price;
}
function availableTokens() public view returns (uint) {
return address(this).tokenBalance(TOKEN_ID).sub(owned_plots);
}
function buyTokens(uint amount) external payable {
require(amount > 0, "Amount must be > 0");
require(amount <= availableTokens(),
"Not enough tokens available");
require(msg.value == amount.mul(token_price),
"Wrong number of TRX sent");
require(msg.tokenid == 0 && msg.tokenvalue == 0,
"Not allowed to send tokens");
msg.sender.transferToken(amount, TOKEN_ID);
}
function getPlot(uint x, uint y) external view returns (
address plot_owner,
uint[10] memory pixel_data,
string memory url
) {
require(x < PLOT_COLS, "x is out of range");
require(y < PLOT_ROWS, "y is out of range");
Plot storage plot = plots[y][x];
return (plot.plot_owner, plot.pixel_data, plot.url);
}
function buyPlot(
uint x,
uint y,
uint[10] calldata pixel_data,
string calldata url
) external payable {
require(x < PLOT_COLS, "x is out of range");
require(y < PLOT_ROWS, "y is out of range");
require(msg.value == 0, "Not allowed to send TRX");
require(msg.tokenid == TOKEN_ID, "Wrong token type sent");
require(msg.tokenvalue == 1, "Must send exactly 1 token");
Plot storage plot = plots[y][x];
require(plot.plot_owner == address(0), "Plot is already owned");
plot.plot_owner = msg.sender;
plot.pixel_data = pixel_data;
plot.url = url;
++owned_plots;
emit Buy(x, y, msg.sender, pixel_data, url);
}
function updatePlot(
uint x,
uint y,
uint[10] calldata pixel_data,
string calldata url
) external {
require(x < PLOT_COLS, "x is out of range");
require(y < PLOT_ROWS, "y is out of range");
Plot storage plot = plots[y][x];
require(msg.sender == owner || msg.sender == moderator
|| msg.sender == plot.plot_owner,
"You do not own this plot");
plot.pixel_data = pixel_data;
plot.url = url;
emit Update(x, y, pixel_data, url);
}
function sellPlot(uint x, uint y) external {
require(x < PLOT_COLS, "x is out of range");
require(y < PLOT_ROWS, "y is out of range");
Plot storage plot = plots[y][x];
require(plot.plot_owner != address(0), "Plot is not currently owned");
require(plot.plot_owner == msg.sender, "You do not own this plot");
plot.plot_owner = address(0);
--owned_plots;
msg.sender.transferToken(1, TOKEN_ID);
emit Sell(x, y);
}
modifier onlyOwner() {
require(msg.sender == owner, "You are not authorized");
_;
}
function setTokenPrice(uint price) external onlyOwner {
token_price = price;
}
function withdrawTRX(
address payable beneficiary,
uint amount
) external onlyOwner {
require(amount > 0, "Amount must be > 0");
beneficiary.transfer(amount);
}
function withdrawTRC10(
address payable beneficiary,
trcToken id,
uint amount
) external onlyOwner {
require(amount > 0, "Amount must be > 0");
require(id != TOKEN_ID || amount <= availableTokens(),
"Not enough tokens available");
beneficiary.transferToken(amount, id);
}
function forward(
address payable to,
uint value,
bytes calldata data
) external payable onlyOwner {
require(msg.value == value, "Wrong amount of TRX sent");
require(msg.tokenid == 0 && msg.tokenvalue == 0,
"Not allowed to send tokens");
(bool success,) = to.call.value(value)(data);
require(success);
}
function changeOwner(address new_owner) external onlyOwner {
owner = new_owner;
}
function changeModerator(address new_moderator) external onlyOwner {
moderator = new_moderator;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment