Here is the code, which generates the output from the article. You can easily
do the same steps locally:
javascript
async getTransactions(playerAddress, gameId) {
playerAddress = normalizeHexString(playerAddress);
const player = await this.getPlayer(playerAddress);
if(!gameId) {
throw new InvalidInput(`game id is required`);
}
const result = {};
const params = {
croupier: this.address,
player: playerAddress,
gameId,
};
const [betSubmittedEvents,
croupierSeedEvents,
playerSeedEvents,
replacementOrderSubmittedEvents,
gameFinishedEvents,
] = await Promise.all([
this.bankConnector.getAllEvents('BetSubmitted', params),
this.bankConnector.getAllEvents('CroupierSeedSubmitted', params),
this.bankConnector.getAllEvents('PlayerSeedSubmitted', params),
this.bankConnector.getAllEvents('ReplacementOrderSubmitted', params),
this.bankConnector.getAllEvents('GameFinished', params),
]);
let croupierSeeds = [];
let playerSeeds = [];
let seeds = [];
let replacementOrder;
let bet;
if(betSubmittedEvents.length > 1) {
log.warn(`found more than 1 BetSubmitted events for player: ${player.address}, gameId: ${gameId}`);
}
if(betSubmittedEvents.length > 0) {
const betSubmittedEvent = betSubmittedEvents[0];
bet = betSubmittedEvent.args.bet;
result['BetSubmitted'] = {
txHash: betSubmittedEvent.transactionHash,
bet: betSubmittedEvent.args.bet.toNumber(),
level: betSubmittedEvent.args.level.toNumber(),
betPayment: this.formatEther(this.chipCost.times(betSubmittedEvent.args.bet).times(betSubmittedEvent.args.level)),
};
}
for(const croupierSeedEvent of croupierSeedEvents) {
const seedID = croupierSeedEvent.args.seedID.toNumber();
croupierSeeds[seedID] = croupierSeedEvent.args.seed.toString();
result[`CroupierSeedSubmitted: ${seedID}`] = {
txHash: croupierSeedEvent.transactionHash,
seed: croupierSeeds[seedID],
}
}
for(const playerSeedEvent of playerSeedEvents) {
const seedID = playerSeedEvent.args.seedID.toNumber();
playerSeeds[seedID] = playerSeedEvent.args.seed.toString();
result[`PlayerSeedSubmitted: ${seedID}`] = {
txHash: playerSeedEvent.transactionHash,
seed: playerSeeds[seedID],
}
}
if(croupierSeeds[0] && playerSeeds[0]) {
const seeds = [ PRNG.mixSeeds(croupierSeeds[0], playerSeeds[0]), 0 ];
let initialHand = await this.bankConnector.bank.generateHand(seeds, [0, 0, 0, 0, 0]);
initialHand = new CardSet(initialHand);
result['InitialHand'] = initialHand.toString();
}
if(replacementOrderSubmittedEvents.length > 1) {
log.warn(`found more than 1 ReplacementOrderSubmitted events for player: ${player.address}, gameId: ${gameId}`);
}
if(replacementOrderSubmittedEvents.length > 0) {
const replacementOrderSubmittedEvent = replacementOrderSubmittedEvents[0];
replacementOrder = replacementOrderSubmittedEvent.args.replacementOrder;
result['ReplacementOrderSubmitted'] = {
txHash: replacementOrderSubmittedEvent.transactionHash,
replacementOrder: replacementOrderSubmittedEvent.args.replacementOrder,
};
}
if(gameFinishedEvents.length > 1) {
log.warn(`found more than 1 GameFinished events for player: ${player.address}, gameId: ${gameId}`);
}
if(gameFinishedEvents.length > 0) {
const gameFinishedEvent = gameFinishedEvents[0];
result['GameFinishedEvent'] = {
txHash: gameFinishedEvent.transactionHash,
winCoef: gameFinishedEvent.args.winCoef.toNumber(),
winPayment: this.formatEther(this.chipCost.times(bet).times(gameFinishedEvent.args.winCoef.toNumber())),
};
for(let i = 0; i <= 1; i++) {
seeds[i] = PRNG.mixSeeds(croupierSeeds[i], playerSeeds[i]);
}
let finalHand = await this.bankConnector.bank.generateHand(seeds, replacementOrder);
finalHand = new CardSet(finalHand);
result['FinalHand'] = finalHand.toString();
}
return result;
}
1.
this.bankConnector.getAllEvents
get all the events from the contract, with the name specified and filters them according to the
params
object.
2.
this.bankConnector.bank.
makes a call to Ethereum blockchain