Skip to content

Commit

Permalink
fixup! feat: upgrade dice and coinflip to retrieve multiple player re…
Browse files Browse the repository at this point in the history
…sults
  • Loading branch information
RasenGUY committed Dec 6, 2023
1 parent ec8cd31 commit 8b26606
Show file tree
Hide file tree
Showing 64 changed files with 25,527 additions and 4,488 deletions.

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions contracts/FeeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import "./IFeeManager.sol";
import "./utils/Distribution.sol";
import "./games/IWega.sol";

import 'hardhat/console.sol';

/**
* @title GameController (MVP)
* @author @RasenGUY @Daosourced.
Expand All @@ -26,7 +24,7 @@ contract FeeManager is WegaProtocolAdminRole, IFeeManager, IFeeManagerEvents, UU
using Distribution for uint256;

/**@notice fee applier to feeRule */
mapping(address => FeeConfig) public _feeRules;
mapping(address => FeeConfig) internal _feeRules;

function initialize() public initializer {
__UUPSUpgradeable_init();
Expand Down
2 changes: 1 addition & 1 deletion contracts/IFeeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface IFeeManager {
* @param applier address that will apply the fee
* @param transferAmount transfaction amount of transfer actiuon
*/
function calculateFeesForTransfer(address applier, uint256 transferAmount) external returns (address feeTaker, uint256 feeAmount, uint256 sendAmount);
function calculateFeesForTransfer(address applier, uint256 transferAmount) external view returns (address feeTaker, uint256 feeAmount, uint256 sendAmount);
/**
* @notice allows an admin to set fee rules
* @param configs fee rules to be applied
Expand Down
8 changes: 4 additions & 4 deletions contracts/IWegaGameController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ interface IWegaGameController {
function getGame(bytes32 escrowHash) external returns(IWega.Wega memory);

/**
* @notice returns game result for player of game
* @notice returns game result for players of a game
* @param game name of the game
* @param escrowHash id of the escrow
* @param player player to retrieve results of
* @param players players to retrieve results of
*/
function gameResults(
string memory game,
bytes32 escrowHash,
address player
) external returns(uint256[] memory);
address[] memory players
) external returns(uint256[][] memory);

/**
* @notice returns player points for a game
Expand Down
10 changes: 5 additions & 5 deletions contracts/WegaGameController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ contract WegaGameController is
function __WegaController_init(
address erc20EscrowAddress,
GameSettings[] memory gameSettings
) public onlyInitializing {
) internal onlyInitializing {
__WegaController_init_unchained(erc20EscrowAddress, gameSettings);
}

function __WegaController_init_unchained(
address erc20EscrowAddress,
GameSettings[] memory gameSettings
) public onlyInitializing {
) internal onlyInitializing {
erc20Escrow = IWegaERC20Escrow(erc20EscrowAddress);
for(uint256 i = 0; i < gameSettings.length; i++) {
_registeredGames[gameSettings[i].name.keyHash()] = gameSettings[i].proxy;
Expand Down Expand Up @@ -206,9 +206,9 @@ contract WegaGameController is
function gameResults(
string memory game,
bytes32 escrowHash,
address player
) external view override returns(uint256[] memory) {
return IWega(getGameSettings(game).proxy).playerResults(escrowHash, player);
address[] memory players_
) external view override returns(uint256[][] memory) {
return IWega(getGameSettings(game).proxy).multiplePlayersResults(escrowHash, players_);
}

function playerScore(
Expand Down
13 changes: 13 additions & 0 deletions contracts/escrow/IWegaERC20Escrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,17 @@ interface IWegaERC20Escrow {
* @param feeManager address of the fee manager
*/
function setFeeManager(address feeManager) external;

/**
* @notice allows a winner to withdraw from a completed game
* @param escrowHash allows a winner to withdraw from a completed game
*/
function withdraw(bytes32 escrowHash) external;

/**
* @notice retrieves the current escrow balance for of a account
* @param escrowHash the current escrow balance for of a account
* @param account the current escrow balance for of a account
*/
function getClaimAmount(bytes32 escrowHash, address account) external view returns (uint256 feeAmount, uint256 claimAmount);
}
33 changes: 25 additions & 8 deletions contracts/escrow/WegaERC20Escrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ contract WegaERC20Escrow is
mapping(bytes32 => uint256) private _escrowBalances;

// request accountBalances
mapping(address => uint256) private _accountBalances;
mapping(address => mapping(bytes32 => uint256)) private _accountBalances;

// stores all the transfer Ids, will be used enumeration
EnumerableSet.Bytes32Set private _escrowHashes;
Expand Down Expand Up @@ -96,9 +96,9 @@ contract WegaERC20Escrow is
GAME_CONTROLLER_ROLE = keccak256('GAME_CONTROLLER_ROLE');
_setRoleAdmin(GAME_CONTROLLER_ROLE, WEGA_PROTOCOL_ADMIN_ROLE);
NAME = 'Wega ERC20 Escrow Service';
VERSION = '0.0.0';
VERSION = '0.1.0';
TYPE = "TOKEN-ERC20";
APPLY_FEES = false;
APPLY_FEES = true;
}

function toggleFees() external onlyWegaProtocolAdmin {
Expand Down Expand Up @@ -271,19 +271,36 @@ contract WegaERC20Escrow is
uint256 withdrawableAmount = _wagerRequests[escrowHash].totalWager.mulDiv(1, winners_.length);
for (uint256 i = 0; i < winners_.length; i++) {
require(containsPlayer(escrowHash, winners_[i]), INVALID_REQUEST_DATA);
_accountBalances[winners_[i]] = withdrawableAmount;
_accountBalances[winners_[i]][escrowHash] = withdrawableAmount;
}
_wagerRequests[escrowHash].state = TransactionState.READY;
emit SetWithdrawers(escrowHash, winners_);
}

function withdraw(bytes32 escrowHash) public nonReentrant {
function getClaimAmount(bytes32 escrowHash, address account) public view override returns (uint256 feeAmount, uint256 claimAmount) {
if(APPLY_FEES && _feeManager.shouldApplyFees(address(this))){
(
,
feeAmount,
claimAmount
) = _feeManager.calculateFeesForTransfer(address(this), _getTotalBalance(escrowHash, account));
} else {
feeAmount = 0;
claimAmount = _getTotalBalance(escrowHash, account);
}
}

function _getTotalBalance(bytes32 escrowHash, address account) internal view returns (uint256) {
return _accountBalances[account][escrowHash];
}

function withdraw(bytes32 escrowHash) public override nonReentrant {
ERC20WagerRequest memory request = _wagerRequests[escrowHash];
require(request.state == TransactionState.READY, INVALID_REQUEST_STATE);
require(_accountBalances[_msgSender()] > 0 ether, INVALID_WITHDRAW_BALANCE);
uint256 transferAmount = _accountBalances[_msgSender()];
require(_accountBalances[_msgSender()][escrowHash] > 0 ether, INVALID_WITHDRAW_BALANCE);
uint256 transferAmount = _getTotalBalance(escrowHash, _msgSender());
_escrowBalances[escrowHash] -= transferAmount;
delete _accountBalances[_msgSender()];
delete _accountBalances[_msgSender()][escrowHash];
_wagerRequests[escrowHash].state = TransactionState.CLOSED;
if(APPLY_FEES && _feeManager.shouldApplyFees(address(this))) {
(
Expand Down
87 changes: 67 additions & 20 deletions deployed-addresses.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
{
"version": "0.0.0",
"networks": {
"137": {
"contracts": {
"WegaERC20Escrow": {
"address": "0x1fFd5291E09264174d9956187cf8eFAE01E34875",
"legacyAddresses": [
"0x2EE1d66d4c62a8c648f36fC8Faa7f8454F470520",
"0x65BdAB5722E4f5F7c377d73d5c0073Bb0671597d"
],
"deploymentBlock": null,
"implementation": "0xed4a0cb18aF2D6b022F8f99Cf09b91aa06A9513d"
},
"WegaERC20Dummy": {
"address": "0x0000000000000000000000000000000000000000"
},
"WegaGameController": {
"address": "0x15aeA3c65Cec3F7525e611fd12D6476439aF789b",
"legacyAddresses": [],
"deploymentBlock": null,
"implementation": "0x721059fe3dd1c18d1926Eb36Ed792008339e4Ba9"
},
"WegaRandomizerController": {
"address": "0x724611c7dEB9a9D753aDDFF2f305c902FCbA02C8",
"legacyAddresses": [],
"deploymentBlock": null,
"implementation": "0x07A43df8233CD97fC8A3f7b6B75Ef68665478eb3"
},
"WegaDiceGame": {
"address": "0x8E134B1a9fb393186db62321F334C116f1276E1E",
"legacyAddresses": [],
"deploymentBlock": null,
"implementation": "0xd3bA7599b274c3Eb51a04569e2f2762F76f49FDd"
},
"WegaCoinFlipGame": {
"address": "0x2101c8958Dd5461f274168BD8bF7f810fbBf51eC",
"legacyAddresses": [],
"deploymentBlock": null,
"implementation": "0x720736cd88D57aF277A62495037Ce51788681B71"
},
"FeeManager": {
"address": "0x679e91BAb4fA83c5d0543c460A41CEc52E6A6fd1",
"legacyAddresses": [],
"deploymentBlock": null,
"implementation": "0xfB244ac04620695e40bc165e99C1C1381B797DA8"
},
"WegaRandomizer": {
"address": "0x0000000000000000000000000000000000000000"
}
}
},
"1337": {
"contracts": {
"WegaERC20Escrow": {
Expand Down Expand Up @@ -67,51 +116,55 @@
"80001": {
"contracts": {
"WegaERC20Escrow": {
"address": "0xCEBc6b24362cA2F4fA625d340BeF8Af4F91DB5bB",
"legacyAddresses": [],
"deploymentBlock": "263113a",
"implementation": "0x0993a07880aD59E02e72E745dC5b623409Ad24af"
"address": "0x4B4e9126b1701385dc5b0D02b12360635D57F12C",
"legacyAddresses": [
"0xB08555B946a2fcef22074a9115250a6666A3b901",
"0x0993a07880aD59E02e72E745dC5b623409Ad24af"
],
"deploymentBlock": null,
"implementation": "0x03968f1ADAf4C2Ba345DB98b798250cf4cd6fE17"
},
"WegaERC20Dummy": {
"address": "0x802361100a4772847DBc29BC59F76FD1F186E002",
"address": "0x1CdB8176Ad8579aE748703f40864BB2E65e68529",
"legacyAddresses": [],
"deploymentBlock": "263112e"
},
"WegaGameController": {
"address": "0xe4da6EE02D91b7C03DB000B9295A815D6BEeC4ca",
"address": "0x39e851282C11AefE20B149F4509702B2DC8aEB73",
"legacyAddresses": [
"0x21A8Eda1B590268410e520B70635467720B41418",
"0x760D87Ce891F301d19F608044D42356De2D50D14",
"0x740e1416aE865E2bff8f95aa5776F5Fa0a3706e7",
"0x02D7EF0f678BCd9323f2FA88e7197528D113f24a",
"0x0000000000000000000000000000000000000000"
],
"deploymentBlock": "272c37b",
"implementation": "0x21A8Eda1B590268410e520B70635467720B41418"
"deploymentBlock": null,
"implementation": "0x6215002028eA5Ec1F89D5bc5D553ce83Fed6Cd1b"
},
"WegaRandomizerController": {
"address": "0xcd44090c1BC8DC05298540488ca6FA058d27Ac70",
"address": "0xC757438A9eF34020FEBB7E25336e4eC20dE55fD1",
"legacyAddresses": [],
"implementation": "0x24F9E5E74976661E3784E258986Ee9ee6AAe8132",
"deploymentBlock": null
},
"WegaDiceGame": {
"address": "0x5581147651B29D44d4eD95099F1a53aAE052f797",
"address": "0xd806555f94769bEeAF1d60858f3963C095434aA1",
"legacyAddresses": [
"0x4f2Fe30D48961581d65C0a486AE5e83A9b8356b0"
],
"deploymentBlock": null,
"implementation": "0x6c55167230Fb4abD7258755f5c3a1e50ff4321dD"
"implementation": "0x3C119E7148071Ea0e7B2abBa63eae2f097a4cEdf"
},
"WegaCoinFlipGame": {
"address": "0x24133b29067AC3858B4F58112bCAC243941538c8",
"address": "0x57aB2987CBa91f08Db532c57Cfa7478cd5D009b2",
"legacyAddresses": [
"0x0C438E3C3B7b629044573e7cc3B0FBe55DB1b8b6"
],
"deploymentBlock": null,
"implementation": "0x9d31BD6EABAaa5Bf0aCceF23c834E870d2e194B4"
"implementation": "0x1fC17Ca1b3e702E0D93D3BA5FEA2573079e60Af0"
},
"FeeManager": {
"address": "0xe2cDC641b8AF26a208402a3e218EE022E954Afd0",
"address": "0xc392D90803D462Ac5395A76d59AB942a8d9DCe69",
"legacyAddresses": [],
"implementation": "0x30D1C9b043014820cC878ea023bd9Ae53238B395",
"deploymentBlock": null
Expand All @@ -124,12 +177,6 @@
"legacyAddresses": [],
"deploymentBlock": "25e6a43",
"implementation": "0x945Fbb4138040e2ab766CB59A7Aa6D882AfBCcF4"
},
"WegaRandomNumberController": {
"address": "0xaBC8837A5756aF9a915a8cC77b80b06c1997e17e",
"legacyAddresses": [],
"deploymentBlock": "2631148",
"implementation": "0xCC6e334201974DF201EBb4bd84b97ac232fE7380"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ const config: HardhatUserConfig = {
},
polygon: {
url: process.env.POLYGON_RPC_URL,
chainId: 134,
chainId: 137,
accounts: process.env.POLYGON_DEPLOYER_PRIVATE_KEY ? [
process.env.POLYGON_DEPLOYER_PRIVATE_KEY as string,
process.env.POLYGON_PROTOCOL_ADMIN_PRIVATE_KEY as string
] : undefined,
loggingEnabled: true,
blockGasLimit: 268435455,
blockGasLimit: 44000000000
},
skaleCalypsoTestnet: {
url: process.env.SKALE_CALYPSO_RPC_URL,
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,20 @@
"deploy:mumbai:coinflip-game": "hardhat run --network mumbai scripts/deploy_coinflip-game.ts",
"deploy:mumbai:game-controller": "hardhat run --network mumbai scripts/deploy_game-controller.ts",
"deploy:mumbai:fee-manager": "hardhat run --network mumbai scripts/deploy_fee-manager.ts",
"deploy:polygon:fee-manager": "hardhat run --network polygon scripts/deploy_fee-manager.ts",
"deploy:mumbai:full": "hardhat run --network mumbai scripts/deploy_full.ts && UI_PATH=../wega-platform-app yarn run exp:demo-ui",
"deploy:polygon:full": "hardhat run --network polygon scripts/deploy_full.ts && UI_PATH=../wega-platform-app yarn run exp:demo-ui",
"upgrade:localhost:contract": "hardhat run --network localhost scripts/upgrade_contract.ts",
"upgrade:localhost:game-controller": "hardhat run --network localhost scripts/upgrade_game-controller.ts",
"upgrade:mumbai:contract": "hardhat run --network mumbai scripts/upgrade_contract.ts",
"upgrade:polygon:contract": "hardhat run --network polygon scripts/upgrade_contract.ts",
"verify:mumbai:contract": "hardhat run --network mumbai scripts/verify_contract.ts",
"verify:polygon:contract": "hardhat run --network polygon scripts/verify_contract.ts",
"upgrade:mumbai:game-controller": "hardhat run --network mumbai scripts/upgrade_game-controller.ts",
"configure:mumbai": "hardhat run --network mumbai scripts/configure_protocol.ts",
"configure:polygon": "hardhat run --network polygon scripts/configure_protocol.ts",
"configure-fees:mumbai": "hardhat run --network mumbai scripts/set_fee-rules.ts",
"configure-fees:polygon": "hardhat run --network polygon scripts/set_fee-rules.ts",
"exp:demo-ui": "rm -rf $UI_PATH/deployed-addresses.json && cp -rf deployed-addresses.json $UI_PATH",
"start:node": "hardhat node",
"drand": "ts-node ./scripts/crawl-random-numbers.ts",
Expand Down
5 changes: 3 additions & 2 deletions scripts/configure_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ async function main () {
}
const deployer = await Deployer.create();
const drandIndexesToAdd = 1000;
const initialDrands = randomNumConfig[chainId].drands.map(({ randomness }) => toBigInt(randomness)).slice(0, 101);
const drands = randomNumConfig[chainId].drands.map(({ randomness }) => toBigInt(randomness)).slice(101, drandIndexesToAdd + 1);
const initialDrands = randomNumConfig[chainId].drands.map(({ randomness }) => toBigInt(randomness)).slice(0, 1100);
const drands = randomNumConfig[chainId].drands.map(({ randomness }) => toBigInt(randomness)).slice(1101, 2101);
console.log('these are the drands', initialDrands, drands)
deployer.log('Network:', network.name);


Expand Down
2 changes: 1 addition & 1 deletion scripts/deploy_full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function main() {
escrow: ['Wega ERC20 Escrow Service', '0.0.0'],
initialDrands,
drands,
feeTaker: chainId == 1337 ? '0xBcd4042DE499D14e55001CcbB24a551F3b954096' : chainId === 80001 ? "0x011dF297Da65Cb87a9a8878fF4C8F7d0D3814314" : '0x13C38b2bd4cF15985a1505fc4FAA65aE2AdE45A5',
feeTaker: chainId == 1337 ? '0xBcd4042DE499D14e55001CcbB24a551F3b954096' : chainId === 137 ? '0x13C38b2bd4cF15985a1505fc4FAA65aE2AdE45A5' : "0x011dF297Da65Cb87a9a8878fF4C8F7d0D3814314",
});
mergeNetworkConfig(deployConfig);
mergeRandomNumConfig({[chainId]: {
Expand Down
8 changes: 2 additions & 6 deletions scripts/upgrade_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ async function main () {
}
await deployer.execute(['upgrade_contract'], config, [
{
artifactName: ArtifactName.WegaDiceGame,
contractName: ContractName.WegaDiceGame,
},
{
artifactName: ArtifactName.WegaCoinFlipGame,
contractName: ContractName.WegaCoinFlipGame,
artifactName: ArtifactName.WegaERC20Escrow,
contractName: ContractName.WegaERC20Escrow,
},
]);
deployer.log('Upgrade: Contract', network.name);
Expand Down
8 changes: 2 additions & 6 deletions scripts/verify_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ async function main () {
}
await deployer.execute(['verify_contract'], config, [
{
artifactName: ArtifactName.WegaDiceGame,
contractName: ContractName.WegaDiceGame,
},
{
artifactName: ArtifactName.WegaCoinFlipGame,
contractName: ContractName.WegaCoinFlipGame,
artifactName: ArtifactName.WegaERC20Escrow,
contractName: ContractName.WegaERC20Escrow,
},
]);
deployer.log('Verify: Contract', network.name);
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const drandConfigPath = './.random-numbers'

export function getConfig (): DeployedContractsConfig {
const file = fs.existsSync(configPath) ? fs.readFileSync(configPath).toString() : '{}';
return JSON.parse(file.length ? file : '{ "version": "0.0.0 }');
return JSON.parse(file.length ? file : '{ "version": "0.1.0 }');
}

export function getNetworkConfig (chainId: number): DeployedContractList {
Expand Down
Loading

0 comments on commit 8b26606

Please sign in to comment.