Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

features/get-rank #7

Merged
merged 2 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/anchor_types/dino_game_info.dart
Original file line number Diff line number Diff line change
@@ -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);
}
64 changes: 64 additions & 0 deletions lib/anchor_types/dino_game_info.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions lib/anchor_types/dino_score_info.dart
Original file line number Diff line number Diff line change
@@ -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);
}
64 changes: 64 additions & 0 deletions lib/anchor_types/dino_score_info.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/anchor_types/score_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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._();
Expand Down
8 changes: 4 additions & 4 deletions lib/anchor_types/score_parameters.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 54 additions & 8 deletions lib/pages/my-dinogrow/my_dinogrow.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
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';
import 'package:flutter/material.dart';
import 'package:solana/dto.dart';
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;

Expand Down Expand Up @@ -43,6 +49,10 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
text: 'Save Score',
onPressed: saveScore,
),
IntroButtonWidget(
text: 'Get Ranking',
onPressed: getRanking,
),
const SizedBox(height: 30),
Container(
color: Colors.orange[700],
Expand Down Expand Up @@ -280,7 +290,7 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
}

saveScore() async {
await dotenv.load(fileName: ".env");
await dotenv.load(fileName: ".env");

SolanaClient? client;
client = SolanaClient(
Expand All @@ -300,11 +310,12 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
solana.Ed25519HDPublicKey.fromBase58(solana.SystemProgram.programId);

//direccion mint del DINO
final dinoTest = solana.Ed25519HDPublicKey.fromBase58("2tGzpAbJVuB91dzJbUG7m45F88WqswcbznqP2KBZcurw");
final dinoTest = solana.Ed25519HDPublicKey.fromBase58(
"GM3EGmMCYjZs7UstuJ1fvF1Pkocn9GV34BnTGabB8Maf");

final programIdPublicKey = solana.Ed25519HDPublicKey.fromBase58(programId);

final gscorePda = await solana.Ed25519HDPublicKey.findProgramAddress(
final gscorePda = await solana.Ed25519HDPublicKey.findProgramAddress(
programId: programIdPublicKey,
seeds: [
solana_buffer.Buffer.fromString("score"),
Expand All @@ -314,14 +325,14 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
]);
print(gscorePda.toBase58());


final instructions = [
final instructions = [
await solana_anchor.AnchorInstruction.forMethod(
programId: programIdPublicKey,
method: 'savescore',
arguments: solana_encoder.ByteArray(anchor_types_parameters.ScoreArguments(
arguments:
solana_encoder.ByteArray(anchor_types_parameters.ScoreArguments(
game: 1,
score: BigInt.from(100),
score: 1120,
).toBorsh().toList()),
accounts: <solana_encoder.AccountMeta>[
solana_encoder.AccountMeta.writeable(
Expand All @@ -330,7 +341,7 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
pubKey: mainWalletSolana.publicKey, isSigner: true),
solana_encoder.AccountMeta.writeable(
pubKey: dinoTest, isSigner: false),
solana_encoder.AccountMeta.readonly(
solana_encoder.AccountMeta.readonly(
pubKey: systemProgramId, isSigner: false),
],
namespace: 'global',
Expand All @@ -344,4 +355,39 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
);
print('Tx successful with hash: $signature');
}

getRanking() async {
//Get rank from blockchain
await dotenv.load(fileName: ".env");

SolanaClient? client;
client = SolanaClient(
rpcUrl: Uri.parse(dotenv.env['QUICKNODE_RPC_URL'].toString()),
websocketUrl: Uri.parse(dotenv.env['QUICKNODE_RPC_WSS'].toString()),
);

const programId = '9V9ttZw7WTYW78Dx3hi2hV7V76PxAs5ZwbCkGi7qq8FW';

// Obtener todas las cuentas del programa
final accounts = await client.rpcClient.getProgramAccounts(
programId,
encoding: Encoding.jsonParsed,
);

// 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}");
}
}
}