From 8f61f0b3b5f9547a49117bc36a67f2bce3ffcdde Mon Sep 17 00:00:00 2001 From: Linerre Date: Wed, 29 Jan 2025 01:03:18 +1100 Subject: [PATCH] Get registration by derserializing its bcs raw bytes from on chain --- js/sdk-sui/bin/cli.ts | 14 +--- js/sdk-sui/src/sui-transport.ts | 82 ++++--------------- js/sdk-sui/src/types.ts | 2 +- .../{registration.ts_ => registration.ts} | 10 +-- transport/src/sui.rs | 5 +- 5 files changed, 25 insertions(+), 88 deletions(-) rename js/sdk-sui/src/types/{registration.ts_ => registration.ts} (89%) diff --git a/js/sdk-sui/bin/cli.ts b/js/sdk-sui/bin/cli.ts index 90512598..396f49f5 100755 --- a/js/sdk-sui/bin/cli.ts +++ b/js/sdk-sui/bin/cli.ts @@ -12,7 +12,7 @@ const TEST_PACKAGE_ID = "0x094ab410b77496fc9ddccc9f330e2495583df5e6ea59e4498fff5 const TEST_CASH_GAME_ID = "0x5d5e5b48ba5decc365a46777ad20e4ed926e3b6fb38c5fd06729a999496c0c6a"; const TEST_TICKET_GAME_ID = "0xcfc82be4212e504a2bc8b9a6b5b66ed0db92be4e2ab0befe5ba7146a59f54665" const TEST_RECIPIENT_ID = "0x8b8e76d661080e47d76248cc33b43324b4126a8532d7642ab6c47946857c1e1c"; -const TEST_REGISTRY_ID = "0xcb430f98bd97f8c3697cbdbf0de6b9b59411b2634aeebd07f4434fec30f443c7"; +const TEST_REGISTRY_ID = "0xad7a5f0ab1dadb7018032e6d74e5aceaa8b208e2b9d3c24e06418f60c3508aaf"; const TEST_GAME_NFT = "0x5ebed419309e71c1cd28a3249bbf792d2f2cc8b94b0e21e45a9873642c0a5cdc"; function testCreatePlayerProfile() { @@ -113,24 +113,16 @@ async function testListTokensWithBalance() { async function testGetGameAccount() { const suiTransport = new SuiTransport('https://fullnode.devnet.sui.io:443'); - const objectId = TEST_CASH_GAME_ID; - let res = await suiTransport.getGameAccount(objectId); + let res = await suiTransport.getGameAccount(TEST_CASH_GAME_ID); console.log('testGetGameAccount', res) } async function testGetRegistration() { const suiTransport = new SuiTransport('https://fullnode.devnet.sui.io:443'); - const objectId = '0xedc53aecfdf417d6a57d5c2cbf8ad30f877d39cc67ed77ddf5e0ad7a8827d15c' - let res = await suiTransport.getRegistration(objectId); + let res = await suiTransport.getRegistration(TEST_REGISTRY_ID); console.log('testGetRegistration', res) } -// async function testGetRegistrationWithGames() { -// const suiTransport = new SuiTransport('https://fullnode.devnet.sui.io:443'); -// const objectId = '0x65f80e8f4e82f4885c96ccba4da02668428662e975b0a6cd1fa08b61e4e3a2fc' -// let res = await suiTransport.getRegistrationWithGames(objectId); -// console.log('testGetRegistrationWithGames', res) -// } async function testRegisterGame() { const suiTransport = new SuiTransport('https://fullnode.devnet.sui.io:443'); diff --git a/js/sdk-sui/src/sui-transport.ts b/js/sdk-sui/src/sui-transport.ts index 71bfcda0..e76c22d8 100644 --- a/js/sdk-sui/src/sui-transport.ts +++ b/js/sdk-sui/src/sui-transport.ts @@ -71,7 +71,12 @@ import { import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519' import { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions' import { bcs, BcsType, fromBase64 } from '@mysten/bcs' -import { Parser, GameAccountParser, PlayerPorfileParser } from './types' +import { + Parser, + GameAccountParser, + PlayerPorfileParser, + RegistrationAccountParser +} from './types' import { SuiWallet } from './sui-wallet' import { LocalSuiWallet } from './local-wallet' import { @@ -486,56 +491,10 @@ export class SuiTransport implements ITransport { options: { showBcs: true, showType: true, - // showContent: true, }, }) return parseObjectData(parseSingleObjectResponse(resp), GameAccountParser) - - // const content = info.data?.content - // if (!content || content.dataType !== 'moveObject') { - // return undefined - // } - // if (!content.fields) return undefined - // let fields: MoveStruct = content.fields - // if (Array.isArray(fields)) { - // return undefined - // } - // if ('fields' in fields) { - // return undefined - // } - // return { - // addr: addr, - // title: fields?.title as string, - // bundleAddr: fields.bundle_addr as string, - // tokenAddr: fields.token_addr as string, - // ownerAddr: fields.owner as string, - // settleVersion: BigInt(fields.settle_version?.toString() || 0), - // accessVersion: BigInt(fields.access_version?.toString() || 0), - // players: fields.players as [], - // deposits: fields.deposits as [], - // servers: fields.servers as [], - // transactorAddr: fields.transactor_addr as string | undefined, - // votes: fields.votes as [], - // unlockTime: BigInt(fields.unlock_time?.toString() || 0), - // maxPlayers: Number(fields.max_players) || 0, - // dataLen: Number(fields.data_len) || 0, - // data: fields.data ? new Uint8Array(fields.data as number[]) : new Uint8Array(), - // entryType: fields?.entry_type - // ? ((fields.entry_type as MoveVariant).variant as unknown as EntryType) - // : ('None' as unknown as EntryType), - // recipientAddr: fields.recipient_addr as string, - // checkpointOnChain: fields.checkpoint as unknown as CheckpointOnChain | undefined, - // entryLock: fields.entry_lock - // ? ((fields.entry_lock as MoveVariant).variant as 'Closed' | 'Open' | 'JoinOnly' | 'DepositOnly') - // : 'Closed', - // bonuses: (fields.bonuses as any[]).map(b => ({ - // identifier: b.identifier, - // tokenAddr: b.tokenAddr, - // amount: BigInt(b.amount), - // })), - // } - } async listGameAccounts(addrs: string[]): Promise { @@ -558,35 +517,22 @@ export class SuiTransport implements ITransport { // const content = info; throw new Error('Method not implemented.') } + async getRegistration(addr: string): Promise { const suiClient = this.suiClient - const info: SuiObjectResponse = await suiClient.getObject({ + const resp: SuiObjectResponse = await suiClient.getObject({ id: addr, options: { - showContent: true, + showBcs: true, showType: true, }, }) - const content = info.data?.content - if (!content || content.dataType !== 'moveObject') { - return undefined - } - if (!content.fields) return undefined - let fields: MoveStruct = content.fields - if (Array.isArray(fields)) { - return undefined - } - if ('fields' in fields) { - return undefined - } - return { - addr: addr, - isPrivate: fields.is_private as boolean, - size: fields.size as number, - owner: fields.owner as string, - games: fields.games as [], - } + return parseObjectData( + parseSingleObjectResponse(resp), + RegistrationAccountParser + ) } + getRecipient(addr: string): Promise { throw new Error('Method not implemented.') } diff --git a/js/sdk-sui/src/types.ts b/js/sdk-sui/src/types.ts index 901b7614..b54494ce 100644 --- a/js/sdk-sui/src/types.ts +++ b/js/sdk-sui/src/types.ts @@ -1,6 +1,6 @@ export { Parser } from './types/parser' export { PlayerPorfileParser } from './types/player-profile' export { GameAccountParser } from './types/game' +export { RegistrationAccountParser } from './types/registration' // export { RecipientAccountParser } from './types/recipient' -// export { RegistrationAccountParser } from './types/registration' // export { ServerParser } from './types/server' diff --git a/js/sdk-sui/src/types/registration.ts_ b/js/sdk-sui/src/types/registration.ts similarity index 89% rename from js/sdk-sui/src/types/registration.ts_ rename to js/sdk-sui/src/types/registration.ts index 36958ed5..69aad100 100644 --- a/js/sdk-sui/src/types/registration.ts_ +++ b/js/sdk-sui/src/types/registration.ts @@ -6,8 +6,8 @@ import { RegistrationAccount } from '@race-foundation/sdk-core' const GameRegistrationSchema = bcs.struct('GameRegistration', { title: bcs.string(), addr: Address, - regTime: bcs.u64(), bundleAddr: Address, + regTime: bcs.u64(), }) // Define the RegistrationAccountSchema @@ -15,7 +15,7 @@ const RegistrationAccountSchema = bcs.struct('RegistrationAccount', { addr: Address, isPrivate: bcs.bool(), size: bcs.u16(), - owner: bcs.option(Address), + owner: Address, games: bcs.vector(GameRegistrationSchema), }) @@ -27,12 +27,12 @@ export const RegistrationAccountParser: Parser ({ + owner: input.owner ?? undefined, + games: Array.from(input.games).map((game) => ({ title: game.title, addr: game.addr, - regTime: BigInt(game.regTime), bundleAddr: game.bundleAddr, + regTime: BigInt(game.regTime), })), } }, diff --git a/transport/src/sui.rs b/transport/src/sui.rs index d6951f53..aa59face 100755 --- a/transport/src/sui.rs +++ b/transport/src/sui.rs @@ -1585,7 +1585,7 @@ mod tests { const TEST_CASH_GAME_ID: &str = "0x5d5e5b48ba5decc365a46777ad20e4ed926e3b6fb38c5fd06729a999496c0c6a"; const TEST_TICKET_GAME_ID: &str = "0xcfc82be4212e504a2bc8b9a6b5b66ed0db92be4e2ab0befe5ba7146a59f54665"; const TEST_RECIPIENT_ID: &str = "0x8b8e76d661080e47d76248cc33b43324b4126a8532d7642ab6c47946857c1e1c"; - const TEST_REGISTRY: &str = "0xab54b9be8e9662428c4e94400b847d1c5a5608bb22c690898818d73bf548fa0b"; + const TEST_REGISTRY: &str = "0xad7a5f0ab1dadb7018032e6d74e5aceaa8b208e2b9d3c24e06418f60c3508aaf"; const TEST_GAME_NFT: &str = "0x5ebed419309e71c1cd28a3249bbf792d2f2cc8b94b0e21e45a9873642c0a5cdc"; // helper fns to generate some large structures for tests @@ -1852,9 +1852,8 @@ mod tests { SUI_DEVNET_URL.into(), TEST_PACKAGE_ID, ).await.unwrap(); - let game_addr: String = transport.create_game_account(game_params).await?; - // register game in the test registry (created before hand) + let game_addr = TEST_TICKET_GAME_ID.to_string(); let reg_params = RegisterGameParams { game_addr: game_addr.clone(), reg_addr: TEST_REGISTRY.to_string()