Skip to content

Instantly share code, notes, and snippets.

@TheCyberWatchers
Created October 3, 2022 08:15
Show Gist options
  • Save TheCyberWatchers/d0e23329b3082d457f30d44c86ca9424 to your computer and use it in GitHub Desktop.
Save TheCyberWatchers/d0e23329b3082d457f30d44c86ca9424 to your computer and use it in GitHub Desktop.
//index.mjs
import { loadStdlib } from '@reach-sh/stdlib';
import * as backend from './build/index.main.mjs';
const stdlib = loadStdlib();
const startingBalance = stdlib.parseCurrency(100);
const accAlice = await stdlib.newTestAccount(startingBalance);
const accBob = await stdlib.newTestAccount(startingBalance);
const ctcAlice = accAlice.contract(backend);
const ctcBob = accBob.contract(backend, ctcAlice.getInfo());
const HAND = ['Rock', 'Paper', 'Scissors'];
const OUTCOME = ['Bob wins', 'Draw', 'Alice wins'];
const Player = (Who) => ({
getHand: () => {
const hand = Math.floor(Math.random() * 3);
console.log(`${Who} played ${HAND[hand]}`);
return hand;
},
seeOutcome: (outcome) => {
console.log(`${Who} saw outcome ${OUTCOME[outcome]}`);
},
});
await Promise.all([
ctcAlice.p.Alice({
...Player('Alice'),
}),
ctcBob.p.Bob({
...Player('Bob'),
}),
]);
//index.rsh
'reach 0.1';
const Player = {
getHand: Fun([], UInt),
seeOutcome: Fun([UInt], Null),
};
export const main = Reach.App(() => {
const Alice = Participant('Alice', {
...Player,
});
const Bob = Participant('Bob', {
...Player,
})
init()
Alice.only(() => {
const handAlice = declassify(interact.getHand());
});
Alice.publish(handAlice)
commit();
Bob.only(() => {
const handBob = declassify(interact.getHand())
})
Bob.publish(handBob)
const outcome = (handAlice + (4 - handBob)) % 3
commit()
each([Alice, Bob], () => {
interact.seeOutcome(outcome);
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment