From ac82671b600a75deeab8f1d261c3708dab23b54d Mon Sep 17 00:00:00 2001 From: Arsalan Date: Sat, 5 Oct 2024 20:13:27 +0330 Subject: [PATCH] Add getTablesInfo to OnChainDataSource --- apps/web/src/app/@modal/(.)join/page.tsx | 4 +- packages/ts-sdk/src/Jeton/Jeton.ts | 17 ++++---- .../AptosOnChainDataSource.ts | 26 ++++++++++-- .../OnChainDataSource/OnChainDataSource.ts | 13 +++++- .../src/contracts/contractInteractions.ts | 2 + packages/ts-sdk/src/getTables.ts | 41 ------------------- packages/ts-sdk/src/index.ts | 2 +- 7 files changed, 47 insertions(+), 58 deletions(-) delete mode 100644 packages/ts-sdk/src/getTables.ts diff --git a/apps/web/src/app/@modal/(.)join/page.tsx b/apps/web/src/app/@modal/(.)join/page.tsx index 59f38cc..013a4ac 100644 --- a/apps/web/src/app/@modal/(.)join/page.tsx +++ b/apps/web/src/app/@modal/(.)join/page.tsx @@ -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"; @@ -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); diff --git a/packages/ts-sdk/src/Jeton/Jeton.ts b/packages/ts-sdk/src/Jeton/Jeton.ts index 7234166..6477a60 100644 --- a/packages/ts-sdk/src/Jeton/Jeton.ts +++ b/packages/ts-sdk/src/Jeton/Jeton.ts @@ -2,11 +2,12 @@ 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, @@ -14,8 +15,8 @@ import type { PlacingBettingActions, Player, TableInfo, -} from "../types"; -import { createLocalZKDeck } from "../utils/createZKDeck"; +} from "@src/types"; +import { createLocalZKDeck } from "@src/utils/createZKDeck"; export type ZkDeckUrls = { shuffleEncryptDeckWasm: string; @@ -27,7 +28,7 @@ export type ZkDeckUrls = { export type JetonConfigs = { tableInfo: TableInfo; address: string; - onChainDataSource: OnChainDataSource; + onChainDataSource: OnChainDataSourceInstance; zkDeck: ZKDeck; secretKey?: bigint; }; @@ -43,7 +44,7 @@ export interface JGameState { export class Jeton extends EventEmitter { private playerId: string; public tableInfo: TableInfo; - private onChainDataSource: OnChainDataSource; + private onChainDataSource: OnChainDataSourceInstance; private zkDeck: ZKDeck; private secretKey: bigint; private publicKey: Uint8Array; @@ -152,7 +153,7 @@ export class Jeton extends EventEmitter { const publicKey = zkDeck.generatePublicKey(secretKey); console.log("secret key", secretKey); - const onChainDataSource: OnChainDataSource = new AptosOnChainDataSource( + const onChainDataSource: OnChainDataSourceInstance = new AptosOnChainDataSource( accountAddress, signAndSubmitTransaction, ); @@ -192,7 +193,7 @@ export class Jeton extends EventEmitter { console.log("downloading", percentage); }); - const onChainDataSource: OnChainDataSource = new AptosOnChainDataSource( + const onChainDataSource: OnChainDataSourceInstance = new AptosOnChainDataSource( accountAddress, signAndSubmitTransaction, ); diff --git a/packages/ts-sdk/src/OnChainDataSource/AptosOnChainDataSource.ts b/packages/ts-sdk/src/OnChainDataSource/AptosOnChainDataSource.ts index e48442b..d313639 100644 --- a/packages/ts-sdk/src/OnChainDataSource/AptosOnChainDataSource.ts +++ b/packages/ts-sdk/src/OnChainDataSource/AptosOnChainDataSource.ts @@ -1,6 +1,7 @@ import { EventEmitter } from "events"; import type { OnChainDataSource, + OnChainDataSourceInstance, OnChainEventMap, OnChainTableObject, } from "@src/OnChainDataSource"; @@ -9,6 +10,7 @@ import { callCheckInContract, createTableObject, getTableObject, + getTableObjectAddresses, } from "@src/contracts/contractInteractions"; import type { ChipUnits, TableInfo } from "@src/types"; // @ts-ignore @@ -16,7 +18,7 @@ import { gql, request } from "graffle"; export class AptosOnChainDataSource extends EventEmitter - implements OnChainDataSource + implements OnChainDataSourceInstance { constructor( public address: string, @@ -26,9 +28,6 @@ export class AptosOnChainDataSource super(); this.address = address; } - getTableInfo(id: string): Promise { - throw new Error("Method not implemented."); - } public async createTable( smallBlind: number, @@ -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[] = []; + + 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); + } } diff --git a/packages/ts-sdk/src/OnChainDataSource/OnChainDataSource.ts b/packages/ts-sdk/src/OnChainDataSource/OnChainDataSource.ts index 2874826..5b551aa 100644 --- a/packages/ts-sdk/src/OnChainDataSource/OnChainDataSource.ts +++ b/packages/ts-sdk/src/OnChainDataSource/OnChainDataSource.ts @@ -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 { +export interface OnChainDataSource { + new ( + address: string, + // biome-ignore lint/suspicious/noExplicitAny: + singAndSubmitTransaction: (...args: any) => Promise<{ hash: string }>, + ): OnChainDataSourceInstance; + + getTableInfo(id: string): Promise; + getTablesInfo(id: string): Promise; +} +export interface OnChainDataSourceInstance extends EventEmitter { createTable( smallBlind: number, // number of raises allowed in one round of betting @@ -20,7 +30,6 @@ export interface OnChainDataSource extends EventEmitter { chipUnit: ChipUnits, publicKey: Uint8Array, ): Promise; - getTableInfo(id: string): Promise; queryGameState( id: string, diff --git a/packages/ts-sdk/src/contracts/contractInteractions.ts b/packages/ts-sdk/src/contracts/contractInteractions.ts index b422a88..c2432ce 100644 --- a/packages/ts-sdk/src/contracts/contractInteractions.ts +++ b/packages/ts-sdk/src/contracts/contractInteractions.ts @@ -14,6 +14,7 @@ import { export const getTableObjectAddresses = async (): Promise => { return aptos.getModuleEventsByEventType({ eventType: contractTableCreatedEventType, + options: { orderBy: [{ transaction_block_height: "desc" }] }, }); }; @@ -34,6 +35,7 @@ export const callCheckInContract = async ( // biome-ignore lint/suspicious/noExplicitAny: signAndSubmitTransaction: any, ) => { + console.log("call check-in contract", address, buyInAmount, publicKey, signAndSubmitTransaction); const submitCheckInTransactionHash = await signAndSubmitTransaction({ sender: address, data: { diff --git a/packages/ts-sdk/src/getTables.ts b/packages/ts-sdk/src/getTables.ts deleted file mode 100644 index ee3356c..0000000 --- a/packages/ts-sdk/src/getTables.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { ChipUnits, type TableInfo } from "@src/types/Table"; -import { Jeton } from "./Jeton/Jeton"; -import { AptosOnChainDataSource } from "./OnChainDataSource/AptosOnChainDataSource"; -import { createTableInfo } from "./contracts/contractDataMapper"; -import { getTableObject, getTableObjectAddresses } from "./contracts/contractInteractions"; -const tables: TableInfo[] = [ - { - id: "tbc11", - smallBlind: 1, - numberOfRaises: 2, - minPlayers: 2, - maxPlayers: 8, - minBuyIn: 400, - maxBuyIn: 2000, - chipUnit: ChipUnits.apt, - }, -]; - -/** - * should read different table parameters (probably from a contract) and return a list of them - * @returns {TableInfo[]} - */ -export const getTablesInfo = async (): Promise => { - //TODO paginate - const result = await getTableObjectAddresses(); - - for (const event of result) { - const tableObjectAddress = event.data.table_object.inner; - const tableObjectResource = await getTableObject(tableObjectAddress); - console.log(tableObjectResource); - //TODO check table validation before push and end pagination - const tableInfo = createTableInfo(tableObjectAddress, tableObjectResource); - tables.push(tableInfo); - } - - return tables; -}; - -export const getTableInfo = AptosOnChainDataSource.getTableInfo; - -export const createTable = Jeton.createTableAndJoin; diff --git a/packages/ts-sdk/src/index.ts b/packages/ts-sdk/src/index.ts index cf88bf6..471ceb5 100644 --- a/packages/ts-sdk/src/index.ts +++ b/packages/ts-sdk/src/index.ts @@ -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";