Skip to content

Commit

Permalink
fix: resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
dnielopez committed Oct 12, 2023
2 parents da398ee + 1413266 commit 6076923
Show file tree
Hide file tree
Showing 10 changed files with 743 additions and 7 deletions.
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.

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.

207 changes: 205 additions & 2 deletions lib/pages/my-dinogrow/my_dinogrow.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
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:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:solana/solana.dart';

import '../../anchor_types/score_parameters.dart' as anchor_types_parameters;
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 {
final String address;

Expand Down Expand Up @@ -35,7 +44,11 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
const SizedBox(height: 30),
IntroButtonWidget(
text: 'Claim your Dino',
onPressed: _onClaim,
onPressed: createNft,
),
IntroButtonWidget(
text: 'Save Score',
onPressed: saveScore,
),
const SizedBox(height: 30),
Container(
Expand Down Expand Up @@ -229,4 +242,194 @@ class _MydinogrowScreenState extends State<MydinogrowScreen> {
});
}
}

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');
}

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');
}
}
Loading

0 comments on commit 6076923

Please sign in to comment.