Skip to content
This repository has been archived by the owner on Dec 13, 2019. It is now read-only.

Commit

Permalink
[contract] Interpreters
Browse files Browse the repository at this point in the history
  • Loading branch information
ldct committed Apr 25, 2019
1 parent 4084510 commit f6cef5e
Show file tree
Hide file tree
Showing 83 changed files with 704 additions and 1,239 deletions.
46 changes: 13 additions & 33 deletions packages/apps/contracts/HighRollerApp.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pragma solidity 0.5.7;
pragma experimental "ABIEncoderV2";

import "@counterfactual/contracts/contracts/CounterfactualApp.sol";
import "@counterfactual/contracts/contracts/interfaces/CounterfactualApp.sol";
import "@counterfactual/contracts/contracts/interpreters/TwoPartyEthAsLump.sol";


/// @title High Roller App
Expand All @@ -10,7 +11,7 @@ import "@counterfactual/contracts/contracts/CounterfactualApp.sol";
/// The winner is the player whose sum of dice outcomes is highest.
/// @dev This contract is an example of a dApp built to run on
/// the CounterFactual framework
contract HighRollerApp is CounterfactualApp {
contract HighRollerApp {

enum ActionType {
START_GAME,
Expand All @@ -33,7 +34,6 @@ contract HighRollerApp is CounterfactualApp {
}

struct AppState {
address[2] playerAddrs;
Stage stage;
bytes32 salt;
bytes32 commitHash;
Expand Down Expand Up @@ -120,59 +120,39 @@ contract HighRollerApp is CounterfactualApp {
return abi.encode(nextState);
}

function resolve(bytes calldata encodedState, Transfer.Terms calldata terms)
function resolve(bytes calldata encodedState)
external
pure
returns (Transfer.Transaction memory)
returns (TwoPartyEthAsLump.Resolution)
{
AppState memory appState = abi.decode(encodedState, (AppState));

uint256[] memory amounts = new uint256[](2);
address[] memory to = new address[](2);
to[0] = appState.playerAddrs[0];
to[1] = appState.playerAddrs[1];
bytes32 expectedCommitHash = keccak256(
abi.encodePacked(appState.salt, appState.playerFirstNumber)
);
if (expectedCommitHash == appState.commitHash) {
amounts = getWinningAmounts(
appState.playerFirstNumber, appState.playerSecondNumber, terms.limit
return getWinningAmounts(
appState.playerFirstNumber, appState.playerSecondNumber
);
} else {
amounts[0] = 0;
amounts[1] = terms.limit;
return TwoPartyEthAsLump.Resolution.SEND_TO_ADDR_TWO;
}

bytes[] memory data = new bytes[](2);

return Transfer.Transaction(
terms.assetType,
terms.token,
to,
amounts,
data
);
}

function getWinningAmounts(uint256 num1, uint256 num2, uint256 termsLimit)
function getWinningAmounts(uint256 num1, uint256 num2)
public
pure
returns (uint256[] memory)
returns (TwoPartyEthAsLump.Resolution)
{
uint256[] memory amounts = new uint256[](2);
bytes32 randomSalt = calculateRandomSalt(num1, num2);
(uint8 playerFirstTotal, uint8 playerSecondTotal) = highRoller(randomSalt);
if (playerFirstTotal > playerSecondTotal) {
amounts[0] = termsLimit;
amounts[1] = 0;
return TwoPartyEthAsLump.Resolution.SEND_TO_ADDR_ONE;
} else if (playerFirstTotal < playerSecondTotal) {
amounts[0] = 0;
amounts[1] = termsLimit;
return TwoPartyEthAsLump.Resolution.SEND_TO_ADDR_TWO;
} else {
amounts[0] = termsLimit / 2;
amounts[1] = termsLimit / 2;
return TwoPartyEthAsLump.Resolution.SPLIT_AND_SEND_TO_BOTH_ADDRS;
}
return amounts;
}

function highRoller(bytes32 randomness)
Expand Down
37 changes: 11 additions & 26 deletions packages/apps/contracts/NimApp.sol
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
pragma solidity 0.5.7;
pragma experimental "ABIEncoderV2";

import "@counterfactual/contracts/contracts/CounterfactualApp.sol";
import "@counterfactual/contracts/contracts/interfaces/CounterfactualApp.sol";
import "@counterfactual/contracts/contracts/interpreters/TwoPartyEthAsLump.sol";


/*
Normal-form Nim
https://en.wikipedia.org/wiki/Nim
*/
contract NimApp is CounterfactualApp {
contract NimApp {

struct Action {
uint256 pileIdx;
uint256 takeAmnt;
}

struct AppState {
address[2] players;
uint256 turnNum;
uint256[3] pileHeights;
}
Expand Down Expand Up @@ -64,35 +64,20 @@ contract NimApp is CounterfactualApp {
return abi.encode(ret);
}

function resolve(
bytes calldata encodedState, Transfer.Terms calldata terms
)
function resolve(bytes calldata encodedState)
external
pure
returns (Transfer.Transaction memory)
returns (TwoPartyEthAsLump.Resolution)
{
AppState memory state = abi.decode(encodedState, (AppState));

require(isWin(state), "Resolution state was not in a winning position");
address loser = state.players[state.turnNum % 2];
address winner = state.players[1 - (state.turnNum % 2)];

uint256[] memory amounts = new uint256[](2);
amounts[0] = terms.limit;
amounts[1] = 0;

address[] memory to = new address[](2);
to[0] = loser;
to[1] = winner;
bytes[] memory data = new bytes[](2);

return Transfer.Transaction(
terms.assetType,
terms.token,
to,
amounts,
data
);

if (state.turnNum % 2 == 0) {
return TwoPartyEthAsLump.Resolution.SEND_TO_ADDR_ONE;
} else {
return TwoPartyEthAsLump.Resolution.SEND_TO_ADDR_TWO;
}
}

function isWin(AppState memory state)
Expand Down
70 changes: 70 additions & 0 deletions packages/apps/contracts/PreimageApp.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
pragma solidity 0.5.7;
pragma experimental "ABIEncoderV2";

import "@counterfactual/contracts/contracts/interfaces/CounterfactualApp.sol";


contract PreimageApp {

struct AppState {
bytes32 image;
bool preimageRevealed;
}

struct Action {
bytes preimage;
}

function isStateTerminal(bytes memory encodedState)
public
pure
returns (bool)
{
AppState memory state = abi.decode(encodedState, (AppState));
return state.preimageRevealed;
}

function getTurnTaker(
bytes memory /* encodedState */, address[] memory signingKeys
)
public
pure
returns (address)
{
return signingKeys[0];
}

function applyAction(bytes memory encodedState, bytes memory encodedAction)
public
pure
returns (bytes memory)
{
AppState memory state = abi.decode(encodedState, (AppState));
Action memory action = abi.decode(encodedAction, (Action));

require(
!state.preimageRevealed,
"No actions possible after preimage revealed");

AppState memory nextState = state;

if (keccak256(action.preimage) == state.image) {
nextState.preimageRevealed = true;
}
return abi.encode(nextState);
}

function resolve(bytes memory encodedState)
public
pure
returns (bytes memory)
{
AppState memory state = abi.decode(encodedState, (AppState));

if (state.preimageRevealed) {
return abi.encode(1);
} else {
return abi.encode(0);
}
}
}
61 changes: 20 additions & 41 deletions packages/apps/contracts/TicTacToeApp.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
pragma solidity 0.5.7;
pragma experimental "ABIEncoderV2";

import "@counterfactual/contracts/contracts/CounterfactualApp.sol";
import "@counterfactual/contracts/contracts/interfaces/CounterfactualApp.sol";
import "@counterfactual/contracts/contracts/interpreters/TwoPartyEthAsLump.sol";


contract TicTacToeApp is CounterfactualApp {
contract TicTacToeApp {

enum ActionType {
PLAY,
Expand Down Expand Up @@ -37,7 +38,6 @@ contract TicTacToeApp is CounterfactualApp {
uint256 constant EMPTY_SQUARE = 0;

struct AppState {
address[2] players;
uint256 turnNum;
uint256 winner;
uint256[3][3] board;
Expand Down Expand Up @@ -102,50 +102,20 @@ contract TicTacToeApp is CounterfactualApp {
return abi.encode(postState);
}

function resolve(bytes calldata encodedState, Transfer.Terms calldata terms)
function resolve(bytes calldata encodedState)
external
pure
returns (Transfer.Transaction memory)
returns (bytes memory)
{
AppState memory state = abi.decode(encodedState, (AppState));
require(state.winner != 0, "Winner was set to 0; invalid");

uint256[] memory amounts = new uint256[](2);
address[] memory to = new address[](2);
bytes[] memory data = new bytes[](2);

if (state.winner == 3) {
amounts[0] = terms.limit / 2;
amounts[1] = terms.limit / 2;

to[0] = state.players[0];
to[1] = state.players[1];

return Transfer.Transaction(
terms.assetType,
terms.token,
to,
amounts,
data
);

} else {
address winner = state.players[state.winner - 1];
address loser = state.players[2 - state.winner];

amounts[0] = terms.limit;
amounts[1] = 0;

to[0] = winner;
to[1] = loser;

return Transfer.Transaction(
terms.assetType,
terms.token,
to,
amounts,
data
);
if (state.winner == 2) {
return abi.encode(TwoPartyEthAsLump.Resolution.SEND_TO_ADDR_TWO);
} else if (state.winner == 1) {
return abi.encode(TwoPartyEthAsLump.Resolution.SEND_TO_ADDR_ONE);
} else /* state.winner == 3, or fallback */ {
return abi.encode(TwoPartyEthAsLump.Resolution.SPLIT_AND_SEND_TO_BOTH_ADDRS);
}

}
Expand Down Expand Up @@ -223,4 +193,13 @@ contract TicTacToeApp is CounterfactualApp {
}
}

function resolveSelector()
external
pure
returns (bytes4)
{
return this.resolve.selector;
}


}
Loading

0 comments on commit f6cef5e

Please sign in to comment.