Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
clean up dapp example
Browse files Browse the repository at this point in the history
  • Loading branch information
URANI committed Mar 28, 2024
1 parent af9c320 commit d72779f
Show file tree
Hide file tree
Showing 28 changed files with 413 additions and 454 deletions.
69 changes: 15 additions & 54 deletions demos/frontend/07_create_dapp_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<br>

### Creating a New Project
### 1. Creating a New Project

<br>

Expand All @@ -25,59 +25,29 @@ npx create-solana-dapp@latest
npm install
```

<br>

* Start the Next.js app with:

<br>

```
npm run dev
```


<br>


---

### Starting the Web App

<br>

```
npm run dev
```

<br>

---

### Syncing the program id
### 2. Syncing the program id

<br>

* 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:

<br>

```shell
npm run anchor keys sync
```

<br>

* Then save the address to the Anchor config file and update the `declare_id!` macro in the `./src/lib.rs` file of the program.

<br>

* Finally, update the constant in `anchor/lib/counter-exports.ts` to match the new program id.

<br>

---

### Building the Program:
### 3. Building the Program

<br>

Expand All @@ -89,19 +59,7 @@ npm run anchor-build

---

### Staring the Test Validator

<br>

```shell
npm run anchor-localnet
```

<br>

---

### Running Tests
### 4. Running Tests

<br>

Expand All @@ -113,36 +71,39 @@ npm run anchor-test

---

### Deploying to Devnet
### 5. Building the dApp

<br>

```shell
npm run anchor deploy --provider.cluster devnet
npm run build
```

<br>

---

### Starting the Web App
### 6. Starting the dApp

<br>

```shell
```
npm run dev
```


<br>

---

### 7. Deploying the dApp

<br>

### Building the web app
* Start the validator in another window with `solana-test-validator`, and then run:

<br>

```shell
npm run build
npm run anchor deploy
```

2 changes: 1 addition & 1 deletion demos/frontend/07_create_dapp_cli/anchor/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ seeds = false
skip-lint = false

[programs.localnet]
dapp_example = "5ASqQu2RHgcxLfvMhnpVpECWswBm8QHLz37duPosGh7"
counter = "FaC3oSqrzou2eaJft4UpCxZYtCe6hGa4WWSLTzg1yRMD"

[registry]
url = "https://api.apr.dev"
Expand Down
2 changes: 1 addition & 1 deletion demos/frontend/07_create_dapp_cli/anchor/Cargo.lock

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

2 changes: 1 addition & 1 deletion demos/frontend/07_create_dapp_cli/anchor/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@dapp-example/anchor",
"name": "@counter/anchor",
"version": "0.0.1",
"dependencies": {
"@coral-xyz/anchor": "^0.29.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,69 @@

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<CloseDappExample>) -> Result<()> {
pub fn close(_ctx: Context<CloseCounter>) -> Result<()> {
Ok(())
}

pub fn decrement(ctx: Context<Update>) -> 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<Update>) -> 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<InitializeDappExample>) -> Result<()> {
pub fn initialize(_ctx: Context<InitializeCounter>) -> Result<()> {
Ok(())
}

pub fn set(ctx: Context<Update>, 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>,

#[account(
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,
}
25 changes: 25 additions & 0 deletions demos/frontend/07_create_dapp_cli/anchor/src/counter-exports.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion demos/frontend/07_create_dapp_cli/anchor/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "0.1.0",
"name": "dapp_example",
"name": "counter",
"instructions": [
{
"name": "close",
Expand All @@ -11,7 +11,7 @@
"isSigner": true
},
{
"name": "dappExample",
"name": "counter",
"isMut": true,
"isSigner": false
}
Expand All @@ -22,7 +22,7 @@
"name": "decrement",
"accounts": [
{
"name": "dappExample",
"name": "counter",
"isMut": true,
"isSigner": false
}
Expand All @@ -33,7 +33,7 @@
"name": "increment",
"accounts": [
{
"name": "dappExample",
"name": "counter",
"isMut": true,
"isSigner": false
}
Expand All @@ -49,7 +49,7 @@
"isSigner": true
},
{
"name": "dappExample",
"name": "counter",
"isMut": true,
"isSigner": true
},
Expand All @@ -65,7 +65,7 @@
"name": "set",
"accounts": [
{
"name": "dappExample",
"name": "counter",
"isMut": true,
"isSigner": false
}
Expand All @@ -80,7 +80,7 @@
],
"accounts": [
{
"name": "DappExample",
"name": "Counter",
"type": {
"kind": "struct",
"fields": [
Expand All @@ -93,6 +93,6 @@
}
],
"metadata": {
"address": "5ASqQu2RHgcxLfvMhnpVpECWswBm8QHLz37duPosGh7"
"address": "FaC3oSqrzou2eaJft4UpCxZYtCe6hGa4WWSLTzg1yRMD"
}
}
}
Loading

0 comments on commit d72779f

Please sign in to comment.