forked from coral-xyz/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lang: Add
Lamports
trait (coral-xyz#2552)
- Loading branch information
1 parent
5624bfe
commit e55cd3e
Showing
10 changed files
with
178 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "lamports" | ||
version = "0.1.0" | ||
description = "Created with Anchor" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
|
||
[features] | ||
no-entrypoint = [] | ||
cpi = ["no-entrypoint"] | ||
|
||
[dependencies] | ||
anchor-lang = { path = "../../../../lang" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
declare_id!("Lamports11111111111111111111111111111111111"); | ||
|
||
#[program] | ||
pub mod lamports { | ||
use super::*; | ||
|
||
pub fn test_lamports_trait(ctx: Context<TestLamportsTrait>, amount: u64) -> Result<()> { | ||
let pda = &ctx.accounts.pda; | ||
let signer = &ctx.accounts.signer; | ||
|
||
// Transfer **to** PDA | ||
{ | ||
// Get the balance of the PDA **before** the transfer to PDA | ||
let pda_balance_before = pda.get_lamports(); | ||
|
||
// Transfer to the PDA | ||
anchor_lang::system_program::transfer( | ||
CpiContext::new( | ||
ctx.accounts.system_program.to_account_info(), | ||
anchor_lang::system_program::Transfer { | ||
from: signer.to_account_info(), | ||
to: pda.to_account_info(), | ||
}, | ||
), | ||
amount, | ||
)?; | ||
|
||
// Get the balance of the PDA **after** the transfer to PDA | ||
let pda_balance_after = pda.get_lamports(); | ||
|
||
// Validate balance | ||
require_eq!(pda_balance_after, pda_balance_before + amount); | ||
} | ||
|
||
// Transfer **from** PDA | ||
{ | ||
// Get the balance of the PDA **before** the transfer from PDA | ||
let pda_balance_before = pda.get_lamports(); | ||
|
||
// Transfer from the PDA | ||
pda.sub_lamports(amount)?; | ||
signer.add_lamports(amount)?; | ||
|
||
// Get the balance of the PDA **after** the transfer from PDA | ||
let pda_balance_after = pda.get_lamports(); | ||
|
||
// Validate balance | ||
require_eq!(pda_balance_after, pda_balance_before - amount); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Accounts)] | ||
pub struct TestLamportsTrait<'info> { | ||
#[account(mut)] | ||
pub signer: Signer<'info>, | ||
|
||
#[account( | ||
init, | ||
payer = signer, | ||
space = 8, | ||
seeds = [b"lamports"], | ||
bump | ||
)] | ||
pub pda: Account<'info, LamportsPda>, | ||
|
||
pub system_program: Program<'info, System>, | ||
} | ||
|
||
#[account] | ||
pub struct LamportsPda {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[scripts] | ||
test = "yarn run ts-mocha -t 1000000 ./tests/lamports/*.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as anchor from "@coral-xyz/anchor"; | ||
|
||
import { Lamports, IDL } from "../../target/types/lamports"; | ||
|
||
describe(IDL.name, () => { | ||
// Configure the client to use the local cluster | ||
anchor.setProvider(anchor.AnchorProvider.env()); | ||
|
||
const program = anchor.workspace.Lamports as anchor.Program<Lamports>; | ||
|
||
it("Can use the Lamports trait", async () => { | ||
const signer = program.provider.publicKey!; | ||
const [pda] = anchor.web3.PublicKey.findProgramAddressSync( | ||
[Buffer.from("lamports")], | ||
program.programId | ||
); | ||
|
||
await program.methods | ||
.testLamportsTrait(new anchor.BN(anchor.web3.LAMPORTS_PER_SOL)) | ||
.accounts({ signer, pda }) | ||
.rpc(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"types": ["mocha", "chai"], | ||
"typeRoots": ["./node_modules/@types"], | ||
"lib": ["es2015"], | ||
"module": "commonjs", | ||
"target": "es6", | ||
"esModuleInterop": true, | ||
"skipLibCheck": true | ||
}, | ||
} | ||
"compilerOptions": { | ||
"types": ["mocha", "node"], | ||
"lib": ["ES6"], | ||
"module": "commonjs", | ||
"target": "es6", | ||
"esModuleInterop": true, | ||
"skipLibCheck": true | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.