Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add precommit #238

Merged
merged 4 commits into from
Sep 14, 2023
Merged
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
27 changes: 27 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Pre-commit checks

on:
pull_request:
push:
branches: [main]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
# Need to grab the history of the PR
fetch-depth: 0
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2023-03-01
components: rustfmt, clippy
- uses: pre-commit/[email protected]
if: ${{ github.event_name == 'pull_request' }}
with:
# Run only on files changed in the PR
extra_args: --from-ref ${{ github.event.pull_request.base.sha }} --to-ref ${{ github.event.pull_request.head.sha }}
- uses: pre-commit/[email protected]
if: ${{ github.event_name != 'pull_request' }}
26 changes: 0 additions & 26 deletions .github/workflows/prettier.yml

This file was deleted.

22 changes: 0 additions & 22 deletions .github/workflows/rustfmt.yml

This file was deleted.

24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
exclude: package-lock.json
# Hook to format many type of files in the repo
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v2.7.1"
hooks:
- id: prettier
additional_dependencies:
- "[email protected]"
- repo: local
hooks:
# Hooks for the staking program
- id: cargo-fmt-staking
name: Cargo format for staking
language: "rust"
entry: cargo +nightly-2023-03-01 fmt --manifest-path ./staking/Cargo.toml --all -- --config-path rustfmt.toml
pass_filenames: false
files: staking
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
frontend/.next
frontend/out
staking/target
staking/lib
wasm
19 changes: 19 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
edition = "2021"

# Merge all imports into a clean vertical list of module imports.
imports_granularity = "One"
group_imports = "One"
imports_layout = "Vertical"

# Better grep-ability.
empty_item_single_line = false

# Consistent pipe layout.
match_arm_leading_pipes = "Preserve"

# Align Fields
enum_discrim_align_threshold = 80
struct_field_align_threshold = 80

# Allow up to two blank lines for visual grouping.
blank_lines_upper_bound = 2
2 changes: 0 additions & 2 deletions staking/.husky/pre-commit

This file was deleted.

16 changes: 2 additions & 14 deletions staking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
"@types/node": "^17.0.34",
"@types/shelljs": "^0.8.11",
"chai": "^4.3.4",
"husky": "^7.0.4",
"lint-staged": "^12.3.7",
"mocha": "^9.2.2",
"prettier": "^2.6.2",
"shelljs": "^0.8.5",
Expand All @@ -35,17 +33,7 @@
"test": "npm run build_wasm && anchor build -- --features mock-clock && npm run dump_governance && ts-mocha --parallel -p ./tsconfig.json -t 1000000 tests/*.ts",
"build": "npm run build_wasm && tsc -p tsconfig.api.json",
"build_wasm": "./scripts/build_wasm.sh",
"localnet" : "anchor build && npm run dump_governance && ts-node ./app/scripts/localnet.ts",
"dump_governance": "./clone_programs.sh",
"prepare": "cd .. && husky install staking/.husky"

},
"lint-staged": {
"{app,tests}/**/*.ts": [
"prettier --w"
],
"programs/staking/**/*.rs": [
"rustfmt +nightly"
]
"localnet": "anchor build && npm run dump_governance && ts-node ./app/scripts/localnet.ts",
"dump_governance": "./clone_programs.sh"
}
}
18 changes: 10 additions & 8 deletions staking/programs/staking/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::state::*;
use anchor_lang::prelude::*;
use anchor_spl::token::{
Mint,
Token,
TokenAccount,
Transfer,
use {
crate::state::*,
anchor_lang::prelude::*,
anchor_spl::token::{
Mint,
Token,
TokenAccount,
Transfer,
},
std::iter::Iterator,
};
use std::iter::Iterator;

pub const AUTHORITY_SEED: &str = "authority";
pub const CUSTODY_SEED: &str = "custody";
Expand Down
62 changes: 36 additions & 26 deletions staking/programs/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,43 @@
// Objects of type Result must be used, otherwise we might
// call a function that returns a Result and not handle the error

use crate::error::ErrorCode;
use anchor_lang::prelude::*;
use anchor_lang::solana_program::pubkey;
use anchor_spl::token::transfer;
use context::*;
use spl_governance::state::governance::get_governance_data_for_realm;
use spl_governance::state::proposal::{
get_proposal_data,
ProposalV2,
use {
crate::error::ErrorCode,
anchor_lang::{
prelude::*,
solana_program::pubkey,
},
anchor_spl::token::transfer,
context::*,
spl_governance::state::{
governance::get_governance_data_for_realm,
proposal::{
get_proposal_data,
ProposalV2,
},
},
state::{
global_config::GlobalConfig,
max_voter_weight_record::MAX_VOTER_WEIGHT,
positions::{
Position,
PositionData,
PositionState,
Target,
TargetWithParameters,
},
vesting::VestingSchedule,
voter_weight_record::VoterWeightAction,
},
std::convert::TryInto,
utils::{
clock::{
get_current_epoch,
time_to_epoch,
},
voter_weight::compute_voter_weight,
},
};
use state::global_config::GlobalConfig;
use state::max_voter_weight_record::MAX_VOTER_WEIGHT;
use state::positions::{
Position,
PositionData,
PositionState,
Target,
TargetWithParameters,
};
use state::vesting::VestingSchedule;
use state::voter_weight_record::VoterWeightAction;
use std::convert::TryInto;
use utils::clock::{
get_current_epoch,
time_to_epoch,
};
use utils::voter_weight::compute_voter_weight;

mod constants;
mod context;
Expand Down
12 changes: 8 additions & 4 deletions staking/programs/staking/src/state/global_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::error::ErrorCode;
use anchor_lang::prelude::*;
use {
crate::error::ErrorCode,
anchor_lang::prelude::*,
};

pub const GLOBAL_CONFIG_SIZE: usize = 10240;

Expand Down Expand Up @@ -37,8 +39,10 @@ impl GlobalConfig {

#[cfg(test)]
pub mod tests {
use crate::state::global_config::GlobalConfig;
use anchor_lang::prelude::*;
use {
crate::state::global_config::GlobalConfig,
anchor_lang::prelude::*,
};

#[test]
fn test_unfrozen() {
Expand Down
16 changes: 10 additions & 6 deletions staking/programs/staking/src/state/max_voter_weight_record.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anchor_lang::prelude::borsh::BorshSchema;
use anchor_lang::prelude::*;
use anchor_lang::prelude::{
borsh::BorshSchema,
*,
};

pub const MAX_VOTER_WEIGHT_RECORD_SIZE: usize = 8 + 32 + 32 + 8 + 9 + 8;

Expand Down Expand Up @@ -36,11 +38,13 @@ pub struct MaxVoterWeightRecord {

#[cfg(test)]
pub mod tests {
use crate::state::max_voter_weight_record::{
MaxVoterWeightRecord,
MAX_VOTER_WEIGHT_RECORD_SIZE,
use {
crate::state::max_voter_weight_record::{
MaxVoterWeightRecord,
MAX_VOTER_WEIGHT_RECORD_SIZE,
},
anchor_lang::Discriminator,
};
use anchor_lang::Discriminator;

#[test]
fn check_size() {
Expand Down
66 changes: 39 additions & 27 deletions staking/programs/staking/src/state/positions.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use crate::error::ErrorCode;
use anchor_lang::prelude::borsh::BorshSchema;
use anchor_lang::prelude::*;
use anchor_lang::solana_program::borsh::try_from_slice_unchecked;
use anchor_lang::solana_program::wasm_bindgen;
use std::fmt::{
self,
Debug,
use {
crate::error::ErrorCode,
anchor_lang::{
prelude::{
borsh::BorshSchema,
*,
},
solana_program::{
borsh::try_from_slice_unchecked,
wasm_bindgen,
},
},
std::fmt::{
self,
Debug,
},
};

pub const MAX_POSITIONS: usize = 100;
Expand Down Expand Up @@ -212,26 +220,30 @@ impl std::fmt::Display for PositionState {

#[cfg(test)]
pub mod tests {
use crate::state::positions::{
Position,
PositionData,
PositionState,
Publisher,
TargetWithParameters,
TryBorsh,
MAX_POSITIONS,
POSITIONS_ACCOUNT_SIZE,
POSITION_BUFFER_SIZE,
};
use anchor_lang::prelude::*;
use anchor_lang::solana_program::borsh::get_packed_len;
use quickcheck::{
Arbitrary,
Gen,
use {
crate::state::positions::{
Position,
PositionData,
PositionState,
Publisher,
TargetWithParameters,
TryBorsh,
MAX_POSITIONS,
POSITIONS_ACCOUNT_SIZE,
POSITION_BUFFER_SIZE,
},
anchor_lang::{
prelude::*,
solana_program::borsh::get_packed_len,
},
quickcheck::{
Arbitrary,
Gen,
},
quickcheck_macros::quickcheck,
rand::Rng,
std::collections::HashSet,
};
use quickcheck_macros::quickcheck;
use rand::Rng;
use std::collections::HashSet;
#[test]
fn lifecycle_lock_unlock() {
let p = Position {
Expand Down
Loading
Loading