Contract Source Code:
File 1 of 1 : SonicUI
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0;
interface IERC20 {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);
}
interface Iui{
function newPlayerAndHero(uint numberOfPlayers, string memory baseName, uint groupId) external;
function newPlayers(uint numberOfPlayers, uint groupId) external;
function newHeroes(uint fromId, uint toId, string calldata baseName) external;
function runCommand (bytes calldata command) external;
function runCommands(uint[] calldata ids) external;
function runSavedCommand(uint id) external;
function saveCommand(uint id, bytes calldata command, bool overwrite) external;
function deleteCommand(uint id) external;
function execute(address to, bytes calldata data) external;
function helperExecute(address to, bytes calldata data) external;
function processGroup(uint id, bytes4 playerSig, bytes calldata data)external;
function processGroupAction(uint id)external;
function process(bytes4 s, bytes calldata data)external;
function updateHelper(address h) external;
function updateSig(uint i, bytes4 s) external;
function addSig(bytes4 s) external ;
function addAction(bytes calldata action) external;
function updateAction(uint index, bytes calldata action) external;
function setGroupAction(uint groupId, uint actionId) external ;
function setGroupsAction(uint[] calldata groupIds, uint[] calldata actionIds) external;
function transferOwner(address newOwner) external;
function setHelperOwner(address newOwner) external;
function setOffsetHero(uint value) external;
function offsetMultiHero(uint from, uint to, uint value, bool subtract) external;
function totalSupply() external view returns(uint);
function getPlayer(uint id) external view returns(address);
function getPlayerId(address player) external view returns(uint);
function getGroup(uint idx) external view returns(uint[] memory);
function getNumberOfGroups() external view returns(uint);
function withdrawFromGroup(address to, uint groupId, uint tokenId) external;
function withdrawBatchFromGroup(address to, uint groupId, uint[] calldata tokenIds) external;
function withdrawERC20(address to, address token, uint amount) external;
function withdrawEth(address to, uint value, bytes calldata data) external;
}
interface IERC1155{
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes calldata data
) external;
}
contract ERC1155Receiver{
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
) external returns (bytes4){
return 0xf23a6e61;
}
function onERC1155BatchReceived(
address operator,
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
) external returns (bytes4){
return 0xbc197c81;
}
}
contract SonicUI is ERC1155Receiver{
Iui private controller;
address private owner;
address public heroMinter;
address public items;
mapping(uint=>uint) public actionTime;
mapping(uint=>uint) public link;
mapping(uint=>uint) public linkToken;
modifier onlyOwner() {
require(owner == msg.sender, "Ownable: caller is not the owner");
_;
}
constructor(address _controller) {
owner = msg.sender;
controller = Iui(_controller);
heroMinter = address(0x076aEec336f5abbdF64Ba8DDF96FC974B0463528);
items = address(0x8970c63da309d5359A579C2F53BfD64F72B7B706);
}
receive() external payable {}
/*
Internal functions
*/
function _processGroupAction(uint id)internal{
controller.processGroupAction(id);
actionTime[id]=block.timestamp;
}
/*
Main functions
*/
function newPlayerAndHero(uint numberOfPlayers, string memory baseName, uint groupId) external onlyOwner{
controller.newPlayerAndHero(numberOfPlayers, baseName, groupId);
}
function newPlayers(uint numberOfPlayers, uint groupId) external onlyOwner{
controller.newPlayers(numberOfPlayers, groupId);
}
function newHeroes(uint fromId, uint toId, string calldata baseName) external onlyOwner {
controller.newHeroes(fromId, toId, baseName);
}
function runCommand (bytes calldata command) external onlyOwner{
controller.runCommand(command);
}
function runCommands(uint[] calldata ids) external onlyOwner{
controller.runCommands(ids);
}
function runSavedCommand(uint id) external onlyOwner{
controller.runSavedCommand(id);
}
function saveCommand(uint id, bytes calldata command, bool overwrite) external onlyOwner{
controller.saveCommand(id,command,overwrite);
}
function deleteCommand(uint id) external onlyOwner{
controller.deleteCommand(id);
}
function execute(address to, bytes calldata data) external onlyOwner{
controller.execute(to,data);
}
function helperExecute(address to, bytes calldata data) external onlyOwner{
controller.helperExecute(to,data);
}
function processGroup(uint id, bytes4 playerSig, bytes calldata data)external onlyOwner{
controller.processGroup(id,playerSig,data);
actionTime[id]=block.timestamp;
}
function processGroupAction(uint id)external onlyOwner{
_processGroupAction(id);
}
function processGroupActionAndLink(uint id)external onlyOwner{
_processGroupAction(id);
_transferItemFromGroupToGroup(linkToken[id], id, link[id]);
}
function processGroupsAction(uint[] calldata groupIds)external onlyOwner{
for (uint i=0; i<groupIds.length; i++){
_processGroupAction(groupIds[i]);
}
}
function process(bytes4 s, bytes calldata data)external onlyOwner{
controller.process(s,data);
}
/*
Setting functions
*/
function setItem(address item_) external onlyOwner{
items = item_;
}
function setHeroMinter(address heroMinter_) external onlyOwner{
heroMinter = heroMinter_;
}
function setController(address c) external onlyOwner{
controller = Iui(c);
}
function updateHelper(address h) external onlyOwner{
controller.updateHelper(h);
}
function updateSig(uint i, bytes4 s) external onlyOwner{
controller.updateSig(i,s);
}
function addSig(bytes4 s) external onlyOwner{
controller.addSig(s);
}
function addAction(bytes calldata action) external onlyOwner{
controller.addAction(action);
}
function updateAction(uint index, bytes calldata action) external onlyOwner{
controller.updateAction(index,action);
}
function setGroupAction(uint groupId, uint actionId) external onlyOwner{
controller.setGroupAction(groupId,actionId);
}
function setGroupsAction(uint[] calldata groupIds, uint[] calldata actionIds) external onlyOwner{
controller.setGroupsAction(groupIds,actionIds);
}
function setGroupsAction(uint[] calldata groupIds, uint actionId) external onlyOwner{
for (uint i=0; i<groupIds.length; i++){
controller.setGroupAction(groupIds[i],actionId);
}
}
function addLink(uint[] memory gr1, uint[] memory gr2, uint linkToken_) external onlyOwner{
require(gr1.length==gr2.length);
for(uint i=0; i<gr1.length; i++){
link[gr1[i]]=gr2[i];
linkToken[gr1[i]]=linkToken_;
}
}
function updateLink(uint gr1, uint gr2) external onlyOwner{
link[gr1]=gr2;
}
/*
Ownership functions
*/
function transferOwner(address newOwner) external onlyOwner{
owner = newOwner;
}
function transferControlOwner(address newOwner) external onlyOwner{
controller.transferOwner(newOwner);
}
function setHelperOwner(address newOwner) external onlyOwner{
controller.setHelperOwner(newOwner);
}
/*
Misc functions
*/
function setOffsetHero(uint value) external onlyOwner{
controller.setOffsetHero(value);
}
function offsetMultiHero(uint from, uint to, uint value, bool subtract) external onlyOwner{
controller.offsetMultiHero(from,to,value,subtract);
}
function DepositHeroes(uint fromPlayer, uint toPlayer, uint[] calldata heroIds) external onlyOwner{
uint len = toPlayer - fromPlayer;
require(heroIds.length==len+1,"length not matched");
uint j=0;
for(uint i=fromPlayer; i<= toPlayer; i++){
//transfer to player
IERC1155(heroMinter).safeTransferFrom(msg.sender,getPlayer(i),heroIds[j],1,new bytes(0));
//set heroId to player
controller.offsetMultiHero(i,i,heroIds[j++],false);
//activate hero
//controller.processGroup(id,playerSig,data);
}
}
/*
View functions
*/
function totalSupply() public view returns(uint){
return controller.totalSupply();
}
function groupAge(uint id) public view returns(uint){
return block.timestamp - actionTime[id];
}
function oneDayleft(uint id) public view returns(uint){
uint timeleft=0;
if(24 hours > block.timestamp - actionTime[id]){
timeleft = 24 hours - (block.timestamp - actionTime[id]);
}
return timeleft;
}
function getPlayer(uint id) public view returns(address){
return controller.getPlayer(id);
}
function getHeroId(address player) public view returns(uint){
return controller.getPlayerId(player);
}
function getHeroId(uint playerId) public view returns(uint){
return controller.getPlayerId(getPlayer(playerId));
}
function getGroup(uint idx) public view returns(uint[] memory){
return controller.getGroup(idx);
}
function getNumberOfGroups() public view returns(uint){
return controller.getNumberOfGroups();
}
/*
Misc functions
*/
function withdrawFromGroup(address to, uint groupId, uint tokenId) external onlyOwner{
controller.withdrawFromGroup(to,groupId,tokenId);
}
function withdrawFromGroups(address to, uint[] calldata groupIds, uint tokenId) external onlyOwner{
for (uint i=0; i<groupIds.length; i++){
controller.withdrawFromGroup(to,groupIds[i],tokenId);
} }
function withdrawBatchFromGroup(address to, uint groupId, uint[] calldata tokenIds) external onlyOwner{
controller.withdrawBatchFromGroup(to,groupId,tokenIds);
}
function withdrawBatchFromGroups(address to, uint[] calldata groupIds, uint[] calldata tokenIds) external onlyOwner{
for (uint i=0; i<groupIds.length; i++){
controller.withdrawBatchFromGroup(to,groupIds[i],tokenIds);
}
}
function transferItemFromGroupToGroup(uint tokenId, uint fromGroup, uint toGroup) external onlyOwner{
_transferItemFromGroupToGroup( tokenId, fromGroup, toGroup);
}
function _transferItemFromGroupToGroup(uint tokenId, uint fromGroup, uint toGroup) internal {
bytes4 sig = bytes4(keccak256("transferItemFromGroupToGroup(uint256,uint256,uint256)"));
controller.process(sig, abi.encode(tokenId,fromGroup,toGroup));
}
function withdrawControlERC20(address to, address token, uint amount) external onlyOwner {
controller.withdrawERC20(to,token,amount);
}
function withdrawControlEth(address to, uint value, bytes calldata data) external payable onlyOwner{
controller.withdrawEth(to,value,data);
}
function withdrawERC20(address to, address token, uint amount) external onlyOwner {
IERC20(token).transferFrom(address(this),to,amount);
}
function withdrawEth(address to, uint value, bytes calldata data) external payable onlyOwner{
(bool success,) = to.call{value:value}(data);
require(success, 'TransferHelper: ETH_TRANSFER_FAILED');
}
/*Distributor*/
function _DistributeGroup(uint id, uint tokenId, uint amount) internal{
uint[] memory playerIdxs = controller.getGroup(id);
for (uint i=0 ; i<playerIdxs.length; i++){
_safeTransferFrom(controller.getPlayer(playerIdxs[i]),items, tokenId, amount);
}
}
function DistributeGroup(uint id, uint tokenId, uint amount) external onlyOwner{
_DistributeGroup(id, tokenId, amount);
}
function DistributeGroups(uint[] calldata ids, uint tokenId, uint amount) external onlyOwner{
for (uint i=0 ; i<ids.length; i++){
_DistributeGroup(ids[i], tokenId, amount);
}
}
function DistributePlayers(uint[] calldata ids, uint tokenId, uint amount) external onlyOwner{
for (uint i=0 ; i<ids.length; i++){
_safeTransferFrom(controller.getPlayer(ids[i]),items, tokenId, amount);
}
}
function DistributePlayersRange(uint fromPlayer,uint toPlayer, uint tokenId, uint amount) external onlyOwner{
for (uint i=fromPlayer ; i<= toPlayer; i++){
_safeTransferFrom(controller.getPlayer(i),items, tokenId, amount);
}
}
function depositItems(uint tokenId, uint amount) external{
IERC1155(items).safeTransferFrom(msg.sender,address(this),tokenId,amount, new bytes(0));
}
function _safeTransferFrom(address to, address token, uint tokenId, uint amount) internal {
IERC1155(token).safeTransferFrom(address(this),to,tokenId,amount, new bytes(0));
}
}