Skip to content

Commit

Permalink
Get registration by derserializing its bcs raw bytes from on chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Linerre committed Jan 28, 2025
1 parent 13f9532 commit 8f61f0b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 88 deletions.
14 changes: 3 additions & 11 deletions js/sdk-sui/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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');
Expand Down
82 changes: 14 additions & 68 deletions js/sdk-sui/src/sui-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<GameAccount[]> {
Expand All @@ -558,35 +517,22 @@ export class SuiTransport implements ITransport {
// const content = info;
throw new Error('Method not implemented.')
}

async getRegistration(addr: string): Promise<RegistrationAccount | undefined> {
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<RecipientAccount | undefined> {
throw new Error('Method not implemented.')
}
Expand Down
2 changes: 1 addition & 1 deletion js/sdk-sui/src/types.ts
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ 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
const RegistrationAccountSchema = bcs.struct('RegistrationAccount', {
addr: Address,
isPrivate: bcs.bool(),
size: bcs.u16(),
owner: bcs.option(Address),
owner: Address,
games: bcs.vector(GameRegistrationSchema),
})

Expand All @@ -27,12 +27,12 @@ export const RegistrationAccountParser: Parser<RegistrationAccount, typeof Regis
addr: input.addr,
isPrivate: input.isPrivate,
size: input.size,
owner: input.owner ? input.owner : undefined,
games: input.games.map(game => ({
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),
})),
}
},
Expand Down
5 changes: 2 additions & 3 deletions transport/src/sui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 8f61f0b

Please sign in to comment.