diff --git a/lib/anchor_types/score_parameters.dart b/lib/anchor_types/score_parameters.dart new file mode 100644 index 0000000..ed24306 --- /dev/null +++ b/lib/anchor_types/score_parameters.dart @@ -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); +} diff --git a/lib/anchor_types/score_parameters.g.dart b/lib/anchor_types/score_parameters.g.dart new file mode 100644 index 0000000..f8bb97d --- /dev/null +++ b/lib/anchor_types/score_parameters.g.dart @@ -0,0 +1,54 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'score_parameters.dart'; + +// ************************************************************************** +// BorshSerializableGenerator +// ************************************************************************** + +mixin _$ScoreArguments { + int get game => throw UnimplementedError(); + BigInt get score => throw UnimplementedError(); + + Uint8List toBorsh() { + final writer = BinaryWriter(); + + const BU32().write(writer, game); + const BU64().write(writer, score); + + return writer.toArray(); + } +} + +class _ScoreArguments extends ScoreArguments { + _ScoreArguments({ + required this.game, + required this.score, + }) : super._(); + + final int game; + final BigInt score; +} + +class BScoreArguments implements BType { + const BScoreArguments(); + + @override + void write(BinaryWriter writer, ScoreArguments value) { + writer.writeStruct(value.toBorsh()); + } + + @override + ScoreArguments read(BinaryReader reader) { + return ScoreArguments( + game: const BU32().read(reader), + score: const BU64().read(reader), + ); + } +} + +ScoreArguments _$ScoreArgumentsFromBorsh(Uint8List data) { + final reader = BinaryReader(data.buffer.asByteData()); + + return const BScoreArguments().read(reader); +} diff --git a/lib/pages/my-dinogrow/my_dinogrow.dart b/lib/pages/my-dinogrow/my_dinogrow.dart index 36f2b5e..eca45fc 100644 --- a/lib/pages/my-dinogrow/my_dinogrow.dart +++ b/lib/pages/my-dinogrow/my_dinogrow.dart @@ -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'; @@ -37,6 +39,10 @@ class _MydinogrowScreenState extends State { text: 'Claim your Dino', onPressed: createNft, ), + IntroButtonWidget( + text: 'Save Score', + onPressed: saveScore, + ), const SizedBox(height: 30), Container( color: Colors.orange[700], @@ -183,7 +189,7 @@ class _MydinogrowScreenState extends State { print(nftMintPda.toBase58()); final ataProgramId = solana.Ed25519HDPublicKey.fromBase58( - solana.AssociatedTokenAccountProgram.programId); + solana.AssociatedTokenAccountProgram.programId); final systemProgramId = solana.Ed25519HDPublicKey.fromBase58(solana.SystemProgram.programId); @@ -253,7 +259,7 @@ class _MydinogrowScreenState extends State { 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( @@ -272,4 +278,70 @@ class _MydinogrowScreenState extends State { ); 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.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'); + } }