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

[High Roller] Get Contract Address from apps and calculate rolls accurately #24

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 packages/dapp-high-roller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"test:e2e": "echo 'E2E tests are disabled, skipping'"
},
"dependencies": {
"@counterfactual/apps": "0.1.12",
"@counterfactual/cf.js": "0.2.6",
"@counterfactual/types": "0.0.38",
"@counterfactual/typescript-typings": "0.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import HighRollerUITunnel from "../../data/high-roller";
import { AppInstance } from "../../data/mock-app-instance";
import MockNodeProvider from "../../data/mock-node-provider";
import { cf, HighRollerUIMutableState, Node } from "../../data/types";
import { getHighRollerContractAddress } from "../../utils/utils";

declare var cf;
declare var ethers;
Expand Down Expand Up @@ -79,11 +80,10 @@ export class AppProvider {
this.cfProvider.on("chan_uninstall", this.onUninstall.bind(this));
this.cfProvider.on("chan_installVirtual", this.onInstall.bind(this));

const highRollerAppDefinitionAddr =
"0x144F1A5C2db59B58f2c73d09A2acb27a57E47618";
const contractAddress = getHighRollerContractAddress();

this.appFactory = new cf.AppFactory(
// TODO: This probably should be in a configuration, somewhere.
highRollerAppDefinitionAddr,
contractAddress,
{
actionEncoding:
"tuple(uint8 actionType, uint256 number, bytes32 actionHash)",
Expand Down
46 changes: 20 additions & 26 deletions packages/dapp-high-roller/src/components/app-root/app-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
HighRollerUIMutableState,
HighRollerUIState
} from "../../data/types";
import { getHighRollerContractAddress } from "../../utils/utils";

declare var ethers;
declare var web3;
Expand Down Expand Up @@ -193,47 +194,40 @@ export class AppRoot {
// The Contract interface
const abi = [
`
function highRoller(bytes32 randomness)
public
function cutBytes32(bytes32 h)
internal
pure
returns(uint8 playerFirstTotal, uint8 playerSecondTotal)
`
returns (bytes8 q1, bytes8 q2, bytes8 q3, bytes8 q4)
`,
`
function bytes8toDiceRoll(bytes8 q)
internal
pure
returns (uint8)
`
];

// Connect to the network
const provider = new ethers.providers.Web3Provider(web3.currentProvider);

const contractAddress = "0x144F1A5C2db59B58f2c73d09A2acb27a57E47618";
const contractAddress = getHighRollerContractAddress();

// We connect to the Contract using a Provider, so we will only
// have read-only access to the Contract
const contract = new ethers.Contract(contractAddress, abi, provider);

const result = await contract.highRoller(randomness);
const {
playerFirstRollOne,
playerFirstRollTwo,
playerSecondRollOne,
playerSecondRollTwo
} = await contract.getPlayerRolls(randomness);

return {
playerFirstRoll: this.getDieNumbers(result[0]),
playerSecondRoll: this.getDieNumbers(result[1])
playerFirstRoll: [playerFirstRollOne, playerFirstRollTwo],
playerSecondRoll: [playerSecondRollOne, playerSecondRollTwo]
};
}

getDieNumbers(totalSum: number): [number, number] {
// Choose result for each die.
if (totalSum === 12) {
return [6, 6];
}

if (totalSum > 2 && totalSum < 12) {
return [Math.floor(totalSum / 2), Math.ceil(totalSum / 2)];
}

if (totalSum > 2 && totalSum % 2 === 0) {
return [Math.floor(totalSum / 2) - 1, Math.ceil(totalSum / 2) + 1];
}

return [totalSum / 2, totalSum / 2];
}

goToGame(history: RouterHistory, isProposing: boolean = true) {
history.push({
pathname: "/game",
Expand Down
44 changes: 43 additions & 1 deletion packages/dapp-high-roller/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// @ts-ignore - no idea why this causes an error...
import RopstenContracts from "@counterfactual/apps/networks/3.json";
// @ts-ignore - no idea why this causes an error...
import KovanContracts from "@counterfactual/apps/networks/42.json";

declare var ethereum;
declare var ethers;

function getProp(propertyName: string, context: { [key: string]: any }) {
Expand All @@ -23,4 +29,40 @@ function generateSalt() {
return ethers.utils.bigNumberify(ethers.utils.randomBytes(32)).toHexString();
}

export { getProp, computeCommitHash, generateSalt };
function getHighRollerContractAddress(): string {
const networkVersion =
ethereum && ethereum.networkVersion ? ethereum.networkVersion : "42";
const contractName = "HighRollerApp";
switch (networkVersion) {
case "3":
return getAddressFromNetwork(RopstenContracts, contractName);
case "42":
return getAddressFromNetwork(KovanContracts, contractName);
default:
throw new Error(
`The App has not been deployed to network ID ${networkVersion}`
);
}
}

function getAddressFromNetwork(
migrations: { address: string; contractName: string }[],
contractName: string
): string {
const appContract = migrations.find(
migration => migration.contractName === contractName
);
if (!appContract) {
throw new Error(
`No contract address found for contractName: ${contractName}`
);
}
return appContract.address;
}

export {
getProp,
computeCommitHash,
generateSalt,
getHighRollerContractAddress
};
4 changes: 2 additions & 2 deletions packages/dapp-tic-tac-toe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"node": "10.15.3"
},
"dependencies": {
"@counterfactual/apps": "0.1.9",
"@counterfactual/apps": "0.1.12",
"@counterfactual/cf.js": "0.2.6",
"@counterfactual/types": "0.0.38",
"@counterfactual/typescript-typings": "0.1.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.0",
"react-scripts": "3.0.1"
"react-scripts": "3.1.1"
},
"scripts": {
"start": "SKIP_PREFLIGHT_CHECK=true react-scripts start",
Expand Down
11 changes: 5 additions & 6 deletions packages/dapp-tic-tac-toe/src/Wager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import React, { Component } from "react";
import { ReactComponent as Logo } from "./assets/images/logo.svg";
import Waiting from "./Waiting";

const ethereum = window["ethereum"];

class Wager extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -69,9 +71,8 @@ class Wager extends Component {

createAppFactory() {
let contractAddress;
// const networkVersion = window["web3"].currentProvider.networkVersion;
// FIXME: hard-coding to use Kovan until we fix this
const networkVersion = "42";
const networkVersion =
ethereum && ethereum.networkVersion ? ethereum.networkVersion : "42";
const contractName = "TicTacToeApp";
switch (networkVersion) {
case "3":
Expand Down Expand Up @@ -111,9 +112,7 @@ class Wager extends Component {

if (currentEthBalance.lt(bet)) {
this.setState({
error: `Insufficient funds: You need at least ${
this.props.gameInfo.betAmount
} ETH to play.`
error: `Insufficient funds: You need at least ${this.props.gameInfo.betAmount} ETH to play.`
});
return;
}
Expand Down
Loading