Skip to content

Commit

Permalink
Add getTablesInfo to OnChainDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
arssly committed Oct 5, 2024
1 parent 8ff9176 commit ac82671
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 58 deletions.
4 changes: 2 additions & 2 deletions apps/web/src/app/@modal/(.)join/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

export const runtime = "edge";

import { type TableInfo, getTablesInfo } from "@jeton/ts-sdk";
import { AptosOnChainDataSource, type TableInfo } from "@jeton/ts-sdk";
import Modal from "@src/components/Modal";
import Spinner from "@src/components/Spinner";
import Link from "next/link";
Expand All @@ -17,7 +17,7 @@ export default function GameJoinModal() {
useEffect(() => {
const fetchTables = async () => {
try {
const data = await getTablesInfo();
const data = await AptosOnChainDataSource.getTablesInfo();
setGameTables(data);
} finally {
setLoading(false);
Expand Down
17 changes: 9 additions & 8 deletions packages/ts-sdk/src/Jeton/Jeton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ import { EventEmitter } from "events";
import type { ZKDeck } from "@jeton/zk-deck";
import {
type OnChainDataSource,
type OnChainDataSourceInstance,
OnChainEventTypes,
type OnChainPlayerCheckedInData,
} from "@src/OnChainDataSource";
import { AptosOnChainDataSource } from "../OnChainDataSource/AptosOnChainDataSource";
import onChainDataMapper from "../OnChainDataSource/onChainDataMapper";
import { AptosOnChainDataSource } from "@src/OnChainDataSource/AptosOnChainDataSource";
import onChainDataMapper from "@src/OnChainDataSource/onChainDataMapper";
import type {
ChipUnits,
GameEventMap,
GameStatus,
PlacingBettingActions,
Player,
TableInfo,
} from "../types";
import { createLocalZKDeck } from "../utils/createZKDeck";
} from "@src/types";
import { createLocalZKDeck } from "@src/utils/createZKDeck";

export type ZkDeckUrls = {
shuffleEncryptDeckWasm: string;
Expand All @@ -27,7 +28,7 @@ export type ZkDeckUrls = {
export type JetonConfigs = {
tableInfo: TableInfo;
address: string;
onChainDataSource: OnChainDataSource;
onChainDataSource: OnChainDataSourceInstance;
zkDeck: ZKDeck;
secretKey?: bigint;
};
Expand All @@ -43,7 +44,7 @@ export interface JGameState {
export class Jeton extends EventEmitter<GameEventMap> {
private playerId: string;
public tableInfo: TableInfo;
private onChainDataSource: OnChainDataSource;
private onChainDataSource: OnChainDataSourceInstance;
private zkDeck: ZKDeck;
private secretKey: bigint;
private publicKey: Uint8Array;
Expand Down Expand Up @@ -152,7 +153,7 @@ export class Jeton extends EventEmitter<GameEventMap> {
const publicKey = zkDeck.generatePublicKey(secretKey);

console.log("secret key", secretKey);
const onChainDataSource: OnChainDataSource = new AptosOnChainDataSource(
const onChainDataSource: OnChainDataSourceInstance = new AptosOnChainDataSource(
accountAddress,
signAndSubmitTransaction,
);
Expand Down Expand Up @@ -192,7 +193,7 @@ export class Jeton extends EventEmitter<GameEventMap> {
console.log("downloading", percentage);
});

const onChainDataSource: OnChainDataSource = new AptosOnChainDataSource(
const onChainDataSource: OnChainDataSourceInstance = new AptosOnChainDataSource(
accountAddress,
signAndSubmitTransaction,
);
Expand Down
26 changes: 22 additions & 4 deletions packages/ts-sdk/src/OnChainDataSource/AptosOnChainDataSource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EventEmitter } from "events";
import type {
OnChainDataSource,
OnChainDataSourceInstance,
OnChainEventMap,
OnChainTableObject,
} from "@src/OnChainDataSource";
Expand All @@ -9,14 +10,15 @@ import {
callCheckInContract,
createTableObject,
getTableObject,
getTableObjectAddresses,
} from "@src/contracts/contractInteractions";
import type { ChipUnits, TableInfo } from "@src/types";
// @ts-ignore
import { gql, request } from "graffle";

export class AptosOnChainDataSource
extends EventEmitter<OnChainEventMap>
implements OnChainDataSource
implements OnChainDataSourceInstance
{
constructor(
public address: string,
Expand All @@ -26,9 +28,6 @@ export class AptosOnChainDataSource
super();
this.address = address;
}
getTableInfo(id: string): Promise<TableInfo> {
throw new Error("Method not implemented.");
}

public async createTable(
smallBlind: number,
Expand Down Expand Up @@ -106,4 +105,23 @@ export class AptosOnChainDataSource
const tableInfo = createTableInfo(id, tableObjectResource);
return tableInfo;
}

static async getTablesInfo() {
//TODO paginate
const result = await getTableObjectAddresses();
console.log("get tables objectsAddresses", result);
const tablePromises: Promise<TableInfo>[] = [];

for (const event of result) {
const tableObjectAddress = event.data.table_object.inner;
tablePromises.push(
getTableObject(tableObjectAddress).then((tableObjectResource) => {
console.log(tableObjectResource);
return createTableInfo(tableObjectAddress, tableObjectResource);
}),
);
}

return await Promise.all(tablePromises);
}
}
13 changes: 11 additions & 2 deletions packages/ts-sdk/src/OnChainDataSource/OnChainDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ import type { ChipUnits, TableInfo } from "../types/Table";
import type { OnChainEventMap } from "./onChainEvents.types";
import type { OnChainTableObject } from "./onChainObjects.types";

export interface OnChainDataSource extends EventEmitter<OnChainEventMap> {
export interface OnChainDataSource {
new (
address: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
singAndSubmitTransaction: (...args: any) => Promise<{ hash: string }>,
): OnChainDataSourceInstance;

getTableInfo(id: string): Promise<TableInfo>;
getTablesInfo(id: string): Promise<TableInfo>;
}
export interface OnChainDataSourceInstance extends EventEmitter<OnChainEventMap> {
createTable(
smallBlind: number,
// number of raises allowed in one round of betting
Expand All @@ -20,7 +30,6 @@ export interface OnChainDataSource extends EventEmitter<OnChainEventMap> {
chipUnit: ChipUnits,
publicKey: Uint8Array,
): Promise<TableInfo>;
getTableInfo(id: string): Promise<TableInfo>;

queryGameState<T extends keyof OnChainTableObject>(
id: string,
Expand Down
2 changes: 2 additions & 0 deletions packages/ts-sdk/src/contracts/contractInteractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
export const getTableObjectAddresses = async (): Promise<GetEventsResponse> => {
return aptos.getModuleEventsByEventType({
eventType: contractTableCreatedEventType,
options: { orderBy: [{ transaction_block_height: "desc" }] },
});
};

Expand All @@ -34,6 +35,7 @@ export const callCheckInContract = async (
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
signAndSubmitTransaction: any,
) => {
console.log("call check-in contract", address, buyInAmount, publicKey, signAndSubmitTransaction);
const submitCheckInTransactionHash = await signAndSubmitTransaction({
sender: address,
data: {
Expand Down
41 changes: 0 additions & 41 deletions packages/ts-sdk/src/getTables.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/ts-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "@src/getTables";
export * from "@src/types/index";
export * from "@src/Jeton";
export * from "@src/OnChainDataSource";
export * from "@src/utils/convertTypes";

0 comments on commit ac82671

Please sign in to comment.