Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"dependencies": {
"@project-serum/anchor": "^0.24.2",
"@solana/spl-token": "^0.2.0",
"mitt": "^3.0.0",
"react-outside-click-handler": "^1.3.0"
},
Expand Down
1 change: 1 addition & 0 deletions programs/karma/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ overflow-checks = true

[dependencies]
anchor-lang = "0.24.2"
anchor-spl = "0.24.2"
43 changes: 41 additions & 2 deletions programs/karma/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
use anchor_lang::prelude::*;
use anchor_spl::token;
use anchor_spl::token::{MintTo, Token, Transfer};

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

// #[program]
// pub mod karma {
// use super::*;

// pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
// Ok(())
// }
// }

#[program]
pub mod karma {
pub mod karma_token {
use super::*;

pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
pub fn mint_token(ctx: Context<MintToken>) -> Result<()> {
// Create the MintTo struct for our context
let cpi_accounts: MintTo = MintTo {
mint: ctx.accounts.mint.to_account_info(),
to: ctx.accounts.token_account.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
};

let cpi_program:AccountInfo = ctx.accounts.token_program.to_account_info();
// Create the CpiContext we need for the request
let cpi_ctx: CpiContext<MintTo> = CpiContext::new(cpi_program, cpi_accounts);

// Execute anchor's helper function to mint tokens
token::mint_to(cpi_ctx, 10)?;

Ok(())
}
}

#[derive(Accounts)]
pub struct Initialize {}

#[derive(Accounts)]
pub struct MintToken<'info> {
/// CHECK: This is the token that we want to mint
#[account(mut)]
pub mint: UncheckedAccount<'info>,
pub token_program: Program<'info, Token>,
/// CHECK: This is the token account that we want to mint tokens to
#[account(mut)]
pub token_account: UncheckedAccount<'info>,
/// CHECK: the authority of the mint account
#[account(mut)]
pub authority: AccountInfo<'info>,
}
26 changes: 13 additions & 13 deletions tests/karma.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { Karma } from "../target/types/karma";
// import * as anchor from "@project-serum/anchor";
// import { Program } from "@project-serum/anchor";
// import { Karma } from "../target/types/karma";

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

const program = anchor.workspace.Karma as Program<Karma>;
// const program = anchor.workspace.Karma as Program<Karma>;

it("Is initialized!", async () => {
// Add your test here.
const tx = await program.methods.initialize().rpc();
console.log("Your transaction signature", tx);
});
});
// it("Is initialized!", async () => {
// // Add your test here.
// const tx = await program.methods.initialize().rpc();
// console.log("Your transaction signature", tx);
// });
// });
84 changes: 84 additions & 0 deletions tests/karmaToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { KarmaToken } from "../target/types/karma_token";
import {
TOKEN_PROGRAM_ID,
MINT_SIZE,
createAssociatedTokenAccountInstruction,
getAssociatedTokenAddress,
createInitializeMintInstruction,
} from "@solana/spl-token";
import { assert } from "chai";

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

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

// Generate a random keypair that will represent the token
const mintKeyPair: anchor.web3.Keypair = anchor.web3.Keypair.generate();

it("Minting a new token.", async () => {
// Get anchor wallet's public key
const walletPubKey = provider.wallet.publicKey;

// Amout of sol for rent
const lamports: number = await program.provider.connection.getMinimumBalanceForRentExemption(
MINT_SIZE
)

// Get associated token account(ATA) and acccount we want to own the ATA
const associatedTokenAccount = await getAssociatedTokenAddress(
mintKeyPair.publicKey,
walletPubKey
)

// Fire a list of instructions in a single transaction
const mint_tx = new anchor.web3.Transaction().add(
// Create ana account from the mint key we created
anchor.web3.SystemProgram.createAccount({
fromPubkey: walletPubKey,
newAccountPubkey: mintKeyPair.publicKey,
space: MINT_SIZE,
programId: TOKEN_PROGRAM_ID,
lamports
}),

// Transaction to create our mint account that is controlled by our anchor wallet
createInitializeMintInstruction(
mintKeyPair.publicKey, 0, walletPubKey, walletPubKey
),

// Create ATA that is associated with our mint on our anchor wallet
createAssociatedTokenAccountInstruction(
walletPubKey, associatedTokenAccount, walletPubKey, mintKeyPair.publicKey
)
)

// send and create the transaction
const res = await provider.sendAndConfirm(mint_tx, [mintKeyPair])

console.log(
await provider.connection.getParsedAccountInfo(mintKeyPair.publicKey)
)

console.log("Account: ", res);
console.log("Mint key: ", mintKeyPair.publicKey.toString());
console.log("User: ", walletPubKey.toString());

// Executes our code to mint our token into our specified ATA
await program.methods.mintToken().accounts({
mint: mintKeyPair.publicKey,
tokenProgram: TOKEN_PROGRAM_ID,
tokenAccount: associatedTokenAccount,
authority: walletPubKey,
}).rpc();

// Get minted token amount on the ATA for our anchor wallet
const minted = (await provider.connection.getParsedAccountInfo(associatedTokenAccount)).value.data.parsed.info.tokenAmount.amount;
assert.equal(minted, 10);
});
});
Loading