Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- uses: Swatinem/rust-cache@v2
# Set up Node environment
- name: Setup Node
uses: actions/setup-node@v3.1.1
uses: actions/setup-node@v4
with:
node-version: 20.12.0
cache: npm
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ We adopt unit tests and heavily used the [`workspace-js`](https://github.com/nea
- Run LiNEAR simulation tests:
- Run all: `make test-linear`
- Run specific test file: `TEST_FILE={filename} make test-linear`
- Print contract logs: `LOGS=1 make test-linear`
- Print contract logs: `NO_LOGS=false make test-linear`

### Deploy

Expand Down
23 changes: 4 additions & 19 deletions contracts/linear/src/epoch_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::utils::*;

const MIN_AMOUNT_TO_PERFORM_STAKE: Balance = ONE_NEAR;
const MAX_SYNC_BALANCE_DIFF: Balance = 100;
const MANAGER_SYNC_BALANCE_DIFF_THRESHOLD: Balance = 1_000_000;

/// Actions that should be called by off-chain actors
/// during each epoch.
Expand Down Expand Up @@ -266,11 +265,7 @@ trait EpochActionCallbacks {

fn validator_get_balance_callback(&mut self, validator_id: AccountId);

fn validator_get_account_callback(
&mut self,
validator_id: AccountId,
post_action: bool,
) -> bool;
fn validator_get_account_callback(&mut self, validator_id: AccountId) -> bool;

fn validator_withdraw_callback(&mut self, validator_id: AccountId, amount: U128);
}
Expand Down Expand Up @@ -307,7 +302,6 @@ impl LiquidStakingContract {
.sync_account_balance(&mut self.validator_pool, true)
.then(ext_self_action_cb::validator_get_account_callback(
validator_id,
true,
env::current_account_id(),
NO_DEPOSIT,
GAS_CB_VALIDATOR_SYNC_BALANCE,
Expand Down Expand Up @@ -357,7 +351,6 @@ impl LiquidStakingContract {
.sync_account_balance(&mut self.validator_pool, true)
.then(ext_self_action_cb::validator_get_account_callback(
validator_id,
true,
env::current_account_id(),
NO_DEPOSIT,
GAS_CB_VALIDATOR_SYNC_BALANCE,
Expand Down Expand Up @@ -416,41 +409,33 @@ impl LiquidStakingContract {
///
/// Params:
/// - validator_id: the validator to sync balance
/// - post_action: sync balance is called after stake or unstake
#[private]
pub fn validator_get_account_callback(
&mut self,
validator_id: AccountId,
post_action: bool,
#[callback_result] result: Result<HumanReadableAccount, PromiseError>,
) -> bool {
let mut validator = self
.validator_pool
.get_validator(&validator_id)
.unwrap_or_else(|| panic!("{}: {}", ERR_VALIDATOR_NOT_EXIST, &validator_id));

let max_sync_balance_diff = if !post_action && self.signed_by_manager() {
MANAGER_SYNC_BALANCE_DIFF_THRESHOLD
} else {
MAX_SYNC_BALANCE_DIFF
};

match result {
Ok(account) => {
// allow at most max_sync_balance_diff diff in total balance, staked balance and unstake balance
let new_total_balance = account.staked_balance.0 + account.unstaked_balance.0;
if abs_diff_eq(
new_total_balance,
validator.total_balance(),
max_sync_balance_diff,
MAX_SYNC_BALANCE_DIFF,
) && abs_diff_eq(
account.staked_balance.0,
validator.staked_amount,
max_sync_balance_diff,
MAX_SYNC_BALANCE_DIFF,
) && abs_diff_eq(
account.unstaked_balance.0,
validator.unstaked_amount,
max_sync_balance_diff,
MAX_SYNC_BALANCE_DIFF,
) {
Event::SyncValidatorBalanceSuccess {
validator_id: &validator_id,
Expand Down
4 changes: 0 additions & 4 deletions contracts/linear/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,4 @@ impl LiquidStakingContract {
ERR_NOT_MANAGER
);
}

pub(crate) fn signed_by_manager(&self) -> bool {
self.managers.contains(&env::signer_account_id())
}
}
4 changes: 0 additions & 4 deletions contracts/linear/src/validator_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,8 @@ impl LiquidStakingContract {
}

/// Sync contract staked and unstaked balance from validator
/// - Only allowed by manager
pub fn sync_balance_from_validator(&mut self, validator_id: AccountId) {
self.assert_running();
self.assert_manager();

let min_gas = GAS_SYNC_BALANCE + GAS_EXT_GET_ACCOUNT + GAS_CB_VALIDATOR_SYNC_BALANCE;
require!(
Expand All @@ -607,7 +605,6 @@ impl LiquidStakingContract {
.sync_account_balance(&mut self.validator_pool, false)
.then(ext_self_action_cb::validator_get_account_callback(
validator.account_id,
false,
env::current_account_id(),
NO_DEPOSIT,
GAS_CB_VALIDATOR_SYNC_BALANCE,
Expand Down Expand Up @@ -798,7 +795,6 @@ impl LiquidStakingContract {
.sync_account_balance(&mut self.validator_pool, true)
.then(ext_self_action_cb::validator_get_account_callback(
validator_id,
true,
env::current_account_id(),
NO_DEPOSIT,
GAS_CB_VALIDATOR_SYNC_BALANCE,
Expand Down
5 changes: 3 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ test-unit:
cargo test --features "test"

TEST_FILE ?= **
LOGS ?=
NO_LOGS=true

test-contracts: linear_test mock-staking-pool mock-fungible-token mock-dex mock-lockup mock-whitelist
@mkdir -p ./tests/compiled-contracts/
@cp ./res/linear_test.wasm ./tests/compiled-contracts/linear.wasm
Expand All @@ -68,7 +69,7 @@ test-contracts: linear_test mock-staking-pool mock-fungible-token mock-dex mock-
@cp ./res/mock_whitelist.wasm ./tests/compiled-contracts/mock_whitelist.wasm

test-linear: test-contracts
cd tests && NEAR_PRINT_LOGS=$(LOGS) npx ava --timeout=2m __tests__/linear/$(TEST_FILE).ava.ts --verbose
cd tests && NEAR_WORKSPACES_NO_LOGS=$(NO_LOGS) npx ava --timeout=2m __tests__/linear/$(TEST_FILE).ava.ts --verbose

test-mock-staking-pool: mock-staking-pool
@mkdir -p ./tests/compiled-contracts/
Expand Down
10 changes: 5 additions & 5 deletions tests/__tests__/linear/helper.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import anyTest, { TestFn } from 'ava';
import {
Worker,
NEAR,
NearAccount,
BN,
Gas,
NEAR,
NearAccount,
TransactionResult,
Worker,
} from 'near-workspaces';
import anyTest, { TestFn } from 'ava';

export const test = anyTest as TestFn<Workspace>;

export const ONE_YOCTO = '1';
export const NUM_EPOCHS_TO_UNLOCK = 4;
export const MAX_SYNC_BALANCE_DIFF = NEAR.from(100);
export const MANAGER_SYNC_BALANCE_DIFF_THRESHOLD = NEAR.from(1_000_000);
export const SYNC_BALANCE_DIFF_THRESHOLD = NEAR.from(100);

interface RewardFee {
numerator: number;
Expand Down
Loading
Loading