Skip to content

Commit

Permalink
Adds a new test cases and fixes the initialization of the HybridAccou…
Browse files Browse the repository at this point in the history
…nt address.

 Changes to be committed:
	modified:   contracts/test/TestCounter.sol
  • Loading branch information
mmontour1306 committed May 23, 2024
1 parent 9991fc9 commit 043363a
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions contracts/test/TestCounter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ import "../samples/HybridAccount.sol";
contract TestCounter {
mapping(address => uint256) public counters;

address payable immutable helperAddr;
address payable immutable demoAddr;

constructor(address payable _helperAddr) {
helperAddr = _helperAddr;
constructor(address payable _demoAddr) {
demoAddr = _demoAddr;
}

function count(uint32 a, uint32 b, address ha_addr) public {

//HybridAccount HA = HybridAccount(helperAddr);
HybridAccount HA = HybridAccount(payable(ha_addr));

function count(uint32 a, uint32 b) public {
HybridAccount HA = HybridAccount(demoAddr);
uint256 x;
uint256 y;
if (b == 0) {
Expand Down Expand Up @@ -69,4 +67,49 @@ contract TestCounter {
xxx[offset] = i;
}
}

/* This example is a word-guessing game. The user picks a four-letter word as their guess,
and pays for the number of entries they wish to purchase. This wager is added to a pool.
The offchain provider generates a random array of words and returns it as a string[]. If
the user's guess appears in the list returned from the server then they win the entire pool.
A boolean flag allows the user to cheat by guaranteeing that the word "frog" will appear
in the list.
*/

event GameResult(address indexed caller,uint256 indexed win, uint256 indexed Pool);
uint256 public constant EntryCost = 2 gwei;
uint256 public Pool = 0;

function wordGuess(string calldata myGuess, bool cheat) public payable {
HybridAccount HA = HybridAccount(payable(demoAddr));
uint256 entries = msg.value / EntryCost;
require(entries > 0, "No entries purchased");
require(entries <= 100, "Excess payment");
Pool += msg.value;
require(bytes(myGuess).length == 4, "Game uses 4-letter words");

bytes memory req = abi.encodeWithSignature("ramble(uint256,bool)", entries, cheat);
bytes32 userKey = bytes32(abi.encode(msg.sender));
(uint32 error, bytes memory ret) = HA.CallOffchain(userKey, req);
if (error != 0) {
revert(string(ret));
}

uint256 win = 0;
string[] memory words = abi.decode(ret,(string[]));

for (uint256 i=0; i<words.length; i++) {
if (keccak256(bytes(words[i])) == keccak256(bytes(myGuess))) {
win = Pool; // Safe if there's more than one match in the list
}
}

if (win == Pool) {
Pool = 0;
(bool sent,) = msg.sender.call{value: win}("");
require(sent, "Payment failed");
}
emit GameResult(msg.sender,win,Pool);
}
}

0 comments on commit 043363a

Please sign in to comment.