From adb5d07485c7a871371acae8ba6678936d51d48f Mon Sep 17 00:00:00 2001 From: Jesus Silva Date: Fri, 13 Oct 2023 16:34:15 -0400 Subject: [PATCH] adding fetch rank --- lib/anchor_types/dino_game_info.dart | 22 ++++++++ lib/anchor_types/dino_game_info.g.dart | 64 ++++++++++++++++++++++++ lib/anchor_types/dino_score_info.dart | 21 ++++++++ lib/anchor_types/dino_score_info.g.dart | 64 ++++++++++++++++++++++++ lib/anchor_types/score_parameters.dart | 2 +- lib/anchor_types/score_parameters.g.dart | 8 +-- lib/pages/my-dinogrow/my_dinogrow.dart | 52 ++++++++----------- 7 files changed, 197 insertions(+), 36 deletions(-) create mode 100644 lib/anchor_types/dino_game_info.dart create mode 100644 lib/anchor_types/dino_game_info.g.dart create mode 100644 lib/anchor_types/dino_score_info.dart create mode 100644 lib/anchor_types/dino_score_info.g.dart diff --git a/lib/anchor_types/dino_game_info.dart b/lib/anchor_types/dino_game_info.dart new file mode 100644 index 0000000..6db195b --- /dev/null +++ b/lib/anchor_types/dino_game_info.dart @@ -0,0 +1,22 @@ +import 'package:borsh_annotation/borsh_annotation.dart'; +import 'package:solana/solana.dart'; + +part 'dino_game_info.g.dart'; + + +@BorshSerializable() +class DinoGameArguments with _$DinoGameArguments { + factory DinoGameArguments( + { + @BU64() required BigInt score, + @BU64() required BigInt game, + @BPublicKey() required Ed25519HDPublicKey playerPubkey, + @BPublicKey() required Ed25519HDPublicKey dinoPubkey, + + }) = _DinoGameArguments; + + const DinoGameArguments._(); + + factory DinoGameArguments.fromBorsh(Uint8List data) => + _$DinoGameArgumentsFromBorsh(data); +} diff --git a/lib/anchor_types/dino_game_info.g.dart b/lib/anchor_types/dino_game_info.g.dart new file mode 100644 index 0000000..3e2332c --- /dev/null +++ b/lib/anchor_types/dino_game_info.g.dart @@ -0,0 +1,64 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'dino_game_info.dart'; + +// ************************************************************************** +// BorshSerializableGenerator +// ************************************************************************** + +mixin _$DinoGameArguments { + BigInt get score => throw UnimplementedError(); + BigInt get game => throw UnimplementedError(); + Ed25519HDPublicKey get playerPubkey => throw UnimplementedError(); + Ed25519HDPublicKey get dinoPubkey => throw UnimplementedError(); + + Uint8List toBorsh() { + final writer = BinaryWriter(); + + const BU64().write(writer, score); + const BU64().write(writer, game); + const BPublicKey().write(writer, playerPubkey); + const BPublicKey().write(writer, dinoPubkey); + + return writer.toArray(); + } +} + +class _DinoGameArguments extends DinoGameArguments { + _DinoGameArguments({ + required this.score, + required this.game, + required this.playerPubkey, + required this.dinoPubkey, + }) : super._(); + + final BigInt score; + final BigInt game; + final Ed25519HDPublicKey playerPubkey; + final Ed25519HDPublicKey dinoPubkey; +} + +class BDinoGameArguments implements BType { + const BDinoGameArguments(); + + @override + void write(BinaryWriter writer, DinoGameArguments value) { + writer.writeStruct(value.toBorsh()); + } + + @override + DinoGameArguments read(BinaryReader reader) { + return DinoGameArguments( + score: const BU64().read(reader), + game: const BU64().read(reader), + playerPubkey: const BPublicKey().read(reader), + dinoPubkey: const BPublicKey().read(reader), + ); + } +} + +DinoGameArguments _$DinoGameArgumentsFromBorsh(Uint8List data) { + final reader = BinaryReader(data.buffer.asByteData()); + + return const BDinoGameArguments().read(reader); +} diff --git a/lib/anchor_types/dino_score_info.dart b/lib/anchor_types/dino_score_info.dart new file mode 100644 index 0000000..85ea674 --- /dev/null +++ b/lib/anchor_types/dino_score_info.dart @@ -0,0 +1,21 @@ +import 'package:borsh_annotation/borsh_annotation.dart'; +import 'package:solana/solana.dart'; + +part 'dino_score_info.g.dart'; + + +@BorshSerializable() +class DinoScoreArguments with _$DinoScoreArguments { + factory DinoScoreArguments( + { + @BU64() required BigInt score, + @BU32() required int gamescore, + @BPublicKey() required Ed25519HDPublicKey playerPubkey, + @BPublicKey() required Ed25519HDPublicKey dinoPubkey, + }) = _DinoScoreArguments; + + const DinoScoreArguments._(); + + factory DinoScoreArguments.fromBorsh(Uint8List data) => + _$DinoScoreArgumentsFromBorsh(data); +} diff --git a/lib/anchor_types/dino_score_info.g.dart b/lib/anchor_types/dino_score_info.g.dart new file mode 100644 index 0000000..33f8851 --- /dev/null +++ b/lib/anchor_types/dino_score_info.g.dart @@ -0,0 +1,64 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'dino_score_info.dart'; + +// ************************************************************************** +// BorshSerializableGenerator +// ************************************************************************** + +mixin _$DinoScoreArguments { + BigInt get score => throw UnimplementedError(); + int get gamescore => throw UnimplementedError(); + Ed25519HDPublicKey get playerPubkey => throw UnimplementedError(); + Ed25519HDPublicKey get dinoPubkey => throw UnimplementedError(); + + Uint8List toBorsh() { + final writer = BinaryWriter(); + + const BU64().write(writer, score); + const BU32().write(writer, gamescore); + const BPublicKey().write(writer, playerPubkey); + const BPublicKey().write(writer, dinoPubkey); + + return writer.toArray(); + } +} + +class _DinoScoreArguments extends DinoScoreArguments { + _DinoScoreArguments({ + required this.score, + required this.gamescore, + required this.playerPubkey, + required this.dinoPubkey, + }) : super._(); + + final BigInt score; + final int gamescore; + final Ed25519HDPublicKey playerPubkey; + final Ed25519HDPublicKey dinoPubkey; +} + +class BDinoScoreArguments implements BType { + const BDinoScoreArguments(); + + @override + void write(BinaryWriter writer, DinoScoreArguments value) { + writer.writeStruct(value.toBorsh()); + } + + @override + DinoScoreArguments read(BinaryReader reader) { + return DinoScoreArguments( + score: const BU64().read(reader), + gamescore: const BU32().read(reader), + playerPubkey: const BPublicKey().read(reader), + dinoPubkey: const BPublicKey().read(reader), + ); + } +} + +DinoScoreArguments _$DinoScoreArgumentsFromBorsh(Uint8List data) { + final reader = BinaryReader(data.buffer.asByteData()); + + return const BDinoScoreArguments().read(reader); +} diff --git a/lib/anchor_types/score_parameters.dart b/lib/anchor_types/score_parameters.dart index ed24306..32a28e6 100644 --- a/lib/anchor_types/score_parameters.dart +++ b/lib/anchor_types/score_parameters.dart @@ -7,7 +7,7 @@ class ScoreArguments with _$ScoreArguments { factory ScoreArguments( { @BU32() required int game, - @BU64() required BigInt score, + @BU32() required int score, }) = _ScoreArguments; const ScoreArguments._(); diff --git a/lib/anchor_types/score_parameters.g.dart b/lib/anchor_types/score_parameters.g.dart index f8bb97d..6a9b0ce 100644 --- a/lib/anchor_types/score_parameters.g.dart +++ b/lib/anchor_types/score_parameters.g.dart @@ -8,13 +8,13 @@ part of 'score_parameters.dart'; mixin _$ScoreArguments { int get game => throw UnimplementedError(); - BigInt get score => throw UnimplementedError(); + int get score => throw UnimplementedError(); Uint8List toBorsh() { final writer = BinaryWriter(); const BU32().write(writer, game); - const BU64().write(writer, score); + const BU32().write(writer, score); return writer.toArray(); } @@ -27,7 +27,7 @@ class _ScoreArguments extends ScoreArguments { }) : super._(); final int game; - final BigInt score; + final int score; } class BScoreArguments implements BType { @@ -42,7 +42,7 @@ class BScoreArguments implements BType { ScoreArguments read(BinaryReader reader) { return ScoreArguments( game: const BU32().read(reader), - score: const BU64().read(reader), + score: const BU32().read(reader), ); } } diff --git a/lib/pages/my-dinogrow/my_dinogrow.dart b/lib/pages/my-dinogrow/my_dinogrow.dart index 4ff7fbb..c629128 100644 --- a/lib/pages/my-dinogrow/my_dinogrow.dart +++ b/lib/pages/my-dinogrow/my_dinogrow.dart @@ -1,5 +1,5 @@ import 'dart:convert'; - +import 'dart:typed_data'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:go_router/go_router.dart'; @@ -9,12 +9,15 @@ import 'package:solana/solana.dart'; import 'package:solana_web3/solana_web3.dart'; import '../../anchor_types/score_parameters.dart' as anchor_types_parameters; +import '../../anchor_types/dino_score_info.dart' as anchor_types_dino; +import '../../anchor_types/dino_game_info.dart' as anchor_types_dino_game; import '../../ui/widgets/widgets.dart'; import 'dart:math'; import 'package:solana/solana.dart' as solana; import 'package:solana/anchor.dart' as solana_anchor; import 'package:solana/encoder.dart' as solana_encoder; +import 'package:solana_common/borsh/borsh.dart' as solana_borsh; import 'package:solana_common/utils/buffer.dart' as solana_buffer; import '../../anchor_types/nft_parameters.dart' as anchor_types; @@ -308,7 +311,7 @@ class _MydinogrowScreenState extends State { //direccion mint del DINO final dinoTest = solana.Ed25519HDPublicKey.fromBase58( - "2tGzpAbJVuB91dzJbUG7m45F88WqswcbznqP2KBZcurw"); + "GM3EGmMCYjZs7UstuJ1fvF1Pkocn9GV34BnTGabB8Maf"); final programIdPublicKey = solana.Ed25519HDPublicKey.fromBase58(programId); @@ -329,7 +332,7 @@ class _MydinogrowScreenState extends State { arguments: solana_encoder.ByteArray(anchor_types_parameters.ScoreArguments( game: 1, - score: BigInt.from(100), + score: 1120, ).toBorsh().toList()), accounts: [ solana_encoder.AccountMeta.writeable( @@ -371,33 +374,20 @@ class _MydinogrowScreenState extends State { encoding: Encoding.jsonParsed, ); - // Recorre las cuentas y muestra los datos - for (var account in accounts) { - final datatest = BinaryAccountData(account.account.data as List); - //AccountData accountData = AccountData.fromJson(account.account); - //print(accountData); - //final jsonData = json.decode(account.account.data); - // ignore: avoid_print - print(datatest); - - //final data = account.account.data; - - // Decodifica el dato como JSON - //final jsonData = json.decode(utf8.decode(data.toString() as List)); - /* final publicKey = account.account.owner.toBase58(); - final data = account.account.data; - - // Decodifica el dato como JSON - final jsonData = json.decode(utf8.decode(data)); - - print('publicKey: $publicKey'); - print('account: $jsonData'); - print('score: ${jsonData['score']}'); - print('game: ${jsonData['game']}'); - print('playerPubkey: ${jsonData['playerPubkey']}'); - print('dinoPubkey: ${jsonData['dinoPubkey']}'); - print('isInitialized: ${jsonData['isInitialized']}'); */ - } - + // Recorre las cuentas y muestra los datos + for (var account in accounts) { + final bytes = account.account.data as BinaryAccountData; + + //Get Score + final decoderDataScore = anchor_types_dino.DinoScoreArguments.fromBorsh( + bytes.data as Uint8List); + print("score: ${decoderDataScore.gamescore}"); + + //Get Game Data + final decoderDataGame = anchor_types_dino_game.DinoGameArguments.fromBorsh( + bytes.data as Uint8List); + print("score: ${decoderDataGame.playerPubkey}"); + print("score: ${decoderDataGame.dinoPubkey}"); + } } }