diff --git a/demos/frontend/07_create_dapp_cli/README.md b/demos/frontend/07_create_dapp_cli/README.md index 61254bef..aaf1d8c7 100644 --- a/demos/frontend/07_create_dapp_cli/README.md +++ b/demos/frontend/07_create_dapp_cli/README.md @@ -2,7 +2,7 @@
-### Creating a New Project +### 1. Creating a New Project
@@ -25,39 +25,17 @@ npx create-solana-dapp@latest npm install ``` -
- -* Start the Next.js app with: - -
- -``` -npm run dev -``` - - -
- - ---- - -### Starting the Web App
-``` -npm run dev -``` - -
--- -### Syncing the program id +### 2. Syncing the program id
-* Run the follow to create a new keypair in the `anchor/target/deploy` directory: +* Run the follow to create a new keypair inside the `./anchor/target/deploy` directory:
@@ -65,19 +43,11 @@ npm run dev npm run anchor keys sync ``` -
- -* Then save the address to the Anchor config file and update the `declare_id!` macro in the `./src/lib.rs` file of the program. - -
- -* Finally, update the constant in `anchor/lib/counter-exports.ts` to match the new program id. -
--- -### Building the Program: +### 3. Building the Program
@@ -89,19 +59,7 @@ npm run anchor-build --- -### Staring the Test Validator - -
- -```shell -npm run anchor-localnet -``` - -
- ---- - -### Running Tests +### 4. Running Tests
@@ -113,36 +71,39 @@ npm run anchor-test --- -### Deploying to Devnet +### 5. Building the dApp
```shell -npm run anchor deploy --provider.cluster devnet +npm run build ```
--- -### Starting the Web App +### 6. Starting the dApp
-```shell +``` npm run dev ``` -
--- +### 7. Deploying the dApp + +
-### Building the web app +* Start the validator in another window with `solana-test-validator`, and then run:
```shell -npm run build +npm run anchor deploy ``` + diff --git a/demos/frontend/07_create_dapp_cli/anchor/Anchor.toml b/demos/frontend/07_create_dapp_cli/anchor/Anchor.toml index 086dbd0e..28ed031f 100644 --- a/demos/frontend/07_create_dapp_cli/anchor/Anchor.toml +++ b/demos/frontend/07_create_dapp_cli/anchor/Anchor.toml @@ -5,7 +5,7 @@ seeds = false skip-lint = false [programs.localnet] -dapp_example = "5ASqQu2RHgcxLfvMhnpVpECWswBm8QHLz37duPosGh7" +counter = "FaC3oSqrzou2eaJft4UpCxZYtCe6hGa4WWSLTzg1yRMD" [registry] url = "https://api.apr.dev" diff --git a/demos/frontend/07_create_dapp_cli/anchor/Cargo.lock b/demos/frontend/07_create_dapp_cli/anchor/Cargo.lock index e427415b..326b0c61 100644 --- a/demos/frontend/07_create_dapp_cli/anchor/Cargo.lock +++ b/demos/frontend/07_create_dapp_cli/anchor/Cargo.lock @@ -617,7 +617,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2" [[package]] -name = "dapp-example" +name = "counter" version = "0.1.0" dependencies = [ "anchor-lang", diff --git a/demos/frontend/07_create_dapp_cli/anchor/package.json b/demos/frontend/07_create_dapp_cli/anchor/package.json index cd97ad64..809956bb 100644 --- a/demos/frontend/07_create_dapp_cli/anchor/package.json +++ b/demos/frontend/07_create_dapp_cli/anchor/package.json @@ -1,5 +1,5 @@ { - "name": "@dapp-example/anchor", + "name": "@counter/anchor", "version": "0.0.1", "dependencies": { "@coral-xyz/anchor": "^0.29.0", diff --git a/demos/frontend/07_create_dapp_cli/anchor/programs/dapp-example/Cargo.toml b/demos/frontend/07_create_dapp_cli/anchor/programs/counter/Cargo.toml similarity index 85% rename from demos/frontend/07_create_dapp_cli/anchor/programs/dapp-example/Cargo.toml rename to demos/frontend/07_create_dapp_cli/anchor/programs/counter/Cargo.toml index cc2b7359..0275e443 100644 --- a/demos/frontend/07_create_dapp_cli/anchor/programs/dapp-example/Cargo.toml +++ b/demos/frontend/07_create_dapp_cli/anchor/programs/counter/Cargo.toml @@ -1,12 +1,12 @@ [package] -name = "dapp-example" +name = "counter" version = "0.1.0" description = "Created with Anchor" edition = "2021" [lib] crate-type = ["cdylib", "lib"] -name = "dapp_example" +name = "counter" [features] no-entrypoint = [] diff --git a/demos/frontend/07_create_dapp_cli/anchor/programs/dapp-example/Xargo.toml b/demos/frontend/07_create_dapp_cli/anchor/programs/counter/Xargo.toml similarity index 100% rename from demos/frontend/07_create_dapp_cli/anchor/programs/dapp-example/Xargo.toml rename to demos/frontend/07_create_dapp_cli/anchor/programs/counter/Xargo.toml diff --git a/demos/frontend/07_create_dapp_cli/anchor/programs/dapp-example/src/lib.rs b/demos/frontend/07_create_dapp_cli/anchor/programs/counter/src/lib.rs similarity index 51% rename from demos/frontend/07_create_dapp_cli/anchor/programs/dapp-example/src/lib.rs rename to demos/frontend/07_create_dapp_cli/anchor/programs/counter/src/lib.rs index 9462dc6d..a2a7d883 100644 --- a/demos/frontend/07_create_dapp_cli/anchor/programs/dapp-example/src/lib.rs +++ b/demos/frontend/07_create_dapp_cli/anchor/programs/counter/src/lib.rs @@ -2,51 +2,51 @@ use anchor_lang::prelude::*; -declare_id!("5ASqQu2RHgcxLfvMhnpVpECWswBm8QHLz37duPosGh7"); +declare_id!("FaC3oSqrzou2eaJft4UpCxZYtCe6hGa4WWSLTzg1yRMD"); #[program] -pub mod dapp_example { +pub mod counter { use super::*; - pub fn close(_ctx: Context) -> Result<()> { + pub fn close(_ctx: Context) -> Result<()> { Ok(()) } pub fn decrement(ctx: Context) -> Result<()> { - ctx.accounts.dapp_example.count = ctx.accounts.dapp_example.count.checked_sub(1).unwrap(); + ctx.accounts.counter.count = ctx.accounts.counter.count.checked_sub(1).unwrap(); Ok(()) } pub fn increment(ctx: Context) -> Result<()> { - ctx.accounts.dapp_example.count = ctx.accounts.dapp_example.count.checked_add(1).unwrap(); + ctx.accounts.counter.count = ctx.accounts.counter.count.checked_add(1).unwrap(); Ok(()) } - pub fn initialize(_ctx: Context) -> Result<()> { + pub fn initialize(_ctx: Context) -> Result<()> { Ok(()) } pub fn set(ctx: Context, value: u8) -> Result<()> { - ctx.accounts.dapp_example.count = value.clone(); + ctx.accounts.counter.count = value.clone(); Ok(()) } } #[derive(Accounts)] -pub struct InitializeDappExample<'info> { +pub struct InitializeCounter<'info> { #[account(mut)] pub payer: Signer<'info>, #[account( init, - space = 8 + DappExample::INIT_SPACE, + space = 8 + Counter::INIT_SPACE, payer = payer )] - pub dapp_example: Account<'info, DappExample>, + pub counter: Account<'info, Counter>, pub system_program: Program<'info, System>, } #[derive(Accounts)] -pub struct CloseDappExample<'info> { +pub struct CloseCounter<'info> { #[account(mut)] pub payer: Signer<'info>, @@ -54,17 +54,17 @@ pub struct CloseDappExample<'info> { mut, close = payer, // close account and return lamports to payer )] - pub dapp_example: Account<'info, DappExample>, + pub counter: Account<'info, Counter>, } #[derive(Accounts)] pub struct Update<'info> { #[account(mut)] - pub dapp_example: Account<'info, DappExample>, + pub counter: Account<'info, Counter>, } #[account] #[derive(InitSpace)] -pub struct DappExample { +pub struct Counter { count: u8, } diff --git a/demos/frontend/07_create_dapp_cli/anchor/src/counter-exports.ts b/demos/frontend/07_create_dapp_cli/anchor/src/counter-exports.ts new file mode 100644 index 00000000..911e09d8 --- /dev/null +++ b/demos/frontend/07_create_dapp_cli/anchor/src/counter-exports.ts @@ -0,0 +1,25 @@ +// Here we export some useful types and functions for interacting with the Anchor program. +import { Cluster, PublicKey } from '@solana/web3.js'; +import type { Counter } from '../target/types/counter'; +import { IDL as CounterIDL } from '../target/types/counter'; + +// Re-export the generated IDL and type +export { Counter, CounterIDL }; + +// After updating your program ID (e.g. after running `anchor keys sync`) update the value below. +export const COUNTER_PROGRAM_ID = new PublicKey( + 'FaC3oSqrzou2eaJft4UpCxZYtCe6hGa4WWSLTzg1yRMD' +); + +// This is a helper function to get the program ID for the Counter program depending on the cluster. +export function getCounterProgramId(cluster: Cluster) { + switch (cluster) { + case 'devnet': + case 'testnet': + // This is the program ID for the Counter program on devnet and testnet. + return new PublicKey('CounNZdmsQmWh7uVngV9FXW2dZ6zAgbJyYsvBpqbykg'); + case 'mainnet-beta': + default: + return COUNTER_PROGRAM_ID; + } +} diff --git a/demos/frontend/07_create_dapp_cli/anchor/src/dapp-example-exports.ts b/demos/frontend/07_create_dapp_cli/anchor/src/dapp-example-exports.ts deleted file mode 100644 index 00b30600..00000000 --- a/demos/frontend/07_create_dapp_cli/anchor/src/dapp-example-exports.ts +++ /dev/null @@ -1,23 +0,0 @@ -// Here we export some useful types and functions for interacting with the Anchor program. -import { Cluster, PublicKey } from '@solana/web3.js'; -import type { DappExample } from '../target/types/dapp_example'; -import { IDL as DappExampleIDL } from '../target/types/dapp_example'; - -// Re-export the generated IDL and type -export { DappExample, DappExampleIDL }; - -// After updating your program ID (e.g. after running `anchor keys sync`) update the value below. -export const DAPP_EXAMPLE_PROGRAM_ID = new PublicKey( - '5ASqQu2RHgcxLfvMhnpVpECWswBm8QHLz37duPosGh7' -); - -// This is a helper function to get the program ID for the DappExample program depending on the cluster. -export function getDappExampleProgramId(cluster: Cluster) { - switch (cluster) { - case 'devnet': - case 'testnet': - case 'mainnet-beta': - default: - return DAPP_EXAMPLE_PROGRAM_ID; - } -} diff --git a/demos/frontend/07_create_dapp_cli/anchor/src/index.ts b/demos/frontend/07_create_dapp_cli/anchor/src/index.ts index de014c41..7d4a8ef8 100644 --- a/demos/frontend/07_create_dapp_cli/anchor/src/index.ts +++ b/demos/frontend/07_create_dapp_cli/anchor/src/index.ts @@ -1,3 +1,3 @@ // This file was generated by preset-anchor. Programs are exported from this file. -export * from './dapp-example-exports'; +export * from './counter-exports'; diff --git a/demos/frontend/07_create_dapp_cli/anchor/target/idl/dapp_example.json b/demos/frontend/07_create_dapp_cli/anchor/target/idl/counter.json similarity index 83% rename from demos/frontend/07_create_dapp_cli/anchor/target/idl/dapp_example.json rename to demos/frontend/07_create_dapp_cli/anchor/target/idl/counter.json index 3d0d812c..4cbae2af 100644 --- a/demos/frontend/07_create_dapp_cli/anchor/target/idl/dapp_example.json +++ b/demos/frontend/07_create_dapp_cli/anchor/target/idl/counter.json @@ -1,6 +1,6 @@ { "version": "0.1.0", - "name": "dapp_example", + "name": "counter", "instructions": [ { "name": "close", @@ -11,7 +11,7 @@ "isSigner": true }, { - "name": "dappExample", + "name": "counter", "isMut": true, "isSigner": false } @@ -22,7 +22,7 @@ "name": "decrement", "accounts": [ { - "name": "dappExample", + "name": "counter", "isMut": true, "isSigner": false } @@ -33,7 +33,7 @@ "name": "increment", "accounts": [ { - "name": "dappExample", + "name": "counter", "isMut": true, "isSigner": false } @@ -49,7 +49,7 @@ "isSigner": true }, { - "name": "dappExample", + "name": "counter", "isMut": true, "isSigner": true }, @@ -65,7 +65,7 @@ "name": "set", "accounts": [ { - "name": "dappExample", + "name": "counter", "isMut": true, "isSigner": false } @@ -80,7 +80,7 @@ ], "accounts": [ { - "name": "DappExample", + "name": "Counter", "type": { "kind": "struct", "fields": [ @@ -93,6 +93,6 @@ } ], "metadata": { - "address": "5ASqQu2RHgcxLfvMhnpVpECWswBm8QHLz37duPosGh7" + "address": "FaC3oSqrzou2eaJft4UpCxZYtCe6hGa4WWSLTzg1yRMD" } -} +} \ No newline at end of file diff --git a/demos/frontend/07_create_dapp_cli/anchor/target/types/counter.ts b/demos/frontend/07_create_dapp_cli/anchor/target/types/counter.ts new file mode 100644 index 00000000..bf730452 --- /dev/null +++ b/demos/frontend/07_create_dapp_cli/anchor/target/types/counter.ts @@ -0,0 +1,191 @@ +export type Counter = { + "version": "0.1.0", + "name": "counter", + "instructions": [ + { + "name": "close", + "accounts": [ + { + "name": "payer", + "isMut": true, + "isSigner": true + }, + { + "name": "counter", + "isMut": true, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "decrement", + "accounts": [ + { + "name": "counter", + "isMut": true, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "increment", + "accounts": [ + { + "name": "counter", + "isMut": true, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "initialize", + "accounts": [ + { + "name": "payer", + "isMut": true, + "isSigner": true + }, + { + "name": "counter", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "set", + "accounts": [ + { + "name": "counter", + "isMut": true, + "isSigner": false + } + ], + "args": [ + { + "name": "value", + "type": "u8" + } + ] + } + ], + "accounts": [ + { + "name": "counter", + "type": { + "kind": "struct", + "fields": [ + { + "name": "count", + "type": "u8" + } + ] + } + } + ] +}; + +export const IDL: Counter = { + "version": "0.1.0", + "name": "counter", + "instructions": [ + { + "name": "close", + "accounts": [ + { + "name": "payer", + "isMut": true, + "isSigner": true + }, + { + "name": "counter", + "isMut": true, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "decrement", + "accounts": [ + { + "name": "counter", + "isMut": true, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "increment", + "accounts": [ + { + "name": "counter", + "isMut": true, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "initialize", + "accounts": [ + { + "name": "payer", + "isMut": true, + "isSigner": true + }, + { + "name": "counter", + "isMut": true, + "isSigner": true + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } + ], + "args": [] + }, + { + "name": "set", + "accounts": [ + { + "name": "counter", + "isMut": true, + "isSigner": false + } + ], + "args": [ + { + "name": "value", + "type": "u8" + } + ] + } + ], + "accounts": [ + { + "name": "counter", + "type": { + "kind": "struct", + "fields": [ + { + "name": "count", + "type": "u8" + } + ] + } + } + ] +}; diff --git a/demos/frontend/07_create_dapp_cli/anchor/target/types/dapp_example.ts b/demos/frontend/07_create_dapp_cli/anchor/target/types/dapp_example.ts deleted file mode 100644 index 2956564f..00000000 --- a/demos/frontend/07_create_dapp_cli/anchor/target/types/dapp_example.ts +++ /dev/null @@ -1,191 +0,0 @@ -export type DappExample = { - version: '0.1.0'; - name: 'dapp_example'; - instructions: [ - { - name: 'close'; - accounts: [ - { - name: 'payer'; - isMut: true; - isSigner: true; - }, - { - name: 'dappExample'; - isMut: true; - isSigner: false; - } - ]; - args: []; - }, - { - name: 'decrement'; - accounts: [ - { - name: 'dappExample'; - isMut: true; - isSigner: false; - } - ]; - args: []; - }, - { - name: 'increment'; - accounts: [ - { - name: 'dappExample'; - isMut: true; - isSigner: false; - } - ]; - args: []; - }, - { - name: 'initialize'; - accounts: [ - { - name: 'payer'; - isMut: true; - isSigner: true; - }, - { - name: 'dappExample'; - isMut: true; - isSigner: true; - }, - { - name: 'systemProgram'; - isMut: false; - isSigner: false; - } - ]; - args: []; - }, - { - name: 'set'; - accounts: [ - { - name: 'dappExample'; - isMut: true; - isSigner: false; - } - ]; - args: [ - { - name: 'value'; - type: 'u8'; - } - ]; - } - ]; - accounts: [ - { - name: 'dappExample'; - type: { - kind: 'struct'; - fields: [ - { - name: 'count'; - type: 'u8'; - } - ]; - }; - } - ]; -}; - -export const IDL: DappExample = { - version: '0.1.0', - name: 'dapp_example', - instructions: [ - { - name: 'close', - accounts: [ - { - name: 'payer', - isMut: true, - isSigner: true, - }, - { - name: 'dappExample', - isMut: true, - isSigner: false, - }, - ], - args: [], - }, - { - name: 'decrement', - accounts: [ - { - name: 'dappExample', - isMut: true, - isSigner: false, - }, - ], - args: [], - }, - { - name: 'increment', - accounts: [ - { - name: 'dappExample', - isMut: true, - isSigner: false, - }, - ], - args: [], - }, - { - name: 'initialize', - accounts: [ - { - name: 'payer', - isMut: true, - isSigner: true, - }, - { - name: 'dappExample', - isMut: true, - isSigner: true, - }, - { - name: 'systemProgram', - isMut: false, - isSigner: false, - }, - ], - args: [], - }, - { - name: 'set', - accounts: [ - { - name: 'dappExample', - isMut: true, - isSigner: false, - }, - ], - args: [ - { - name: 'value', - type: 'u8', - }, - ], - }, - ], - accounts: [ - { - name: 'dappExample', - type: { - kind: 'struct', - fields: [ - { - name: 'count', - type: 'u8', - }, - ], - }, - }, - ], -}; diff --git a/demos/frontend/07_create_dapp_cli/anchor/tests/counter.spec.ts b/demos/frontend/07_create_dapp_cli/anchor/tests/counter.spec.ts new file mode 100644 index 00000000..18a1f603 --- /dev/null +++ b/demos/frontend/07_create_dapp_cli/anchor/tests/counter.spec.ts @@ -0,0 +1,100 @@ +import * as anchor from '@coral-xyz/anchor'; +import { Program } from '@coral-xyz/anchor'; +import { Keypair } from '@solana/web3.js'; +import { Counter } from '../target/types/counter'; + +describe('counter', () => { + // Configure the client to use the local cluster. + const provider = anchor.AnchorProvider.env(); + anchor.setProvider(provider); + const payer = provider.wallet as anchor.Wallet; + + const program = anchor.workspace.Counter as Program; + + const counterKeypair = Keypair.generate(); + + it('Initialize Counter', async () => { + await program.methods + .initialize() + .accounts({ + counter: counterKeypair.publicKey, + payer: payer.publicKey, + }) + .signers([counterKeypair]) + .rpc(); + + const currentCount = await program.account.counter.fetch( + counterKeypair.publicKey + ); + + expect(currentCount.count).toEqual(0); + }); + + it('Increment Counter', async () => { + await program.methods + .increment() + .accounts({ counter: counterKeypair.publicKey }) + .rpc(); + + const currentCount = await program.account.counter.fetch( + counterKeypair.publicKey + ); + + expect(currentCount.count).toEqual(1); + }); + + it('Increment Counter Again', async () => { + await program.methods + .increment() + .accounts({ counter: counterKeypair.publicKey }) + .rpc(); + + const currentCount = await program.account.counter.fetch( + counterKeypair.publicKey + ); + + expect(currentCount.count).toEqual(2); + }); + + it('Decrement Counter', async () => { + await program.methods + .decrement() + .accounts({ counter: counterKeypair.publicKey }) + .rpc(); + + const currentCount = await program.account.counter.fetch( + counterKeypair.publicKey + ); + + expect(currentCount.count).toEqual(1); + }); + + it('Set counter value', async () => { + await program.methods + .set(42) + .accounts({ counter: counterKeypair.publicKey }) + .rpc(); + + const currentCount = await program.account.counter.fetch( + counterKeypair.publicKey + ); + + expect(currentCount.count).toEqual(42); + }); + + it('Set close the counter account', async () => { + await program.methods + .close() + .accounts({ + payer: payer.publicKey, + counter: counterKeypair.publicKey, + }) + .rpc(); + + // The account should no longer exist, returning null. + const userAccount = await program.account.counter.fetchNullable( + counterKeypair.publicKey + ); + expect(userAccount).toBeNull(); + }); +}); diff --git a/demos/frontend/07_create_dapp_cli/anchor/tests/dapp-example.spec.ts b/demos/frontend/07_create_dapp_cli/anchor/tests/dapp-example.spec.ts deleted file mode 100644 index c51d0ecb..00000000 --- a/demos/frontend/07_create_dapp_cli/anchor/tests/dapp-example.spec.ts +++ /dev/null @@ -1,100 +0,0 @@ -import * as anchor from '@coral-xyz/anchor'; -import { Program } from '@coral-xyz/anchor'; -import { Keypair } from '@solana/web3.js'; -import { DappExample } from '../target/types/dapp_example'; - -describe('dapp-example', () => { - // Configure the client to use the local cluster. - const provider = anchor.AnchorProvider.env(); - anchor.setProvider(provider); - const payer = provider.wallet as anchor.Wallet; - - const program = anchor.workspace.DappExample as Program; - - const dappExampleKeypair = Keypair.generate(); - - it('Initialize DappExample', async () => { - await program.methods - .initialize() - .accounts({ - dappExample: dappExampleKeypair.publicKey, - payer: payer.publicKey, - }) - .signers([dappExampleKeypair]) - .rpc(); - - const currentCount = await program.account.dappExample.fetch( - dappExampleKeypair.publicKey - ); - - expect(currentCount.count).toEqual(0); - }); - - it('Increment DappExample', async () => { - await program.methods - .increment() - .accounts({ dappExample: dappExampleKeypair.publicKey }) - .rpc(); - - const currentCount = await program.account.dappExample.fetch( - dappExampleKeypair.publicKey - ); - - expect(currentCount.count).toEqual(1); - }); - - it('Increment DappExample Again', async () => { - await program.methods - .increment() - .accounts({ dappExample: dappExampleKeypair.publicKey }) - .rpc(); - - const currentCount = await program.account.dappExample.fetch( - dappExampleKeypair.publicKey - ); - - expect(currentCount.count).toEqual(2); - }); - - it('Decrement DappExample', async () => { - await program.methods - .decrement() - .accounts({ dappExample: dappExampleKeypair.publicKey }) - .rpc(); - - const currentCount = await program.account.dappExample.fetch( - dappExampleKeypair.publicKey - ); - - expect(currentCount.count).toEqual(1); - }); - - it('Set dappExample value', async () => { - await program.methods - .set(42) - .accounts({ dappExample: dappExampleKeypair.publicKey }) - .rpc(); - - const currentCount = await program.account.dappExample.fetch( - dappExampleKeypair.publicKey - ); - - expect(currentCount.count).toEqual(42); - }); - - it('Set close the dappExample account', async () => { - await program.methods - .close() - .accounts({ - payer: payer.publicKey, - dappExample: dappExampleKeypair.publicKey, - }) - .rpc(); - - // The account should no longer exist, returning null. - const userAccount = await program.account.dappExample.fetchNullable( - dappExampleKeypair.publicKey - ); - expect(userAccount).toBeNull(); - }); -}); diff --git a/demos/frontend/07_create_dapp_cli/package-lock.json b/demos/frontend/07_create_dapp_cli/package-lock.json index 6ec82a5b..f41f0a92 100644 --- a/demos/frontend/07_create_dapp_cli/package-lock.json +++ b/demos/frontend/07_create_dapp_cli/package-lock.json @@ -1,11 +1,11 @@ { - "name": "@dapp-example/source", + "name": "@counter/source", "version": "0.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "@dapp-example/source", + "name": "@counter/source", "version": "0.0.0", "license": "MIT", "dependencies": { diff --git a/demos/frontend/07_create_dapp_cli/package.json b/demos/frontend/07_create_dapp_cli/package.json index 54b49e56..b093c4ce 100644 --- a/demos/frontend/07_create_dapp_cli/package.json +++ b/demos/frontend/07_create_dapp_cli/package.json @@ -1,5 +1,5 @@ { - "name": "@dapp-example/source", + "name": "@counter/source", "version": "0.0.0", "license": "MIT", "scripts": { diff --git a/demos/frontend/07_create_dapp_cli/tsconfig.base.json b/demos/frontend/07_create_dapp_cli/tsconfig.base.json index 48959fb4..55f90cb7 100644 --- a/demos/frontend/07_create_dapp_cli/tsconfig.base.json +++ b/demos/frontend/07_create_dapp_cli/tsconfig.base.json @@ -16,7 +16,7 @@ "baseUrl": ".", "paths": { "@/*": ["./web/*"], - "@dapp-example/anchor": ["anchor/src/index.ts"] + "@counter/anchor": ["anchor/src/index.ts"] } }, "exclude": ["node_modules", "tmp"] diff --git a/demos/frontend/07_create_dapp_cli/web/app/counter/page.tsx b/demos/frontend/07_create_dapp_cli/web/app/counter/page.tsx new file mode 100644 index 00000000..021e9af5 --- /dev/null +++ b/demos/frontend/07_create_dapp_cli/web/app/counter/page.tsx @@ -0,0 +1,5 @@ +import CounterFeature from '@/components/counter/counter-feature'; + +export default function Page() { + return ; +} diff --git a/demos/frontend/07_create_dapp_cli/web/app/dapp-example/page.tsx b/demos/frontend/07_create_dapp_cli/web/app/dapp-example/page.tsx deleted file mode 100644 index c432cb21..00000000 --- a/demos/frontend/07_create_dapp_cli/web/app/dapp-example/page.tsx +++ /dev/null @@ -1,5 +0,0 @@ -import DappExampleFeature from '@/components/dapp-example/dapp-example-feature'; - -export default function Page() { - return ; -} diff --git a/demos/frontend/07_create_dapp_cli/web/app/layout.tsx b/demos/frontend/07_create_dapp_cli/web/app/layout.tsx index 47d5ee25..b7d0fd9d 100644 --- a/demos/frontend/07_create_dapp_cli/web/app/layout.tsx +++ b/demos/frontend/07_create_dapp_cli/web/app/layout.tsx @@ -5,14 +5,14 @@ import { SolanaProvider } from '@/components/solana/solana-provider'; import { ReactQueryProvider } from './react-query-provider'; export const metadata = { - title: 'Urani Labs', - description: 'Urani Labs Demo', + title: 'counter', + description: 'Generated by create-solana-dapp', }; const links: { label: string; path: string }[] = [ { label: 'Account', path: '/account' }, { label: 'Clusters', path: '/clusters' }, - { label: 'DappExample Program', path: '/dapp-example' }, + { label: 'Counter Program', path: '/counter' }, ]; export default function RootLayout({ diff --git a/demos/frontend/07_create_dapp_cli/web/components/dapp-example/dapp-example-data-access.tsx b/demos/frontend/07_create_dapp_cli/web/components/counter/counter-data-access.tsx similarity index 63% rename from demos/frontend/07_create_dapp_cli/web/components/dapp-example/dapp-example-data-access.tsx rename to demos/frontend/07_create_dapp_cli/web/components/counter/counter-data-access.tsx index 4a570a32..d849228f 100644 --- a/demos/frontend/07_create_dapp_cli/web/components/dapp-example/dapp-example-data-access.tsx +++ b/demos/frontend/07_create_dapp_cli/web/components/counter/counter-data-access.tsx @@ -1,6 +1,6 @@ 'use client'; -import { DappExampleIDL, getDappExampleProgramId } from '@dapp-example/anchor'; +import { CounterIDL, getCounterProgramId } from '@counter/anchor'; import { Program } from '@coral-xyz/anchor'; import { useConnection } from '@solana/wallet-adapter-react'; import { Cluster, Keypair, PublicKey } from '@solana/web3.js'; @@ -11,20 +11,20 @@ import { useCluster } from '../cluster/cluster-data-access'; import { useAnchorProvider } from '../solana/solana-provider'; import { useTransactionToast } from '../ui/ui-layout'; -export function useDappExampleProgram() { +export function useCounterProgram() { const { connection } = useConnection(); const { cluster } = useCluster(); const transactionToast = useTransactionToast(); const provider = useAnchorProvider(); const programId = useMemo( - () => getDappExampleProgramId(cluster.network as Cluster), + () => getCounterProgramId(cluster.network as Cluster), [cluster] ); - const program = new Program(DappExampleIDL, programId, provider); + const program = new Program(CounterIDL, programId, provider); const accounts = useQuery({ - queryKey: ['dapp-example', 'all', { cluster }], - queryFn: () => program.account.dappExample.all(), + queryKey: ['counter', 'all', { cluster }], + queryFn: () => program.account.counter.all(), }); const getProgramAccount = useQuery({ @@ -33,11 +33,11 @@ export function useDappExampleProgram() { }); const initialize = useMutation({ - mutationKey: ['dapp-example', 'initialize', { cluster }], + mutationKey: ['counter', 'initialize', { cluster }], mutationFn: (keypair: Keypair) => program.methods .initialize() - .accounts({ dappExample: keypair.publicKey }) + .accounts({ counter: keypair.publicKey }) .signers([keypair]) .rpc(), onSuccess: (signature) => { @@ -56,24 +56,20 @@ export function useDappExampleProgram() { }; } -export function useDappExampleProgramAccount({ - account, -}: { - account: PublicKey; -}) { +export function useCounterProgramAccount({ account }: { account: PublicKey }) { const { cluster } = useCluster(); const transactionToast = useTransactionToast(); - const { program, accounts } = useDappExampleProgram(); + const { program, accounts } = useCounterProgram(); const accountQuery = useQuery({ - queryKey: ['dapp-example', 'fetch', { cluster, account }], - queryFn: () => program.account.dappExample.fetch(account), + queryKey: ['counter', 'fetch', { cluster, account }], + queryFn: () => program.account.counter.fetch(account), }); const closeMutation = useMutation({ - mutationKey: ['dapp-example', 'close', { cluster, account }], + mutationKey: ['counter', 'close', { cluster, account }], mutationFn: () => - program.methods.close().accounts({ dappExample: account }).rpc(), + program.methods.close().accounts({ counter: account }).rpc(), onSuccess: (tx) => { transactionToast(tx); return accounts.refetch(); @@ -81,9 +77,9 @@ export function useDappExampleProgramAccount({ }); const decrementMutation = useMutation({ - mutationKey: ['dapp-example', 'decrement', { cluster, account }], + mutationKey: ['counter', 'decrement', { cluster, account }], mutationFn: () => - program.methods.decrement().accounts({ dappExample: account }).rpc(), + program.methods.decrement().accounts({ counter: account }).rpc(), onSuccess: (tx) => { transactionToast(tx); return accountQuery.refetch(); @@ -91,9 +87,9 @@ export function useDappExampleProgramAccount({ }); const incrementMutation = useMutation({ - mutationKey: ['dapp-example', 'increment', { cluster, account }], + mutationKey: ['counter', 'increment', { cluster, account }], mutationFn: () => - program.methods.increment().accounts({ dappExample: account }).rpc(), + program.methods.increment().accounts({ counter: account }).rpc(), onSuccess: (tx) => { transactionToast(tx); return accountQuery.refetch(); @@ -101,9 +97,9 @@ export function useDappExampleProgramAccount({ }); const setMutation = useMutation({ - mutationKey: ['dapp-example', 'set', { cluster, account }], + mutationKey: ['counter', 'set', { cluster, account }], mutationFn: (value: number) => - program.methods.set(value).accounts({ dappExample: account }).rpc(), + program.methods.set(value).accounts({ counter: account }).rpc(), onSuccess: (tx) => { transactionToast(tx); return accountQuery.refetch(); diff --git a/demos/frontend/07_create_dapp_cli/web/components/dapp-example/dapp-example-feature.tsx b/demos/frontend/07_create_dapp_cli/web/components/counter/counter-feature.tsx similarity index 75% rename from demos/frontend/07_create_dapp_cli/web/components/dapp-example/dapp-example-feature.tsx rename to demos/frontend/07_create_dapp_cli/web/components/counter/counter-feature.tsx index c604d8d9..d7f82c78 100644 --- a/demos/frontend/07_create_dapp_cli/web/components/dapp-example/dapp-example-feature.tsx +++ b/demos/frontend/07_create_dapp_cli/web/components/counter/counter-feature.tsx @@ -4,17 +4,17 @@ import { useWallet } from '@solana/wallet-adapter-react'; import { WalletButton } from '../solana/solana-provider'; import { AppHero, ellipsify } from '../ui/ui-layout'; import { ExplorerLink } from '../cluster/cluster-ui'; -import { useDappExampleProgram } from './dapp-example-data-access'; -import { DappExampleCreate, DappExampleList } from './dapp-example-ui'; +import { useCounterProgram } from './counter-data-access'; +import { CounterCreate, CounterList } from './counter-ui'; -export default function DappExampleFeature() { +export default function CounterFeature() { const { publicKey } = useWallet(); - const { programId } = useDappExampleProgram(); + const { programId } = useCounterProgram(); return publicKey ? (

- +
- +
) : (
diff --git a/demos/frontend/07_create_dapp_cli/web/components/dapp-example/dapp-example-ui.tsx b/demos/frontend/07_create_dapp_cli/web/components/counter/counter-ui.tsx similarity index 90% rename from demos/frontend/07_create_dapp_cli/web/components/dapp-example/dapp-example-ui.tsx rename to demos/frontend/07_create_dapp_cli/web/components/counter/counter-ui.tsx index c6636f35..b4bd313a 100644 --- a/demos/frontend/07_create_dapp_cli/web/components/dapp-example/dapp-example-ui.tsx +++ b/demos/frontend/07_create_dapp_cli/web/components/counter/counter-ui.tsx @@ -5,12 +5,12 @@ import { useMemo } from 'react'; import { ellipsify } from '../ui/ui-layout'; import { ExplorerLink } from '../cluster/cluster-ui'; import { - useDappExampleProgram, - useDappExampleProgramAccount, -} from './dapp-example-data-access'; + useCounterProgram, + useCounterProgramAccount, +} from './counter-data-access'; -export function DappExampleCreate() { - const { initialize } = useDappExampleProgram(); +export function CounterCreate() { + const { initialize } = useCounterProgram(); return (