Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start leaderboard contract #27

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ data/snapshotBalance
data/holders
flattened
distribution-data*.json
.DS_Store

#Hardhat files
cache
Expand Down
138 changes: 138 additions & 0 deletions contracts/competition/Competition.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
pragma solidity ^0.6.0;

import "../referrals/interfaces/IReferralStorage.sol";

contract Competition
{
morazzela marked this conversation as resolved.
Show resolved Hide resolved
uint public start;
uint public end;
uint public registrationStart;
uint public registrationEnd;
IReferralStorage private referralStorage;

struct Team {
address leader;
string name;
bytes32 referral;
address[] members;
address[] joinRequests;
}

address[] private leaders;
morazzela marked this conversation as resolved.
Show resolved Hide resolved
mapping(address => Team) private teams;
mapping(string => bool) private teamNames;
mapping(address => address) private membersToTeam;
mapping(address => mapping(address => bool)) private requests;
morazzela marked this conversation as resolved.
Show resolved Hide resolved

modifier registrationIsOpen()
morazzela marked this conversation as resolved.
Show resolved Hide resolved
{
require(block.timestamp >= registrationStart, "Registration is not opened yet.");
require(block.timestamp < registrationEnd, "Registration is closed.");
_;
}

modifier isNotLeader()
{
require(teams[msg.sender].leader == address(0), "Team leaders are not allowed.");
_;
}

modifier isNotMember()
{
require(membersToTeam[msg.sender] == address(0), "Team members are not allowed.");
_;
}

constructor (
uint _start,
uint _end,
uint _registrationStart,
uint _registrationEnd,
morazzela marked this conversation as resolved.
Show resolved Hide resolved
IReferralStorage _referralStorage
) public
{
start = _start;
end = _end;
registrationStart = _registrationStart;
registrationEnd = _registrationEnd;
referralStorage = _referralStorage;
}

function registerTeam (string calldata name, bytes32 referral) external registrationIsOpen isNotLeader
{
require(referralStorage.codeOwners(referral) != address(0), "Referral code does not exist.");
require(teamNames[name] == false, "Team name already registered.");

Team storage team;
morazzela marked this conversation as resolved.
Show resolved Hide resolved

team.leader = msg.sender;
team.name = name;
team.referral = referral;
team.members.push(msg.sender);

teams[msg.sender] = team;
leaders.push(msg.sender);
teamNames[name] = true;
}

function createJoinRequest (address leaderAddress) external registrationIsOpen isNotLeader isNotMember
morazzela marked this conversation as resolved.
Show resolved Hide resolved
{
require(membersToTeam[msg.sender] == address(0), "You can't join multiple teams.");
require(teams[msg.sender].leader != address(0), "The team does not exist.");
require(requests[leaderAddress][msg.sender] == false, "You already applied for this team.");

teams[leaderAddress].joinRequests.push(msg.sender);
requests[leaderAddress][msg.sender] = true;
}

function approveJoinRequest (address memberAddress) external registrationIsOpen isNotMember
{
require(requests[msg.sender][memberAddress] == false, "This member did not apply.");
require(membersToTeam[memberAddress] == address(0), "This member already joined a team.");

referralStorage.setTraderReferralCode(memberAddress, teams[msg.sender].referral);
teams[msg.sender].members.push(msg.sender);
membersToTeam[memberAddress] = msg.sender;
}

function getLeaders() external view returns (address[] memory)
{
address[] memory res;

for (uint i = 0; i < leaders.length; i++) {
morazzela marked this conversation as resolved.
Show resolved Hide resolved
res[i] = leaders[i];
}

return res;
}

function getTeam(address leaderAddr) external view returns (address leader, string memory name, bytes32 referral)
{
Team memory team = teams[leaderAddr];
return (team.leader, team.name, team.referral);
}

function getMemberTeam(address memberAddr) external view returns (address)
{
return membersToTeam[memberAddr];
morazzela marked this conversation as resolved.
Show resolved Hide resolved
}

function getTeamMembers(address leaderAddr) external view returns (address[] memory)
{
return teams[leaderAddr].members;
}

function getTeamJoinRequests(address leaderAddr) external view returns (address[] memory)
morazzela marked this conversation as resolved.
Show resolved Hide resolved
{
address[] memory res;

for (uint i = 0; i < teams[leaderAddr].joinRequests.length; i++) {
address jr = teams[leaderAddr].joinRequests[i];
if (membersToTeam[jr] == address(0)) {
res[i] = jr;
}
}

return res;
}
}
52 changes: 52 additions & 0 deletions scripts/core/deployCompetition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { deployContract, contractAt } = require("../shared/helpers")

const network = (process.env.HARDHAT_NETWORK || 'mainnet');

async function getArbValues() {
const positionRouter = await contractAt("PositionRouter", "0x3D6bA331e3D9702C5e8A8d254e5d8a285F223aba")

return { positionRouter }
}

async function getAvaxValues() {
const positionRouter = await contractAt("PositionRouter", "0x195256074192170d1530527abC9943759c7167d8")

return { positionRouter }
}

async function getValues() {
if (network === "arbitrum") {
return getArbValues()
}

if (network === "avax") {
return getAvaxValues()
}

if (network === "testnet") {
return getTestnetValues()
}
}

async function main() {
const { positionRouter } = await getValues()
const referralStorage = await contractAt("ReferralStorage", await positionRouter.referralStorage())

const startTime = Math.round(Date.now() / 1000)
const endTime = startTime + 100000000000

await deployContract("Competition", [
startTime,
endTime,
startTime,
endTime,
referralStorage.address,
]);
}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})