Skip to content

Commit

Permalink
fixed createGame playbook (#87)
Browse files Browse the repository at this point in the history
* fixed createGame pbook, adjusted config and creating subject to support this.

* changeset

* uncomment playbooks from config during playbook launch
  • Loading branch information
peersky authored Dec 2, 2024
1 parent 5a44931 commit 27e1c1a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-carrots-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rankify-contracts': minor
---

fixed createGame playbook
6 changes: 2 additions & 4 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import 'hardhat-abi-exporter';
import { toSignature, isIncluded } from './scripts/diamond';
import { cutFacets, replaceFacet } from './scripts/libraries/diamond';
import 'hardhat-gas-reporter';
// import * as ipfsUtils from "./utils/ipfs";
// import fs from "fs";
import 'hardhat-contract-sizer';
import 'hardhat-deploy';
import 'hardhat-tracer';
import 'solidity-docgen';
import './playbook';
// import './playbook';
import getSuperInterface from './scripts/getSuperInterface';
import { ErrorFragment, EventFragment, FunctionFragment } from '@ethersproject/abi';
import './scripts/generateSelectorDocs';
Expand Down Expand Up @@ -104,7 +102,7 @@ export default {
default: '0x520E00225C4a43B6c55474Db44a4a44199b4c3eE',
anvil: '0x507c2d32185667156de5B4C440FEEf3800078bDb',
},
registrar: {
gameMaster: {
localhost: '0xaA63aA2D921F23f204B6Bcb43c2844Fb83c82eb9',
},
defaultPlayer: {
Expand Down
163 changes: 99 additions & 64 deletions playbook/createGame.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,112 @@
import { task } from 'hardhat/config';
import { RankifyDiamondInstance, Rankify } from '../types';
import { IRankifyInstanceCommons } from '../types/src/facets/RankifyInstanceMainFacet';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { IRankifyInstance } from '../types/src/facets/RankifyInstanceMainFacet';
import rankifyInstanceAbi from '../abi/hardhat-diamond-abi/HardhatDiamondABI.sol/RankifyDiamondInstance.json';

task('gameCreated', 'Create new game')
.addOptionalParam('gameCreator', 'Player address who will create game')
task('createGame', 'Create new game')
.addOptionalParam('gameCreatorPK', 'Player private key who will create game')
.addOptionalParam('gameMaster', 'Game master address')
.addOptionalParam('gameRank', 'Game rank', '1')
.addOptionalParam('maxPlayerCnt', 'Max player count', '8')
.addOptionalParam('minPlayerCnt', 'Min player count', '4')
.addOptionalParam('minGameTime', 'Minimum game time in seconds', '3600')
.addOptionalParam('nTurns', 'Number of turns', '10')
.addOptionalParam('timePerTurn', 'Time per turn in seconds', '1800')
.addOptionalParam('voteCredits', 'Vote credits per player', '14')
.addOptionalParam('timeToJoin', 'Time to join in seconds', '1800')
.addParam('rankifyInstanceAddress', 'Rankify instance address')
.setAction(
async ({
gameCreator,
}: { gameCreator: string;},
hre: HardhatRuntimeEnvironment) => {
const { deployments, getNamedAccounts } = hre;
const rankifyDeployment = await deployments.get('Rankify');
const rankifyInstanceDeployment = await deployments.get('RankifyInstance');
const { owner, registrar, defaultPlayer } = await getNamedAccounts();
gameCreator = gameCreator ?? defaultPlayer;
async (
{
gameCreatorPK,
gameMaster,
gameRank,
rankifyInstanceAddress,
maxPlayerCnt,
minPlayerCnt,
minGameTime,
nTurns,
timePerTurn,
voteCredits,
timeToJoin,
}: {
gameCreatorPK: string;
gameMaster: string;
gameRank: string;
rankifyInstanceAddress: string;
maxPlayerCnt: string;
minPlayerCnt: string;
minGameTime: string;
nTurns: string;
timePerTurn: string;
voteCredits: string;
timeToJoin: string;
},
hre,
) => {
const { ethers, getNamedAccounts } = hre;
const { owner, gameMaster: defaultGameMaster, defaultPlayer } = await getNamedAccounts();

await mintTokensToGameCreator(rankifyDeployment, gameCreator, owner, hre);
await approveTokensUse(rankifyDeployment, rankifyInstanceDeployment, gameCreator, hre);
await createGame(rankifyInstanceDeployment, gameCreator, registrar, hre);
},
);
const gameCreatorSigner = gameCreatorPK
? new hre.ethers.Wallet(gameCreatorPK, hre.ethers.provider)
: await hre.ethers.getSigner(defaultPlayer);

// Get game price
const gameInstance = new ethers.Contract(
rankifyInstanceAddress,
rankifyInstanceAbi,
gameCreatorSigner,
) as RankifyDiamondInstance;

export const mintTokensToGameCreator = async (
rankifyDeployment: any,
gameCreator: string,
owner: string,
hre: HardhatRuntimeEnvironment
) => {
const rankifyContract = new hre.ethers.Contract(
rankifyDeployment.address,
rankifyDeployment.abi,
hre.ethers.provider.getSigner(owner),
) as Rankify;
const tx = await rankifyContract
.mint(gameCreator, hre.ethers.utils.parseEther('1000'));
};
const gamePrice = await gameInstance.estimateGamePrice(minGameTime);
console.log('Estimated game price:', ethers.utils.formatEther(gamePrice), 'tokens');

export const approveTokensUse = async (
rankifyDeployment: any,
rankifyInstanceDeployment: any,
gameCreator: string,
hre: HardhatRuntimeEnvironment
) => {
const rankifyContract = new hre.ethers.Contract(
rankifyDeployment.address,
rankifyDeployment.abi,
hre.ethers.provider.getSigner(gameCreator),
) as Rankify;
// Mint tokens if needed
const rankifyDeployment = await hre.deployments.get('Rankify');
const rankifyContract = new hre.ethers.Contract(
rankifyDeployment.address,
rankifyDeployment.abi,
await hre.ethers.getSigner(owner),
) as Rankify;

const tx = await rankifyContract
.approve(rankifyInstanceDeployment.address, hre.ethers.constants.MaxUint256);
};
const gameCreatorBalance = await rankifyContract.balanceOf(gameCreatorSigner.address);
if (gameCreatorBalance.lt(gamePrice)) {
console.log('Minting tokens for game creator...');
const tx = await rankifyContract.mint(gameCreatorSigner.address, gamePrice);
await tx.wait();
}

export const createGame = async (
rankifyInstanceDeployment: any,
gameCreator: string,
registrar: string,
hre: HardhatRuntimeEnvironment
) => {
const rankifyInstanceContract = new hre.ethers.Contract(
rankifyInstanceDeployment.address,
rankifyInstanceDeployment.abi,
hre.ethers.provider.getSigner(gameCreator),
) as RankifyDiamondInstance;
// Approve tokens
const allowance = await rankifyContract.allowance(gameCreatorSigner.address, rankifyInstanceAddress);
if (allowance.lt(gamePrice)) {
console.log('Approving tokens for game instance...');
const approveTx = await rankifyContract
.connect(gameCreatorSigner)
.approve(rankifyInstanceAddress, ethers.constants.MaxUint256);
await approveTx.wait();
}

const tx = await rankifyInstanceContract
['createGame(address,uint256)'](registrar, 1);
// Create game
const params: IRankifyInstance.NewGameParamsInputStruct = {
gameMaster: gameMaster || defaultGameMaster,
gameRank: gameRank,
maxPlayerCnt: maxPlayerCnt,
minPlayerCnt: minPlayerCnt,
minGameTime: minGameTime,
nTurns: nTurns,
timePerTurn: timePerTurn,
voteCredits: voteCredits,
timeToJoin: timeToJoin,
};

const gameId = await rankifyInstanceContract
.getContractState()
.then((state: IRankifyInstanceCommons.RInstanceStateStructOutput) => state.BestOfState.numGames);
console.log('Creating game with params:', params);
const tx = await gameInstance.createGame(params);
const receipt = await tx.wait();

const gameId = await gameInstance.getContractState().then(state => state.numGames);
console.log('Game with id ' + gameId.toNumber() + ' created!');
return { gameId: gameId, receipt };
},
);

console.log('Game with id ' + gameId.toNumber() + ' created!');
};
export default {};
15 changes: 14 additions & 1 deletion playbook/createSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,18 @@ task('createSubject', 'Creates a new subject with MAO distribution')
console.log('Token Symbol:', taskArgs.tokenSymbol);
console.log('instances created', parsedLog.args.instances);
console.log('instance id', parsedLog.args.newInstanceId);
return { instances, newInstanceId: parsedLog.args.newInstanceId, receipt };
return {
instances,
newInstanceId: parsedLog.args.newInstanceId,
receipt,
instancesParsed: {
rankToken: parsedLog.args.instances[12],
rankifyInstance: parsedLog.args.instances[3],
daoInstance: parsedLog.args.instances[0],
govToken: parsedLog.args.instances[1],
govTokenAccessManager: parsedLog.args.instances[2],
acidInstance: parsedLog.args.instances[3],
acidAccessManager: parsedLog.args.instances[11],
},
};
});
3 changes: 3 additions & 0 deletions playbook/utils/deploy-to-local-anvil.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#! /bin/bash

# Uncomment playbook import in hardhat.config.ts
sed -i '' 's|// import '\''./playbook'\''|import '\''./playbook'\''|' ./hardhat.config.ts

rm -rf ./deployments/localhost
export NODE_ENV=TEST
pnpm hardhat deploy --tags MAO,rankify --network localhost

0 comments on commit 27e1c1a

Please sign in to comment.