Skip to content

Commit

Permalink
feat: complete flow to mint, select and render user nfts
Browse files Browse the repository at this point in the history
  • Loading branch information
dnielopez committed Oct 12, 2023
1 parent 6076923 commit fd6eeb8
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 135 deletions.
12 changes: 6 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -55,11 +55,11 @@ final GoRouter _router = GoRouter(routes: <GoRoute>[
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) {
Expand Down
290 changes: 161 additions & 129 deletions lib/pages/my-dinogrow/my_dinogrow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
)
];

List<Widget> mintContent(_onClaim) => [
List<Widget> 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],
Expand Down Expand Up @@ -83,7 +83,7 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
color: nftSelected == index
? Colors.white
: Colors.transparent,
width: 3,
width: 6,
),
borderRadius: BorderRadius.circular(45),
),
Expand Down Expand Up @@ -120,7 +120,11 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
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"),
];

Expand Down Expand Up @@ -156,7 +160,7 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
children: [
...(showDinos && userNfts.isNotEmpty
? myDinosContent(returnImageColor)
: mintContent(onClaim)),
: mintContent()),
const SizedBox(height: 30),
IntroButtonWidget(
text: 'Log out',
Expand All @@ -179,12 +183,6 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
// await storage.delete(key: 'mnemonic');
}

void onClaim() {
setState(() {
showDinos = true;
});
}

Image returnImageColor(int index) {
if (index == 0) {
return Image.asset(
Expand All @@ -208,8 +206,11 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {

Future<void> fetchNfts() async {
try {
// print('widget.address: ${widget.address}');
setState(() {
_loading = true;
userNfts = [];
showDinos = false;
});

await dotenv.load(fileName: ".env");
Expand All @@ -233,137 +234,168 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
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>[
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>[
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 {
Expand Down

0 comments on commit fd6eeb8

Please sign in to comment.