Skip to content

Commit

Permalink
save score
Browse files Browse the repository at this point in the history
  • Loading branch information
sistemaseltigre committed Oct 10, 2023
1 parent a5ced94 commit 1413266
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
17 changes: 17 additions & 0 deletions lib/anchor_types/score_parameters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:borsh_annotation/borsh_annotation.dart';

part 'score_parameters.g.dart';

@BorshSerializable()
class ScoreArguments with _$ScoreArguments {
factory ScoreArguments(
{
@BU32() required int game,
@BU64() required BigInt score,
}) = _ScoreArguments;

const ScoreArguments._();

factory ScoreArguments.fromBorsh(Uint8List data) =>
_$ScoreArgumentsFromBorsh(data);
}
54 changes: 54 additions & 0 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.

76 changes: 74 additions & 2 deletions lib/pages/my-dinogrow/my_dinogrow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter/material.dart';
import 'package:solana/solana.dart';
import 'package:solana_web3/solana_web3.dart';

import '../../anchor_types/score_parameters.dart' as anchor_types_parameters;
import '../../ui/widgets/widgets.dart';

import 'dart:math';
Expand Down Expand Up @@ -37,6 +39,10 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
text: 'Claim your Dino',
onPressed: createNft,
),
IntroButtonWidget(
text: 'Save Score',
onPressed: saveScore,
),
const SizedBox(height: 30),
Container(
color: Colors.orange[700],
Expand Down Expand Up @@ -183,7 +189,7 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
print(nftMintPda.toBase58());

final ataProgramId = solana.Ed25519HDPublicKey.fromBase58(
solana.AssociatedTokenAccountProgram.programId);
solana.AssociatedTokenAccountProgram.programId);

final systemProgramId =
solana.Ed25519HDPublicKey.fromBase58(solana.SystemProgram.programId);
Expand Down Expand Up @@ -253,7 +259,7 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
solana_encoder.AccountMeta.readonly(
pubKey: systemProgramId, isSigner: false),
solana_encoder.AccountMeta.readonly(
pubKey: tokenProgramId, isSigner: false),
pubKey: tokenProgramId, isSigner: false),
solana_encoder.AccountMeta.readonly(
pubKey: metaplexProgramIdPublicKey, isSigner: false),
solana_encoder.AccountMeta.writeable(
Expand All @@ -272,4 +278,70 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
);
print('Tx successful with hash: $signature');
}

saveScore() async {
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 storage = FlutterSecureStorage();

final mainWalletKey = await storage.read(key: 'mnemonic');

final mainWalletSolana = await solana.Ed25519HDKeyPair.fromMnemonic(
mainWalletKey!,
);

const programId = '9V9ttZw7WTYW78Dx3hi2hV7V76PxAs5ZwbCkGi7qq8FW';
final systemProgramId =
solana.Ed25519HDPublicKey.fromBase58(solana.SystemProgram.programId);

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

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

final gscorePda = await solana.Ed25519HDPublicKey.findProgramAddress(
programId: programIdPublicKey,
seeds: [
solana_buffer.Buffer.fromString("score"),
mainWalletSolana.publicKey.bytes,
dinoTest.bytes,
solana_buffer.Buffer.fromInt32(1),
]);
print(gscorePda.toBase58());


final instructions = [
await solana_anchor.AnchorInstruction.forMethod(
programId: programIdPublicKey,
method: 'savescore',
arguments: solana_encoder.ByteArray(anchor_types_parameters.ScoreArguments(
game: 1,
score: BigInt.from(100),
).toBorsh().toList()),
accounts: <solana_encoder.AccountMeta>[
solana_encoder.AccountMeta.writeable(
pubKey: gscorePda, isSigner: false),
solana_encoder.AccountMeta.writeable(
pubKey: mainWalletSolana.publicKey, isSigner: true),
solana_encoder.AccountMeta.writeable(
pubKey: dinoTest, isSigner: false),
solana_encoder.AccountMeta.readonly(
pubKey: systemProgramId, isSigner: false),
],
namespace: 'global',
),
];
final message = solana.Message(instructions: instructions);
final signature = await client.sendAndConfirmTransaction(
message: message,
signers: [mainWalletSolana],
commitment: solana.Commitment.confirmed,
);
print('Tx successful with hash: $signature');
}
}

0 comments on commit 1413266

Please sign in to comment.