Skip to content

Instantly share code, notes, and snippets.

@buildgreatthings
Forked from h4rkl/harkl.js
Last active June 6, 2022 09:30
Show Gist options
  • Save buildgreatthings/f865afefcda74d1985c066fa26775a7c to your computer and use it in GitHub Desktop.
Save buildgreatthings/f865afefcda74d1985c066fa26775a7c to your computer and use it in GitHub Desktop.
Anchor Contract to Transfer Token from a Reserve to newly created wallet account
const anchor = require("@project-serum/anchor");
const serumCmn = require("@project-serum/common");
const { TOKEN_PROGRAM_ID } = require("@solana/spl-token");
describe("harkl-tests", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.Provider.env());
const program = anchor.workspace.Harkl;
const MINT_TOKENS = 4200000000000000; // 42M with 8dp
const MINT_DECIMALS = 8;
let mint = null;
let god = null;
let creatorAcc = anchor.web3.Keypair.generate();
let creatorTokenAcc = null;
it("Sets up initial test state", async () => {
const [_mint, _god] = await serumCmn.createMintAndVault(
program.provider,
new anchor.BN(MINT_TOKENS),
undefined,
MINT_DECIMALS
);
mint = _mint;
god = _god;
creatorTokenAcc =await serumCmn.createTokenAccount(
program.provider,
mint,
creatorAcc.publicKey
);
});
it("Actions an interaction", async () => {
const INTERACTION_FEE = 200000000000000;
// let [_authority, nonce] = await anchor.web3.PublicKey.findProgramAddress(
// [god.toBuffer()],
// program.programId
// );
// authority = _authority;
console.log('*************', {
from: god.toBase58(),
to: creatorTokenAcc.toBase58(),
tokenProgram: TOKEN_PROGRAM_ID.toBase58(),
programId: program.programId.toBase58(),
});
await program.rpc.interaction(new anchor.BN(INTERACTION_FEE), {
accounts: {
from: god,
to: creatorTokenAcc,
owner: program.provider.wallet.publicKey,
tokenProgram: TOKEN_PROGRAM_ID,
},
});
});
});
//! Harkl intereaction
use anchor_lang::prelude::*;
use anchor_spl::token::{self, TokenAccount, Transfer};
use std::convert::Into;
#[program]
pub mod harkl {
use super::*;
pub fn interaction(ctx: Context<Interaction>, interaction_fee: u64) -> ProgramResult {
let cpi_accounts = Transfer {
from: ctx.accounts.from.to_account_info().clone(),
to: ctx.accounts.to.to_account_info().clone(),
authority: ctx.accounts.owner.clone(),
};
let cpi_program = ctx.accounts.token_program.clone();
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
token::transfer(cpi_ctx, interaction_fee)?;
Ok(())
}
}
#[derive(Accounts)]
pub struct Interaction<'info> {
#[account(mut, has_one = owner)]
from: CpiAccount<'info, TokenAccount>,
#[account(mut, "from.mint == to.mint")]
to: CpiAccount<'info, TokenAccount>,
#[account(signer)]
owner: AccountInfo<'info>,
token_program: AccountInfo<'info>,
}
#[error]
pub enum ErrorCode {
#[msg("The derived interaction signer does not match that which was given.")]
InvalidInteractionSigner,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment