This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,095 additions
and
0 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,126 @@ | ||
pragma solidity 0.4.25; | ||
pragma experimental "ABIEncoderV2"; | ||
|
||
import "../libs/Transfer.sol"; | ||
|
||
|
||
contract CommitRevealApp { | ||
|
||
enum ActionType { | ||
SET_MAX, | ||
CHOOSE_NUMBER, | ||
GUESS_NUMBER, | ||
REVEAL_NUMBER | ||
} | ||
|
||
enum Stage { | ||
SETTING_MAX, CHOOSING, GUESSING, REVEALING, DONE | ||
} | ||
|
||
enum Player { | ||
CHOOSING, | ||
GUESSING | ||
} | ||
|
||
struct AppState { | ||
address[2] playerAddrs; | ||
Stage stage; | ||
uint256 maximum; | ||
uint256 guessedNumber; | ||
bytes32 commitHash; | ||
Player winner; | ||
} | ||
|
||
struct Action { | ||
ActionType actionType; | ||
uint256 number; | ||
bytes32 hash; | ||
} | ||
|
||
function isStateTerminal(AppState state) | ||
public | ||
pure | ||
returns (bool) | ||
{ | ||
return state.stage == Stage.DONE; | ||
} | ||
|
||
function getTurnTaker(AppState state) | ||
public | ||
pure | ||
returns (Player) | ||
{ | ||
if (state.stage == Stage.GUESSING) { | ||
return Player.GUESSING; | ||
} | ||
return Player.CHOOSING; | ||
} | ||
|
||
function applyAction(AppState state, Action action) | ||
public | ||
pure | ||
returns (bytes) | ||
{ | ||
AppState memory nextState = state; | ||
if (action.actionType == ActionType.SET_MAX) { | ||
require(state.stage == Stage.SETTING_MAX, "Cannot apply SET_MAX on SETTING_MAX"); | ||
nextState.stage = Stage.CHOOSING; | ||
|
||
nextState.maximum = action.number; | ||
} else if (action.actionType == ActionType.CHOOSE_NUMBER) { | ||
require(state.stage == Stage.CHOOSING, "Cannot apply CHOOSE_NUMBER on CHOOSING"); | ||
nextState.stage = Stage.GUESSING; | ||
|
||
nextState.commitHash = action.hash; | ||
} else if (action.actionType == ActionType.GUESS_NUMBER) { | ||
require(state.stage == Stage.GUESSING, "Cannot apply GUESS_NUMBER on GUESSING"); | ||
nextState.stage = Stage.REVEALING; | ||
|
||
require(action.number < state.maximum, "Action number was >= state.maximum"); | ||
nextState.guessedNumber = action.number; | ||
} else if (action.actionType == ActionType.REVEAL_NUMBER) { | ||
require(state.stage == Stage.REVEALING, "Cannot apply REVEAL_NUMBER on REVEALING"); | ||
nextState.stage = Stage.DONE; | ||
|
||
bytes32 salt = action.hash; | ||
uint256 chosenNumber = action.number; | ||
if (keccak256(abi.encodePacked(salt, chosenNumber)) == state.commitHash && state.guessedNumber != chosenNumber && chosenNumber < state.maximum) { | ||
nextState.winner = Player.CHOOSING; | ||
} else { | ||
nextState.winner = Player.GUESSING; | ||
} | ||
} else { | ||
revert("Invalid action type"); | ||
} | ||
return abi.encode(nextState); | ||
} | ||
|
||
function resolve(AppState state, Transfer.Terms terms) | ||
public | ||
pure | ||
returns (Transfer.Transaction) | ||
{ | ||
uint256[] memory amounts = new uint256[](1); | ||
amounts[0] = terms.limit; | ||
|
||
address[] memory to = new address[](1); | ||
uint256 player; | ||
if (state.stage == Stage.DONE) { | ||
player = uint256(state.winner); | ||
} else { | ||
// The player who is not the turn taker | ||
player = 1 - uint256(getTurnTaker(state)); | ||
} | ||
to[0] = state.playerAddrs[player]; | ||
|
||
bytes[] memory data = new bytes[](2); | ||
|
||
return Transfer.Transaction( | ||
terms.assetType, | ||
terms.token, | ||
to, | ||
amounts, | ||
data | ||
); | ||
} | ||
} |
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,98 @@ | ||
pragma solidity 0.4.25; | ||
pragma experimental "ABIEncoderV2"; | ||
|
||
import "../libs/Transfer.sol"; | ||
|
||
|
||
contract CountingApp { | ||
|
||
enum ActionTypes { INCREMENT, DECREMENT} | ||
|
||
struct Action { | ||
ActionTypes actionType; | ||
uint256 byHowMuch; | ||
} | ||
|
||
struct AppState { | ||
address player1; | ||
address player2; | ||
uint256 count; | ||
uint256 turnNum; | ||
} | ||
|
||
function isStateTerminal(AppState state) | ||
public | ||
pure | ||
returns (bool) | ||
{ | ||
return state.count == 2; | ||
} | ||
|
||
function getTurnTaker(AppState state) | ||
public | ||
pure | ||
returns (uint256) | ||
{ | ||
return state.turnNum % 2; | ||
} | ||
|
||
function resolve(AppState state, Transfer.Terms terms) | ||
public | ||
pure | ||
returns (Transfer.Transaction) | ||
{ | ||
uint256[] memory amounts = new uint256[](2); | ||
amounts[0] = terms.limit; | ||
amounts[1] = 0; | ||
|
||
address[] memory to = new address[](2); | ||
to[0] = state.player1; | ||
to[1] = state.player2; | ||
bytes[] memory data = new bytes[](2); | ||
|
||
return Transfer.Transaction( | ||
terms.assetType, | ||
terms.token, | ||
to, | ||
amounts, | ||
data | ||
); | ||
} | ||
|
||
function applyAction(AppState state, Action action) | ||
public | ||
pure | ||
returns (bytes) | ||
{ | ||
if (action.actionType == ActionTypes.INCREMENT) { | ||
return onIncrement(state, action); | ||
} else if (action.actionType == ActionTypes.DECREMENT) { | ||
return onDecrement(state, action); | ||
} else { | ||
revert("Invalid action type"); | ||
} | ||
} | ||
|
||
function onIncrement(AppState state, Action inc) | ||
public | ||
pure | ||
returns (bytes) | ||
{ | ||
AppState memory ret = state; | ||
state.count += inc.byHowMuch; | ||
state.turnNum += 1; | ||
return abi.encode(ret); | ||
} | ||
|
||
function onDecrement(AppState state, Action dec) | ||
public | ||
pure | ||
returns (bytes) | ||
{ | ||
AppState memory ret = state; | ||
state.count -= dec.byHowMuch; | ||
state.turnNum += 1; | ||
return abi.encode(ret); | ||
} | ||
|
||
} |
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,36 @@ | ||
pragma solidity 0.4.25; | ||
pragma experimental "ABIEncoderV2"; | ||
|
||
import "../libs/Transfer.sol"; | ||
|
||
|
||
contract ETHBalanceRefundApp { | ||
|
||
struct AppState { | ||
address recipient; | ||
address multisig; | ||
uint256 threshold; | ||
} | ||
|
||
function resolve(AppState state, Transfer.Terms terms) | ||
public | ||
view | ||
returns (Transfer.Transaction) | ||
{ | ||
uint256[] memory amounts = new uint256[](1); | ||
amounts[0] = address(state.multisig).balance - state.threshold; | ||
|
||
address[] memory to = new address[](1); | ||
to[0] = state.recipient; | ||
|
||
bytes[] memory data; | ||
|
||
return Transfer.Transaction( | ||
terms.assetType, | ||
terms.token, | ||
to, | ||
amounts, | ||
data | ||
); | ||
} | ||
} |
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,92 @@ | ||
pragma solidity 0.4.25; | ||
pragma experimental "ABIEncoderV2"; | ||
|
||
import "../libs/Transfer.sol"; | ||
|
||
|
||
/* | ||
Normal-form Nim | ||
https://en.wikipedia.org/wiki/Nim | ||
*/ | ||
contract NimApp { | ||
|
||
struct Action { | ||
uint256 pileIdx; | ||
uint256 takeAmnt; | ||
} | ||
|
||
struct AppState { | ||
address[2] players; | ||
uint256 turnNum; | ||
uint256[3] pileHeights; | ||
} | ||
|
||
function isStateTerminal(AppState state) | ||
public | ||
pure | ||
returns (bool) | ||
{ | ||
return isWin(state); | ||
} | ||
|
||
function getTurnTaker(AppState state) | ||
public | ||
pure | ||
returns (uint256) | ||
{ | ||
return state.turnNum % 2; | ||
} | ||
|
||
function applyAction(AppState state, Action action) | ||
public | ||
pure | ||
returns (bytes) | ||
{ | ||
require(action.pileIdx < 3, "pileIdx must be 0, 1 or 2"); | ||
require(state.pileHeights[action.pileIdx] >= action.takeAmnt, "invalid pileIdx"); | ||
|
||
AppState memory ret = state; | ||
|
||
ret.pileHeights[action.pileIdx] -= action.takeAmnt; | ||
ret.turnNum += 1; | ||
|
||
return abi.encode(ret); | ||
} | ||
|
||
function resolve(AppState state, Transfer.Terms terms) | ||
public | ||
pure | ||
returns (Transfer.Transaction) | ||
{ | ||
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 | ||
); | ||
} | ||
|
||
function isWin(AppState state) | ||
internal | ||
pure | ||
returns (bool) | ||
{ | ||
return ((state.pileHeights[0] == 0) && (state.pileHeights[1] == 0) && (state.pileHeights[2] == 0)); | ||
} | ||
|
||
|
||
} |
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,39 @@ | ||
pragma solidity 0.4.25; | ||
pragma experimental "ABIEncoderV2"; | ||
|
||
import "../libs/Transfer.sol"; | ||
|
||
|
||
contract PaymentApp { | ||
|
||
struct AppState { | ||
address alice; | ||
address bob; | ||
uint256 aliceBalance; | ||
uint256 bobBalance; | ||
} | ||
|
||
function resolve(AppState state, Transfer.Terms terms) | ||
public | ||
pure | ||
returns (Transfer.Transaction) | ||
{ | ||
uint256[] memory amounts = new uint256[](2); | ||
amounts[0] = state.aliceBalance; | ||
amounts[1] = state.bobBalance; | ||
|
||
address[] memory to = new address[](2); | ||
to[0] = state.alice; | ||
to[1] = state.bob; | ||
bytes[] memory data = new bytes[](2); | ||
|
||
return Transfer.Transaction( | ||
terms.assetType, | ||
terms.token, | ||
to, | ||
amounts, | ||
data | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.