Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ansabgillani committed May 16, 2024
1 parent 6fab691 commit 323a25a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
40 changes: 27 additions & 13 deletions contract/src/gambling-game.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import { AmountMath } from '@agoric/ertp';
import { Far } from '@endo/far';

const start = zcf => {
const { zcfSeat: contractSeat } = zcf.makeEmptySeatKit();
const { zcfSeat: contractSeat } = zcf.makeEmptySeatKit();

const MAX_ENTRIES = 14;
let entries = [];
const MAX_ENTRIES = 14;
let entries = [];

const depositInvitation = zcf.makeInvitation(async seat => {
const depositInvitation = zcf.makeInvitation(async seat => {
// Ensure the player only sends IST
const offerAmount = seat.getAmountAllocated('IST');

if (!offerAmount) {
throw new Error('Only IST is accepted');
}

// Coerce the amount to the correct brand
const depositAmount = AmountMath.coerce(
zcf.getBrandForIssuer(zcf.getIssuerForBrand(offerAmount.brand)),
offerAmount
Expand All @@ -25,24 +32,31 @@ const start = zcf => {
// Prevent reentrancy attacks by updating state before making external calls
entries.push(seat);

// Check if the game is over
if (entries.length === MAX_ENTRIES) {
const winnerSeat = entries[MAX_ENTRIES - 1];
const payoutAmount = contractSeat.getAmountAllocated('IST');
contractSeat.decrementBy(contractSeat.getCurrentAllocation());
winnerSeat.incrementBy(payoutAmount);

zcf.reallocate(contractSeat, winnerSeat);
// Select the winner
const winnerSeat = entries[MAX_ENTRIES - 1];

winnerSeat.exit();
entries = []; // Reset the game
// Payout the winner
const payoutAmount = contractSeat.getAmountAllocated('IST');
contractSeat.decrementBy(contractSeat.getCurrentAllocation('IST'));
winnerSeat.incrementBy(payoutAmount);

// Transfer funds from contract to winner
zcf.reallocate(contractSeat, winnerSeat);

winnerSeat.exit();
entries = []; // Reset the game
} else {
seat.exit();
seat.exit();
}

// Transfer funds from player to contract
contractSeat.incrementBy(depositAmount);
zcf.reallocate(contractSeat, seat);
}, 'Deposit IST');

}, 'Deposit IST');

return {
publicFacet: Far('publicFacet', {
Expand Down
2 changes: 2 additions & 0 deletions contract/src/gambling-game.proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ const startGame = async () => {
const { publicFacet } = await E(zoeService).startInstance(installation, { IST: issuer });

// Mint some IST to test with
console.log(brand);
const aliceAmount = AmountMath.make(brand, 100n);
const alicePayment = mint.mintPayment(aliceAmount);

// Create a deposit invitation
const aliceInvitation = E(publicFacet).makeDepositInvitation();
const proposal = { give: { IST: aliceAmount } };
const payments = { IST: alicePayment };
Expand Down
1 change: 0 additions & 1 deletion contract/test/test-gambling-game.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ test('Make a deposit', async t => {
const installation = await E(zoe).install(bundle);

Check warning on line 79 in contract/test/test-gambling-game.js

View workflow job for this annotation

GitHub Actions / unit

The first `await` appearing in an async function must not be nested
const { issuer, mint, brand } = makeIssuerKit('IST');
const { publicFacet } = await E(zoe).startInstance(installation, { IST: issuer });
t.log(brand, mint)

const aliceAmount = AmountMath.make(brand, 100n);
const alicePayment = mint.mintPayment(aliceAmount);
Expand Down

0 comments on commit 323a25a

Please sign in to comment.