diff --git a/lib/main.dart b/lib/main.dart index 313925f..4a8c2ca 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -8,7 +8,7 @@ import 'package:dinogrow/pages/login.dart'; import 'package:dinogrow/pages/setup_account.dart'; import 'package:dinogrow/pages/setup_password.dart'; import 'package:dinogrow/pages/mini-games/mini_games.dart'; -import 'package:dinogrow/pages/mini-games/up/up.dart'; +// import 'package:dinogrow/pages/mini-games/up/up.dart'; import 'package:dinogrow/pages/mini-games/coming_soon.dart'; void main() { @@ -55,11 +55,11 @@ final GoRouter _router = GoRouter(routes: [ builder: (context, state) { return const MiniGamesScreen(); }), - GoRoute( - path: '/mini_games/up', - builder: (context, state) { - return GameWidgetUp(game: UpGame()); - }), + // GoRoute( + // path: '/mini_games/up', + // builder: (context, state) { + // return GameWidgetUp(game: UpGame()); + // }), GoRoute( path: '/mini_games/comming_soon', builder: (context, state) { diff --git a/lib/pages/my-dinogrow/my_dinogrow.dart b/lib/pages/my-dinogrow/my_dinogrow.dart index ef4d6f2..a3e0c7d 100644 --- a/lib/pages/my-dinogrow/my_dinogrow.dart +++ b/lib/pages/my-dinogrow/my_dinogrow.dart @@ -39,17 +39,17 @@ class _MydinogrowScreenState extends State { ) ]; - List mintContent(_onClaim) => [ + List mintContent() => [ const IntroLogoWidget(), const SizedBox(height: 30), IntroButtonWidget( text: 'Claim your Dino', onPressed: createNft, ), - IntroButtonWidget( - text: 'Save Score', - onPressed: saveScore, - ), + // IntroButtonWidget( + // text: 'Save Score', + // onPressed: saveScore, + // ), const SizedBox(height: 30), Container( color: Colors.orange[700], @@ -83,7 +83,7 @@ class _MydinogrowScreenState extends State { color: nftSelected == index ? Colors.white : Colors.transparent, - width: 3, + width: 6, ), borderRadius: BorderRadius.circular(45), ), @@ -120,7 +120,11 @@ class _MydinogrowScreenState extends State { urlImage: userNfts.isNotEmpty ? userNfts[nftSelected]['imageUrl'] : '', ), - // const SizedBox(height: 30), + const SizedBox(height: 30), + IntroButtonWidget( + text: 'Claim other Dino', + onPressed: createNft, + ), // TextBoxWidget(text: "Hi ^.^ I'm $nameNft"), ]; @@ -156,7 +160,7 @@ class _MydinogrowScreenState extends State { children: [ ...(showDinos && userNfts.isNotEmpty ? myDinosContent(returnImageColor) - : mintContent(onClaim)), + : mintContent()), const SizedBox(height: 30), IntroButtonWidget( text: 'Log out', @@ -179,12 +183,6 @@ class _MydinogrowScreenState extends State { // await storage.delete(key: 'mnemonic'); } - void onClaim() { - setState(() { - showDinos = true; - }); - } - Image returnImageColor(int index) { if (index == 0) { return Image.asset( @@ -208,8 +206,11 @@ class _MydinogrowScreenState extends State { Future fetchNfts() async { try { + // print('widget.address: ${widget.address}'); setState(() { _loading = true; + userNfts = []; + showDinos = false; }); await dotenv.load(fileName: ".env"); @@ -233,137 +234,168 @@ class _MydinogrowScreenState extends State { final dataResponse = jsonDecode(response.body); final arrayAssets = dataResponse['result']['assets']; - setState(() { - userNfts = arrayAssets.where((nft) => nft['imageUrl'] != '').toList(); - }); + if (mounted) { + setState(() { + userNfts = arrayAssets.where((nft) => nft['imageUrl'] != '').toList(); + showDinos = true; + }); + } } finally { - setState(() { - _loading = false; - }); + if (mounted) { + setState(() { + _loading = false; + }); + } } } createNft() async { - await dotenv.load(fileName: ".env"); + try { + if (mounted) { + setState(() { + _loading = true; + }); + } - 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(); + await dotenv.load(fileName: ".env"); - final mainWalletKey = await storage.read(key: 'mnemonic'); + 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 mainWalletSolana = await solana.Ed25519HDKeyPair.fromMnemonic( - mainWalletKey!, - ); + final mainWalletKey = await storage.read(key: 'mnemonic'); - const programId = '9V9ttZw7WTYW78Dx3hi2hV7V76PxAs5ZwbCkGi7qq8FW'; + final mainWalletSolana = await solana.Ed25519HDKeyPair.fromMnemonic( + mainWalletKey!, + ); - final programIdPublicKey = solana.Ed25519HDPublicKey.fromBase58(programId); + const programId = '9V9ttZw7WTYW78Dx3hi2hV7V76PxAs5ZwbCkGi7qq8FW'; - int idrnd = Random().nextInt(999); - String id = "Dino$idrnd"; - print(id); + final programIdPublicKey = + solana.Ed25519HDPublicKey.fromBase58(programId); - final nftMintPda = await solana.Ed25519HDPublicKey.findProgramAddress( - programId: programIdPublicKey, - seeds: [ - solana_buffer.Buffer.fromString("mint"), - solana_buffer.Buffer.fromString(id), - ]); - print(nftMintPda.toBase58()); + int idrnd = Random().nextInt(999); + String id = "Dino$idrnd"; + print(id); - final ataProgramId = solana.Ed25519HDPublicKey.fromBase58( - solana.AssociatedTokenAccountProgram.programId); + final nftMintPda = await solana.Ed25519HDPublicKey.findProgramAddress( + programId: programIdPublicKey, + seeds: [ + solana_buffer.Buffer.fromString("mint"), + solana_buffer.Buffer.fromString(id), + ]); + print(nftMintPda.toBase58()); - final systemProgramId = - solana.Ed25519HDPublicKey.fromBase58(solana.SystemProgram.programId); - final tokenProgramId = - solana.Ed25519HDPublicKey.fromBase58(solana.TokenProgram.programId); - - final rentProgramId = solana.Ed25519HDPublicKey.fromBase58( - "SysvarRent111111111111111111111111111111111"); - - const metaplexProgramId = 'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'; - final metaplexProgramIdPublicKey = - solana.Ed25519HDPublicKey.fromBase58(metaplexProgramId); - - final aTokenAccount = await solana.Ed25519HDPublicKey.findProgramAddress( - seeds: [ - mainWalletSolana.publicKey.bytes, - tokenProgramId.bytes, - nftMintPda.bytes, - ], - programId: ataProgramId, - ); - print(aTokenAccount.toBase58()); - - final masterEditionAccountPda = - await solana.Ed25519HDPublicKey.findProgramAddress( - seeds: [ - solana_buffer.Buffer.fromString("metadata"), - metaplexProgramIdPublicKey.bytes, - nftMintPda.bytes, - solana_buffer.Buffer.fromString("edition"), - ], - programId: metaplexProgramIdPublicKey, - ); - final nftMetadataPda = await solana.Ed25519HDPublicKey.findProgramAddress( - seeds: [ - solana_buffer.Buffer.fromString("metadata"), - metaplexProgramIdPublicKey.bytes, - nftMintPda.bytes, - ], - programId: metaplexProgramIdPublicKey, - ); + final ataProgramId = solana.Ed25519HDPublicKey.fromBase58( + solana.AssociatedTokenAccountProgram.programId); - final instructions = [ - await solana_anchor.AnchorInstruction.forMethod( - programId: programIdPublicKey, - method: 'create_dino_nft', - arguments: solana_encoder.ByteArray(anchor_types.NftArguments( - id: id, - name: "DINOGROW #005", - symbol: "DNG", - uri: - "https://quicknode.myfilebase.com/ipfs/QmPeUExCwWmpqB47EKErgf3E5JWrQPv3kCpfqpzWVHHux8/", - ).toBorsh().toList()), - accounts: [ - solana_encoder.AccountMeta.writeable( - pubKey: nftMintPda, isSigner: false), - solana_encoder.AccountMeta.writeable( - pubKey: aTokenAccount, isSigner: false), - solana_encoder.AccountMeta.readonly( - pubKey: ataProgramId, isSigner: false), - solana_encoder.AccountMeta.writeable( - pubKey: mainWalletSolana.publicKey, isSigner: true), - solana_encoder.AccountMeta.writeable( - pubKey: mainWalletSolana.publicKey, isSigner: true), - solana_encoder.AccountMeta.readonly( - pubKey: rentProgramId, isSigner: false), - solana_encoder.AccountMeta.readonly( - pubKey: systemProgramId, isSigner: false), - solana_encoder.AccountMeta.readonly( - pubKey: tokenProgramId, isSigner: false), - solana_encoder.AccountMeta.readonly( - pubKey: metaplexProgramIdPublicKey, isSigner: false), - solana_encoder.AccountMeta.writeable( - pubKey: masterEditionAccountPda, isSigner: false), - solana_encoder.AccountMeta.writeable( - pubKey: nftMetadataPda, isSigner: false), + final systemProgramId = + solana.Ed25519HDPublicKey.fromBase58(solana.SystemProgram.programId); + final tokenProgramId = + solana.Ed25519HDPublicKey.fromBase58(solana.TokenProgram.programId); + + final rentProgramId = solana.Ed25519HDPublicKey.fromBase58( + "SysvarRent111111111111111111111111111111111"); + + const metaplexProgramId = 'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'; + final metaplexProgramIdPublicKey = + solana.Ed25519HDPublicKey.fromBase58(metaplexProgramId); + + final aTokenAccount = await solana.Ed25519HDPublicKey.findProgramAddress( + seeds: [ + mainWalletSolana.publicKey.bytes, + tokenProgramId.bytes, + nftMintPda.bytes, ], - 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'); + programId: ataProgramId, + ); + print(aTokenAccount.toBase58()); + + final masterEditionAccountPda = + await solana.Ed25519HDPublicKey.findProgramAddress( + seeds: [ + solana_buffer.Buffer.fromString("metadata"), + metaplexProgramIdPublicKey.bytes, + nftMintPda.bytes, + solana_buffer.Buffer.fromString("edition"), + ], + programId: metaplexProgramIdPublicKey, + ); + final nftMetadataPda = await solana.Ed25519HDPublicKey.findProgramAddress( + seeds: [ + solana_buffer.Buffer.fromString("metadata"), + metaplexProgramIdPublicKey.bytes, + nftMintPda.bytes, + ], + programId: metaplexProgramIdPublicKey, + ); + + int indexImage = Random().nextInt(5); + + final imagesNfts = [ + 'QmPeUExCwWmpqB47EKErgf3E5JWrQPv3kCpfqpzWVHHux8', + 'QmdHkmcWiMmwnwmz6SJDR1J5LLsPH5uSy1BgFAQzkHJWxJ', + 'QmUueQKAY5SFRZBYzKowms3YyJkK7VfJHSBhBYT1GAcs2H', + 'QmQgk3vJFjhphhV1riLEjnLa6cUgKzGwE75egfT3jhuTfM', + 'QmeaphAPRmf1rueJ6QBMyRPYBLKWZ9YMZhHQmcR8csxPxr', + ]; + + final instructions = [ + await solana_anchor.AnchorInstruction.forMethod( + programId: programIdPublicKey, + method: 'create_dino_nft', + arguments: solana_encoder.ByteArray(anchor_types.NftArguments( + id: id, + name: "DINOGROW #${userNfts.length + 1}", + symbol: "DNG", + uri: + "https://quicknode.myfilebase.com/ipfs/${imagesNfts[indexImage]}/", + ).toBorsh().toList()), + accounts: [ + solana_encoder.AccountMeta.writeable( + pubKey: nftMintPda, isSigner: false), + solana_encoder.AccountMeta.writeable( + pubKey: aTokenAccount, isSigner: false), + solana_encoder.AccountMeta.readonly( + pubKey: ataProgramId, isSigner: false), + solana_encoder.AccountMeta.writeable( + pubKey: mainWalletSolana.publicKey, isSigner: true), + solana_encoder.AccountMeta.writeable( + pubKey: mainWalletSolana.publicKey, isSigner: true), + solana_encoder.AccountMeta.readonly( + pubKey: rentProgramId, isSigner: false), + solana_encoder.AccountMeta.readonly( + pubKey: systemProgramId, isSigner: false), + solana_encoder.AccountMeta.readonly( + pubKey: tokenProgramId, isSigner: false), + solana_encoder.AccountMeta.readonly( + pubKey: metaplexProgramIdPublicKey, isSigner: false), + solana_encoder.AccountMeta.writeable( + pubKey: masterEditionAccountPda, isSigner: false), + solana_encoder.AccountMeta.writeable( + pubKey: nftMetadataPda, 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'); + fetchNfts(); + } finally { + if (mounted) { + setState(() { + _loading = false; + }); + } + } } saveScore() async {