-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fixed createGame pbook, adjusted config and creating subject to support this. * changeset * uncomment playbooks from config during playbook launch
- Loading branch information
Showing
5 changed files
with
123 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'rankify-contracts': minor | ||
--- | ||
|
||
fixed createGame playbook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |