Skip to content

Commit

Permalink
Updated contract with withdraw method; removed message event
Browse files Browse the repository at this point in the history
  • Loading branch information
lukem512 committed Dec 2, 2017
1 parent 0822e84 commit 3e5d625
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
47 changes: 34 additions & 13 deletions contracts/smartdice.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ contract SmartDice is usingOraclize {

string public lastRoll;
string public lastPrice;
address owner;
event diceRolled(uint value);
event message(string description);

function SmartDice() payable {
rollDice();
owner = msg.sender;
}

function __callback(bytes32 myid, string result) {
Expand All @@ -18,20 +19,40 @@ contract SmartDice is usingOraclize {
diceRolled(parseInt(lastRoll));
}

function rollDice() payable {
function rollDice() payable returns (bool) {
// Retrieve price for oraclize query
uint oraclizePrice = oraclize_getPrice("WolframAlpha");

// Throw if not covered by the sender
if (msg.value < oraclizePrice) throw;

// Sanity check contract balance is sufficient
if (oraclizePrice > this.balance) {
message("Could not retrieve random number, please add some ETH to cover the query fee");
} else {
lastPrice = uint2str(oraclizePrice);
message("Rolling the dice...");
oraclize_query("WolframAlpha", "random number between 1 and 6");

// Check the price is covered by the transaction
if (msg.value < oraclizePrice) {
return false;
}

// Update last lastPrice
lastPrice = uint2str(oraclizePrice);

// Call WolframAlpha via Oraclize to roll the dice
oraclize_query("WolframAlpha", "random number between 1 and 6");

return true;
}

function withdraw(uint amount) returns (bool) {
// Only the owner may withdraw
if (msg.sender != owner) {
return false;
}

// Sanity check balance
if (amount > this.balance) {
return false;
}

// Try to send, throw if
if (!msg.sender.send(amount)) {
return false;
}

return true;
}
}
38 changes: 25 additions & 13 deletions web/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "amount",
"type": "uint256"
}
],
"name": "withdraw",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
Expand All @@ -57,7 +76,12 @@
"constant": false,
"inputs": [],
"name": "rollDice",
"outputs": [],
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": true,
"stateMutability": "payable",
"type": "function"
Expand Down Expand Up @@ -93,17 +117,5 @@
],
"name": "diceRolled",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"name": "description",
"type": "string"
}
],
"name": "message",
"type": "event"
}
]

0 comments on commit 3e5d625

Please sign in to comment.