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

mint dino #5

Merged
merged 1 commit 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
19 changes: 19 additions & 0 deletions lib/anchor_types/nft_parameters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:borsh_annotation/borsh_annotation.dart';

part 'nft_parameters.g.dart';

@BorshSerializable()
class NftArguments with _$NftArguments {
factory NftArguments(
{
@BString() required String id,
@BString() required String name,
@BString() required String symbol,
@BString() required String uri
}) = _NftArguments;

const NftArguments._();

factory NftArguments.fromBorsh(Uint8List data) =>
_$NftArgumentsFromBorsh(data);
}
64 changes: 64 additions & 0 deletions lib/anchor_types/nft_parameters.g.dart

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

134 changes: 133 additions & 1 deletion lib/pages/my-dinogrow/my_dinogrow.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
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/solana.dart';

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/utils/buffer.dart' as solana_buffer;
import '../../anchor_types/nft_parameters.dart' as anchor_types;

class MydinogrowScreen extends StatefulWidget {
const MydinogrowScreen({super.key});

Expand All @@ -26,7 +35,7 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
const SizedBox(height: 30),
IntroButtonWidget(
text: 'Claim your Dino',
onPressed: _onClaim,
onPressed: createNft,
),
const SizedBox(height: 30),
Container(
Expand Down Expand Up @@ -140,4 +149,127 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
color: filters[index],
);
}

createNft() 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 programIdPublicKey = solana.Ed25519HDPublicKey.fromBase58(programId);

int idrnd = Random().nextInt(999);
String id = "Dino$idrnd";
print(id);

final nftMintPda = await solana.Ed25519HDPublicKey.findProgramAddress(
programId: programIdPublicKey,
seeds: [
solana_buffer.Buffer.fromString("mint"),
solana_buffer.Buffer.fromString(id),
]);
print(nftMintPda.toBase58());

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

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 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),
],
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');
}
}
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import FlutterMacOS
import Foundation

import connectivity_plus
import flutter_secure_storage_macos
import url_launcher_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin"))
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}
Loading