df-sol

Crates.iodf-sol
lib.rsdf-sol
version0.1.4
sourcesrc
created_at2024-06-08 07:08:37.657791
updated_at2024-06-13 14:21:28.922648
descriptioncreate template repo for anchor framework
homepage
repositoryhttps://github.com/dwarvesf/df-sol
max_upload_size
id1265525
size113,687
Huy Nguyen Quang (quanghuynguyen1902)

documentation

README

Df Sol

A Rust package designed to help you create template repositories easily to start with anchor framework on Solana. This README.md file will guide you through the installation, usage, and contribution process for this package.

Table of Contents

  1. Setting up the environment
  2. Creating a new project
  3. Writing and compiling smart contracts
  4. Testing contracts
  5. Deploying to a live network
  6. Integrate with Frontend

Setting up the environment

To use package, you need to have Rust, nodejs, and git installed on your system. If you don't have them installed:

Once these packages are installed, install df-sol package by run:

cargo install df-sol

Creating a new project

To create a new project, you can use the following command:

df-sol init <name-project>

Example

df-sol init counter

To create a project using an optional template

df-sol init <name-project> --template <template>

Example

df-sol init counter-program --template counter

Program template includes:

  • basic: Generate basic template
  • counter: Generate counter template
  • mint-token: Generate mint token template

Navigate to the folder you created and use Devbox to install the environment. If you don't install, follow Follow the instruction from the installation guide. Open a terminal in that folder.

  • Init devbox
    devbox init
    
  • Start a new shell
    devbox shell --pure
    

Writing and compiling smart contracts

Writing smart contracts

Start by creating a new directory called programs and write logic smart contract to file ./src/lib.rs.

Compiling contracts

To compile the contract run anchor build in your terminal

 anchor build

Testing contracts

A file test is generated for you. The shell of the test imports the Anchor framework files and gets the program ready to run. The tests to using the mocha test framework, so each it function defines a test and describe can be used to group tests together.

import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Counter } from "../target/types/counter";

describe("counter", () => {
  // Configure the client to use the local cluster.
  anchor.setProvider(anchor.AnchorProvider.env());

  const program = anchor.workspace.Counter as Program<Counter>;

  it("Is initialized!", async () => {
    // Add your test here.
    const tx = await program.methods.initialize().rpc();
    console.log("Your transaction signature", tx);
  });
});

To run test, use:

anchor test

Deploying to a live network

Once you're ready to share your dApp with other people, you may want to deploy it to a live network. This way others can access an instance that's not running locally on your system.

The mainnet Solana network deals with real money, but there are separate devnet networks that do not.

To deploy to the devnet network, follow these steps:

  1. Configure Solana URL to Devnet

    solana config set --url https://api.devnet.solana.com
    
  2. Airdrop SOL to Address

  • To deploy a program to the devnet network, ensure that you have SOL in your wallet. The amount of SOL depends on the size of the program.

  • Get the address of the wallet:

    solana address --keypair wallet.json
    
  • Airdrop SOL to your address:

    solana airdrop 2 <address>
    
  • Check the balance of the address:

    solana balance <address>
    
  1. Deploy Program

    anchor deploy
    
  2. Test Program

    anchor test --skip-deploy
    

Integrate with Frontend

Import the generated TypeScript module into your front-end application, and use it to interact with your program. The module provides functions that correspond to the functions defined in your IDL.

Copy idl of your program from target/idl/{project_name}.json to file idl.json in your front-end project folder. Then, following the code below:

import { Program, AnchorProvider, setProvider } from "@project-serum/anchor";
import { Connection, KeyPair, PublicKey } from "@solana/web3.js";
import idl from "./idl.json";
import { Idl } from "@coral-xyz/anchor";
// where IDL is the .json created by anchor build

export const yourFunction = async () => {
    const wallet = KeyPair.generate();
    const connection = new Connection("https://api.devnet.solana.com");
    const provider = new AnchorProvider(connection, wallet, {});
    setProvider(provider);
    const programId = new PublicKey(idl.address);
    const program = new Program(idl as Idl, programId);
    // ... your code
    // e.g. await program.methods.yourMethod(YOUR_PARAMETERS).accounts({YOUR_ACCOUNTS}).rpc();
};

Reporting Issues

If you encounter any issues, please report them on the GitHub Issues page.

Thank you for using Df Sol! We hope this package helps you manage your templates efficiently. If you have any questions or feedback, feel free to open an issue on GitHub.

Commit count: 5

cargo fmt