5 releases
0.1.4 | Jun 13, 2024 |
---|---|
0.1.3 | Jun 13, 2024 |
0.1.2 | Jun 8, 2024 |
0.1.1 | Jun 8, 2024 |
0.1.0 | Jun 8, 2024 |
#7 in #sol
43KB
1K
SLoC
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
- Setting up the environment
- Creating a new project
- Writing and compiling smart contracts
- Testing contracts
- Deploying to a live network
- 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:
- Install
rust
from rust-lang.org, - Install
node.js >=18.0
from nodejs.org - Install
git
from git
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:
-
Configure Solana URL to Devnet
solana config set --url https://api.devnet.solana.com
-
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>
-
Deploy Program
anchor deploy
-
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.
Dependencies
~25–40MB
~690K SLoC